8. Advanced CNN (Build AlexNet using Advanced CNN)

  Building and training an AlexNet-like model using the CIFAR-10 dataset in TensorFlow/Keras:


Program to build the model:


import tensorflow as tf
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split

# Load and preprocess CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
y_train = to_categorical(y_train, num_classes=10)
y_test = to_categorical(y_test, num_classes=10)

# Split into training and validation sets
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.1, random_state=42)

# Define the model architecture
'''model = tf.keras.Sequential([
    Conv2D(96, (11, 11), strides=(4, 4), activation='relu', input_shape=(32, 32, 3), padding='same'),
    MaxPooling2D((3, 3), strides=(2, 2)),
    Conv2D(256, (5, 5), activation='relu', padding='same'),
    MaxPooling2D((3, 3), strides=(2, 2)),
    Conv2D(384, (3, 3), activation='relu', padding='same'),
    Conv2D(384, (3, 3), activation='relu', padding='same'),
    Conv2D(256, (3, 3), activation='relu', padding='same'),
    MaxPooling2D((3, 3), strides=(2, 2)),
    Flatten(),
    Dense(4096, activation='relu'),
    Dropout(0.5),
    Dense(4096, activation='relu'),
    Dropout(0.5),
    Dense(10, activation='softmax')
])'''

model = tf.keras.Sequential([
    Conv2D(96, (3, 3), activation='relu', input_shape=(32, 32, 3), padding='same'),
    MaxPooling2D((2, 2)),
    Conv2D(256, (3, 3), activation='relu', padding='same'),
    MaxPooling2D((2, 2)),
    Conv2D(384, (3, 3), activation='relu', padding='same'),
    Conv2D(384, (3, 3), activation='relu', padding='same'),
    Conv2D(256, (3, 3), activation='relu', padding='same'),
    MaxPooling2D((2, 2)),
    Flatten(),
    Dense(4096, activation='relu'),
    Dropout(0.5),
    Dense(4096, activation='relu'),
    Dropout(0.5),
    Dense(10, activation='softmax')
])


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

# Train the model
batch_size = 900
epochs = 25
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_val, y_val))

# Evaluate the model on test data
test_loss, test_accuracy = model.evaluate(x_test, y_test)
print("Test Accuracy:", test_accuracy)

# Save the trained model
model.save('/content/drive/MyDrive/8/alexnet_cifar10.h5')

O/p:
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz 170498071/170498071 [==============================] - 2s 0us/step Epoch 1/25 50/50 [==============================] - 31s 246ms/step - loss: 2.1391 - accuracy: 0.1900 - val_loss: 1.9311 - val_accuracy: 0.2798 Epoch 2/25 50/50 [==============================] - 11s 211ms/step - loss: 1.6911 - accuracy: 0.3657 - val_loss: 1.4774 - val_accuracy: 0.4374 Epoch 3/25 50/50 [==============================] - 11s 219ms/step - loss: 1.4517 - accuracy: 0.4653 - val_loss: 1.3972 - val_accuracy: 0.4906 Epoch 4/25 50/50 [==============================] - 11s 222ms/step - loss: 1.2210 - accuracy: 0.5542 - val_loss: 1.1366 - val_accuracy: 0.5798 Epoch 5/25 50/50 [==============================] - 11s 225ms/step - loss: 1.0782 - accuracy: 0.6118 - val_loss: 1.0094 - val_accuracy: 0.6370 Epoch 6/25 50/50 [==============================] - 11s 227ms/step - loss: 0.9403 - accuracy: 0.6649 - val_loss: 0.9151 - val_accuracy: 0.6690 Epoch 7/25 50/50 [==============================] - 11s 224ms/step - loss: 0.8453 - accuracy: 0.7017 - val_loss: 0.8317 - val_accuracy: 0.7010 Epoch 8/25 50/50 [==============================] - 11s 223ms/step - loss: 0.7278 - accuracy: 0.7451 - val_loss: 0.7959 - val_accuracy: 0.7212 Epoch 9/25 50/50 [==============================] - 11s 222ms/step - loss: 0.6358 - accuracy: 0.7780 - val_loss: 0.7457 - val_accuracy: 0.7398 Epoch 10/25 50/50 [==============================] - 11s 225ms/step - loss: 0.5576 - accuracy: 0.8053 - val_loss: 0.6974 - val_accuracy: 0.7656 Epoch 11/25 50/50 [==============================] - 11s 220ms/step - loss: 0.4846 - accuracy: 0.8306 - val_loss: 0.6751 - val_accuracy: 0.7732 Epoch 12/25 50/50 [==============================] - 11s 225ms/step - loss: 0.4122 - accuracy: 0.8559 - val_loss: 0.7314 - val_accuracy: 0.7704 Epoch 13/25 50/50 [==============================] - 11s 222ms/step - loss: 0.3385 - accuracy: 0.8811 - val_loss: 0.7646 - val_accuracy: 0.7654 Epoch 14/25 50/50 [==============================] - 11s 222ms/step - loss: 0.2866 - accuracy: 0.9002 - val_loss: 0.7524 - val_accuracy: 0.7714 Epoch 15/25 50/50 [==============================] - 11s 221ms/step - loss: 0.2489 - accuracy: 0.9133 - val_loss: 0.7659 - val_accuracy: 0.7826 Epoch 16/25 50/50 [==============================] - 11s 226ms/step - loss: 0.1812 - accuracy: 0.9365 - val_loss: 0.8619 - val_accuracy: 0.7666 Epoch 17/25 50/50 [==============================] - 11s 226ms/step - loss: 0.1554 - accuracy: 0.9475 - val_loss: 0.9040 - val_accuracy: 0.7730 Epoch 18/25 50/50 [==============================] - 11s 226ms/step - loss: 0.1135 - accuracy: 0.9614 - val_loss: 0.9970 - val_accuracy: 0.7662 Epoch 19/25 50/50 [==============================] - 11s 226ms/step - loss: 0.0941 - accuracy: 0.9675 - val_loss: 1.1001 - val_accuracy: 0.7662 Epoch 20/25 50/50 [==============================] - 11s 225ms/step - loss: 0.1204 - accuracy: 0.9582 - val_loss: 0.9694 - val_accuracy: 0.7742 Epoch 21/25 50/50 [==============================] - 11s 226ms/step - loss: 0.0831 - accuracy: 0.9726 - val_loss: 1.1143 - val_accuracy: 0.7660 Epoch 22/25 50/50 [==============================] - 11s 227ms/step - loss: 0.0748 - accuracy: 0.9732 - val_loss: 1.2051 - val_accuracy: 0.7804 Epoch 23/25 50/50 [==============================] - 11s 226ms/step - loss: 0.0786 - accuracy: 0.9729 - val_loss: 1.1086 - val_accuracy: 0.7710 Epoch 24/25 50/50 [==============================] - 11s 221ms/step - loss: 0.0719 - accuracy: 0.9753 - val_loss: 1.1663 - val_accuracy: 0.7746 Epoch 25/25 50/50 [==============================] - 11s 221ms/step - loss: 0.0494 - accuracy: 0.9840 - val_loss: 1.2690 - val_accuracy: 0.7632 313/313 [==============================] - 2s 6ms/step - loss: 1.3445 - accuracy: 0.7508 Test Accuracy: 0.7508000135421753

