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.

The data preparation step is exactly the same one we used in Chapter 5:

Data Preparation

1 class TransformedTensorDataset(Dataset):

2 def __init__(self, x, y, transform=None):

3 self.x = x

4 self.y = y

5 self.transform = transform

6

7 def __getitem__(self, index):

8 x = self.x[index]

9 if self.transform:

10 x = self.transform(x)

11

12 return x, self.y[index]

13

14 def __len__(self):

15 return len(self.x)

16

17 # Builds tensors from numpy arrays BEFORE split

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

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

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

21

22 # Uses index_splitter to generate indices for training and

23 # validation sets

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

25 # Uses indices to perform the split

26 x_train_tensor = x_tensor[train_idx]

27 y_train_tensor = y_tensor[train_idx]

28 x_val_tensor = x_tensor[val_idx]

29 y_val_tensor = y_tensor[val_idx]

30

31 # We're not doing any data augmentation now

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

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

34

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

36 train_dataset = TransformedTensorDataset(

37 x_train_tensor, y_train_tensor, transform=train_composer)

38 val_dataset = TransformedTensorDataset(

39 x_val_tensor, y_val_tensor, transform=val_composer)

848 | Chapter 10: Transform and Roll Out

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

Saved successfully!

Ooh no, something went wrong!