-1

My deep learning topic is classifying images into 5 different categories. I used the ImageDataGenerator library to split my dataset into train and test. The negates the need for creating csv file which requires a lot of manual work. I've successfully developed a model architecture following the CNN method and evaluated the performance of my model on a test dataset, which gave me an accuracy of 83%. Now, when I try to predict my model to classify an image, this is the error it gives:

ValueError                                Traceback (most recent call last)
<ipython-input-147-51d5b018a219> in <module>()
     11    images = np.vstack([X])
     12    val = model.predict(images)
---> 13    if val == 0:
     14      print("Clean")
     15    elif val ==1:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Here is my code:

    import os
    test_dir = 'directory containing random images'

    for i in os.listdir(test_dir ):
       img = image.load_img(test_dir+ '/'+i, target_size=(200,200))
       plt.imshow(img)
       plt.show()
     
       X = image.img_to_array(img)
       X = np.expand_dims(X,axis = 0)
       images = np.vstack([X])
       val = model.predict(images)
       if val == 0:
         print("Clean")
       elif val ==1:
         print("Covered In Bird Droppings")
       elif val ==2:
         print ("Covered In Dust")
       elif val ==3:
         print("Covered In Grey Sand")
       elif val ==4:
         print("Covered In Sand") 

I'm quite clueless as to how to fix this.

  • To make easier to people to help you put the whole stacktrace, or at last enough to figure out the line that is raising the error. The error is saying that there is an array with more than one element being used as a boolean value but I cant even guess where is the problem – geckos Sep 23 '20 at 12:09
  • 1
    I did, now. Thank you very much. – Alvi Ahmed Abir Sep 23 '20 at 12:14

1 Answers1

0

If you print the result of model.predict(), you will see that it's not a single value, but a numpy array. Which value of that array is the number you're looking for is something you'll have to find in the documentation. But assuming for example it's the first one, you'd do val = val[0] first.

Itamar Turner-Trauring
  • 3,430
  • 1
  • 13
  • 17