Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 一幅图像上的pytorch vgg模型测试_Python_Pytorch - Fatal编程技术网

Python 一幅图像上的pytorch vgg模型测试

Python 一幅图像上的pytorch vgg模型测试,python,pytorch,Python,Pytorch,我已经训练了一个vgg模型,这就是我转换测试数据的方式 test_transform_2= transforms.Compose([transforms.RandomResizedCrop(224), transforms.ToTensor()]) test_data = datasets.ImageFolder(test_dir, transform=test_transform_2) 模型已经完成了训练,现在我想

我已经训练了一个vgg模型,这就是我转换测试数据的方式

test_transform_2= transforms.Compose([transforms.RandomResizedCrop(224), 
                                     transforms.ToTensor()])

test_data = datasets.ImageFolder(test_dir, transform=test_transform_2)
模型已经完成了训练,现在我想在一张图片上测试它

from scipy import misc

test_image = misc.imread('flower_data/valid/1/image_06739.jpg')
vgg16(torch.from_numpy(test_image))
错误
---------------------------------------------------------------------------
运行时错误回溯(上次最近调用)
在里面
---->1 vgg16(火炬,来自numpy(测试图像))
c:\users\sam\mydocu~1\code\envs\data science\lib\site packages\torch\nn\modules\module.py in\uuuuuu调用(self,*input,**kwargs)
475结果=self.\u slow\u forward(*输入,**kwargs)
476其他:
-->477结果=自我转发(*输入,**kwargs)
478用于钩住自身。\u向前\u钩住.values():
479钩子结果=钩子(自身、输入、结果)
c:\users\sam\mydocu~1\code\envs\data science\lib\site packages\torchvision\models\vgg.py in forward(self,x)
40
41 def前进档(自身,x):
--->42 x=自身特征(x)
43 x=x.view(x.size(0),-1)
44 x=自分类器(x)
c:\users\sam\mydocu~1\code\envs\data science\lib\site packages\torch\nn\modules\module.py in\uuuuuu调用(self,*input,**kwargs)
475结果=self.\u slow\u forward(*输入,**kwargs)
476其他:
-->477结果=自我转发(*输入,**kwargs)
478用于钩住自身。\u向前\u钩住.values():
479钩子结果=钩子(自身、输入、结果)
c:\users\sam\mydocu~1\code\envs\data science\lib\site packages\torch\nn\modules\container.py in forward(self,input)
89 def前进档(自身,输入):
90表示自组中的模块。\u modules.values():
--->91输入=模块(输入)
92返回输入
93
c:\users\sam\mydocu~1\code\envs\data science\lib\site packages\torch\nn\modules\module.py in\uuuuuu调用(self,*input,**kwargs)
475结果=self.\u slow\u forward(*输入,**kwargs)
476其他:
-->477结果=自我转发(*输入,**kwargs)
478用于钩住自身。\u向前\u钩住.values():
479钩子结果=钩子(自身、输入、结果)
c:\users\sam\mydocu~1\code\envs\data science\lib\site packages\torch\nn\modules\conv.py in forward(self,input)
299 def前进(自,输入):
300返回F.conv2d(输入、自重、自偏倚、自步幅、,
-->301自填充、自膨胀、自组)
302
303
RuntimeError:四维权重[64,3,3,3]应为四维输入,但得到的输入大小为[628500,3]

我可以告诉你我需要塑造输入,但是我不知道如何根据它似乎期望输入被告知批次的方式。你的图像是
[h,w,3]
其中3表示rgb通道,pytorch期望
[b,3,h,w]
其中
b
是批次大小。因此,您可以通过调用
restraped=img.permute(2,0,1).unsqueze(0)
来重塑它。我想在某个地方也有一个实用函数,但我现在找不到

那么你的情况呢

tensor = torch.from_numpy(test_image)
reshaped = tensor.permute(2, 0 1).unsqueeze(0)
your_result = vgg16(reshaped)

您的图像是
[h,w,3]
其中3表示rgb通道,pytorch期望
[b,3,h,w]
其中
b
是批量大小。因此,您可以通过调用
restraped=img.permute(2,0,1).unsqueze(0)
来重塑它。我想在某个地方也有一个实用函数,但我现在找不到

那么你的情况呢

tensor = torch.from_numpy(test_image)
reshaped = tensor.permute(2, 0 1).unsqueeze(0)
your_result = vgg16(reshaped)

vgg16(torch.from_numpy(test_image)。查看(1628500,3)
是否会让您前进?@dedObed此返回此错误
RuntimeError:给定组=1,大小权重[64,3,3],预期输入[1628500,3]有3个通道,但使用您的建议得到628个通道
vgg16(torch.from_numpy(test image)。查看(1628500,3))
太好了,所以它确实让你前进了。所以请参考Jatentaki的答案,他处理了所需的全部重塑。会
vgg16(torch.from_numpy(test_image)。view(1628500,3)
让你前进?@dedObed此返回此错误
运行时错误:给定组=1,大小权重[64,3,3],预期输入[1628500,3]拥有3个频道,但使用Your's suggestion获得了628个频道
vgg16(torch.from_numpy(test_image).view(1628500,3))
太好了,所以它确实推动了你的前进。所以请参考Jatentaki的答案,他处理了所需的全部重塑。你已经得到了它,但我不能使用这个建议,因为它不是scipy或torch的一部分。Num更新了我的答案。谢谢,尝试了它,我得到了这个错误,
运行时错误:thnn\U conv2d\u forward没有为typ实现e torch.ByteTensor
表示您的图像类型为
torch.uint8
(标准255值rgb),应该是
torch.float32
。您可能需要添加另一行
floatified=重塑的.to(torch.float32)/255.
并就此调用vgg16。但这只是对您正在使用的规范化约定的猜测,它需要与网络的训练方式保持一致。换句话说:
重塑.to(torch.float32)
会使消息消失,但不一定会使网络产生合理的预测,这是可行的,还有一件事,我得到了这个错误
运行时错误:大小不匹配,m1:[1 x 145920],m2:[25088 x 4096]在c:\new-builder\u 3\win wheel\pytorch\aten\src\th\generic/THTensorMath.cpp:2070,相信是因为我忘了将其调整为224乘224。你能指导我吗?你已经得到了,但我不能使用这个建议,因为它不是scipy或torch的一部分。Num更新了我的答案,尝试了,我得到了这个错误,
RuntimeError:thnn_conv2d_forward不是为torch类型实现的。ByteTensor
表示您的图像是torch.uint8类型(标准255值rgb),应该是torch.float32
。您可能需要将另一行
floatified=reformed.to(torch.float32)/255