Creating a ResNet model

Using the layers of the resnet34 pretrained model, we create a PyTorch sequential model by discarding the last linear layer. We will use this trained model for extracting features from our images. The following code demonstrates this:

#Create ResNet modelmy_resnet = resnet34(pretrained=True)if is_cuda:    my_resnet = my_resnet.cuda()my_resnet = nn.Sequential(*list(my_resnet.children())[:-1])for p in my_resnet.parameters():    p.requires_grad = False

In the preceding code, we created a resnet34 model available in torchvision models. In the following line, we pick all the ResNet layers, excluding the last layer, and create a new model using nn.Sequential:

for p in my_resnet.parameters():    p.requires_grad = False

The nn.Sequential ...

Get Deep Learning with PyTorch now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.