【pytorch】【已解决】RuntimeError: stack expects each tensor to be equal size, but got [4, 448, 448] at ent

详细错误:

Traceback (most recent call last):
  File "/home/yjys/anaconda3/lib/python3.8/site-packages/torch/utils/data/_utils/collate.py", line 55, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: stack expects each tensor to be equal size, but got [4, 448, 448] at entry 0 and [3, 448, 448] at entry 1

此错误是在创建dataloader后,进行访问时出现,因为trainloader得到的是批量图片,格式为(batch,channel,height,width)。
每次通过__getitem_ _获取单个图片,后经过stack后变为上述的格式。
通过打印每个图片的shape,发现有的shape为(x,x,4),四通道的rgba图片。

skimage解决:
在这里插入图片描述
PIL解决:
PIL将png的RGBA四通道改为jpg的RGB三通道方法

结论:
制作数据集,收集的图片可能在灰度图片(channel=1)或者png图片(channel=4)或者jpeg图片(channel=3)都要考虑。
附上skimage.color包列举的函数

猜你喜欢

转载自blog.csdn.net/YJYS_ZHX/article/details/114370418
今日推荐