ResNet Model in Pytorch

Resnet Model in Pytorch

Motivation

During using the pytorch in traing/designing model, specifically using ‘torchvision.models’, there are several models can be used in this package. For ResNet in this package, several kind of ResNets i.e.(ResNet18, ResNet34, ResNet50, ResNet101, ResNet152) are included to satisfy our need Which can be used to instantiate a ResNet model.

The motivation for this post is that there are several kind of ResNet, understanidng them will enable us to use them more efficiently.

In this post we will claim why they called ResNetn and how to use them in pytorch.

Why ‘ResNet18’ is ‘18

18 = 17 (convolutional layer) + 1 (fully connected)
18 mainly means there are 18 steps with weights, including convolutional and fully connected layers, excluding pooling and BN layers. (even if the BN layer is parameterised)

let’s see how is counts based on the graph of ResNet18:

As seen in the graph above, for each Basic block we have two convolution layer. Therefore when we count the number of convolutional layer, it should be the number of Basic blocks * 2.

And by summing the convolution layer before the Max pooling and the fully connected layer used for output, the result is 8*2+2=18, which leads to ResNet18

The structure of those ResNet

Here I post the structure list in Kaiming.H’s paper called ‘Deep Residual Learning for Image Recognition’
arxiv link: https://arxiv.org/abs/1512.03385

and in this structure list, for those we can easily calculate why they called 34,50,101,152.

For example, for ResNet50, 50 = 3*(3+4+6+3)+2, where 3 comes from the basic block in ResNet50 have 3 convolution with filter 1*1, 3*3,1*1 respectively.

Check the form for 101 and 152 by yourself!

How to use them with torchvision.model