1.RuntimeError: output with shape [1, 28, 28] doesn't match the broadcast shape [3, 28, 28]
博客的大佬的解释都是mnist数据集的灰度图片需要转变为RGB图片,也就是通道数需要从1变成3
解决方式:transform里添加transforms.Normalize((0.1307,), (0.3081,))
# 导入数据
train_dataset = datasets.MNIST(root = 'data/'
,train = True
,transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
2.RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[4, 1, 28, 28] to have 3 channels, but got 1 channels instead
解决这个问题之后还没完,又出现这个报错,主要是尺寸不匹配的问题。但是按照博客中的解释,主要还是通道数的问题:训练模型中需要通道数为3的图片,但是提供的图像通道数只有1。吐血,为什么两种报错明明不同,但是要解决的问题却是一个呢。。。
解决办法:其实没有解决,但是套模型的时候把图片的保存路径以及图片的保存方式完全设置成一模一样的了,这样读取的时候可以按照已经训练好的模型直接套,自然不会出现通道的问题。
以下两图前为处理前的数据,后为处理后的数据;代码在最下。
# 读取图片并保存为指定格式
import numpy as np
import struct
import matplotlib.pyplot as plt
import os
filename = r'D:\大学\202202\神经网络\data\MNIST\raw\train-images-idx3-ubyte'
binfile = open(filename , 'rb')
buf = binfile.read()
index = 0
magic, numImages , numRows , numColumns = struct.unpack_from('>IIII' , buf , index)
index += struct.calcsize('IIII' )
images = []
for i in range(numImages):
imgVal = struct.unpack_from('>784B', buf, index)
index += struct.calcsize('>784B')
imgVal = list(imgVal)
for j in range(len(imgVal)):
if imgVal[j] > 1:
imgVal[j] = 1
images.append(imgVal)
arrX = np.array(images)
# 读取标签
binFile = open(r'D:\大学\202202\神经网络\data\MNIST\raw\train-labels-idx1-ubyte','rb')
buf = binFile.read()
binFile.close()
index = 0
magic, numItems= struct.unpack_from('>II', buf,index)
index += struct.calcsize('>II')
labels = []
for x in range(numItems):
im = struct.unpack_from('>1B',buf,index)
index += struct.calcsize('>1B')
labels.append(im[0])
arrY = np.array(labels)
print(np.shape(arrY))
# print(np.shape(trainX))
#以下内容是将图像保存到本地文件中
path_trainset = r"D:\大学\202202\神经网络\data\MNIST\train"
path_testset = r"D:\大学\202202\神经网络\data\MNIST\val"
if not os.path.exists(path_trainset):
os.mkdir(path_trainset)
if not os.path.exists(path_testset):
os.mkdir(path_testset)
for i in range(500,700):
img = np.array(arrX[i])
print(img)
img = img.reshape(28,28)
outfile = str(i) + "_" + str(arrY[i]) + ".jpg"
# outfile = str(i)+".png"
plt.figure()
plt.imshow(img, cmap = 'binary') #将图像黑白显示
plt.savefig(path_testset + "\\" +str(arrY[i])+"/"+ outfile)
print("save"+str(i)+"张")
for i in range(500):
img = np.array(arrX[i])
print(img)
img = img.reshape(28,28)
outfile = str(i) + "_" + str(arrY[i]) + ".jpg"
# outfile = str(i)+".png"
plt.figure()
plt.imshow(img, cmap = 'binary') #将图像黑白显示
plt.savefig(path_trainset + "\\" +str(arrY[i])+"/"+ outfile)
print("save"+str(i)+"张")