This project implements a Convolutional Neural Network (CNN) to classify images from the CIFAR-10 dataset. The model is built using TensorFlow and Keras.
The script cnn_image_classification.py performs the following steps:
- Loads the CIFAR-10 dataset: This dataset consists of 60,000 32x32 color images in 10 classes.
- Preprocesses the data: It normalizes pixel values to be between 0 and 1 and one-hot encodes the labels.
- Builds a CNN model: The architecture consists of multiple convolutional and pooling layers, followed by dense layers for classification.
- Trains the model: The model is trained on the training set, using a portion of it for validation. Early stopping is used to prevent overfitting.
- Evaluates the model: The trained model is evaluated on the test set to measure its performance.
- Visualizes results: It plots the training and validation accuracy and loss over epochs.
- Saves the model: The final trained model is saved to
cnn_cifar10_model.h5.
The CIFAR-10 dataset is used for training and testing the model. It contains 10 classes:
- airplane
- automobile
- bird
- cat
- deer
- dog
- frog
- horse
- ship
- truck
The CNN model has the following structure:
- Conv2D Layer: 32 filters, (3, 3) kernel, ReLU activation.
- MaxPooling2D Layer: (2, 2) pool size.
- Conv2D Layer: 64 filters, (3, 3) kernel, ReLU activation.
- MaxPooling2D Layer: (2, 2) pool size.
- Conv2D Layer: 128 filters, (3, 3) kernel, ReLU activation.
- MaxPooling2D Layer: (2, 2) pool size.
- Flatten Layer: To flatten the 3D feature maps into a 1D vector.
- Dense Layer: 128 units, ReLU activation.
- Dropout Layer: Dropout rate of 0.5 for regularization.
- Dense Layer (Output): 10 units, softmax activation for multi-class classification.
To run this project, you need Python and the following libraries:
- TensorFlow
- Matplotlib
You can install them using pip:
pip install tensorflow matplotlibTo run the script, execute the following command in your terminal:
python cnn_image_classification.pyThe script will train the model, display the summary and progress, show the performance plots, and save the trained model as cnn_cifar10_model.h5.
The model's performance is evaluated on the unseen test data using two key metrics:
- Test Loss: This value represents the average error of the model on the test set. A lower loss indicates better performance. The loss function used is
categorical_crossentropy, which is suitable for multi-class classification problems. - Test Accuracy: This is the percentage of images in the test set that the model correctly classifies. A higher accuracy is desirable.
The script generates two plots to visualize the model's performance during training:
-
Model Accuracy Over Epochs: This plot shows the training and validation accuracy for each epoch.
- Training Accuracy: The accuracy of the model on the data it was trained on.
- Validation Accuracy: The accuracy of the model on a separate validation set that was not used for training. This provides an unbiased estimate of the model's performance.
- Ideally, both accuracies should increase over time. A large gap between training and validation accuracy can indicate overfitting.
-
Model Loss Over Epochs: This plot displays the training and validation loss for each epoch.
- Training Loss: The error of the model on the training data.
- Validation Loss: The error of the model on the validation data.
- Both loss values should ideally decrease. If the validation loss starts to increase while the training loss decreases, it is a strong sign of overfitting. The early stopping callback is configured to monitor the validation loss and stop training when it no longer improves, preventing overfitting.
The script will output:
- The test accuracy and loss of the model.
- A plot showing the training and validation accuracy over epochs.
- A plot showing the training and validation loss over epochs.
- The saved model file
cnn_cifar10_model.h5.
- Hyperparameter Tuning: Experiment with different learning rates, batch sizes, and number of epochs.
- Data Augmentation: Use techniques like random rotations, shifts, and flips to increase the diversity of the training data and improve generalization.
- Architecture Enhancement: Try different architectures, such as deeper networks or using pre-trained models (transfer learning).