Predict a photo specifically(Error traying it)

Hello. I was traying to load the lesson1 model and then try to predict a photo outside of the train and test data sets.

I was doing something like this:

from fastai.imports import *
from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *
PATH = "data/"
sz=224
arch=resnet34
data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model(arch, sz),bs=8)
learn = ConvLearner.pretrained(arch, data, precompute=True)
learn.load(“my_model_cat_dogs”)
tfms,val_tfms = tfms_from_model(arch, sz)
image_loader = open_image(‘fa7.png’)
trans_img = val_tfms(image_loader)
learn.precompute = False
preds = learn.predict_array(trans_img[None])

but it gives me an error:


ValueError Traceback (most recent call last)
in ()
----> 1 preds = learn.predict_array(trans_img[None])

~\Desktop\ML Course\fastai\fastai\learner.py in predict_array(self, arr)
260
261 def predict_dl(self, dl): return predict_with_targs(self.model, dl)[0]
–> 262 def predict_array(self, arr): return to_np(self.model(V(T(arr).cuda())))
263
264 def TTA(self, n_aug=4, is_test=False):

C:\Conda\envs\fastai\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
323 for hook in self._forward_pre_hooks.values():
324 hook(self, input)
–> 325 result = self.forward(*input, **kwargs)
326 for hook in self._forward_hooks.values():
327 hook_result = hook(self, input, result)

C:\Conda\envs\fastai\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
65 def forward(self, input):
66 for module in self._modules.values():
—> 67 input = module(input)
68 return input
69

C:\Conda\envs\fastai\lib\site-packages\torch\nn\modules\module.py in call(self, *input, **kwargs)
323 for hook in self._forward_pre_hooks.values():
324 hook(self, input)
–> 325 result = self.forward(*input, **kwargs)
326 for hook in self._forward_hooks.values():
327 hook_result = hook(self, input, result)

C:\Conda\envs\fastai\lib\site-packages\torch\nn\modules\batchnorm.py in forward(self, input)
35 return F.batch_norm(
36 input, self.running_mean, self.running_var, self.weight, self.bias,
—> 37 self.training, self.momentum, self.eps)
38
39 def repr(self):

C:\Conda\envs\fastai\lib\site-packages\torch\nn\functional.py in batch_norm(input, running_mean, running_var, weight, bias, training, momentum, eps)
1009 size = list(input.size())
1010 if reduce(mul, size[2:], size[0]) == 1:
-> 1011 raise ValueError(‘Expected more than 1 value per channel when training, got input size {}’.format(size))
1012 f = torch._C._functions.BatchNorm(running_mean, running_var, training, momentum, eps, torch.backends.cudnn.enabled)
1013 return f(input, weight, bias)

ValueError: Expected more than 1 value per channel when training, got input size [1, 1024]

I don’t understand what I am doing wrong.

Someone an idea ?

Thank you

I suspect the “precompute” attribute to have something to do with the error otherwise everything seems to be good (for my limited understanding !).
So I would suggest you to set precompute to false when calling pretrained :

learn = ConvLearner.pretrained(arch, data, precompute=False)

and remove this line:

learn.precompute = False

Try running it again and let us know if it works.

I was able to solve my problem.

When I don’t call learn.predict() before the learn.predict_array() , I get an error. But when I call first learn.predict() , I am always able to use learn.predict_array(). I don’t know why , but it works!

Thank you

1 Like

Did you figure out the way around learn.predict()