Home > Software engineering >  Accuracy is zero for cifar10 dataset with Keras Sequential Model
Accuracy is zero for cifar10 dataset with Keras Sequential Model

Time:05-04

Surprisingly, the Accuracy is zero for all the 15 epochs inspite-of using multiple Conv2D and Max Pooling Layers. I am using ImageDataGenerator for Data Augmentation.

Request someone to help me. Thank you in advance.

Complete code is given below:

# importing all the required libraries
import tensorflow as tf
from tensorflow.keras.layers import Dense, Conv2D, Flatten, MaxPool2D, Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt

# Loading the Data from the in built library
(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()

# Normalize the Pixel Data
train_images = train_images/255.0
test_images = test_images/255.0

# Instantiate the Image Data Generator Class with the Data Augmentation
datagen = ImageDataGenerator(width_shift_range = 0.2, height_shift_range = 0.2, 
                             rotation_range = 20, horizontal_flip = True, 
                             vertical_flip = True, validation_split = 0.2)

# Apply the Data Augmentation to the Training Images
datagen.fit(train_images)

# Create the Generator for the Training Images
train_gen = datagen.flow(train_images, train_labels, batch_size = 32, 
                         subset = 'training')

# Create the Generator for the Validation Images
val_gen = datagen.flow(train_images, train_labels, batch_size = 8, 
                         subset = 'validation')

num_classes = 10

# One Hot Encoding of Labels using to_categorical
train_labels = to_categorical(train_labels, num_classes)
test_labels = to_categorical(test_labels, num_classes)

img_height = 32
img_width = 32

# Building the Keras Model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(MaxPool2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPool2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
#model.add(Dropout(rate = 0.2))
model.add(Dense(units = num_classes, activation = 'softmax'))
model.summary()

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

steps_per_epoch = len(train_images) * 0.8//32

history = model.fit(train_gen, validation_data = val_gen, 
          steps_per_epoch = steps_per_epoch, epochs = 15)

CodePudding user response:

Your problem is you ran this code

train_gen = datagen.flow(train_images, train_labels, batch_size = 32, 
                         subset = 'training')

# Create the Generator for the Validation Images
val_gen = datagen.flow(train_images, train_labels, batch_size = 8, 
                         subset = 'validation')

but only after this did you convert the labels to categorical. So take the code

num_classes = 10

# One Hot Encoding of Labels using to_categorical
train_labels = to_categorical(train_labels, num_classes)
test_labels = to_categorical(test_labels, num_classes)

and place it PRIOR to the train_gen and val_gen code. On a finer point you have the code

datagen.fit(train_images)

You only need to fit the generator is you have any of the parameters featurewise_center, samplewise_center, featurewise_std_normalization, or samplewise_std_normalization set to true.

CodePudding user response:

Transform your label to one hot right before the .flow.

...
# One Hot Encoding of Labels using to_categorical
train_labels = to_categorical(train_labels, num_classes)
test_labels = to_categorical(test_labels, num_classes)

# Create the Generator for the Training Images
train_gen = datagen.flow(train_images, train_labels, batch_size = 32, 
                         subset = 'training')

# Create the Generator for the Validation Images
val_gen = datagen.flow(train_images, train_labels, batch_size = 8, 
                         subset = 'validation')
...
  • Related