Как получить прогноз модели VGG для заданного входного изображения? - PullRequest
0 голосов
/ 05 ноября 2018

Здесь изображение должно быть преобразовано в соответствии с типом ввода для модели 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

1 Ответ

0 голосов
/ 14 декабря 2018

vgg16 ожидает ввода [1, 3, 224, 224], но у вас есть ввод [3, 224, 224].

Try ...

print(VGG16(normalize(img).unsqueeze(0)))

...