axis don’t match array的问题

写一个神经网络进行图像分类,在读取数据的部分报错如下

(1)numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc ‘subtract’ output from dtype(‘float64’) to dtype(‘uint8’) with casting rule ‘same_kind’
在这里插入图片描述
however,发现如果把报错的那句A-=B 改为 A=A-B,这个报错就解决了

#X_trian=X_train-mean_image  改
X_trian=X_train-mean_image

有人说原因是:

It may be a numpy/broadcasting issue in python

(2)原代码中,上面的命令下面还有命令如下,这时又报错
axes don’t match array

 X_train = X_train.transpose(0, 3, 1, 2).copy()

在这里插入图片描述
搜了很多,有一个说法是:你的数据集中有灰度图

PIL Image will give you 2 dimension output if it is grayscale image. (e.g. (64, 64, 1) -> (64, 64))

所以解决办法:
(1)将 X_train 进行reshape为和输入一样格式,我用了这个方法有效

    X_train=X_train.reshape(-1,3,32,32)

(2)转换读图模式image.open(filename) .convert(‘RGB’)
我没试过,可以试试

参考:
开源项目问答

https://github.com/statsmodels/statsmodels/issues/3504

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