Comparing ANNs and CNNs in Image Recognition: A Hands-On Project with CIFAR-10 Dataset
Have you ever considered how Artificial Neural Networks (ANN) compare to Convolutional Neural Networks (CNN) when it comes to image classification? In this article, we will delve into a practical project where we train both ANN and CNN models using the CIFAR-10 dataset. We'll also create an interactive user interface (UI) that allows you to upload your own images and observe how each model performs in real-time. Let's get started by breaking down the process step by step. First, we need to import the necessary libraries. These libraries will help us handle data, build models, and create the interactive UI: python import numpy as np import matplotlib.pyplot as plt import seaborn as sns from tensorflow.keras import layers, models from tensorflow.keras.datasets import cifar10 import ipywidgets as widgets from IPython.display import display, clear_output from PIL import Image import io import os Next, we load and preprocess the CIFAR-10 dataset. The CIFAR-10 dataset consists of 50,000 training images and 10,000 testing images, each of which belongs to one of ten classes, such as airplane, automobile, bird, cat, deer, dog, frog, horse, ship, and truck. Preprocessing involves normalizing the pixel values and splitting the dataset into training and validation sets to ensure our models can generalize well to unseen data. ```python Load the CIFAR-10 dataset (train_images, train_labels), (test_images, test_labels) = cifar10.load_data() Normalize pixel values to the range [0, 1] train_images, test_images = train_images / 255.0, test_images / 255.0 Define the class names class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] ``` To evaluate our models effectively, we split the training data into a training set and a validation set. This helps us monitor the performance of our models during training and avoid overfitting. ```python Split the training data into training and validation sets validation_images = train_images[-10000:] validation_labels = train_labels[-10000:] train_images = train_images[:-10000] train_labels = train_labels[:-10000] ``` Now that the dataset is ready, we can proceed to build our models. First, let's construct an Artificial Neural Network (ANN) model. ANNs are basic machine learning models that mimic the structure of the human brain. They are effective for various tasks but may struggle with complex image data due to their inability to capture spatial hierarchies. ```python Build the ANN model model_ann = models.Sequential() model_ann.add(layers.Flatten(input_shape=(32, 32, 3))) model_ann.add(layers.Dense(128, activation='relu')) model_ann.add(layers.Dense(10, activation='softmax')) model_ann.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) Train the ANN model history_ann = model_ann.fit(train_images, train_labels, epochs=10, validation_data=(validation_images, validation_labels)) ``` Once the ANN is trained, we can evaluate its performance on the test set. ```python Evaluate the ANN model test_loss_ann, test_acc_ann = model_ann.evaluate(test_images, test_labels) print(f"Test accuracy (ANN): {test_acc_ann}") ``` Next, we build a Convolutional Neural Network (CNN) model. CNNs are specifically designed to handle image data by capturing spatial hierarchies through convolutional layers. They are highly effective for image classification tasks. ```python Build the CNN model model_cnn = models.Sequential() model_cnn.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))) model_cnn.add(layers.MaxPooling2D((2, 2))) model_cnn.add(layers.Conv2D(64, (3, 3), activation='relu')) model_cnn.add(layers.MaxPooling2D((2, 2))) model_cnn.add(layers.Conv2D(64, (3, 3), activation='relu')) model_cnn.add(layers.Flatten()) model_cnn.add(layers.Dense(64, activation='relu')) model_cnn.add(layers.Dense(10, activation='softmax')) model_cnn.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) Train the CNN model history_cnn = model_cnn.fit(train_images, train_labels, epochs=10, validation_data=(validation_images, validation_labels)) ``` After training the CNN, we evaluate its performance on the test set. ```python Evaluate the CNN model test_loss_cnn, test_acc_cnn = model_cnn.evaluate(test_images, test_labels) print(f"Test accuracy (CNN): {test_acc_cnn}") ``` To make the comparison more engaging, we create a simple UI using the ipywidgets library. This UI allows users to upload their own images and see how both the ANN and CNN models classify them. ```python Create the interactive UI def predict_image(model, img_path): img = Image.open(img_path) img = img.resize((32, 32)) img = np.array(img) / 255.0 img = np.expand_dims(img, axis=0) prediction = model.predict(img) return class_names[np.argmax(prediction)], np.max(prediction) def on_uploadchange(change): uploaded_file = change['new'][0] img_path = os.path.join(uploaded_file.name) result_ann, confidence_ann = predict_image(model_ann, img_path) result_cnn, confidence_cnn = predict_image(model_cnn, img_path) # Display the uploaded image and predictions clear_output(wait=True) img = Image.open(img_path) display(img) print(f"ANN prediction: {result_ann}, Confidence: {confidence_ann:.2f}") print(f"CNN prediction: {result_cnn}, Confidence: {confidence_cnn:.2f}") upload_widget = widgets.FileUpload( accept='image/*', # Accepted file extensions multiple=False, # Allow only single file uploads layout=Layout(width='30%') ) upload_widget.observe(on_uploadchange, names='value') display(upload_widget) ``` This code sets up a file upload widget in the Jupyter notebook. When an image is uploaded, it is resized to match the input dimensions of the models, normalized, and then fed through both the ANN and CNN. The predictions and their confidence levels are displayed alongside the uploaded image. By comparing the test accuracies of the ANN and CNN models, we gain insights into their respective strengths and weaknesses. Typically, CNNs outperform ANNs in image classification tasks due to their ability to capture intricate patterns and features in the data. This hands-on project not only demonstrates the power of deep learning models but also shows the practical steps involved in building and evaluating them. Feel free to experiment with different configurations and parameters to further improve the performance of both models. Happy coding!
