Data Science Asked on December 8, 2020
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_dataRock_Paper_ScissorsTrain'
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_dataRock_Paper_ScissorsTrain'
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]
I got the answer so I'm posting it for future reference for others:
The 3 classes defined in
train_directory = 'D:D_dataRock_Paper_ScissorsTrain'
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!
Answered by Ankit Chawla on December 8, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP