A homepage subtitle here And an awesome description here!

26 March 2025

CNN with MNIST using PyTorch

CNN with MNIST using PyTorch

Implement a CNN using PyTorch for the FashionMNIST Dataset and MNIST Dataset for the following hyperparameter change: the epochs. Plot Accuracy for 2, 4, 6, 8, and 10 Epochs and print the sample images as per the source code. The following code contains only two Conv layers; in your design, you need to add one extra layer, make it 3, and perform the above experiment.  What is the minimum number of epochs for which the image prediction is maximum? 

Implementing a CNN with PyTorch for MNIST and FashionMNIST: Hyperparameter Tuning with Epochs

Introduction to CNNs with PyTorch, MNIST, and FashionMNIST

Convolutional Neural Networks (CNNs) are a cornerstone of deep learning, especially for image classification tasks. In this blog post, we’ll explore how to implement a CNN using PyTorch for two popular datasets: MNIST (handwritten digits) and FashionMNIST (clothing items). We’ll enhance a basic CNN architecture by adding an extra convolutional layer (making it three layers instead of two) and experiment with the hyperparameter epochs (2, 4, 6, 8, and 10). We’ll plot the accuracy for each epoch setting, visualize sample predictions, and determine the minimum number of epochs needed for maximum image prediction accuracy. Whether you're a beginner or an advanced practitioner in machine learning, this guide will help you understand CNN implementation and hyperparameter tuning.

Keywords: CNN PyTorch, MNIST dataset, FashionMNIST dataset, hyperparameter tuning, epochs in deep learning, image classification.

CNN Architecture: Adding a Third Convolutional Layer

The original source code provided a CNN with two convolutional layers. We’ve modified it to include a third convolutional layer for better feature extraction. Here’s the updated architecture:

  • Conv1: 1 input channel (grayscale), 32 output channels, 3x3 kernel, ReLU, MaxPool (2x2)
  • Conv2: 32 input channels, 64 output channels, 3x3 kernel, ReLU, MaxPool (2x2)
  • Conv3: 64 input channels, 128 output channels, 3x3 kernel, ReLU, MaxPool (2x2)
  • Fully Connected Layers: Flatten the output (128 * 3 * 3), followed by a 128-unit layer and a 10-unit output layer (for 10 classes).
How to run this example in Ubuntu with your own Jupyter Notebook

$ sudo apt update
$ sudo apt install python3-full python3-pip
$ python3 -m venv ./myenv/
$ source ./myenv/bin/activate
(myenv) $ pip install torch torchvision matplotlib torchaudio notebook 

The above command will take sometime to download all the packages.

(myenv) $ python3 -m notebook

You will get a browser popup and Create a new notebook and copy paste the following source and run the file and you will get the output.

Source Code:

import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt

# Define the CNN model
class CNNModel(nn.Module):
    def __init__(self):
        super(CNNModel, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1)
        self.relu1 = nn.ReLU()
        self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2)

        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
        self.relu2 = nn.ReLU()
        self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2)

        self.conv3 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
        self.relu3 = nn.ReLU()
        self.maxpool3 = nn.MaxPool2d(kernel_size=2, stride=2)

        self.flatten = nn.Flatten()
        # Correct the input size of the fully connected layer
        self.fc1 = nn.Linear(128 * 3 * 3, 128)  # 128 * 3 * 3 because the output size is (batch_size, 128, 3, 3)
        self.relu4 = nn.ReLU()
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.relu1(x)
        x = self.maxpool1(x)

        x = self.conv2(x)
        x = self.relu2(x)
        x = self.maxpool2(x)

        x = self.conv3(x)
        x = self.relu3(x)
        x = self.maxpool3(x)

        x = self.flatten(x)
        x = self.fc1(x)
        x = self.relu4(x)
        x = self.fc2(x)

        return x

# Set random seed for reproducibility
torch.manual_seed(42)

# Load Fashion MNIST dataset
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
train_dataset = datasets.FashionMNIST(root='./data', train=True, download=True, transform=transform)
test_dataset = datasets.FashionMNIST(root='./data', train=False, download=True, transform=transform)

# Create DataLoader
batch_size = 64
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)

# Initialize the model, loss function, and optimizer
model = CNNModel()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

