How do we use our model against a specific image?

Your preds is a matrix, not a vector (i.e. it has two sets of square brackets around it). So try preds[0].

1 Like

yes that worked!

1 Like

I do have another question though, the bar graph is not matching the prediction. Would appreciate any thoughts on why that would be the case.

This is the image of a cat: It has a prediction of 0

However the bar chart is different:

Is it possible to use TTA with a specific image? I took a picture of my roommate’s cat Missy and resnet34 seems to think it is a dog (at 77.6% confidence!).

My roommate would not be pleased with resnet34 if she found this out, so I’m trying to think of ways to optimize before I produce the results for her.

Obviously, I have to be mindful of not overfitting the network to Missy, but thought I’d see if things get better with TTA. I can’t tell if this capability already exists in the fastai framework, but if not I can take a crack at writing a TTA-type wrapper around predict_array.

Before looking closer at the image, I thought it was a dog too :slight_smile:

You would need to train the model on more images like this with data augmentation (including perhaps lightening the image up within some range). Remember, that the golden rule with using pre-trained models is that the more similar they are to what the pre-trained model was trained on, the less likely you will have to train any layers except the last. The less similar the images, the more likely it will be that training more layers over more epochs will be helpful.

In this case, your image is pretty different from the ImageNet set and so I think you can improve by a) getting more similar images to this and adding them into your /cats folder, b) data augmentation, and c) training earlier layers.

2 Likes

Only way to use TTA with missy right now is to put her in a test set. I agree predict_array_TTA() would be a nice addition :slight_smile:

2 Likes

Interestingly, I retrained the model on resnext50 and now it’s properly classifying Missy as a cat with 99.7% confidence!

2 Likes

Hello @yinterian,

I have a question about passing test_name='test' AFTER learning the model. It is too late and I must rerun all my jupyter notebook or I can learn my model without this argument and passed it after ? (if yes, how ?). Thank you.

1 Like

I ran into the same problem in my experiments. @jeremy, since more often that not TTA yields better results, do you think the predict_array function could be improved to take additional parameters tta=True, n_aug=4 and get away from adding a new func or do you suggest predict_array_TTA ?

Keeping it consistent with predict seems like a good idea…

Not sure why this error is occuring. Code worked fine before, im stumped!

#0 = capsules
#1 = tablets
trn_tfms, val_tfms = tfms_from_model(arch, sz)
im = val_tfms(PIL.Image.open(test_image))
preds = learn.predict_array(im[None])
np.argmax(preds)

This is the error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-99-f8c7aacfaae1> in <module>()
      2 #1 = tablets
      3 trn_tfms, val_tfms = tfms_from_model(arch, sz)
----> 4 im = val_tfms(PIL.Image.open(test_image))
      5 preds = learn.predict_array(im[None])
      6 np.argmax(preds)

/output/fastai/transforms.py in __call__(self, im, y)
    448         if crop_type == CropType.NO: crop_tfm = NoCropXY(sz, tfm_y)
    449         self.tfms = tfms + [crop_tfm, normalizer, channel_dim]
--> 450     def __call__(self, im, y=None): return compose(im, y, self.tfms)
    451 
    452 

/output/fastai/transforms.py in compose(im, y, fns)
    429 def compose(im, y, fns):
    430     for fn in fns:
--> 431         im, y =fn(im, y)
    432     return im if y is None else (im, y)
    433 

/output/fastai/transforms.py in __call__(self, x, y)
    223     def __call__(self, x, y):
    224         self.set_state()
--> 225         x,y = ((self.transform(x),y) if self.tfm_y==TfmType.NO
    226                 else self.transform(x,y) if self.tfm_y==TfmType.PIXEL
    227                 else self.transform_coord(x,y))

/output/fastai/transforms.py in transform(self, x, y)
    231 
    232     def transform(self, x, y=None):
--> 233         x = self.do_transform(x)
    234         return (x, self.do_transform(y)) if y is not None else x
    235 

/output/fastai/transforms.py in do_transform(self, x)
    312 
    313     def do_transform(self, x):
--> 314         return scale_min(x, self.sz)
    315 
    316 

/output/fastai/transforms.py in scale_min(im, targ)
      9         targ (int): target size
     10     """
---> 11     r,c,*_ = im.shape
     12     ratio = targ/min(r,c)
     13     sz = (scale_to(c, ratio, targ), scale_to(r, ratio, targ))

AttributeError: 'JpegImageFile' object has no attribute 'shape'
1 Like

I believe needs to be im = val_tfms(np.array(PIL.Image.open(test_image))), to convert the PIL Image to Numpy array before passing to transforms functiion.

10 Likes

@ramesh thank you that worked!

I have the following to get my prediction for a single image. How can I get the reference to the classification for the predictions?

Here are a few of the possible classifications

Are the classifications still available through learn.data.classes in your code?

Yes I do. When I use argmax, I can get the most likely class out of there, but not sure how to get all the non-zero predicted values.

Also, I am now seeing this error. I am using the same values as my successful runs, but about 10x the amount of data. There are about 200k in this dataset so finding an error in the CSV is proving tough. ny thoughts on what the issue might be here?

~/fastai/courses/dl1/fastai/dataset.py in <listcomp>(.0)
 64     skip = 1 if skip_header else 0
 65     csv_lines = [o.strip().split(',') for o in open(fn)][skip:]
---> 66     fnames = [fname for fname, _ in csv_lines]
 67     csv_labels = {a:b.split(' ') for a,b in csv_lines}
 68     all_labels = sorted(list(set(p for o in csv_labels.values() for p in o)))
ValueError: too many values to unpack (expected 2)

(fastai) ubuntu@ip-172-31-34-254:~/data/shopstyle$ wc -l prod_train.csv
198869 prod_train.csv
(fastai) ubuntu@ip-172-31-34-254:~/data/shopstyle$ ls train/ | wc -l
198869

This happened to be a single line with a comma in the tags…something to add to my generator. :slight_smile:

Still looking for info on how to grab all the classes that have a non-zero prediction for an image.

Also, does anyone have a code example of service prediction results via an endpoint? That is my use case here.

Birch

One more question related to this.

I have all my images in the ‘train’ folder. my CSV has the image id and the tags separated by space, all that is good. What should be in the test directory? In the planet data directory, there are almost as many in test as there are in train, and I am not seeing a test csv. Is the fastai library automatically spitting the training/test set for me? If so, why did it complain that the test dir was empty before I added stuff to it?

Ah. In that case, wouldn’t you just use zip(* to transpose the columns out, or just iterate through the rows and columns? (I guess there’s a more elegant solution …)

I haven’t tried to connect all this to a service, would be interested to hear how you get on with it :slight_smile:

I wouldn’t have thought that the test set would be mandatory, what’s the message you’re getting?