如何获得给定输入图像的VGG模型预测?

时间:2018-11-05 06:06:40

标签: python-3.x conv-neural-network vgg-net transfer-learning torchvision

在这里必须根据VGG16模型的输入类型来转换图像。为此,我使用了以下代码,我正在从库中使用VGG16模型并将预训练值设置为真

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模型后,我想预测给定输入图像的输出

1 个答案:

答案 0 :(得分:0)

vgg16需要[1、3、224、224]输入,但是您有[3、224、224]输入。

尝试...

SignIn