In this part implement AlexNet, check how to improve the model, and apply that to solve…
- User
In this part implement AlexNet, check how to improve the model, and apply that to solve an image dataset containing three classes: dogs(33.6mb), cars(34.5mb) and a food folder of images with size(36.3mb).
The expected accuracy for this part is more than 90%. CNN dataset consists of 10,000 examples for each category, thus in total 30,000 samples. Each example is a 64x64 image. STEPS
-
Load, preprocess, analyze the dataset and make it ready for training. Provide brief details about the nature of your dataset. What is it about? What type of data are we encountering? How many entries and variables does the dataset comprise? Provide the main statistics about the entries of the dataset. Provide code for at least 3 visualization graphs with short descriptions for each graph.
-
Build and train an AlexNet CNN architecture
AlexNet Image: 224 (height) x 224 (width) x 3 (channels) convolution with 1111 kernel + 4 stride : 5454*96
ReLu
Pool with 3x3 max. kemel+2 stride: 26x26x96 convolution with 5*5 kernel + 2 pad : 26x26x256 ReLu
Pool with 3x3 max. kernel+2stride: 12x12x256 convolution with 33 kernel + 1 pad : 12x12x384 Relu convolution with 33 kernel + 1 pad : 12x12x384 ReLu convolution with 3*3 kernel + 1 pad : 12x12x256 ReLu Pool with 3x3 max. kernel+2stride:5x5x256
flatten Dense: 4096 fully connected neurons ReLu, dropout p=0.5 Dense: 4096 fully connected neurons ReLu, dropout p=0.5 Dense: 1000 fully connected neurons |
Output: 1 of 1000 classes For your dataset, adjust the size, e.g. for the input and the output layers.
- For your dataset, adjust the size, e.g. for the input and the output layers. Train the network and evaluate the performance of the AlexNet on the testing data.
Give code that provide graphs that compare test and training accuracy on the same plot
-
Modify AlexNet structure (e.g. add/remove layers, update the kernel size, adjust the hyperparameters), add improvement methods that are applicable to CNN architecture (e.g. earlystopping).
-
Train the network and evaluate the performance of the AlexNet on the testing Data.
-
Provide graphs that compare test and training accuracy on the same plot.
-
Discuss how you improve AlexNet, what methods and tools you have tried and how that helped to improve the training accuracy and training time.
-
- Assistant
- Load, preprocess, analyze the dataset and make it ready for training.
Assuming that we have an image dataset containing three classes: dogs, cars, and food, we can preprocess the data as follows:
import os import cv2 import numpy as np import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split image_dir = 'path/to/dataset' categories = ['dogs', 'cars', 'food'] data = [] size = 64 for category in categories: path = os.path.join(image_dir, category) class_num = categories.index(category) for img in os.listdir(path): try: img_array = cv2.imread(os.path.join(path, img)) resized_img_array = cv2.resize(img_array, (size, size)) data.append([resized_img_array, class_num]) except Exception as e: print(e) np.random.shuffle(data) X = [] y = [] for features, label in data: X.append(features) y.append(label) X = np.array(X).reshape(-1, size, size, 3) y = np.array(y) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)Now we're going to visualize our data using some bar charts and pie charts.
import seaborn as sns from collections import Counter y_count = Counter(y) labels = ["dogs", "cars", "food"] fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15, 5)) # Pie chart ax1.pie(y_count.values(), labels=labels, autopct='%1.1f%%') ax1.set_title("Category distribution") # Bar chart sns.barplot(x=labels, y=list(y_count.values()), ax=ax2) ax2.set_title("Number of images per category") # Image Examples idx = [np.random.choice(np.where(y==i)[0], 1)[0] for i in range(3)] images = [X[idx[i]] for i in range(3)] for i in range(3): ax3.imshow(images[i], cmap="gray") ax3.axis('off') ax3.set_title("Example of: " + labels[i]) plt.show() plt.show()- Building and training AlexNet
import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D n_classes = 3 model = Sequential() # Layer 1 model.add(Conv2D(filters=96, kernel_size=(11, 11), strides=(4, 4), input_shape=(size, size, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2))) # Layer 2 model.add(Conv2D(256, (5, 5), padding='same')) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2))) # Layer 3 model.add(Conv2D(384, (3, 3), padding='same')) model.add(Activation('relu')) # Layer 4 model.add(Conv2D(384, (3, 3), padding='same')) model.add(Activation('relu')) # Layer 5 model.add(Conv2D(256, (3, 3), padding='same')) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2))) # Flatten model.add(Flatten()) # Dense Layers model.add(Dense(4096)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(4096)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(n_classes, activation='softmax')) # Compile the model model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Train AlexNet history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10)You can plot training and test accuracy using the following code:
plt.plot(history.history['accuracy']) plt.plot(history.history['val_accuracy']) plt.title('Model accuracy') plt.ylabel('Accuracy') plt.xlabel('Epoch') plt.legend(['Train', 'Test'], loc='upper left') plt.show()- Modify AlexNet structure and implement improvement methods
Add early stopping to the model, as well as replace some Dropout layers or modify the optimizer learning rate:
from tensorflow.keras.callbacks import EarlyStopping model = Sequential() # Layer 1 ... # Dense Layers model.add(Dense(4096)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(4096)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(n_classes, activation='softmax')) # Compile the model optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001) model.compile(loss='sparse_categorical_crossentropy', optimizer=optimizer, metrics=['accuracy']) # Add early stopping early_stopping = EarlyStopping(monitor='val_loss', patience=3) history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=30, callbacks=[early_stopping])Plot training and testing accuracy for your modified model:
plt.plot(history.history['accuracy']) plt.plot(history.history['val_accuracy']) plt.title('Model Modified accuracy') plt.ylabel('Accuracy') plt.xlabel('Epoch') plt.legend(['Train', 'Test'], loc='upper left') plt.show()- Discuss on improvement
In this example, AlexNet was modified by adjusting the input layer and the output layer of the network to match our dataset of 64x64 images and three classes. The optimizer learning rate was reduced, which improved training accuracy. Furthermore, Dropout layers and an early-stopping mechanism were added to the training process which helped to prevent overfitting. These adjustments resulted in a more accurate model and reduced training time.