Problem 12
Run the following code:
from torchvision.models import resnet34
import torchsummary
model = resnet34(pretrained=True)
Part 12.1
What is the total number of learnable parameters in model?
Hint: For an object with type torch.nn.parameter.Parameter, you can use the method enum() to count the number of learnable parameters in it.
Part 12.2
Suppose the input has shape (B,3,224,224). What is the shape of the output from model.layer3?
Part 12.3
What is the total number of learnable parameters in all convolutional modules in model.layer2[0]? To solve this problem, you are not allowed to use the method enum() for an object with type torch.nn.parameter.Parameter. Reasoning is required.
Part 12.4
We want to use the pretrained resnet34 model as a backbone for a classification task with 12 labels. Build such a model.
In your solution, all parameters in the backbone shall be frozen (that is, they are fixed and not learnable).
2 Likes
Part 12.1
21,797,672
Part 12.2
(B, 256, 14, 14)
Part 12.3
In ResNet-34, model.layer2[0] is a BasicBlock that performs downsampling. It contains three convolutional modules:
conv1: A 3 \times 3 convolution with 64 input channels and 128 output channels (stride 2).
- Parameters = \text{out\_channels} \times \text{in\_channels} \times (\text{kernel\_width} \times \text{kernel\_height}).
- 128 \times 64 \times (3 \times 3) = 128 \times 64 \times 9 = 73,728.
conv2: A 3 \times 3 convolution with 128 input channels and 128 output channels.
- 128 \times 128 \times (3 \times 3) = 128 \times 128 \times 9 = 147,456.
downsample[0]: A 1 \times 1 projection convolution used in the shortcut path to match dimensions (64 to 128 channels).
- 128 \times 64 \times (1 \times 1) = 8,192.
Total parameters = 73,728 + 147,456 + 8,192 = \mathbf{229,376}.
Part 12.4
import torch.nn as nn
from torchvision.models import resnet34
model = resnet34(pretrained=True)
for param in model.parameters():
param.requires_grad = False
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 12)
2 Likes