"**Instructions**: The tasks in this notebook are a part of Sheet 1. Look for `TODO` tags throughout the notebook and complete the sections with missing code. Once done, ensure all outputs are visible and correctly displayed. Save your notebook and submit the `.ipynb` file together with the exercise sheet PDF in a single ZIP file."
],
"metadata": {
"id": "994jSp9_dTAp"
}
},
{
"cell_type": "code",
"metadata": {
"id": "jCXXHGdAtdHp"
},
"source": [
"%matplotlib inline"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "JL5-EqeYtdHs"
},
"source": [
"import torch\n",
"\n",
"if torch.cuda.is_available():\n",
" device = torch.device('cuda')\n",
"else:\n",
" device = torch.device('cpu')\n",
"\n",
"print(device)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "73l8LqGetdHu"
},
"source": [
"Note: Tasks in this notebook can be solved without a GPU as well"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5BANL3zstdHv"
},
"source": [
"__Objective of this notebook is to give hands on experience in training a Convolutional Neural Network(CNN) from scratch using Pytorch framework. Further the tasks will also give an insight on how to change the network architecture and hyperparameter settings to achieve better results.__\n",
"\n",
"Fill the code in the notebook where it is explicitly mentioned and solve all tasks to complete the assignment"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "UJDuAZI8tdHv"
},
"source": [
"We divide our task in followind steps:\n",
"\n",
"1- Load the CIFAR-10 dataset and look at some properties of the dataset like:\n",
" * size of the images\n",
" * number of images in the dataset\n",
" * number of channels in the image (RGB=3, grayscale=1)\n",
" * number of classes in the dataset\n",
" \n",
"2- Define a network architecture which takes batches of the images in the form of a 4-dimensional tensor and compute the output scores for each class in the dataset.\n",
"\n",
"3- Define a loss function for training the network, and an optimizer (SGD, Adam, or Adagrad) which defines the strategy to update weights of the network.\n",
"\n",
"4- Finally, train the network and find out the accuracy on validation set\n",
"\n",
"5- Improve the network architecture to get better accuracy."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PN6B0Rc3tdHw"
},
"source": [
"---\n",
"\n",
"__Step-1__ : Loading the CIFAR-10 dataset is very easy using ``torchvision`` package, which has readily availabe famous datasets and API to load these datasets into batches known as dataloaders via ``torchvision.Datasets``and ``torch.utils.data.DataLoader'``"
"### Task: Define your own network, such that the validation acccuracy of the model is greater than 70%. Following constraints should be strictly followed: ###\n",
"\n",
"1. The network should be trained from scratch (randomly initialized weigths), transfer learning is not allowed\n",
"\n",
"Note: Apply __model.apply(weight_reset)__ method over your model before training to make sure model is randomly initialized\n",
"2. Use the training methods as defined above (train_one_epoch() and validate() should be used and not changed)\n",
"3. Your network should reach atleast 70% accuracy on validatin set under 10 epochs"
"__Hint:__ One might try following ways to obtain the required results:\n",
"\n",
"1) __Filter Size:__ The filter size can be decreased from 5 to 3, this can increase efficiency and reduce overfitting\n",
"\n",
"2) __Number of Filters:__ Increase the number of filters in each convoltuional layer. More convolutional filters gives a network higher representation power\n",
"\n",
"3) __Add more convolutions:__ Increase from two to three convolutional layers. This indeed helps in better receptive fields of the filters and increase represtation power of the network.\n",
"\n",
"4) __Apply Batch Normalizations:__ Batch Normalization will help your model to train faster. Hence it can help the network to converge to better accuracy in less number of epochs\n",
"\n",
"5) __Global Average Pooling:__ Get rid of all the layers and replace them by average pooling and a linear layer for classification. This idea is from the Inception network. \n",
"\n",
"6) __Regularization:__ Apply L2 regularization and dropout, if you see validation accuracy going much lower than the training accuracy\n",
"\n",
"7) __Different Optimizer:__ Instead of SGD, optimizers like Adam and Adagrad can be tried"
]
},
{
"cell_type": "code",
"metadata": {
"id": "qwkbsl6-tdIQ"
},
"source": [
"import torch.optim as optim\n",
"\n",
"\n",
"model = GhostModel().to(device)\n",
"model.apply(weight_reset)\n",
"\n",
"optimizer =\n",
"\n",
"num_epochs = 10\n",
"for epoch in range(num_epochs):\n",
" train_loss, train_acc, model = train_one_epoch(model, trainloader, optimizer, criterion)\n",
<ahref="https://colab.research.google.com/github/mindgarage/very-deep-learning-wise2324/blob/main/exercises/Exercise_2(4).ipynb"target="_parent"><imgsrc="https://colab.research.google.com/assets/colab-badge.svg"alt="Open In Colab"/></a>
%% Cell type:markdown id: tags:
# Programming Exercise 2(4): CIFAR Challenge
## Very Deep Learning (VDL) - Winter Semester 2023/24
---
### Group Details:
-**Group Name:**\[Enter OLAT Group Name Here\]
### Members:
-\[Participant 1 Name\], \[Matrikel-Nr 1\]
-\[Participant 2 Name\], \[Matrikel-Nr 1\]
- ...
---
**Instructions**: The tasks in this notebook are a part of Sheet 1. Look for `TODO` tags throughout the notebook and complete the sections with missing code. Once done, ensure all outputs are visible and correctly displayed. Save your notebook and submit the `.ipynb` file together with the exercise sheet PDF in a single ZIP file.
%% Cell type:code id: tags:
```
%matplotlib inline
```
%% Cell type:code id: tags:
```
import torch
if torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('cpu')
print(device)
```
%% Cell type:markdown id: tags:
Note: Tasks in this notebook can be solved without a GPU as well
%% Cell type:markdown id: tags:
__Objective of this notebook is to give hands on experience in training a Convolutional Neural Network(CNN) from scratch using Pytorch framework. Further the tasks will also give an insight on how to change the network architecture and hyperparameter settings to achieve better results.__
Fill the code in the notebook where it is explicitly mentioned and solve all tasks to complete the assignment
%% Cell type:markdown id: tags:
We divide our task in followind steps:
1- Load the CIFAR-10 dataset and look at some properties of the dataset like:
* size of the images
* number of images in the dataset
* number of channels in the image (RGB=3, grayscale=1)
* number of classes in the dataset
2- Define a network architecture which takes batches of the images in the form of a 4-dimensional tensor and compute the output scores for each class in the dataset.
3- Define a loss function for training the network, and an optimizer (SGD, Adam, or Adagrad) which defines the strategy to update weights of the network.
4- Finally, train the network and find out the accuracy on validation set
5- Improve the network architecture to get better accuracy.
%% Cell type:markdown id: tags:
---
__Step-1__ : Loading the CIFAR-10 dataset is very easy using ``torchvision`` package, which has readily availabe famous datasets and API to load these datasets into batches known as dataloaders via ``torchvision.Datasets``and ``torch.utils.data.DataLoader'``
### Task: Define your own network, such that the validation acccuracy of the model is greater than 70%. Following constraints should be strictly followed: ###
1. The network should be trained from scratch (randomly initialized weigths), transfer learning is not allowed
Note: Apply __model.apply(weight_reset)__ method over your model before training to make sure model is randomly initialized
2. Use the training methods as defined above (train_one_epoch() and validate() should be used and not changed)
3. Your network should reach atleast 70% accuracy on validatin set under 10 epochs
__Hint:__ One might try following ways to obtain the required results:
1) __Filter Size:__ The filter size can be decreased from 5 to 3, this can increase efficiency and reduce overfitting
2) __Number of Filters:__ Increase the number of filters in each convoltuional layer. More convolutional filters gives a network higher representation power
3) __Add more convolutions:__ Increase from two to three convolutional layers. This indeed helps in better receptive fields of the filters and increase represtation power of the network.
4) __Apply Batch Normalizations:__ Batch Normalization will help your model to train faster. Hence it can help the network to converge to better accuracy in less number of epochs
5) __Global Average Pooling:__ Get rid of all the layers and replace them by average pooling and a linear layer for classification. This idea is from the Inception network.
6) __Regularization:__ Apply L2 regularization and dropout, if you see validation accuracy going much lower than the training accuracy
7) __Different Optimizer:__ Instead of SGD, optimizers like Adam and Adagrad can be tried
%% Cell type:code id: tags:
```
import torch.optim as optim
model = GhostModel().to(device)
model.apply(weight_reset)
optimizer =
num_epochs = 10
for epoch in range(num_epochs):
train_loss, train_acc, model = train_one_epoch(model, trainloader, optimizer, criterion)