The goal is to apply a Convolutional Neural Net (CNN) model on the CIFAR10 image dataset and test the accuracy of the model on the basis of image classification. Compare the Accuracy VS the neural network implemented during TD1.
Have a look at the following documentation to be familiar with PyTorch.
Epoch: 0 Training Loss: 45.389846 Validation Loss: 41.777527
Validation loss decreased (inf --> 41.777527). Saving model ...
Epoch: 1 Training Loss: 39.424652 Validation Loss: 35.060819
Validation loss decreased (41.777527 --> 35.060819). Saving model ...
Epoch: 2 Training Loss: 35.046248 Validation Loss: 32.135539
Validation loss decreased (35.060819 --> 32.135539). Saving model ...
Epoch: 3 Training Loss: 32.571120 Validation Loss: 29.923746
Validation loss decreased (32.135539 --> 29.923746). Saving model ...
Epoch: 4 Training Loss: 30.437492 Validation Loss: 27.588401
Validation loss decreased (29.923746 --> 27.588401). Saving model ...
Epoch: 5 Training Loss: 28.696167 Validation Loss: 26.766404
Validation loss decreased (27.588401 --> 26.766404). Saving model ...
Epoch: 6 Training Loss: 27.121808 Validation Loss: 24.613978
Validation loss decreased (26.766404 --> 24.613978). Saving model ...
Epoch: 7 Training Loss: 25.725125 Validation Loss: 23.538336
Validation loss decreased (24.613978 --> 23.538336). Saving model ...
Epoch: 8 Training Loss: 24.334904 Validation Loss: 21.935171
Validation loss decreased (23.538336 --> 21.935171). Saving model ...
Epoch: 9 Training Loss: 23.203895 Validation Loss: 22.022023
Epoch: 10 Training Loss: 22.118146 Validation Loss: 19.978609
Validation loss decreased (21.935171 --> 19.978609). Saving model ...
Epoch: 11 Training Loss: 21.068114 Validation Loss: 19.735263
Validation loss decreased (19.978609 --> 19.735263). Saving model ...
Epoch: 12 Training Loss: 20.225011 Validation Loss: 19.091033
Validation loss decreased (19.735263 --> 19.091033). Saving model ...
Epoch: 13 Training Loss: 19.421816 Validation Loss: 18.326990
Validation loss decreased (19.091033 --> 18.326990). Saving model ...
Epoch: 14 Training Loss: 18.541730 Validation Loss: 17.414942
Validation loss decreased (18.326990 --> 17.414942). Saving model ...
Epoch: 15 Training Loss: 17.814699 Validation Loss: 17.238110
Validation loss decreased (17.414942 --> 17.238110). Saving model ...
Epoch: 16 Training Loss: 16.933247 Validation Loss: 16.829644
Validation loss decreased (17.238110 --> 16.829644). Saving model ...
Epoch: 17 Training Loss: 16.389078 Validation Loss: 16.914702
Epoch: 18 Training Loss: 15.726570 Validation Loss: 16.755409
Validation loss decreased (16.829644 --> 16.755409). Saving model ...
Epoch: 19 Training Loss: 15.119167 Validation Loss: 16.418999
Validation loss decreased (16.755409 --> 16.418999). Saving model ...
Epoch: 20 Training Loss: 14.478328 Validation Loss: 15.640075
Validation loss decreased (16.418999 --> 15.640075). Saving model ...
Epoch: 21 Training Loss: 13.877445 Validation Loss: 16.125324
Epoch: 22 Training Loss: 13.347822 Validation Loss: 16.349312
Epoch: 23 Training Loss: 12.772845 Validation Loss: 16.131425
Epoch: 24 Training Loss: 12.325509 Validation Loss: 15.288487
Validation loss decreased (15.640075 --> 15.288487). Saving model ...
Epoch: 25 Training Loss: 11.783999 Validation Loss: 15.305513
Epoch: 26 Training Loss: 11.342386 Validation Loss: 15.720134
Epoch: 27 Training Loss: 10.812438 Validation Loss: 17.583329
Epoch: 28 Training Loss: 10.439049 Validation Loss: 16.388209
Epoch: 29 Training Loss: 9.974275 Validation Loss: 16.448369
%% Cell type:markdown id:bca17d68 tags:
- Model 2 starts with relatively high validation losses but steadily decreases, achieving better performance in terms of validation loss. The best validation loss of 15.28 is obtained at epoch 24, but as with Model 1, validation loss becomes more unstable from epoch 24 onwards. Training loss continues to decrease with each epoch.
- Model 2 showed a stable decrease in validation loss, achieving better performance than Model 1 in the early epochs (at least up to epoch 24). However, from epoch 24 onwards, validation loss also became more volatile, which could be a sign of long-term overfitting.
- Model 2 seems to work better for the first 24 epochs, with a lower and more stable loss of validation than the Model 1.
# forward pass: compute predicted outputs by passing inputs to the model
output=model_2(data)
# calculate the batch loss
loss=criterion(output,target)
# update test loss
test_loss+=loss.item()*data.size(0)
# convert output probabilities to predicted class
_,pred=torch.max(output,1)
# compare predictions to true label
correct_tensor=pred.eq(target.data.view_as(pred))
correct=(
np.squeeze(correct_tensor.numpy())
ifnottrain_on_gpu
elsenp.squeeze(correct_tensor.cpu().numpy())
)
# calculate test accuracy for each object class
foriinrange(batch_size):
label=target.data[i]
class_correct[label]+=correct[i].item()
class_total[label]+=1
# average test loss
test_loss=test_loss/len(test_loader)
print("Test Loss: {:.6f}\n".format(test_loss))
foriinrange(10):
ifclass_total[i]>0:
print(
"Test Accuracy of %5s: %2d%% (%2d/%2d)"
%(
classes[i],
100*class_correct[i]/class_total[i],
np.sum(class_correct[i]),
np.sum(class_total[i]),
)
)
else:
print("Test Accuracy of %5s: N/A (no training examples)"%(classes[i]))
print(
"\nTest Accuracy (Overall): %2d%% (%2d/%2d)"
%(
100.0*np.sum(class_correct)/np.sum(class_total),
np.sum(class_correct),
np.sum(class_total),
)
)
```
%% Output
Test Loss: 15.510981
Test Accuracy of airplane: 82% (820/1000)
Test Accuracy of automobile: 80% (803/1000)
Test Accuracy of bird: 64% (641/1000)
Test Accuracy of cat: 48% (480/1000)
Test Accuracy of deer: 70% (701/1000)
Test Accuracy of dog: 66% (667/1000)
Test Accuracy of frog: 77% (776/1000)
Test Accuracy of horse: 80% (804/1000)
Test Accuracy of ship: 86% (860/1000)
Test Accuracy of truck: 80% (808/1000)
Test Accuracy (Overall): 73% (7360/10000)
%% Cell type:markdown id:d4b037a0 tags:
- Test loss (Loss):
Model 2 has a significantly lower test loss (15.51) compared with Model 1 (21.15). This suggests that Model 2 is better at generalizing to test data.
- Overall accuracy:
Model 2 has a better overall accuracy of 73% versus 64% for Model 1. This shows that Model 2 is better at classifying the test data.
Model 2 excels particularly in classes such as Airplane (82%) , Ship (86%), Horse (80%), and Truck (80%). It also has better accuracy than the Model 1 in all classes.
- Conclusion :
Model 2 outperforms Model 1 in terms of overall accuracy and test loss. It is better at classifying a wider range of classes, and has better generalization capability on test data.
%% Cell type:markdown id:bc381cf4 tags:
## Exercise 2: Quantization: try to compress the CNN to save space
Quantization doc is available from https://pytorch.org/docs/stable/quantization.html#torch.quantization.quantize_dynamic
The Exercise is to quantize post training the above CNN model. Compare the size reduction and the impact on the classification accuracy
The size of the model is simply the size of the file.
For each class, compare the classification test accuracy of the initial model and the quantized model. Also give the overall test accuracy for both models.
%% Cell type:markdown id:a0a34b90 tags:
Try training aware quantization to mitigate the impact on the accuracy (doc available here https://pytorch.org/docs/stable/quantization.html#torch.quantization.quantize_dynamic)
%% Cell type:markdown id:201470f9 tags:
## Exercise 3: working with pre-trained models.
PyTorch offers several pre-trained models https://pytorch.org/vision/0.8/models.html
We will use ResNet50 trained on ImageNet dataset (https://www.image-net.org/index.php). Use the following code with the files `imagenet-simple-labels.json` that contains the imagenet labels and the image dog.png that we will use as test.
%% Cell type:code id:b4d13080 tags:
``` python
importjson
fromPILimportImage
# Choose an image to pass through the model
test_image="dog.png"
# Configure matplotlib for pretty inline plots
#%matplotlib inline
#%config InlineBackend.figure_format = 'retina'
# Prepare the labels
withopen("imagenet-simple-labels.json")asf:
labels=json.load(f)
# First prepare the transformations: resize the image to what the model was trained on and convert it to a tensor
# Download the model if it's not there already. It will take a bit on the first run, after that it's fast
model=models.resnet50(pretrained=True)
# Send the model to the GPU
# model.cuda()
# Set layers such as dropout and batchnorm in evaluation mode
model.eval()
# Get the 1000-dimensional model output
out=model(image)
# Find the predicted class
print("Predicted class is: {}".format(labels[out.argmax()]))
```
%% Cell type:markdown id:184cfceb tags:
Experiments:
Study the code and the results obtained. Possibly add other images downloaded from the internet.
What is the size of the model? Quantize it and then check if the model is still able to correctly classify the other images.
Experiment with other pre-trained CNN models.
%% Cell type:markdown id:5d57da4b tags:
## Exercise 4: Transfer Learning
For this work, we will use a pre-trained model (ResNet18) as a descriptor extractor and will refine the classification by training only the last fully connected layer of the network. Thus, the output layer of the pre-trained network will be replaced by a layer adapted to the new classes to be recognized which will be in our case ants and bees.
Download and unzip in your working directory the dataset available at the address :
plt.pause(0.001)# pause a bit so that plots are updated
plt.show()
# Get a batch of training data
inputs,classes=next(iter(dataloaders["train"]))
# Make a grid from batch
out=torchvision.utils.make_grid(inputs)
imshow(out,title=[class_names[x]forxinclasses])
```
%% Cell type:markdown id:bbd48800 tags:
Now, execute the following code which uses a pre-trained model ResNet18 having replaced the output layer for the ants/bees classification and performs the model training by only changing the weights of this output layer.
%% Cell type:code id:572d824c tags:
``` python
importcopy
importos
importtime
importmatplotlib.pyplotasplt
importnumpyasnp
importtorch
importtorch.nnasnn
importtorch.optimasoptim
importtorchvision
fromtorch.optimimportlr_scheduler
fromtorchvisionimportdatasets,transforms
# Data augmentation and normalization for training
# Just normalization for validation
data_transforms={
"train":transforms.Compose(
[
transforms.RandomResizedCrop(
224
),# ImageNet models were trained on 224x224 images
transforms.RandomHorizontalFlip(),# flip horizontally 50% of the time - increases train set variability
transforms.ToTensor(),# convert it to a PyTorch tensor
Modify the code and add an "eval_model" function to allow
the evaluation of the model on a test set (different from the learning and validation sets used during the learning phase). Study the results obtained.
Now modify the code to replace the current classification layer with a set of two layers using a "relu" activation function for the middle layer, and the "dropout" mechanism for both layers. Renew the experiments and study the results obtained.
Apply ther quantization (post and quantization aware) and evaluate impact on model size and accuracy.
%% Cell type:markdown id:04a263f0 tags:
## Optional
Try this at home!!
Pytorch offers a framework to export a given CNN to your selfphone (either android or iOS). Have a look at the tutorial https://pytorch.org/mobile/home/
The Exercise consists in deploying the CNN of Exercise 4 in your phone and then test it on live.
%% Cell type:markdown id:fe954ce4 tags:
## Author
Alberto BOSIO - Ph. D.
...
...
%% Cell type:markdown id:7edf7168 tags:
# TD2: Deep learning
%% Cell type:markdown id:fbb8c8df tags:
In this TD, you must modify this notebook to answer the questions. To do this,
1. Fork this repository
2. Clone your forked repository on your local computer
3. Answer the questions
4. Commit and push regularly
The last commit is due on Wednesday, December 4, 11:59 PM. Later commits will not be taken into account.
%% Cell type:markdown id:3d167a29 tags:
Install and test PyTorch from https://pytorch.org/get-started/locally.
%% Cell type:code id:330a42f5 tags:
``` python
%pipinstalltorchtorchvision
```
%% Output
Requirement already satisfied: torch in /usr/local/lib/python3.11/site-packages (2.2.2)
Requirement already satisfied: torchvision in /usr/local/lib/python3.11/site-packages (0.17.2)
Requirement already satisfied: filelock in /usr/local/lib/python3.11/site-packages (from torch) (3.16.1)
Requirement already satisfied: typing-extensions>=4.8.0 in /Users/youcefkessi/Library/Python/3.11/lib/python/site-packages (from torch) (4.12.2)
Requirement already satisfied: sympy in /usr/local/lib/python3.11/site-packages (from torch) (1.13.3)
Requirement already satisfied: networkx in /usr/local/lib/python3.11/site-packages (from torch) (3.4.2)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.11/site-packages (from torch) (3.1.4)
Requirement already satisfied: fsspec in /usr/local/lib/python3.11/site-packages (from torch) (2024.10.0)
Requirement already satisfied: numpy in /usr/local/lib/python3.11/site-packages (from torchvision) (1.24.3)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /usr/local/lib/python3.11/site-packages (from torchvision) (11.0.0)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.11/site-packages (from jinja2->torch) (3.0.2)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.11/site-packages (from sympy->torch) (1.3.0)
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m23.0.1[0m[39;49m -> [0m[32;49m24.3.1[0m
[1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpython3.11 -m pip install --upgrade pip[0m
Note: you may need to restart the kernel to use updated packages.
The goal is to apply a Convolutional Neural Net (CNN) model on the CIFAR10 image dataset and test the accuracy of the model on the basis of image classification. Compare the Accuracy VS the neural network implemented during TD1.
Have a look at the following documentation to be familiar with PyTorch.
Epoch: 0 Training Loss: 45.389846 Validation Loss: 41.777527
Validation loss decreased (inf --> 41.777527). Saving model ...
Epoch: 1 Training Loss: 39.424652 Validation Loss: 35.060819
Validation loss decreased (41.777527 --> 35.060819). Saving model ...
Epoch: 2 Training Loss: 35.046248 Validation Loss: 32.135539
Validation loss decreased (35.060819 --> 32.135539). Saving model ...
Epoch: 3 Training Loss: 32.571120 Validation Loss: 29.923746
Validation loss decreased (32.135539 --> 29.923746). Saving model ...
Epoch: 4 Training Loss: 30.437492 Validation Loss: 27.588401
Validation loss decreased (29.923746 --> 27.588401). Saving model ...
Epoch: 5 Training Loss: 28.696167 Validation Loss: 26.766404
Validation loss decreased (27.588401 --> 26.766404). Saving model ...
Epoch: 6 Training Loss: 27.121808 Validation Loss: 24.613978
Validation loss decreased (26.766404 --> 24.613978). Saving model ...
Epoch: 7 Training Loss: 25.725125 Validation Loss: 23.538336
Validation loss decreased (24.613978 --> 23.538336). Saving model ...
Epoch: 8 Training Loss: 24.334904 Validation Loss: 21.935171
Validation loss decreased (23.538336 --> 21.935171). Saving model ...
Epoch: 9 Training Loss: 23.203895 Validation Loss: 22.022023
Epoch: 10 Training Loss: 22.118146 Validation Loss: 19.978609
Validation loss decreased (21.935171 --> 19.978609). Saving model ...
Epoch: 11 Training Loss: 21.068114 Validation Loss: 19.735263
Validation loss decreased (19.978609 --> 19.735263). Saving model ...
Epoch: 12 Training Loss: 20.225011 Validation Loss: 19.091033
Validation loss decreased (19.735263 --> 19.091033). Saving model ...
Epoch: 13 Training Loss: 19.421816 Validation Loss: 18.326990
Validation loss decreased (19.091033 --> 18.326990). Saving model ...
Epoch: 14 Training Loss: 18.541730 Validation Loss: 17.414942
Validation loss decreased (18.326990 --> 17.414942). Saving model ...
Epoch: 15 Training Loss: 17.814699 Validation Loss: 17.238110
Validation loss decreased (17.414942 --> 17.238110). Saving model ...
Epoch: 16 Training Loss: 16.933247 Validation Loss: 16.829644
Validation loss decreased (17.238110 --> 16.829644). Saving model ...
Epoch: 17 Training Loss: 16.389078 Validation Loss: 16.914702
Epoch: 18 Training Loss: 15.726570 Validation Loss: 16.755409
Validation loss decreased (16.829644 --> 16.755409). Saving model ...
Epoch: 19 Training Loss: 15.119167 Validation Loss: 16.418999
Validation loss decreased (16.755409 --> 16.418999). Saving model ...
Epoch: 20 Training Loss: 14.478328 Validation Loss: 15.640075
Validation loss decreased (16.418999 --> 15.640075). Saving model ...
Epoch: 21 Training Loss: 13.877445 Validation Loss: 16.125324
Epoch: 22 Training Loss: 13.347822 Validation Loss: 16.349312
Epoch: 23 Training Loss: 12.772845 Validation Loss: 16.131425
Epoch: 24 Training Loss: 12.325509 Validation Loss: 15.288487
Validation loss decreased (15.640075 --> 15.288487). Saving model ...
Epoch: 25 Training Loss: 11.783999 Validation Loss: 15.305513
Epoch: 26 Training Loss: 11.342386 Validation Loss: 15.720134
Epoch: 27 Training Loss: 10.812438 Validation Loss: 17.583329
Epoch: 28 Training Loss: 10.439049 Validation Loss: 16.388209
Epoch: 29 Training Loss: 9.974275 Validation Loss: 16.448369
%% Cell type:markdown id:bca17d68 tags:
- Model 2 starts with relatively high validation losses but steadily decreases, achieving better performance in terms of validation loss. The best validation loss of 15.28 is obtained at epoch 24, but as with Model 1, validation loss becomes more unstable from epoch 24 onwards. Training loss continues to decrease with each epoch.
- Model 2 showed a stable decrease in validation loss, achieving better performance than Model 1 in the early epochs (at least up to epoch 24). However, from epoch 24 onwards, validation loss also became more volatile, which could be a sign of long-term overfitting.
- Model 2 seems to work better for the first 24 epochs, with a lower and more stable loss of validation than the Model 1.
# forward pass: compute predicted outputs by passing inputs to the model
output=model_2(data)
# calculate the batch loss
loss=criterion(output,target)
# update test loss
test_loss+=loss.item()*data.size(0)
# convert output probabilities to predicted class
_,pred=torch.max(output,1)
# compare predictions to true label
correct_tensor=pred.eq(target.data.view_as(pred))
correct=(
np.squeeze(correct_tensor.numpy())
ifnottrain_on_gpu
elsenp.squeeze(correct_tensor.cpu().numpy())
)
# calculate test accuracy for each object class
foriinrange(batch_size):
label=target.data[i]
class_correct[label]+=correct[i].item()
class_total[label]+=1
# average test loss
test_loss=test_loss/len(test_loader)
print("Test Loss: {:.6f}\n".format(test_loss))
foriinrange(10):
ifclass_total[i]>0:
print(
"Test Accuracy of %5s: %2d%% (%2d/%2d)"
%(
classes[i],
100*class_correct[i]/class_total[i],
np.sum(class_correct[i]),
np.sum(class_total[i]),
)
)
else:
print("Test Accuracy of %5s: N/A (no training examples)"%(classes[i]))
print(
"\nTest Accuracy (Overall): %2d%% (%2d/%2d)"
%(
100.0*np.sum(class_correct)/np.sum(class_total),
np.sum(class_correct),
np.sum(class_total),
)
)
```
%% Output
Test Loss: 15.510981
Test Accuracy of airplane: 82% (820/1000)
Test Accuracy of automobile: 80% (803/1000)
Test Accuracy of bird: 64% (641/1000)
Test Accuracy of cat: 48% (480/1000)
Test Accuracy of deer: 70% (701/1000)
Test Accuracy of dog: 66% (667/1000)
Test Accuracy of frog: 77% (776/1000)
Test Accuracy of horse: 80% (804/1000)
Test Accuracy of ship: 86% (860/1000)
Test Accuracy of truck: 80% (808/1000)
Test Accuracy (Overall): 73% (7360/10000)
%% Cell type:markdown id:d4b037a0 tags:
- Test loss (Loss):
Model 2 has a significantly lower test loss (15.51) compared with Model 1 (21.15). This suggests that Model 2 is better at generalizing to test data.
- Overall accuracy:
Model 2 has a better overall accuracy of 73% versus 64% for Model 1. This shows that Model 2 is better at classifying the test data.
Model 2 excels particularly in classes such as Airplane (82%) , Ship (86%), Horse (80%), and Truck (80%). It also has better accuracy than the Model 1 in all classes.
- Conclusion :
Model 2 outperforms Model 1 in terms of overall accuracy and test loss. It is better at classifying a wider range of classes, and has better generalization capability on test data.
%% Cell type:markdown id:bc381cf4 tags:
## Exercise 2: Quantization: try to compress the CNN to save space
Quantization doc is available from https://pytorch.org/docs/stable/quantization.html#torch.quantization.quantize_dynamic
The Exercise is to quantize post training the above CNN model. Compare the size reduction and the impact on the classification accuracy
The size of the model is simply the size of the file.
For each class, compare the classification test accuracy of the initial model and the quantized model. Also give the overall test accuracy for both models.
%% Cell type:markdown id:a0a34b90 tags:
Try training aware quantization to mitigate the impact on the accuracy (doc available here https://pytorch.org/docs/stable/quantization.html#torch.quantization.quantize_dynamic)
%% Cell type:markdown id:201470f9 tags:
## Exercise 3: working with pre-trained models.
PyTorch offers several pre-trained models https://pytorch.org/vision/0.8/models.html
We will use ResNet50 trained on ImageNet dataset (https://www.image-net.org/index.php). Use the following code with the files `imagenet-simple-labels.json` that contains the imagenet labels and the image dog.png that we will use as test.
%% Cell type:code id:b4d13080 tags:
``` python
importjson
fromPILimportImage
# Choose an image to pass through the model
test_image="dog.png"
# Configure matplotlib for pretty inline plots
#%matplotlib inline
#%config InlineBackend.figure_format = 'retina'
# Prepare the labels
withopen("imagenet-simple-labels.json")asf:
labels=json.load(f)
# First prepare the transformations: resize the image to what the model was trained on and convert it to a tensor
# Download the model if it's not there already. It will take a bit on the first run, after that it's fast
model=models.resnet50(pretrained=True)
# Send the model to the GPU
# model.cuda()
# Set layers such as dropout and batchnorm in evaluation mode
model.eval()
# Get the 1000-dimensional model output
out=model(image)
# Find the predicted class
print("Predicted class is: {}".format(labels[out.argmax()]))
```
%% Cell type:markdown id:184cfceb tags:
Experiments:
Study the code and the results obtained. Possibly add other images downloaded from the internet.
What is the size of the model? Quantize it and then check if the model is still able to correctly classify the other images.
Experiment with other pre-trained CNN models.
%% Cell type:markdown id:5d57da4b tags:
## Exercise 4: Transfer Learning
For this work, we will use a pre-trained model (ResNet18) as a descriptor extractor and will refine the classification by training only the last fully connected layer of the network. Thus, the output layer of the pre-trained network will be replaced by a layer adapted to the new classes to be recognized which will be in our case ants and bees.
Download and unzip in your working directory the dataset available at the address :
plt.pause(0.001)# pause a bit so that plots are updated
plt.show()
# Get a batch of training data
inputs,classes=next(iter(dataloaders["train"]))
# Make a grid from batch
out=torchvision.utils.make_grid(inputs)
imshow(out,title=[class_names[x]forxinclasses])
```
%% Cell type:markdown id:bbd48800 tags:
Now, execute the following code which uses a pre-trained model ResNet18 having replaced the output layer for the ants/bees classification and performs the model training by only changing the weights of this output layer.
%% Cell type:code id:572d824c tags:
``` python
importcopy
importos
importtime
importmatplotlib.pyplotasplt
importnumpyasnp
importtorch
importtorch.nnasnn
importtorch.optimasoptim
importtorchvision
fromtorch.optimimportlr_scheduler
fromtorchvisionimportdatasets,transforms
# Data augmentation and normalization for training
# Just normalization for validation
data_transforms={
"train":transforms.Compose(
[
transforms.RandomResizedCrop(
224
),# ImageNet models were trained on 224x224 images
transforms.RandomHorizontalFlip(),# flip horizontally 50% of the time - increases train set variability
transforms.ToTensor(),# convert it to a PyTorch tensor
Modify the code and add an "eval_model" function to allow
the evaluation of the model on a test set (different from the learning and validation sets used during the learning phase). Study the results obtained.
Now modify the code to replace the current classification layer with a set of two layers using a "relu" activation function for the middle layer, and the "dropout" mechanism for both layers. Renew the experiments and study the results obtained.
Apply ther quantization (post and quantization aware) and evaluate impact on model size and accuracy.
%% Cell type:markdown id:04a263f0 tags:
## Optional
Try this at home!!
Pytorch offers a framework to export a given CNN to your selfphone (either android or iOS). Have a look at the tutorial https://pytorch.org/mobile/home/
The Exercise consists in deploying the CNN of Exercise 4 in your phone and then test it on live.