0
$\begingroup$

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.add(Flatten(input_shape = (28,28,3)))
model.add(Dense(128,activation = 'relu'))
model.add(Dense(64, activation = 'relu'))
model.add(Dense(16, activation = 'relu'))
model.add(Dense(3, activation = 'softmax'))

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]
$\endgroup$
5
  • $\begingroup$ 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 $\endgroup$ May 7, 2020 at 7:00
  • $\begingroup$ I know that, I did that because there was some error in the original validation_directory. $\endgroup$ May 7, 2020 at 12:30
  • $\begingroup$ @ValentinClomme there are around 600 examples for each of them. $\endgroup$ May 7, 2020 at 12:31
  • $\begingroup$ Ok, so it's not about class imbalance. What is the actual dimension of the output? $\endgroup$ May 7, 2020 at 13:30
  • $\begingroup$ I got it plz see my answer $\endgroup$ May 10, 2020 at 6:11

1 Answer 1

0
$\begingroup$

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!

$\endgroup$
1
  • $\begingroup$ This is exactly the same code as the one in the question. Have you forgotten to put -s at the end of scissors again? And by class_mode you mean classes? $\endgroup$
    – Miss.Alpha
    Oct 11, 2022 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.