Predicting in Lesson 1

I am trying to modify lesson 1 to do predictions of some sample images I got off the internet and I get an error.
I tried doing a test on the test data set and that doesn’t work either.

I’m using:

vgg.test(path+'test1', batch_size=4)

I get the following error:

In [36]: vgg.test(path+‘test1’, batch_size=4)
Found 0 images belonging to 0 classes.
Out[36]: Exception in thread Thread-45:
Traceback (most recent call last):
File “c:\toolkits\anaconda2\lib\threading.py”, line 801, in __bootstrap_inner
self.run()
File “c:\toolkits\anaconda2\lib\threading.py”, line 754, in run
self.__target(*self.__args, **self.__kwargs)
File “c:\toolkits\keras-1.0.5\keras\engine\training.py”, line 409, in data_generator_task
generator_output = next(generator)
File “c:\toolkits\keras-1.0.5\keras\preprocessing\image.py”, line 682, in next
index_array, current_index, current_batch_size = next(self.index_generator)
File “c:\toolkits\keras-1.0.5\keras\preprocessing\image.py”, line 507, in _flow_index
current_index = (self.batch_index * batch_size) % N
ZeroDivisionError: integer division or modulo by zero
(<keras.preprocessing.image.DirectoryIterator at 0x4af4ef0>, [])

2 Likes

Found 0 images belonging to 0 classes.

It doesn’t see your images I think, try using double quotes on “test 1” or being very explicit about where your data is.
Remember that you need a directory structure that matches the structure that was setup for you, with train and valid folders, and labels in those folders…

(This directory struct isn’t perfect, but you should get the idea)

I am trying to predict, so there would not be any classes defined. From what I can see, vgg.test() is doing a predict, which only requires input images and will predict the classes.

    def test(self, path, batch_size=8):
        test_batches = self.get_batches(path, shuffle=False, batch_size=batch_size, class_mode=None)
        return test_batches, self.model.predict_generator(test_batches, test_batches.nb_sample)

I also tried using vgg.predict() but also got errors (different one).

It also throws an error if it does not find the directory, so I know it is finding the directory.

Oh, sorry, didn’t notice you were using test, my bad. Some better googling revealed that this problem has been encountered a few times before. I’m glad I ran into it now with you as I think I was about to run into it anyway. :smiley:

I ended up doing this exact same thing on my own (created a folder unknown) but it just causes it to predict 1 for all the results, which is pointless.

Then something has gone wrong in your training, or you don’t have the right directory structure. You should review the dogs_cats_redux.ipynb or some of the earlier material, I feel you’re missing something basic.

I had this problem at first, and it was because my test directory structure was incorrect. Your images must be one extra directory deep.

So, if your path is, “data”, and your test command says:

vgg.test(path+'test1', batch_size=4)

then your data should be in a directory called:

data/test1/unknown/image1.jpg
data/test1/unknown/image2.jpg
...etc
10 Likes

I tested some random cat fotos from internet using this code. the images are copied into a folder called “unknown” inside the below directory. this runs after the model is fit and fine tuned. It doesnt give accurate results, maybe because not all the cat types that we test here were used in the training.

filename = 'Images/mytest/'
 
vgg.test(filename, batch_size=4)
 
batches1 = vgg.get_batches(filename, batch_size=batch_size)
imgs1,labels1 =  next(batches1) 

vgg.predict(imgs1, True)

This worked for me too…
Thanks a lot :slightly_smiling_face: