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 # 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.reshape(-1, 1)).float()

5 # Uses index_splitter to generate indices

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

7 # Uses indices to perform the split

8 x_train_tensor = x_tensor[train_idx]

9 y_train_tensor = y_tensor[train_idx]

10 x_val_tensor = x_tensor[val_idx]

11 y_val_tensor = y_tensor[val_idx]

12 # Builds different composers because of data augmentation on

training set

13 train_composer = Compose([RandomHorizontalFlip(p=.5),

14 Normalize(mean=(.5,), std=(.5,))])

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

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

17 train_dataset = TransformedTensorDataset(

18 x_train_tensor, y_train_tensor, transform=train_composer

19 )

20 val_dataset = TransformedTensorDataset(

21 x_val_tensor, y_val_tensor, transform=val_composer

22 )

23 # Builds a weighted random sampler to handle imbalanced classes

24 sampler = make_balanced_sampler(y_train_tensor)

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

26 train_loader = DataLoader(

27 dataset=train_dataset, batch_size=16, sampler=sampler

28 )

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

We’re almost finished with the data preparation section! There is one last thing to

discuss…

Pixels as Features

So far, we’ve been handling our data as either PIL images or three-dimensional

tensors (CHW) with shape (1, 5, 5). It is also possible to consider each pixel and

channel as an individual feature by flattening the pixels with a nn.Flatten layer.

296 | Chapter 4: Classifying Images

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

Saved successfully!

Ooh no, something went wrong!