CNN vs. ANN: Battle of Neural Networks on CIFAR-10

CNN vs. ANN: Battle of Neural Networks on CIFAR-10

Which neural network reigns supreme? Dive into this comprehensive analysis of Convolutional Neural Networks (CNN) and Artificial Neural Networks (ANN) on the popular CIFAR-10 dataset. Discover the architectural innovations, key performance metrics, and real-world applications that set these models apart in the realm of image classification.

Understanding the CIFAR-10 Dataset

Before delving into the comparison, it is crucial to comprehend the dataset used for this study. The CIFAR-10 dataset is a well-known benchmark for evaluating machine learning models, especially in the field of computer vision.

The dataset consists of 60,000 32×32 color images distributed evenly across 10 distinct classes: airplanes, cars, birds, cats, deer, dogs, frogs, horses, ships, and trucks. Out of these, 50,000 are training images, and 10,000 are test images.

Sample dataset
Sample Images from CIFAR-10 Dataset

Artificial Neural Networks (ANN): The Basics

Artificial Neural Networks (ANN) are computational models inspired by the human brain. They consist of interconnected groups of artificial neurons that process input data to produce an output. Here are some fundamental principles of ANN:

  • Layers: An ANN is structured into input, hidden, and output layers.
  • Neurons: Each layer contains neurons that process inputs and generate outputs.
  • Activation Functions: Functions like ReLU or Sigmoid introduce non-linearity, enabling the network to learn complex patterns.
  • Backpropagation: This learning method adjusts weights based on the error gradient.
ANN Diagram

ANN Architecture Example


ANN = models.Sequential([
    layers.Flatten(input_shape=(32, 32, 3)),
    layers.Dense(3000, activation='relu'),
    layers.Dense(1000, activation='relu'),
    layers.Dense(10, activation='sigmoid')
])
ANN.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
        

Convolutional Neural Networks (CNN): Specialized for Images

Convolutional Neural Networks (CNN) are a special kind of neural network particularly suited for spatial hierarchies in data like images. Key principles of CNNs include:

  • Convolutional Layers: These layers apply convolutional filters to the input to extract features.
  • Pooling Layers: These layers reduce the spatial dimensions, retaining essential information and reducing computational load.
  • Fully Connected Layers: After passing through convolutional and pooling layers, the data is fed into fully connected layers for final predictions.
CNN Diagram

CNN Architecture Example


CNN = models.Sequential([
    layers.Conv2D(input_shape=(32, 32, 3), filters=32, kernel_size=(3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Flatten(),
    layers.Dense(2000, activation='relu'),
    layers.Dense(1000, activation='relu'),
    layers.Dense(10, activation='softmax')
])
CNN.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
        

Training and Evaluation: ANN vs. CNN

Both the ANN and CNN models were trained on the CIFAR-10 dataset for 10 epochs. The ANN has a simpler architecture with dense layers, whereas the CNN harnesses convolutional and pooling layers tailored for image data.


ANN.fit(X_train, y_train, epochs=10)
ANN.evaluate(X_test, y_test)
        
CNN.fit(X_train, y_train, epochs=10)
CNN.evaluate(X_test, y_test)
        
Training ANN Model
Training ANN Model
Training CNN Model
Training CNN Model

Results Comparison

When comparing the performance of the two models, the CNN significantly outperforms the ANN in accuracy and loss metrics on the test data.

ANN Evaluation

  • Accuracy: 0.4960
  • Loss: 1.4678
Test Data Evaluation for ANN Model
Test Data Evaluation for ANN Model

CNN Evaluation

  • Accuracy: 0.7032
  • Loss: 0.8321
Test Data Evaluation for CNN Model
Test Data Evaluation for CNN Model

Confusion Matrices and Classification Reports

To gain deeper insights into the models’ performance, confusion matrices and classification reports were generated:

ANN Confusion Matrix and Report


y_pred_ann = ANN.predict(X_test)
y_pred_labels_ann = [np.argmax(i) for i in y_pred_ann]
plot_confusion_matrix(y_test, y_pred_labels_ann, "Confusion Matrix for ANN")
print("Classification Report for ANN:")
print(classification_report(y_test, y_pred_labels_ann))
        
Confusion Matrix for ANN

CNN Confusion Matrix and Report


y_pred_cnn = CNN.predict(X_test)
y_pred_labels_cnn = [np.argmax(i) for i in y_pred_cnn]
plot_confusion_matrix(y_test, y_pred_labels_cnn, "Confusion Matrix for CNN")
print("Classification Report for CNN:")
print(classification_report(y_test, y_pred_labels_cnn))
        
Confusion Matrix for CNN

The Winner: CNN for Image Classification

In this comparison, the CNN model outshines the ANN model on the CIFAR-10 dataset by effectively capturing spatial hierarchies and local patterns present in image data.

While ANNs are powerful for a broad variety of tasks, CNNs have an edge in applications like image classification, where understanding spatial relationships is crucial. For anyone venturing into the field of computer vision, mastering CNNs is a vital step towards achieving high-performance models.

Interested in more insights, resources, and discussions on Data Science, Machine Learning, and Deep Learning? Check out my GitHub account and connect with me on LinkedIn.

P.S. Claps and follows are highly appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *