Skip to Content
Deep Learning with PyTorch
book

Deep Learning with PyTorch

by Vishnu Subramanian
February 2018
Intermediate to advanced
262 pages
6h 59m
English
Packt Publishing
Content preview from Deep Learning with PyTorch

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Deep Learning with PyTorch

Deep Learning with PyTorch

Eli Stevens, Thomas Viehmann, Luca Pietro Giovanni Antiga
Grokking Deep Learning

Grokking Deep Learning

Andrew W. Trask

Publisher Resources

ISBN: 9781788624336Supplemental Content