0
0
PyTorchml~20 mins

torchvision pre-trained models in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Torchvision Pretrained Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output shape of pre-trained ResNet18 model
What is the shape of the output tensor when passing a batch of 8 RGB images of size 224x224 through a pre-trained ResNet18 model from torchvision?
PyTorch
import torch
import torchvision.models as models

model = models.resnet18(pretrained=True)
model.eval()

input_tensor = torch.randn(8, 3, 224, 224)
output = model(input_tensor)
output_shape = output.shape
print(output_shape)
Atorch.Size([8, 512])
Btorch.Size([8, 100])
Ctorch.Size([8, 1000])
Dtorch.Size([8, 2048])
Attempts:
2 left
💡 Hint
The pre-trained ResNet18 model outputs class scores for 1000 classes.
Model Choice
intermediate
2:00remaining
Choosing a model for feature extraction
You want to extract 512-dimensional feature vectors from images using a pre-trained torchvision model without the final classification layer. Which model and modification is correct?
AUse squeezenet and replace model.classifier with torch.nn.Linear(512, 1000)
BUse resnet18 and replace model.fc with torch.nn.Identity()
CUse vgg16 and replace model.features with torch.nn.Identity()
DUse alexnet and remove model.classifier entirely
Attempts:
2 left
💡 Hint
The final fully connected layer in ResNet18 is model.fc.
Hyperparameter
advanced
2:00remaining
Correct input normalization for pre-trained models
Which normalization mean and standard deviation values should be used to preprocess images before feeding them into any torchvision pre-trained model?
Amean=[0.406, 0.456, 0.485], std=[0.225, 0.224, 0.229]
Bmean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]
Cmean=[0.0, 0.0, 0.0], std=[1.0, 1.0, 1.0]
Dmean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
Attempts:
2 left
💡 Hint
These values are based on ImageNet dataset statistics.
Metrics
advanced
2:00remaining
Top-1 accuracy of pre-trained models on ImageNet validation
Which of the following is the closest reported Top-1 accuracy on ImageNet validation for the pre-trained torchvision DenseNet121 model?
A74.9%
B69.8%
C77.0%
D82.3%
Attempts:
2 left
💡 Hint
DenseNet121 accuracy is generally around mid-70s percent.
🔧 Debug
expert
2:00remaining
Identifying error when loading pre-trained model weights
What error will occur if you try to load a pre-trained model with torchvision.models.resnet50(pretrained=False) and then call model.load_state_dict(torch.load('resnet50_weights.pth')) where the weights file contains pre-trained weights?
ARuntimeError: Error(s) in loading state_dict for ResNet
BFileNotFoundError: No such file or directory: 'resnet50_weights.pth'
CTypeError: load_state_dict() missing 1 required positional argument
DNo error, model loads weights successfully
Attempts:
2 left
💡 Hint
Mismatch in model architecture or missing keys causes loading errors.