0

I am trying to get the images from a list of predictions called 'classes'

classes = model.predict(test_set)

and to match the result with the picture, I am finding a little troubling to do. Below is what I have done to try and show the images with the result.

location = 2  #Iterate through list of predictions.
print(categories[np.argmax(classes[location])]) #Shows result from prediction
plt.figure()    
plt.imshow(test_set[location])  # Shows image

The error I get doing this is: "could not broadcast input array from shape (32,224,224,3) into shape (32,)" if I do

plt.imshow(classes[location])  # Shows image

then I get: TypeError: Invalid shape (2,) for image data

1 Answers1

1

Your image data may need to be processed to the appropriate format (see article). Try this:

plt.imshow(test_set[location].numpy().astype("uint8"))

Also, your classes[location] is not an image, so you probably should not be trying to pass it to plt.imshow. Instead, it is the prediction result. I suspect it is the softmax probability vector with two elements (?for binary classification you may be attempting).

Snehal Patel
  • 192
  • 10
  • Thank you. I'm quite new to it and using google.colab. I am finding it hard to know what each object type is (unlike vs). I implemented your code and it returned: "AttributeError: 'tuple' object has no attribute 'numpy'" – randomUser1212421 Oct 16 '22 at 17:42
  • About the softmax probability vector. I believe so. It returns an accuracy and a loss when i check the classes. – randomUser1212421 Oct 16 '22 at 17:46
  • 1
    @randomUser1212421, if you are using a public dataset, then it would be helpful if you share the name and location/URL of the dataset to better help you Otherwise, start by using `type()` to determine the data type of `test_set` and `test_set[location]`. – Snehal Patel Oct 16 '22 at 18:05
  • Good Idea, I am using a dataset from Kaggle: https://www.kaggle.com/datasets/constantinwerner/human-detection-dataset I put it into 3 folders, test, validation, train. – randomUser1212421 Oct 16 '22 at 18:36
  • I implemented the 'type()' (thankyou for providing that) and it showed that the 'test_set' is a keras.preprocessing.image.DirectoryIterator and classes is a numpy.ndarray. – randomUser1212421 Oct 16 '22 at 18:40
  • @randomUser1212421, good job on getting data types and thanks for providing the dataset source. Your dataset is in the form of an iterator, which means that you must apply the `.take()` method to first extract a batch of images. The batch size will be that of the default, unless you specified a batch size when you read in the data. If you follow the article link I provided in my answer, you will see how to do this. – Snehal Patel Oct 16 '22 at 18:48
  • I just got back on this. How would I use the ".take()"? Can I use it on the list of predictions? for example: classes = model.predict(test_set).take() – randomUser1212421 Nov 06 '22 at 16:54