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.

• is_valid_file: A function that checks if a file is corrupted or not.

Let’s create a dataset then:

Temporary Dataset

1 temp_transform = Compose([Resize(28), ToTensor()])

2 temp_dataset = ImageFolder(root='rps', transform=temp_transform)

We’re using only the transform optional argument here, and keeping

transformations to a minimum. First, images are resized to 28x28 pixels (and

automatically transformed to the RGB color model by the PIL loader, thus losing

the alpha channel), and then are converted to PyTorch tensors. Smaller images will

make our models faster to train, and more "CPU-friendly." Let’s take the first image

of the dataset and check its shape and corresponding label:

temp_dataset[0][0].shape, temp_dataset[0][1]

Output

(torch.Size([3, 28, 28]), 0)

Perfect!

"Wait, where is the standardization you promised?"

Standardization

To standardize data points, we need to learn their mean and standard deviation

first. What’s the mean pixel value of our rock paper scissors images? And standard

deviation? To compute these, we need to load the data. The good thing is, we have

a (temporary) dataset with the resized images already! We’re only missing a data

loader.

Temporary DataLoader

1 temp_loader = DataLoader(temp_dataset, batch_size=16)

No need to bother with shuffling, as this is not the data loader we’ll use to train the

420 | Chapter 6: Rock, Paper, Scissors

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

Saved successfully!

Ooh no, something went wrong!