Skip to content

videorighter/mnist_classification_lenet5

Repository files navigation

MNIST Classification

In this assignment, you build a neural network classifier with MNIST dataset. For a detailed description about MNIST dataset, please refer to this link.

  • Due date: 2021. 04. 20. Tue 14:00
  • Submission: dataset.py, model.py, main.py files + written report
  • Total score: 100pts
  • Requirements
    1. (20pts) You should write your own pipeline to provide data to your model. Write your code in the template dataset.py. Please read the comments carefully and follow those instructions.
    2. (20pts) (Report) Implement LeNet-5 and your custom MLP models in model.py. Some instructions are given in the file as comments. Note that your custom MLP model should have about the same number of model parameters with LeNet-5. Describe the number of model parameters of LeNet-5 and your custom MLP and how to compute them in your report.
    3. (20pts) Write main.py to train your models, LeNet-5 and custom MLP. Here, you should monitor the training process. To do so, you need some statistics such as average loss values and accuracy at the end of each epoch.
    4. (10pts) (Report) Plot above statistics, average loss value and accuracy, for training and testing. It is fine to use the test dataset as a validation dataset. Therefore, you will have four plots for each model: loss and accuracy curves for training and test datasets, respectively.
    5. (10pts) (Report) Compare the predictive performances of LeNet-5 and your custom MLP. Also, make sure that the accuracy of LeNet-5 (your implementation) is similar to the known accuracy.
    6. (20pts) (Report) Employ at least more than two regularization techniques to improve LeNet-5 model. You can use whatever techniques if you think they may be helpful to improve the performance. Verify that they actually help improve the performance. Keep in mind that when you employ the data augmentation technique, it should be applied only to training data. So, the modification of provided MNIST class in dataset.py may be needed.
  • Note that the details of training configuration which are not mentioned in this document and the comments can be defined yourself. For example, decide how many epochs you will train the model.

Dataset

Original MNIST dataset is already converted into a format of png image. Each of tar files contains 60,000 training images and 10,000 test images respectively. Each image has its own filename like

{ID}_{Label}.png

So, you can extract labels by splitting filename.

Templates

Templates for your own implementation are also provided: dataset.py, model.py and main.py. Some important instructions are given as comments. Please carefully read them before you write your codes. These templates have blank functions and you can find # write your codes here comment. Write your codes there.

dataset.py

# import some packages you need here

class MNIST(Dataset):
    """ MNIST dataset

        To write custom datasets, refer to
        https://pytorch.org/tutorials/beginner/data_loading_tutorial.html

    Args:
        data_dir: directory path containing images

    Note:
        1) Each image should be preprocessed as follows:
            - First, all values should be in a range of [0,1]
            - Substract mean of 0.1307, and divide by std 0.3081
            - These preprocessing can be implemented using torchvision.transforms
        2) Labels can be obtained from filenames: {number}_{label}.png
    """

    def __init__(self, data_dir):

        # write your codes here

    def __len__(self):

        # write your codes here

    def __getitem__(self, idx):

        # write your codes here

        return img, label

if __name__ == '__main__':

    # write test codes to verify your implementations

model.py

import torch.nn as nn

class LeNet5(nn.Module):
    """ LeNet-5 (LeCun et al., 1998)

        - For a detailed architecture, refer to the lecture note
        - Freely choose activation functions as you want
        - For subsampling, use max pooling with kernel_size = (2,2)
        - Output should be a logit vector
    """

    def __init__(self):

        # write your codes here

    def forward(self, img):

        # write your codes here

        return output

class CustomMLP(nn.Module):
    """ Your custom MLP model

        - Note that the number of model parameters should be about the same
          with LeNet-5
    """

    def __init__(self):

        # write your codes here

    def forward(self, img):

        # write your codes here

        return output

main.py

import dataset
from model import LeNet5, CustomMLP

# import some packages you need here

def train(model, trn_loader, device, criterion, optimizer):
    """ Train function

    Args:
        model: network
        trn_loader: torch.utils.data.DataLoader instance for training
        device: device for computing, cpu or gpu
        criterion: cost function
        optimizer: optimization method, refer to torch.optim

    Returns:
        trn_loss: average loss value
        acc: accuracy
    """

    # write your codes here

    return trn_loss, acc

def test(model, tst_loader, device, criterion):
    """ Test function

    Args:
        model: network
        tst_loader: torch.utils.data.DataLoader instance for testing
        device: device for computing, cpu or gpu
        criterion: cost function

    Returns:
        tst_loss: average loss value
        acc: accuracy
    """

    # write your codes here

    return tst_loss, acc

def main():
    """ Main function

        Here, you should instantiate
        1) Dataset objects for training and test datasets
        2) DataLoaders for training and testing
        3) model
        4) optimizer: SGD with initial learning rate 0.01 and momentum 0.9
        5) cost function: use torch.nn.CrossEntropyLoss

    """

    # write your codes here

if __name__ == '__main__':
    main()

About

mnist classification using LeNet-5 and Custom network

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages