TypeError: conv2d() received an invalid combination of arguments - got (int, Parameter, Parameter, tuple, tuple, tuple, int), but expected one of:
- (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)
didn't match because some of the arguments have invalid types: (!int!, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int) - (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)
didn't match because some of the arguments have invalid types: (!int!, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int)
遇到了这个问题,找了好久的问题,最后发现是我犯蠢了,我在类型转换之后,输入的还是原来的图片,崩溃,两个小时。。。。。
解决方法:格式转换
模型中输入的图片格式应该是Tensor类型:
image = Image.open('./images/0.png')
image = image.convert("L") #变灰度图像,不然channel的值可能大于1
image = image.resize((28,28)) #修改尺寸
#image.show()
print(image.size)
transf = transforms.ToTensor()
tensor = transf(image) #转换成tensor格式
print(tensor.shape)
tensor = tensor.type(torch.FloatTensor)
tensor = tensor.reshape((1, 1, 28, 28)) #我的模型里的训练数据就是(1, 1, 28, 28)
print(tensor.shape) #最后直接将tensor传入模型即可