How do we use our model against a specific image?

Trying to write this myself based on what I’m seeing but still get an exception:

xforms, _ = tfms_from_model(arch, sz)
im = xforms(PIL.Image.open(f'{PATH}/valid/in-n-out/2.jpg'))
preds = to_np(learn.model(V(T(im[None]).cuda())))

Exception: running_mean should contain 3 elements not 1024

The code looks identical to yours so I’m not understanding what is going on.

Thanks.

Oi vey …

Read the code a little more closely and figured out that learn.model is different than learn.models.model.

Revised code works:

xforms, _ = tfms_from_model(arch, sz)
im = xforms(Image.open(f'{PATH}/valid/in-n-out/2.jpg'))
preds = to_np(learn.models.model(V(T(im[None]).cuda())))
np.argmax(preds, axis=1)

A little more long winded, but would eliminate any dependencies on the fast.ai framework in production (which might prove helpful if trying to put this in an Android or iOS app at some point):

torch_model = learn.models.model
img = Image.open(f'{PATH}/valid/in-n-out/2.jpg')

normalize = torchvision.transforms.Normalize(
   mean=[0.485, 0.456, 0.406],
   std=[0.229, 0.224, 0.225]
)
preprocess = torchvision.transforms.Compose([
   torchvision.transforms.Scale(256),
   torchvision.transforms.CenterCrop(224),
   torchvision.transforms.ToTensor(),
   normalize
])

img_tensor = preprocess(img).unsqueeze_(0)
img_variable = Variable(img_tensor.cuda())

log_probs = torch_model(img_variable)
preds = np.argmax(log_probs.cpu().data.numpy(), axis=1)

print(preds)
6 Likes

Actually my code isn’t quite right - you should use the 2nd return val from tfms_from_model. The first one includes data augmentation, e.g. for the training set. For predictions you don’t want that, so use the 2nd one.

2 Likes

What’s a .pb file?

Absolutely!

1 Like

Yah I was just seeing that. Revised my code using fast.ai transforoms and helpers as such:

trn_tfms, val_tfrms = tfms_from_model(arch, sz)
im = val_tfrms(Image.open(f'{PATH}/valid/in-n-out/2.jpg'))

log_probs = to_np(learn.models.model(V(T(im[None]).cuda())))
preds = np.argmax(log_probs, axis=1)

print(preds)
6 Likes

I have a question regarding image size. If we are using RGB image of size 224, which is also an requirement of ResNet32 then does it mean that the input feature vector for that image size will be of size 224 * 224 * 3 = 150528. And these many input lines goes to CNN.

224x224 is not a requirement of any model we use. In fact, last lesson we trained a model with multiple image sizes!

1 Like

A silly question which I cannot hold: So if a dataset can have images of different sizes then how the input size of a NN is decided. On basis of what. That’s why we are supposed to bring all images to same size while training?. If this is true then I may have answered my former question.

The re-sizing happens as part of the transformations, that is why you have to pass the size (e.g. “sz”) parameter into it.

Im getting this error and would appreciate any suggestions. The error states its missing one argument but dont know how to fix


TypeError Traceback (most recent call last)
in ()
1 trn_tfms, val_tfms = tfms_from_model(arch, sz)
----> 2 im = trn_tfms(PIL.Image.open(f’data/test/320.jpg’))
3 preds = learn.predict_array(im[None])
4 np.argmax(preds)

TypeError: call() missing 1 required positional argument: ‘y’

And this is the code I am using:
trn_tfms, val_tfms = tfms_from_model(arch, sz)
im = trn_tfms(PIL.Image.open(f’data/test/320.jpg’))
preds = learn.predict_array(im[None])
np.argmax(preds)

Make sure you get the latest code cuz I was getting the same error before I did.

Also, you may want to look at the most recent examples in this thread that use the validation tfms.

@wgpubs

I did try both - same error. I did update to the latest lesson1.ipynb but still same error.


TypeError Traceback (most recent ca.ll last)
in ()
1 trn_tfms, val_tfms = tfms_from_model(arch, sz)
----> 2 im = val_tfms(PIL.Image.open(f’data/test/320.jpg’))
3 preds = learn.predict_array(im[None])
4 np.argmax(preds)

