0

I am a beginner to CNN and using tensorflow in general. I have been referring to this image classification guide to train and classify my own dataset. I have 84310 images in 42 classes for the train set and 21082 images in 42 classes for the validation set.

Some parameters:

batch_size = 384 epochs = 15 IMG_HEIGHT = 150 IMG_WIDTH = 150

Relevant Code:

train_image_generator = ImageDataGenerator(rescale=1./255) # Generator for 
our training data

validation_image_generator = ImageDataGenerator(rescale=1./255) # Generator 
for our validation data

train_data_gen = 
train_image_generator.flow_from_directory(batch_size=batch_size,
                                          directory=train_dir,
                                          shuffle=True,
                                          target_size= 
                                          (IMG_HEIGHT,IMG_WIDTH),                                                           
                                          class_mode='categorical')

val_image_generator.flow_from_directory(batch_size=batch_size,
                                          directory=validation_dir,
                                          target_size= 
                                          (IMG_HEIGHT,IMG_WIDTH),                                                           
                                          class_mode='categorical')

model = Sequential([
Conv2D(16, 3, padding='same', activation='relu', input_shape=(IMG_HEIGHT, 
IMG_WIDTH ,3)),
MaxPooling2D(),
Conv2D(32, 3, padding='same', activation='relu'),
MaxPooling2D(),
Conv2D(64, 3, padding='same', activation='relu'),
MaxPooling2D(),
Flatten(),
Dense(512, activation='relu'),
Dense(1)
])

model.compile(optimizer='adam',
          loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
          metrics=['accuracy'])


history = model.fit(
   train_data_gen,
   steps_per_epoch=84310// batch_size,
   epochs=epochs,
   validation_data=val_data_gen,
   validation_steps=21082 // batch_size)

model.summary() gives

Model: "sequential_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_12 (Conv2D)           (None, 150, 150, 16)      448       
_________________________________________________________________
max_pooling2d_12 (MaxPooling (None, 75, 75, 16)        0         
_________________________________________________________________
conv2d_13 (Conv2D)           (None, 75, 75, 32)        4640      
_________________________________________________________________
max_pooling2d_13 (MaxPooling (None, 37, 37, 32)        0         
_________________________________________________________________
conv2d_14 (Conv2D)           (None, 37, 37, 64)        18496     
_________________________________________________________________
max_pooling2d_14 (MaxPooling (None, 18, 18, 64)        0         
_________________________________________________________________
flatten_4 (Flatten)          (None, 20736)             0         
_________________________________________________________________
dense_8 (Dense)              (None, 512)               10617344  
_________________________________________________________________
dense_9 (Dense)              (None, 1)                 513       
=================================================================
Total params: 10,641,441
Trainable params: 10,641,441
Non-trainable params: 0
_________________________________________________________________

I have made sure to change the class mode in my image data generator to categorical but my concern is that the loss and accuracy of my model is firstly, unchanging and secondly, the train and validation loss and accuracy values are also exactly the same :

Epoch 1/15 219/219 [==============================] - 2889s 13s/step - loss: 0.1264 - accuracy: 0.9762 - val_loss: 0.1126 - val_accuracy: 0.9762

Epoch 2/15 219/219 [==============================] - 2943s 13s/step - loss: 0.1126 - accuracy: 0.9762 - val_loss: 0.1125 - val_accuracy: 0.9762

Epoch 3/15 219/219 [==============================] - 2866s 13s/step - loss: 0.1125 - accuracy: 0.9762 - val_loss: 0.1125 - val_accuracy: 0.9762

Epoch 4/15 219/219 [==============================] - 3036s 14s/step - loss: 0.1125 - accuracy: 0.9762 - val_loss: 0.1126 - val_accuracy: 0.9762

Epoch 5/15 219/219 [==============================] - ETA: 0s - loss: 0.1125 - accuracy: 0.9762

May I get pointed in the right direction as to why I am facing this problem or if this is even a problem in the first place? Thank you for your time!

  • Your network is bugged. You have 42 classes but your network outputs 1 float for each sample. You should output 42 floats and use a cross-entropy function that supports models with 3 or more classes. – Sycorax Jun 22 '20 at 17:23

1 Answers1

0

Since there are 42 classes to be classified into don't use binary cross entropy Use

loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True)