Cat and Dog Problem at Prediction Stage (last two cells)

I’m trying to predict using my model, which I managed to bring upto 70% accuracy. I tried googling, and stackoverlow-ing but cant seem to find any relevant solution. Please, I need help in solving these last 2 cells.

The error I get on the prediction (2nd last) cell is:

ValueError: Asked to retrieve element 0, but the Sequence has length 0

And for the very last cell, I get this error:

NameError: name ‘probabilities’ is not defined

Here is the link to my Google Colab for reference:

I just ran the entire thing no problem. You sure you didn’t just forget to run the cells where these object are initialized?

Yes, I just fixed it, was a about to update my own post now, lol.

Just found the solution myself. But for those who will face similar problems as mine in the future, I hope this solution finds them helpful. Basically I had to change the code of cell 3. Instead of this code:

test_data_gen = test_image_generator.flow_from_directory(batch_size=batch_size,directory=test_dir,target_size=(IMG_HEIGHT,IMG_WIDTH),classes=['test'],class_mode=None,shuffle=False)

I had to use this code:

test_data_gen = test_image_generator.flow_from_directory(batch_size=1,directory=PATH,target_size=(IMG_HEIGHT,IMG_WIDTH),classes=['test'],class_mode=None,shuffle=False)

and on the prediction cell, I had to use the following code:

pred=model.predict(test_data_gen, verbose=1)
predicted_class = np.argmax(model.predict(test_data_gen), axis=-1)
probabilities = predicted_class.tolist()
actual_probabilities = [np.max(vector) for vector in pred]
plotImages([test_data_gen[i][0] for i in range(50)], probabilities=probabilities)
1 Like