TypeError: call() missing 1 required positional argument: ‘y’

I got it from this article:

and quote

Protocol buffers
Protocol Buffers often abreviated Protobufs is the format use by TF to store and transfer data efficiently.
To recapitulate, you can use Protobufs as:
An uncompressed, human friendly, text format with the extension .pbtxt
A compressed, machine friendly, binary format with the extension .pb or no extension at all

I am semi-familiar with android software and am able to transfer already trained models to my phone but in order for me to use my own trained model I need to be able to save my model in that format. I have been able to save it using tensorflow but it doesn’t work on my phone and its difficult to debug why.

With the in class lessons and forums I have been able to understand your code far better so would be able to debug more effectively hence the need to save in that format.

That’s definitely an error from not having the latest in the repo. You’ll need to git pull, and restart your kernel.

As it says there, that’s for Tensorflow. We use Pytorch.

Hi @jeremy -

I’m getting the same error that @wgpubs was getting earlier:

RuntimeError: running_mean should contain 3 elements not 1024

Git is up to date (latest commit: c738837b39829902525e9c17761faf6f1c2ae88c), I have restarted my kernel, restarted jupyter notebook, etc. I am running the AMI on EC2.

I am running the lesson 1 notebook exactly as is, and have added 1 cell after we train the initial model in which I am trying to predict one of the images:

Any help would be appreciated!

Full traceback:

    ---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-13-41c993a2ac6d> in <module>()
      3 ds = FilesIndexArrayDataset([fn], np.array([0]), val_tfms, PATH)
      4 dl = DataLoader(ds)
----> 5 preds = learn.predict_dl(dl)

~/fastai/courses/dl1/fastai/learner.py in predict_dl(self, dl)
    110         return predict_with_targs(self.model, dl)
    111 
--> 112     def predict_dl(self, dl): return predict_with_targs(self.model, dl)[0]
    113     def predict_array(self, arr): return to_np(self.model(V(T(arr).cuda())))
    114 

~/fastai/courses/dl1/fastai/model.py in predict_with_targs(m, dl)
    115     if hasattr(m, 'reset'): m.reset()
    116     preda,targa = zip(*[(get_prediction(m(*VV(x))),y)
--> 117                         for *x,y in iter(dl)])
    118     return to_np(torch.cat(preda)), to_np(torch.cat(targa))
    119 

~/fastai/courses/dl1/fastai/model.py in <listcomp>(.0)
    115     if hasattr(m, 'reset'): m.reset()
    116     preda,targa = zip(*[(get_prediction(m(*VV(x))),y)
--> 117                         for *x,y in iter(dl)])
    118     return to_np(torch.cat(preda)), to_np(torch.cat(targa))
    119 

~/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    222         for hook in self._forward_pre_hooks.values():
    223             hook(self, input)
--> 224         result = self.forward(*input, **kwargs)
    225         for hook in self._forward_hooks.values():
    226             hook_result = hook(self, input, result)

~/src/anaconda3/envs/fastai/lib/python3.6/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 

~/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    222         for hook in self._forward_pre_hooks.values():
    223             hook(self, input)
--> 224         result = self.forward(*input, **kwargs)
    225         for hook in self._forward_hooks.values():
    226             hook_result = hook(self, input, result)

~/src/anaconda3/envs/fastai/lib/python3.6/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):

~/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/functional.py in batch_norm(input, running_mean, running_var, weight, bias, training, momentum, eps)
    637                training=False, momentum=0.1, eps=1e-5):
    638     f = torch._C._functions.BatchNorm(running_mean, running_var, training, momentum, eps, torch.backends.cudnn.enabled)
--> 639     return f(input, weight, bias)
    640 
    641 

RuntimeError: running_mean should contain 3 elements not 1024

Another possibility is to make a test folder and put your image there. Here is an example on how to use a “test_name”

data = ImageClassifierData.from_csv(path, img_folder, csv_fname, bs, tfms, val_idxs, suffix=".png",
                            test_name="test", continuous=True)

Then you can predict

test_preds = learn.predict(is_test=True)

Here is how you get a list of the test files ordered.

test_files = data.test_dl.dataset.fnames
5 Likes

You need to said precompute=False before you do that prediction, since you’re passing in an image, not a precomputed activation.

3 Likes