【pytorch报错解决】expected input to have 3 channels, but got 1 channels instead
遇到的问题
数据是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 知识点:https://blog.csdn.net/u011995719/article/details/85107009
PIL PNG格式通道问题的解决方法 : https://www.cnblogs.com/wzjbg/p/8516531.html
【pytorch报错解决】expected input to have 3 channels, but got 1 channels instead的更多相关文章
- Anaconda 安装 pytorch报错解决方法
一.安装Pytorch: # -c 指定用pytorch镜像源下载软件conda install pytorch torchvision cpuonly -c pytorch 报错: 二.配置: ch ...
- pytorch报错:ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1,512,1,1])
1.pytorch报错:ValueError: Expected more than 1 value per channel when training, got input size torch.S ...
- org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 3报错解决
报错的原因翻译出来: 预期的一个结果(或null)返回selectOne(),但发现:3 意思就是你想得到一个结果值,但是返回了三个结果值. 一般可能测试的时候我们存了几条一样的数据,在登录时,会把同 ...
- selenium调用Firefox和Chrome需要注意的一些问题,和出现的报错selenium:expected [object undefined] undefined to be a string
在高版本selenium下如:selenium3.4.3 1.高版本的selenium需要浏览器安装一些补丁驱动 Firefox:geckodriver 下载网址:http://download.cs ...
- vue 项目 npm install 报错解决
node-sass 安装报错解决办法 2017年04月15日 14:34:25 阅读数:20189 E:\kibana>npm install node-sass > node-sass@ ...
- Pytorch报错:cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/THCTensorMath.cu:26
Pytorch报错:cuda runtime error (59) : device-side assert triggered at /pytorch/aten/src/THC/generic/TH ...
- Linux部署Django:报错 nohup: ignoring input and appending output to ‘nohup.out’
一.部署 Django 到远程 Linux 服务器 利用 xshell 通过 ssh 连接到 Linux服务器,常规的启动命令是 python3 manage.py runserver 但是,关闭 x ...
- JavaScript函数报错SyntaxError: expected expression, got ';'
故事背景:编写Javaweb项目,在火狐浏览器下运行时firebug报错SyntaxError: expected expression, got ';'或者SyntaxError: expected ...
- MySQL5.7.26安装及启动报错解决
一.安装依赖包 [root@db01 ~]# yum install -y lrzsz [文件上传/下载] [root@db01 ~]# yum -y install xfsprogs [安装磁盘格式 ...
随机推荐
- Ubuntu18.04下LAMP环境搭建
可以安装安装Xampp,Xampp是一个集成的LAMP开发环境. 但是这只是对于一个刚安装好的系统来说的,但是很有可能我的电脑上面已经安装过Apache,或者安装过MySQL,或者安装过PHP了,或者 ...
- NB-IOT技术 UP模式 和CP模式,用户面和控制面,数据面
先看下面的图 UE就是NB-IOT设备,那么UE的数据到应用服务器有3条路可以走 第一条,UE,基站,MME,SCEF,应用服务器,这个是CP模式,也叫控制面,也叫信令无线承载面,也叫控制面承载,叫法 ...
- 【ARTS】01_41_左耳听风-201900819~201900825
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- python:如何传递一个列表参数
- How George Washington Angered Lawmakers Over Thanksgiving——VOA慢速英语
听力地址:How George Washington Angered Lawmakers Over Thanksgiving 中英对照:华盛顿总统将感恩节定为全国性节日 Words in This S ...
- socket编程方法,概念
"蚓无爪牙之利,筋骨之强,上食埃土,下饮黄泉,用心一也.蟹六跪而二螯,非蛇鳝之穴无可寄托者,用心躁也." ------------------------------------- ...
- redis 那些事儿
1 我的数据存入redis了但是怎么不见了? redis的内存使用是有限的,一直向redis中写入数据(如果配置了allkeyLRU)就会触发内存淘汰机制,将最近没有访问过的的key,value删除掉 ...
- C++_向函数传递对象
向函数传递对象 1. 使用对象作为函数参数 对象可以作为参数传递给函数,其方法与传递其他类型的数据相同. 在向函数传递对象时,是通过传值调用传递给函数的. 因此,函数中对对象的任何修改均不影响调用该函 ...
- 字符串char vchar性能对比补充
Value CHAR(4) Storage Required VARCHAR(4) Storage Required '' ' ' 4 bytes '' 1 byte 'ab' 'ab ' 4 ...
- 【SCALA】1、我要开始学习scala啦
因为scala也是基于jvm上运行的,所以能跑java,原则上就能跑scala 1.国际惯例,先来个hello world走走 package demo1 //object之下全为静态,scala没有 ...