+1 (315) 557-6473 

Python Program to Create Typical Convolution Neural Network Assignment Solution.


Instructions

Objective
Write a python assignment program to create a typical convolution neural network.

Requirements and Specifications

program to create typical convolution neural network in python

Source Code

import keras

from keras.datasets import cifar10

import matplotlib.pyplot as plt

import numpy as np

import tensorflow as tf

from keras.utils import np_utils

# Introduction

The CIFAR-10 Dataset is a dataset containing images that can be classified in 10 classes. The dataset contains about 50,000 training images and 10,000 test images.

The dataset contains images for **airplanes**, **automobile**, **bird**, **cat**, **deer**, **dog**, **frog**, **horse**, **ship** and **truck**

### Load Dataset

(X_train, y_train), (X_test, y_test) = cifar10.load_data()

# Convert to float32

X_train = X_train.astype('float32')

X_test = X_test.astype('float32')

print(f"There are {X_train.shape[0]} training images and {X_test.shape[0]} test images in the dataset.")

print(f"The images has a size of {X_train[0].shape}")

print(f"There are {len(np.unique(y_train))} classes in the dataset.")

n_classes = len(np.unique(y_train))

### Use OneHotEncoder to convert the classes to categorical

# one hot encode target values

y_train = keras.utils.np_utils.to_categorical(y_train, n_classes)

y_test = keras.utils.np_utils.to_categorical(y_test, n_classes)

### Visualize the first 16 images from the training dataset

fig, axes = plt.subplots(nrows = 4, ncols = 4)

row = 0

col = 0

for i in range(16):

axes[row,col].imshow(X_train[i]/255.0)

# plot raw pixel data

col += 1

if col == 4:

col = 0

row += 1

# show the figure

plt.show()

# Create CNN Model

model = tf.keras.models.Sequential()

# Add the Data augmentation layers. These layers will flip, rotate, rescale, etc

model.add(tf.keras.layers.Rescaling(1./255))

model.add(tf.keras.layers.RandomFlip("horizontal_and_vertical"))

model.add(tf.keras.layers.RandomRotation(0.1))

# Convolutional layers with kernel regularizer

model.add(tf.keras.layers.Conv2D(16, (3, 3), padding = 'same', input_shape=(32, 32, 3), activation='relu', kernel_regularizer = tf.keras.regularizers.l2())) # First CNN layer

model.add(tf.keras.layers.BatchNormalization())

model.add(tf.keras.layers.MaxPooling2D((2, 2)))

model.add(tf.keras.layers.Conv2D(32, (3, 3), padding = 'same', activation='relu', kernel_regularizer = tf.keras.regularizers.l2())) # Second CNN layer

model.add(tf.keras.layers.BatchNormalization())

model.add(tf.keras.layers.MaxPooling2D((2, 2)))

model.add(tf.keras.layers.Conv2D(64, (3, 3), padding = 'same', activation='relu', kernel_regularizer = tf.keras.regularizers.l2())) # Third CNN layer

model.add(tf.keras.layers.BatchNormalization())

model.add(tf.keras.layers.MaxPooling2D((2, 2)))

# Add to convert the images to 1-D

model.add(tf.keras.layers.Flatten())

# Add 3 MLP (Dense) layers with Drouput layers between them.

model.add(tf.keras.layers.Dense(128, activation = 'relu'))

model.add(tf.keras.layers.Dense(256, activation = 'relu'))

model.add(tf.keras.layers.Dense(512, activation = 'relu'))

# Add a Dropout layer

model.add(tf.keras.layers.Dropout(0.2))

# Add output layer

model.add(tf.keras.layers.Dense(n_classes, activation = 'softmax'))

# Compile

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

model.build((None, 32, 32, 3))

model.summary()

# Fit

history = model.fit(X_train, y_train, batch_size = 128, epochs = 100, verbose=1)

## Plot accuracy and loss

fig, axes = plt.subplots(nrows = 1, ncols = 2, figsize=(8,6))

axes[0].plot(history.history['accuracy'])

axes[0].set_xlabel('Epoch')

axes[0].set_ylabel('Accuracy')

axes[0].grid(True)

axes[1].plot(history.history['loss'])

axes[1].set_xlabel('Epoch')

axes[1].set_ylabel('Loss')

axes[1].grid(True)

plt.show()