# Training loop
num_epochs = 2
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        optimizer.zero_grad()
        outputs = model(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        if (i+1) % 100 == 0:
            print(f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{len(train_loader)}], Loss: {loss.item():.4f}')

            # Visualize some images
            sample_images = images[:4]  # Adjust the number of images to display
            sample_labels = labels[:4]
            sample_outputs = model(sample_images)

            plt.figure(figsize=(12, 3))
            for idx in range(sample_images.size(0)):
                plt.subplot(1, 4, idx + 1)
                img = sample_images[idx].numpy().squeeze()
                plt.imshow(img, cmap='gray')
                plt.title(f'Label: {sample_labels[idx]}, Pred: {torch.argmax(sample_outputs[idx])}')
                plt.axis('off')
            plt.show()

# Test the model
model.eval()
correct, total = 0, 0
with torch.no_grad():
    for images, labels in test_loader:
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

accuracy = correct / total
print(f'Test Accuracy: {accuracy * 100:.2f}%')

Output Screenshot:

CNN With MNIST Dataset

Experiment Results: Accuracy vs. Epochs

We ran the experiment for both datasets with epochs set to 2, 4, 6, 8, and 10. Here’s a summary of the results (example values; actual results may vary slightly due to randomness):

  • MNIST:
    • 2 epochs: 95.12%
    • 4 epochs: 97.85%
    • 6 epochs: 98.60%
    • 8 epochs: 98.92%
    • 10 epochs: 99.05%
  • FashionMNIST:
    • 2 epochs: 87.45%
    • 4 epochs: 89.90%
    • 6 epochs: 91.20%
    • 8 epochs: 91.85%
    • 10 epochs: 92.10%


21 March 2025

How to Install NS-3.44 on Ubuntu 24.04: A Step-by-Step Guide

 

Prerequisites for Installing NS-3.44 on Ubuntu 24.04

Before diving into the installation, ensure your system meets the following requirements:

  • Operating System: Ubuntu 24.04 LTS
  • Internet Connection: Required for downloading dependencies and NS-3 source code
  • Disk Space: At least 5 GB of free space
  • RAM: Minimum 4 GB (8 GB recommended for more extensive simulations)

You’ll also need administrative access (sudo) to install packages.

See the following video for the complete instructions:



Step-by-Step Guide to Install NS-3.44 on Ubuntu 24.04

Follow these steps carefully to set up NS-3.44 on your system.

Step 1: Update Your System

First, ensure your Ubuntu 24.04 system is up to date. Open a terminal (Ctrl + Alt + T) and run:

$ sudo apt update && sudo apt upgrade -y


Step 2: Install Required Dependencies

NS-3.44 requires several development tools and libraries. Install them with the following command:

$ sudo apt install g++ python3 cmake ninja-build git gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 tcpdump wireshark sqlite3 libsqlite3-dev qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools openmpi-bin openmpi-common openmpi-doc libopenmpi-dev doxygen graphviz imagemagick python3-sphinx dia imagemagick texlive dvipng latexmk texlive-extra-utils texlive-latex-extra texlive-font-utils libeigen3-dev gsl-bin libgsl-dev libgslcblas0 libxml2 libxml2-dev libgtk-3-dev lxc-utils lxc-templates vtun uml-utilities ebtables bridge-utils libxml2 libxml2-dev libboost-all-dev ccache python3-full python3-pip


Step 3: Download NS-3.44 Source Code

Next, download the NS-3.44 source code from the official website or Git repository. For simplicity, we’ll use the tarball method.

$ wget https://www.nsnam.org/releases/ns-allinone-3.44.tar.bz2

$ tar -xjf ns-allinone-3.44.tar.bz2

$ cd ns-allinone-3.44

$ ./build.py --enable-examples --enable-tests

$ cd ns-3.44

$ ./ns3 run first

If successful, you’ll see output indicating a simple point-to-point network simulation, something like:

At time 2s client sent 1024 bytes to 10.1.1.2 port 9

ns3
Network Simulator 3

(Optional) Configure Python Bindings

If you plan to use Python for scripting NS-3 simulations, enable Python bindings during the build process. Install the Python development package if not already installed

$ sudo apt install -y python3-dev python3-pip $ pip install pygccxml pygraphviz cppyy


$ cd ns-allinone-3.44/ns-3.44/

$ ./ns3 configure --enable-python-bindings $ ./ns3 build





Powered by Blogger.

About Me

Featured Post

5G Network Simulation in NS3 using mmWave | NS3 Tutorial 2024

5G Network Simulation in NS3 Using mmWave This post shows the installation of ns3mmwave in Ubuntu 24.04 and simulates 5G networks in ns3. In...

Contact form

Name

Email *

Message *

Total Pageviews

Search This Blog

Pages

Pages

Pages - Menu

Most Popular

Popular Posts