22.02.2024 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

convolutional layers to our model and defining a different loss function to handle

the multiclass classification problem. We also added some more methods to our

class, such that we can visualize the filters learned by our model, attach hooks to

the model’s forward pass, and use the captured results to visualize the

corresponding feature maps.

Data Preparation

1 # Builds tensors from numpy arrays BEFORE split

2 # Modifies the scale of pixel values from [0, 255] to [0, 1]

3 x_tensor = torch.as_tensor(images / 255).float()

4 y_tensor = torch.as_tensor(labels).long()

5

6 # Uses index_splitter to generate indices for training and

7 # validation sets

8 train_idx, val_idx = index_splitter(len(x_tensor), [80, 20])

9 # Uses indices to perform the split

10 x_train_tensor = x_tensor[train_idx]

11 y_train_tensor = y_tensor[train_idx]

12 x_val_tensor = x_tensor[val_idx]

13 y_val_tensor = y_tensor[val_idx]

14

15 # We're not doing any data augmentation now

16 train_composer = Compose([Normalize(mean=(.5,), std=(.5,))])

17 val_composer = Compose([Normalize(mean=(.5,), std=(.5,))])

18

19 # Uses custom dataset to apply composed transforms to each set

20 train_dataset = TransformedTensorDataset(

21 x_train_tensor, y_train_tensor,

22 transform=train_composer

23 )

24 val_dataset = TransformedTensorDataset(

25 x_val_tensor, y_val_tensor,

26 transform=val_composer

27 )

28 # Builds a weighted random sampler to handle imbalanced classes

29 sampler = make_balanced_sampler(y_train_tensor)

30 # Uses sampler in the training set to get a balanced data loader

31 train_loader = DataLoader(

32 dataset=train_dataset, batch_size=16, sampler=sampler

33 )

34 val_loader = DataLoader(dataset=val_dataset, batch_size=16)

Putting It All Together | 411

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

Saved successfully!

Ooh no, something went wrong!