相关文章推荐
帅气的洋葱  ·  VSCode报错 Unexpected ...·  2 天前    · 
眼睛小的蚂蚁  ·  react-router v4.0+ 的 ...·  1 年前    · 


遇到的问题

数据是png图像的时候,如果用PIL读取图像,获得的是单通道的,不是多通道的。虽然使用opencv读取图片可以获得三通道图像数据,如下:

def __getitem__(self, idx):
image_root = self.train_image_file_paths[idx]
image_name = image_root.split(os.path.sep)[-1]
image = cv.imread(image_root)

if self.transform is not None:
image = self.transform(image)
label = ohe.encode(image_name.split('_')[0])
return image, label

但是会出现报错:


TypeError: img should be PIL Image. Got <class 'numpy.ndarray'>


  File "c:/Users/pprp/Desktop/pytorch-captcha-recognition-master/captcha_train.py", line 77, in <module>
main(args)
File "c:/Users/pprp/Desktop/pytorch-captcha-recognition-master/captcha_train.py", line 47, in main
predict_labels = cnn(images)
File "E:\ProgramData\Miniconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 493, in __call__
result = self.forward(*input, **kwargs)
File "E:\ProgramData\Miniconda3\envs\pytorch\lib\site-packages\torchvision\models\resnet.py", line 192, in forward
x = self.conv1(x)
File "E:\ProgramData\Miniconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 493, in __call__
result = self.forward(*input, **kwargs)
File "E:\ProgramData\Miniconda3\envs\pytorch\lib\site-packages\torch\nn\modules\conv.py", line 338, in forward
self.padding, self.dilation, self.groups)
RuntimeError: Given groups=1, weight of size 64 3 7 7, expected input[64, 60, 160, 3] to have 3 channels, but got 60 channels instead

最终解决方案:

class mydataset(Dataset):
def __init__(self, folder, transform=None):
self.train_image_file_paths = [os.path.join(folder, image_file) for image_file in os.listdir(folder)]
self.transform = transforms.Compose([
transforms.ToTensor(), # 转化为pytorch中的tensor
transforms.Lambda(lambda x: x.repeat(1,1,1)), # 由于图片是单通道的,所以重叠三张图像,获得一个三通道的数据
# transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
]) # 主要改这个地方

def __len__(self):
return len(self.train_image_file_paths)

def __getitem__(self, idx):
image_root = self.train_image_file_paths[idx]
image_name = image_root.split(os.path.sep)[-1]
image = Image.open(image_root)
if self.transform is not None:
image = self.transform(image)
label = ohe.encode(image_name.split('_')[0])
return image, label

pytorch transform 知识点:​ ​javascript:void(0)​

PIL PNG格式通道问题的解决方法 : ​ ​javascript:void(0)​


代码改变世界

Pytorch运行错误: groups=1, weight of size [8, 1, 3, 3], expected input[1, 3, 512, 512] to have 1 channel

这个错误通常是由于卷积层(Convolutional layer)的输入通道数与卷积核(Convolutional kernel)的通道数不匹配导致的。具体地说,卷积核的通道数应该与输入 tensor 的通道数相同。在你的代码中,卷积层的卷积核大小为 ​​[8, 1, 3, 3]​​​,其中第二个维度的大小是 1,表示该卷积核仅适用于单通道的输入。然而,你的输入 tensor 的大小为 ​​[1,

pytorch报错:ValueError: Expected more than 1 value per channel when training, got input size [1, 768,1

Traceback (most recent call last): File "train_ammeter_twoclass.py", line 189, in &lt;module&gt; train(epoch) File "train_ammeter_twoclass.py", line 133, in train outputs = net(inputs)