Lauqz
Lauqz

Reputation: 45

Keras Image Classification Loss

I am trying to make a simple CNN model which can recognize Pokemons. For the first try, I created by myself a very small dataset, composed of 100 pictures of 10 different Pokemons. Using this code in Python, it seems like it is working well.

import tensorflow as tf
import numpy as np

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(32, (3,3), input_shape=(200,200,3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D((2,2)))
model.add(tf.keras.layers.Conv2D(32, (3,3), activation='relu'))
model.add(tf.keras.layers.MaxPooling2D((2,2)))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(units=400, activation='relu'))
model.add(tf.keras.layers.Dense(units=10, activation='sigmoid'))

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

train = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
test = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)

training_set= train.flow_from_directory('datasets/starter/train', target_size=(200,200), class_mode='categorical')
val_set= test.flow_from_directory('datasets/starter/test', target_size=(200,200), class_mode='categorical')

history=model.fit_generator(training_set, steps_per_epoch=32, epochs=3, validation_data=val_set, validation_steps=32)

test_image = tf.keras.preprocessing.image.load_img('datasets/starter/val/attempt.png', target_size=(200, 200))
test_image = tf.keras.preprocessing.image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
result = model.predict(test_image)
print(training_set.class_indices)
print(result)

test_image2 = tf.keras.preprocessing.image.load_img('datasets/starter/val/attempt2.png', target_size=(200, 200))
test_image2 = tf.keras.preprocessing.image.img_to_array(test_image2)
test_image2 = np.expand_dims(test_image2, axis=0)
result2 = model.predict(test_image2)
print(training_set.class_indices)
print(result2)

The training accuracy at the last epoch is fixed at 1. When I try to predict the example images: attempt.png is a Charmander picture, its label is 1, so I am getting this vector: [[0. 1. 0. 0. ... 0.]] attempt2.png is a Torchic picture, its label is 7, so I am getting: [[0. 0. ... 1. 0. 0. ]]

But I noticed that the loss in the 'model.compile' should be 'categorical_crossentropy' and not 'binary_crossentropy'. Using the categorical one, my program will not work anymore. Can someone help me to understand?

Upvotes: 1

Views: 459

Answers (1)

Cambala
Cambala

Reputation: 26

You should try to use Softmax as the last activation with categorical crossentropy as loss function

Upvotes: 1

Related Questions