HIGH ACCURACY DUE TO ALEXNET ARCHITECTURE

Code to Know no of images in training set

num_images = x_train.shape[0] print("Number of images in x_train:", num_images)


Theory:

Here's what each part of the architecture does:

  1. Convolutional Layers: The architecture starts with a series of convolutional layers. These layers extract features from the input images. The convolutional layers are followed by max-pooling layers that downsample the spatial dimensions.

  2. Fully Connected Layers: After the convolutional and pooling layers, there are several fully connected (dense) layers. These layers combine the extracted features and make final decisions about the class of the input image.

  3. Dropout Layers: Dropout layers are inserted after the first and second fully connected layers. Dropout helps prevent overfitting by randomly setting a fraction of the input units to zero during each update, effectively turning off some neurons and reducing co-adaptation between them.

  4. Output Layer: The final dense layer produces the output predictions. It uses the softmax activation function to convert the network's final raw scores into probabilities for each class.

The model is compiled using the Adam optimizer and categorical cross-entropy loss, and it's
trained using a data generator with data augmentation.


The CIFAR-10 dataset consists of 10 classes of images. Here is the list of classes in the CIFAR-10 dataset:

  1. Airplane
  2. Automobile
  3. Bird
  4. Cat
  5. Deer
  6. Dog
  7. Frog
  8. Horse
  9. Ship
  10. Truck

Program to predict or classify a given Image using the prebuilt model:

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
#from tensorflow.keras.applications.resnet50 import preprocess_input
import matplotlib.pyplot as plt

# Load the saved model
model = load_model('/content/drive/MyDrive/8/alexnet_cifar10.h5')

# Load and preprocess an image from a local path for prediction
local_image_path = '/content/drive/MyDrive/8/f.jpg'  # Replace with the path to your local image
local_image = load_img(local_image_path, target_size=(32, 32))
local_image1 = load_img(local_image_path)
local_image_array = img_to_array(local_image)
local_image_array = np.expand_dims(local_image_array, axis=0)
local_image_array /= 255.0  # Apply simple normalization, not preprocess_input

# Make a prediction for the local image
local_predictions = model.predict(local_image_array)
local_predicted_class = np.argmax(local_predictions[0])

# Display the image and prediction
plt.imshow(local_image)
plt.title(f"Predicted Class: {local_predicted_class}")
plt.axis('off')
plt.show()

plt.imshow(local_image1, interpolation='nearest')
plt.title(f"Predicted Class: {local_predicted_class}")
plt.axis('off')
plt.show()

link to images to predict:

https://drive.google.com/drive/folders/1S34ART1LbDlOJsEwxogRxsLCtq_BVAhc?usp=sharing

Comments