这里图像必须根据 VGG16 模型的输入类型进行转换。我为此使用了以下代码,我使用库中的 VGG16 模型并将预训练的值设置为 true
import numpy as np
from glob import glob
dog_files = np.array(glob("/data/dog_images/*/*/*"))
import torch
import torchvision.models as models
# define VGG16 model
VGG16 = models.vgg16(pretrained=True)
# check if CUDA is available
use_cuda = torch.cuda.is_available()
# move model to GPU if CUDA is available
if use_cuda:
VGG16 = VGG16.cuda()
from PIL import Image
import torchvision.transforms as transforms
normalize = transforms.Compose([
transforms.Resize((224,224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224,
0.225])])
for i in dog_files:
img = Image.open(i)
print(VGG16(normalize(img)))
它给了我以下错误:
RuntimeError Traceback (most recent
call last)
<ipython-input-57-cbe658985de1> in <module>()
11 for i in dog_files:
12 img = Image.open(i)
---> 13 print(VGG16(normalize(img)))
14
15 #print(img)
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py
in __call__(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
--> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)
/opt/conda/lib/python3.6/site-packages/torchvision-0.2.1-
py3.6.egg/torchvision/models/vgg.py in forward(self, x)
40
41 def forward(self, x):
---> 42 x = self.features(x)
43 x = x.view(x.size(0), -1)
44 x = self.classifier(x)
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py
in __call__(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
--> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)
/opt/conda/lib/python3.6/site-
packages/torch/nn/modules/container.py in forward(self, input)
89 def forward(self, input):
90 for module in self._modules.values():
---> 91 input = module(input)
92 return input
93
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py
in __call__(self, *input, **kwargs)
489 result = self._slow_forward(*input, **kwargs)
490 else:
--> 491 result = self.forward(*input, **kwargs)
492 for hook in self._forward_hooks.values():
493 hook_result = hook(self, input, result)
/opt/conda/lib/python3.6/site-packages/torch/nn/modules/conv.py in
forward(self, input)
299 def forward(self, input):
300 return F.conv2d(input, self.weight, self.bias,
self.stride,
--> 301 self.padding, self.dilation,
self.groups)
302
303
RuntimeError: expected stride to be a single integer value or a
list of 1 values to match the convolution dimensions, but got
stride=[1, 1]
我想在应用转换并将其馈送到 VGG16 模型后预测给定输入图像的输出