PyTorch Assignment Help | Implementing MobileNet Model Using Python

Setup Code

Before getting started we need to run some boilerplate code to set up our environment. You'll need to rerun this setup code each time you start the notebook.


First, run this cell load the autoreload (https://ipython.readthedocs.io/en/stable/config/extensions/autoreload.html?highlight=autoreload) extension. This allows us to edit .py source files, and re-import them into the notebook for a seamless editing and debugging experience.

%load_ext autoreload 
%autoreload 2

Google Colab Setup

Next we need to run a few commands to set up our environment on Google Colab. If you are running this notebook on a local machine you can skip this section.


Run the following cell to mount your Google Drive. Follow the link, sign in to your Google account (the same account you used to store this notebook!) and copy the authorization code into the text box that appears below.

from google.colab import drive 
drive.mount('/content/drive/'

Mounted at /content/drive


Now recall the path in your Google Drive where you uploaded this notebook, fill it in below. If everything is working correctly then running the folowing cell should print the filenames in the assignment directory


import os
import sys
# TODO: Fill in the Google Drive path where you uploaded the assignment, it s
GOOGLE_DRIVE_PATH_AFTER_MYDRIVE = 'CS354-Assignments-2022/swshah-A2' # change
GOOGLE_DRIVE_PATH = os.path.join('drive', 'My Drive', GOOGLE_DRIVE_PATH_AFTER
print(os.listdir(GOOGLE_DRIVE_PATH))
sys.path.append(GOOGLE_DRIVE_PATH)
import time, os
os.environ["TZ"] = "US/Eastern"
time.tzset()

output:

['images', '__pycache__', 'mobilenet_solutions.ipynb', 'mobilenet.ipynb']



Let us start

Efficient revolutionary neural networks for mobile vision applications to experience Depthwise convolution and Pointwise convolution. (https://arxiv.org/pdf/1704.04861.pdf)


The MobileNet model is based on depthwise separable convolutions which is a form of factorized convolutions which factorize a standard convolution into a depthwise convolution and a 1×1 convolution called a pointwise convolution. For MobileNets the depthwise convolution applies a single filter to each input channel.


The pointwise convolution then applies a 1×1 convolution to combine the outputs the depthwise convolution. A standard convolution both filters and combines inputs into a new set of outputs in one step. The depthwise separable convolution splits this into two layers, a separate layer for filtering and a separate layer for combining. This factorization has the effect of drastically reducing computation and model size. Figure below shows how a standard convolution (a) is factorized into a depthwise convolution (b) and a 1 × 1 pointwise convolution (c). Figure 1. shows standard, depthwise and pointwise convolutional filters.




#check torch version 
import torch 
print(torch.__version__) 

#MobileNet separable convolution

In this cell, you are asked to implement both Mobilenet and Standerd block. showed in the image below.




import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
import torch.optim as optim
class MobileNetBlock(nn.Module):
 def __init__(self, in_depth, out_depth, stride=1):
	super(MobileNetBlock, self).__init__()
	# add your code here ... 
	# hint: For Depthwise convolution, use the Conv2d with groups equal t
 
 def forward(self, x):
	# add your code here

class StandardBlock(nn.Module):
 def __init__(self, in_depth, out_depth, stride=1):
	super(StandardBlock, self).__init__()
	# add your code here ... 
 
 def forward(self, x):
	# add your code here
	return out


#Create DataLoader

Now, you are asked to implement dataloaders that will be used during the training and testing to load the data. To implement the dataloaders you need to implement the following:

  • We will work on CIFAR dataset. I am providing few lines to start from.

  • transform_train and transform_test: Used to transoform and normalize your data. remember that you need to normalize the data. Do you need to use transformation in the test set ? Justify your answer.

  • trainloader, test loader: use batch size of 12


trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=

transform_train = # add your code here
transform_test = # add your code here

# add transformation to your data
trainloader = # add your code here
testloader = # add your code here

output:

Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz (http s://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz) to ./data/cifar-10-pyt hon.tar.gz 0%| | 0/170498071 [00:00


Extracting ./data/cifar-10-python.tar.gz to ./data

Files already downloaded and verified

Data mean [0.49139968 0.48215841 0.44653091]

Data std [0.24703223 0.24348513 0.26158784]

Files already downloaded and verified

Files already downloaded and verified



#Create MobileNetV1 network

Now it is the time to create MobileNet network that will classify CIFAR dataset. You will be asked to implement the following architecture. All the convolutions should use the MobileNetBlock except the first convolutional layer which should be standard one (We are trying to follow same steps in the MobileNet paper by starting with standard convolution).


32×32×3 ==> (Standerd convoltion)

32×32×32 ==> (MobileNet convoltion)

32×32×64 ==> (MobileNet convoltion)

16×16×128 ==> (MobileNet convoltion)

16×16×128 ==>(MobileNet convoltion)

8×8×256 ==>(MobileNet convoltion)

8×8×256 ==>(MobileNet convoltion)

4×4×512 ==>(MobileNet convoltion)

4×4×512 ==>(MobileNet convoltion)

2×2×1024 ==>(MobileNet convoltion)

2×2×1024

Mean pooling ==> 1 × 1 × 1024

Finally, it is fully connected to 10 output nodes


class MobileNetV1(nn.Module):
 
	def __init__(self, num_classes=10):
		super(MobileNetV1, self).__init__()
		# add your code here 
 
	def forward(self, x):
		# add your code here 
		return out

Now, Implement the StandardNet that replace all MobileNet blocks with StanderdBlocks.

class StandardNet(nn.Module):
 
	def __init__(self, num_classes=10):
		super(StandardNet, self).__init__()
		# add your code here 
	def forward(self, x):
		# add your code here 
		return out

#Instantiation network

  • Use GPU for faster training.

  • Define two networks, MobileNet and stNet.

  • Move these networks to the GPU.

  • use an appropriate loss functions.

  • use an appropriate optimization functions.


# To use GPU, you can set it in the menu "Runtime" - > "change runtime type"
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# Put the network on the GPU
stNet = # add your code here for StandardNet, remeber to move the network for
MobileNet = # add your code here
criterion = # add your code here to define the loss function
optimizer = # add your code here to define the optimizer.
MobileNetoptimizer = # add your code here to define the optimizer

#model training

Now put everthing togather and train/test the two networks in the cells below. Train only for 10 epochs for each model.

# train MobileNet here.
for epoch in range(10): # Repeat multiple rounds
	for i, (inputs, labels) in enumerate(trainloader):
		inputs = inputs.to(device)
		labels = labels.to(device)
 
		# add your code here ... 
		# Output statistics
		if i % 100 == 0: 
			print('Epoch: %d Minibatch: %5d loss: %.3f' %(epoch + 1, i + 1, l
print('Finished Training')

Output:

Epoch: 1 Minibatch: 1 loss: 2.344

Epoch: 1 Minibatch: 101 loss: 1.870

Epoch: 1 Minibatch: 201 loss: 1.661

Epoch: 1 Minibatch: 301 loss: 1.362


Epoch: 2 Minibatch: 1 loss: 1.329

Epoch: 2 Minibatch: 101 loss: 1.426

Epoch: 2 Minibatch: 201 loss: 1.151

Epoch: 2 Minibatch: 301 loss: 1.229


Epoch: 3 Minibatch: 1 loss: 1.206

...

...


#Model test

# Test MobileNet here ...
correct = 0
total = 0
for data in testloader:
	# add your code here ...
print('Accuracy of the network on the 10000 test images: %.2f %%' % (
	100 * correct / total))

Accuracy of the network on the 10000 test images: 82.35 %



Model Parameters

Print the summary for each net here

from torchsummary import summary 
summary(stNet, (3,32,32), device = 'cuda') 
summary(MobileNet, (3,32,32), device = 'cuda') 

output:

----------------------------------------------------------------

Layer (type) Output Shape Param # ================================================================

Conv2d-1 [-1, 32, 32, 32] 864

BatchNorm2d-2 [-1, 32, 32, 32] 64

Conv2d-3 [-1, 64, 32, 32] 18,432

BatchNorm2d-4 [-1, 64, 32, 32] 128 StandardBlock-5 [-1, 64, 32, 32] 0

Conv2d-6 [-1, 128, 16, 16] 73,728

...

...

Total params: 18,838,058

Trainable params: 18,838,058

Non-trainable params: 0

----------------------------------------------------------------

Input size (MB): 0.01

Forward/backward pass size (MB): 4.81

Params size (MB): 71.86

Estimated Total Size (MB): 76.69

----------------------------------------------------------------

----------------------------------------------------------------

Layer (type) Output Shape Param # ================================================================

Conv2d-1 [-1, 32, 32, 32] 864

BatchNorm2d-2 [-1, 32, 32, 32] 64

Conv2d-3 [-1, 32, 32, 32] 288 BatchNorm2d-4 [-1, 32, 32, 32] 64

Conv2d-5 [-1, 64, 32, 32] 2,048

...

...

...


  • Comment on the performance vs number of paramters vs number of calculations (speed) for each network here.

  • What are the Pros and Cons for each Net?



We are providing all deep learning related help, our expert have a good knowledge in all deep learning libraries related work like Pytourch, Tensorflow, keras and more others.


Hire realcode4you expert and get instant help with an affordable price. If you have any other related task then share with us at:


realcode4you@gmail.com
18 views0 comments