CIFAR-10 Image Classification
Data Preprocessing
Using tensorflow and keras, load the cifar-10 dataset into a (trainX,trainY) (testX,testY) split. Then normalize the pixels of trainX and testX to the range [0,1] to improve model stability.
The possible labels for this dataset of images are [‘Airplane’,‘Automobile’,‘Bird’,‘Cat’,‘Deer’,‘Dog’,‘Frog’,‘Horse’,‘Ship’,‘Truck’]. Next, encode the labels (trainY, testY) into a 10 dimensional vector to do multiclass classification. Visuals of 16 labeled images are plotted using pyplot.
Model Initialization
Since the dataset consists of images, a convoluted neural network is used to classify data. The general process of creating the model is as follows:
- create 2 convoluted layers with
reluactivation to extract spatial features (32, (3,3)) - add
MaxPooling2Dto reduce the feature map size - use
Dropoutto prevent overfitting by disabling neurons randomly when training (0.25 until flattening layers) - create 2 more convoluted layers (64,(3,3)) with
MaxPooling2DandDropoutlayers added after - create 2 more convoluted layers (128,(3,3)) with
MaxPooling2DandDropoutlayers added after - flatten the layers with
model.add(layers.Flatten()) - add a
Denselayer with 512 andreluactivation - add the final
Dropoutlayer with 0.5 - add the final
Denselayer with 10 andsoftmaxactivation (used to get probabilty scores for classes)
Then, compile the model with the adam optimizer, accuracy metric, and categorical_crossentropy as the loss function.

Model Training
The CNN model is trained with:
- 30
epochs(cycles of training) batch_sizeof 64 (meaning 64 images per batch for the best gradient updates)validation_splitto help with overfitting & generalization The loss and accuracy results are stored in thehistoryvariable for plotting model accuracy and loss.

After 20 epochs, accuracy is 0.8182, loss is 0.5139, validation_accuracy is 0.7934 and validation_loss is 0.6363.

After all 30 epochs, accuracy is 0.8439, loss is 0.45, validation_accuracy is 0.08007 and validation_loss is 0.6123.

The more training cycles, the less training and validation loss. Similarly the more training cycles, accuracy is higher for both training and validation data.
Overall, model accuracy is 84% and model validation_accuracy is 80%, with loss averaging about 0.62.
Model Prediction
Recall that the possible labels are [‘Airplane’,‘Automobile’,‘Bird’,‘Cat’,‘Deer’,‘Dog’,‘Frog’,‘Horse’,‘Ship’,‘Truck’]. The process for classifying an image is as follows:
- get an image from
testX - get the true class label from
testY - calculate the model prediction (the index of the label)
- using the index, get the predicted label
The model correctly labels all 5 test images.
