22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Data Preparation

1 # ImageNet statistics

2 normalizer = Normalize(mean=[0.485, 0.456, 0.406],

3 std=[0.229, 0.224, 0.225])

4

5 composer = Compose([Resize(256),

6 CenterCrop(224),

7 ToTensor(),

8 normalizer])

9

10 train_data = ImageFolder(root='rps', transform=composer)

11 val_data = ImageFolder(root='rps-test-set', transform=composer)

12

13 # Builds a loader of each set

14 train_loader = DataLoader(

15 train_data, batch_size=16, shuffle=True

16 )

17 val_loader = DataLoader(val_data, batch_size=16)

This time, we’ll use the smallest version of the ResNet model (resnet18) and either

fine-tune it or use it as a feature extractor only.

Fine-Tuning

Model Configuration (1)

1 model = resnet18(pretrained=True)

2 torch.manual_seed(42)

3 model.fc = nn.Linear(512, 3)

There is no freezing since fine-tuning entails the training of all the weights, not only

those from the "top" layer.

Model Configuration (2)

1 multi_loss_fn = nn.CrossEntropyLoss(reduction='mean')

2 optimizer_model = optim.Adam(model.parameters(), lr=3e-4)

Putting It All Together | 555

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!