Convolutional NNs for CIFAR 10

In this project, a data set of (small) natural images known as CIFAR10 will be used. The goal is to present:

i) how CNN layers are used in Pytorch, and

ii) evaluate the performance of a simple CNN over this dataset.

Downlading the CIFAR10 with torchvision

The code below will download the MNIST dataset, then create training and test datasets.

torchvision splits the work up between ToTensor() and Normalize():

ToTensor() takes a PIL image (or np.int8 NumPy array) with shape (n_rows, n_cols, n_channels) as input and returns a PyTorch tensor with floats between 0 and 1 and shape (n_channels, n_rows, n_cols). Normalize() subtracts the mean and divides by the standard deviation of the floating point values in the range [0, 1].

A validation set using the 20% of train images

Implementing the LeNet 5 CNN Network

In the CNN network above there are 2 convolutional layers with the following properties:

Each of dimension in CIFAR-10 images are composed by 3 input color maps, therefore, they are $32\times32\times3$.

The spatial size of the output volume as a function of the input volume size (W) can be computed, the kernel/filter size (F), the stride with which they are applied (S), and the amount of zero padding used (P) on the border. The correct formula for calculating how many neurons define the output_W is given by (W−F+2P)/S+1.

For example for a 7x7 input and a 3x3 filter with stride 1 and pad 0, the output would be 5x5. With stride 2 we would get a 3x3 output.

Extending the class to incorporate a training method, to evaluate the both the validation and train losses and to evaluate the classification performance in a set.

Training the model for 5 epochs

Train/validation loss plots during training & Train/validation performance

After running the model with 5 epoch, it is clear that both training and validation losses are decreasing with the number of epochs. The minimum loss is obtained in the last epoch. Therefore, in order to get further insights it would necassary to retrain the model with more epochs to evaluate the model in detail.

GPU-based training

The newtork is already quite deep and gradient evaluation becomes a heavy operation.

PyTorch, along with pretty much every other deep learning framework, uses CUDA to efficiently compute the forward and backwards passes on the GPU. In PyTorch, you move your model parameters and other tensors to the GPU memory using model.to('cuda'). You can move them back from the GPU with model.to('cpu') which you'll commonly do when you need to operate on the network output outside of PyTorch. As a demonstration of the increased speed, I'll compare how long it takes to perform a forward and backward pass with and without a GPU.

With a GPU, it can be seen that the time per epoch roughly decreases significantly. Without GPU, the average execution time per epoch was found 21 seconds while with a GPU it was found more or less 17-18 seconds. As the model is trained deeper and much more complex networks, this difference grows exponentially fast. Using GPUs is a must for lage-scale deployment.

Regularizing the network and comparing with a MLP

1- Checking that the CNN is able to overfit


In order to make the model overfit, the model must be trained with more epochs.

The model has been trained for longer than required to ensure the model overfits. From the above visual it is clear that the training loss decreases throughout the epochs. On the other hand, the validaation loss decreases until the 7th epoch, but then it starts increasing with the number of epochs. This fact indicates that CNN is able to overfit.

2- Regularizing the network with both early stopping and dropout.

As it has been proved before, the Lenet5 model could overfit when training the model during more than 7 epochs. In this step, the early stopping will be implemented in order to solve this problem. Early stopping is a method that allows us to stop training once the model performance stops improving on the validation set. In that way, it prevents overfitting in the model.

For early stopping method, 10 epoches are defined as the number of epoch limitation. In other words, after 10 epoches in which the best found validation loss could not be improved, the training will be stopped early. In each epoch iteration, the minimum loss value will be kept in the best_loss parameter and will be compared with the following epoch validation loss value. To decide whether there is an improvement or not, the difference parameter will be used. If best loss minus the current epoch's loss is larger than 0, that means the epoch could improve the validation loss. Therefore, the count will be 0. If the best loss minus the calculated current epoch's loss is smaller than 0, it means that the current epoch's loss is larger than the best loss. Therefore, there is no improvement in the loss. In that case, the count parameter will be iterated by 1. When the count parameter reaches to 10, the training will be stopped.

In the code below, the model my_CNN_imp_GPU will be run with both dropout and early stopping regularization to eliminate the overfitting in the model. In the model as an epoch parameter, 100 is defined to converge.

The minimum validation loss has been found 0.9159 at 70th epoch. This best loss could not be decreased in the last 10 consecutive epoches. Therefore, the training has been stopped before the epoch value reaches 100.

3- Checking the train/validation/test performance, plot the train and validation losses

As can be seen in the above results, the performance of the model has significantly increased. Looking at the validation loss evaluation curve in each epoch,it can be seen that initially the validation loss decreases sharply, then the loss curve progresses almost stably. Of course, there are some fluctuations in the loss line, but these ups and downs are not sharp. This means that after these regulations, the overfitting situation has been fixed and an improvement in the performance of the model has been achieved.

To conclude, the best validation loss has been found at 70th epoch with 0.91 value. Training, validation and test accuracies have been calculated %78, %67 and %66, respectively. The difference between the accuracy o the train set and on the validation and test set is not very small. However, now the validation loss curve does not increase throughout epochs.

Training an MLP to compare the performance with LeNet 5 CNN Network

In this part, an MLP with four layers with hidden dimmensions 256, 128 and 64 neurons will be trained. As activation functions, ReLU will be used with a log-Softmax output layer. As mentioned, first the input size (3x32x32) is defined. To prevent overfitting in the model, dropout and early stopping regularizations are used.

The best loss has been found 1.34 at the 17th epoch, which is quite worse than regularized CNN model. Moreover, even though the model trained with 60 epoch, the training stopped at 26th epoch as there were no improvements in the validation loss. According to the accuracy scores in training, validation and test, it can be said clearly that this MLP model has performed worse than the CNN model.

Although the performance of the MLP model is lower than that of the CNN model, there is no overfitting in the model as seen in the validation loss curve in the graph above. However, both training and validation final losses are higher than the CNN one. For this data, the regularized CNN model has performed very well.