Tutorial: PyTorch Example Code

Preparation

  1. Create the working directory: such as \TU_MLP_CNN_Classification\

  2. Download the Tutorial Code in Zip file: TU_MLP_CNN_Classification_Example Code 2024.zip

  3. Unzip the files in the working directory.

  4. Make sure you have the subdirectory for saving weights: \TU_MLP_CNN_Classification\weights\

Example 1. ANN(MLP) : Model, Train, Test

Classify the MNIST digit with a simple ANN\

  • Image Input: 1x28x28 image

  • Flatten into a 1x28*28 element vector

  • 1st Layer: linear to 250 dimensions / ReLU

  • 2nd Layer: linear to 100 dimensions / ReLU

  • 3rd Layer: linear to 10 dimensions / log SoftMax

  • Output: 1x10

  • Activation function: ReLU

Creating Model Architecture, Training and Evaluation

We will create a class for the model architecture

Also, we will create PyTorch source files for processing (1) Training and (2) Evaluation of the model

  1. Check if you have created the subfolder .\weights\

  2. Download the source code for Model Architecture: myModel.py

  3. Download the source code for Model Training: TU_PyTorch_MLP_Train.py

    • Run this code and observe the loss

    • Change the training epoch

  4. Download the source code for Model Evaluation: TU_PyTorch_MLP_Eval.py

    • Run this code and observe the evaluation accuracy and sample images

(For CoLAB) Everything in one source file

(For CoLAB) Everything in one source file

Example 2. CNN : Model, Train, Test

LeNet-5 Architecture. Credit: LeCun et al., 1998

Architecture

  • [C1] Conv: Input (32x32x1) to Output (28x28x6) by 5x5 filters, relu

  • [S2] Pooling : Input (28x28x6) to Output (14x14x6), maxPooling 2x2

  • [C3] Conv: Input (14x14x6) to Output (10x10x16) by 5x5 filters, relu

  • [S4] Pooling : Input (10x10x16) to Output (5x5x16), maxPooling 2x2

  • Flatten : Input (5x5x16) to Output 1D 1x (5 * 5 * 16)

  • [F5] FC : Input (1x5 * 5 * 16) to Output (1x120) , relu

  • [F6] FC : Input (1x120) to Output (1x84) , relu

  • [OUTPUT] : Input (1x84) to Output (1x10)

Creating Model Architecture, Training and Evaluation

We will worki in the same working directory as Example 1: e.g. \TU_MLP_CNN_Classification\

We will create a class for the model architecture. Also, we will create PyTorch source files for processing (1) Training and (2) Evaluation of the model

  1. Update myModel.py by adding class LeNet5

  2. Download the source code for Model Training: TU_PyTorch_CNN_Train.py

    • Run this code and observe the loss

    • Change the training epoch

  3. Download the source code for Model Evaluation: TU_PyTorch_CNN_Eval.py

    • Run this code and observe the evaluation accuracy and sample images

Other Style using nn.Sequencial()

Important Note

  • in Sequential(). use nn.ReLU() , NOT F.relu()

  • ( nn.ReLU() vs F.relu() ) Watch out for spelling and Captital Letters

  • add comma , in nn.Sequential(). Such as nn.Conv2d(1,6,5), nn.ReLU() etc

The only changes in Example 2 are (1) creating CNN model class (2) changing the input image dimension. The rest of the code are the same

Train and Evaluation of CNN Classification

LeNet uses 1x32x32 input. Therefore, reshape MNIST from 1x28x28 to 1x32x32

  • MLP uses 1x28x28 as the Input for MNIST

  • LeNet uses 1x32x32 as the Input for MNIST

  1. Download the source code for Model Training: TU_PyTorch_CNN_Train.py

    • Run this code and observe the loss

    • Change the training epoch

  2. Download the source code for Model Evaluation: TU_PyTorch_CNN_Eval.py

    • Run this code and observe the evaluation accuracy and sample images

Last updated

Was this helpful?