# predict_classes() returning only 0 or 1 for multiclass image classification

I am trying to build a multi class image classifier but the only returns 0 or 1 .

Why is it not returning "Rock" , "Paper" , "Scissor" ? and why only 0 and 1 but not 2?

CODE:

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense, Flatten
import numpy as np

train_directory = 'D:\D_data\Rock_Paper_Scissors\Train'
training_datgagen = ImageDataGenerator(rescale = 1./255)
training_generator = training_datgagen.flow_from_directory(
train_directory,
target_size = (28,28),
class_mode = 'categorical', classes = ["Rock", "Paper" , "Scissor"])

validation_directory = 'D:\D_data\Rock_Paper_Scissors\Train'
validation_datagen = ImageDataGenerator(rescale= 1./255)
validation_generator = validation_datagen.flow_from_directory(
validation_directory,
target_size = (28,28),
class_mode = 'categorical',
classes = ["Rock", "Paper" , "Scissor"]
)

model = Sequential()

model.compile(optimizer = 'adam', loss = 'categorical_crossentropy',metrics = ['accuracy'],)
filenames = validation_generator.filenames
nb_samples = len(filenames)
desired_batch_size = 1

model.fit_generator(training_generator,epochs=20,validation_data = validation_generator)
predict = model.predict_classes(validation_generator, batch_size = None)

print(predict)


Output:

[0 1 1 ... 1 0 1]

• By the way, your validation directory is the same as your training one. Besides, how many examples do you have for each class? It might just be that the scissor class is less represented – Valentin Calomme May 7 '20 at 7:00
• I know that, I did that because there was some error in the original validation_directory. – Ankit Chawla May 7 '20 at 12:30
• @ValentinClomme there are around 600 examples for each of them. – Ankit Chawla May 7 '20 at 12:31
• Ok, so it's not about class imbalance. What is the actual dimension of the output? – Valentin Calomme May 7 '20 at 13:30
• I got it plz see my answer – Ankit Chawla May 10 '20 at 6:11

I got the answer so I'm posting it for future reference for others:

The 3 classes defined in

train_directory = 'D:\D_data\Rock_Paper_Scissors\Train'
training_datgagen = ImageDataGenerator(rescale = 1./255)
training_generator = training_datgagen.flow_from_directory(
train_directory,
target_size = (28,28),
class_mode = 'categorical', classes = ["Rock", "Paper" , "Scissor"])


in class_mode should be Rock , Paper , "Scissors" those were the names of the folders i create but i made a mistake and spelled "Scissors" wrong in the code.

Thanks a lot to everyone who commented!