Testing a new image against a model

I have followed the tutorial from Lesson 1, and then created a new (simple) dataset with watches and basketballs (dumb, I know, but just wanted to learn the process). After working through a few issues, mainly bad images, I have a model. Now, I want to test a new image against that model, but can’t figure it out.

I have a new notebook with the following:
from fastai.imports import *
from fastai import *
from fastai.vision import *
from fastai.metrics import error_rate
PATH = “/home/ubuntu/course-v3/nbs/dl1/datasets/basketball_watches”
sz=224
arch=models.resnet34
data = ImageDataBunch.from_folder(PATH, size=sz, test_name=‘test’, ds_tfms=get_transforms())

When I run the cell, I get:
/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/fastai/basic_data.py:226: UserWarning: There seems to be something wrong with your dataset, can’t access any element of self.train_ds.
Tried: 86,114,77,70,110…
warn(warn_msg)

I have also tried an example I found in the docs:
empty_data = ImageDataBunch.load_empty(PATH)
learn = create_cnn(empty_data, models.resnet34)
learn = learn.load(‘ball-watch-stage-1’)

but receive TypeError: unsupported operand type(s) for /: ‘str’ and ‘str’

I am sure this is simple, but I can’t figure it out. Any help is appreciated.

Thanks!

Please try to define your path as:

PATH = Path(“/home/ubuntu/course-v3/nbs/dl1/datasets/basketball_watches”)
Let us know if it works.

Thanks for the quick response. Still get the error when I try the below:

PATH = Path("/home/ubuntu/course-v3/nbs/dl1/datasets/basketball_watches")
sz=224
arch=models.resnet34
data = ImageDataBunch.from_folder(PATH, size=sz, test_name=‘test’, ds_tfms=get_transforms())

/home/ubuntu/src/anaconda3/envs/fastai/lib/python3.6/site-packages/fastai/basic_data.py:226: UserWarning: There seems to be something wrong with your dataset, can’t access any element of self.train_ds.
Tried: 53,103,121,13,58…
warn(warn_msg)

I printed out PATH and see that the format you suggested does change it from ‘/home/ubuntu/course-v3/nbs/dl1/datasets/basketball_watches’ to PosixPath(’/home/ubuntu/course-v3/nbs/dl1/datasets/basketball_watches’)

The below didn’t error, so I now need to figure out how to predict with a new image…

path = Path("/home/ubuntu/course-v3/nbs/dl1/datasets/basketball_watches/")
empty_data = ImageDataBunch.load_empty(path)
learn = create_cnn(empty_data, models.resnet34)
learn = learn.load(‘ball-watch-stage-1’)

UPDATE:
By watching the 1st 45 minutes of week 3 (course v3), Jeremy goes over this exact scenario. The working code is below:

from fastai.imports import *
from fastai import *
from fastai.vision import *
from fastai.metrics import error_rate

path = Path("/home/ubuntu/course-v3/nbs/dl1/datasets/basketball_watches/")
img = open_image(path/‘test’/‘basketballs’/‘0.jpg’)

classes = [‘basketballs’,‘watches’]
data2 = ImageDataBunch.single_from_classes(path, classes, tfms=get_transforms(), size=224).normalize(imagenet_stats)
learn = create_cnn(data2, models.resnet34)
learn = learn.load(‘ball-watch-stage-1b’)

pred_class, pred_idx, outputs = learn.predict(img)
pred_class

Output: Category basketballs

1 Like

Just out of curiosity, did you use the official tutorial for creating the data set or attempt some of the suggestions from the forum threads?

I’ve used one the techniques that suggested the googleimagessearch package, got a collection images, and verified that the images are actually there.

The problem is that when I go to define a data object, a ‘directory not found’ error message comes back.

I also used the googimagesearch package to create a custom data set. My high-level steps are below:

  1. Download images
  2. rename images (I wrote a python script to do this)
  3. delete images that don’t show up with a thumbnail in Finder (not sure why, but some images are like this and break the training process)
  4. Organize the images into the expected format (data_set/test, train, valid - with subfolders for each class (e.g. rats, basketballs, squirrels, dogs, watches)
  5. Train the model
  6. Save
  7. In new notebook, set the path=Path("/home/ubuntu/course-v3/nbs/dl1/data_set/")
  8. Define the classes = ['basketballs",“dogs”…]
  9. create a databunch --> data = ImageDateBunch.single_from_classes(path, classes, tfms=get_transformations(), size=224).normalize(imagenet_stats)
  10. learn = create_cnn(data, models.resnet34)
  11. load the saved model (?) --> learn.load('file you saved in step 6")
  12. Predict --> img = open_img(‘testimg.jpg’)
    pred_class, pred_idx, outputs = learn.predict(img);pred_class

Hope this helps.

3 Likes

Thanks. Very helpful. Think I forgot step 4.

I was not able to import an image of my dog to compare it to the model. I am using Google colab and every time I change the code around I get another weird error.