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.

first_images, first_labels = next(iter(temp_loader))

StepByStep.statistics_per_channel(first_images, first_labels)

Output

tensor([[16.0000, 16.0000, 16.0000],

[13.8748, 13.3048, 13.1962],

[ 3.0507, 3.8268, 3.9754]])

Applying it to the first mini-batch of images, we get the results above: Each column

represents a channel, and the rows are the number of data points, the sum of mean

values, and the sum of standard deviations, respectively.

We can leverage the loader_apply() method we created in the last chapter to get

the sums for the whole dataset:

results = StepByStep.loader_apply(temp_loader,

StepByStep.statistics_per_channel)

results

Output

tensor([[2520.0000, 2520.0000, 2520.0000],

[2142.5359, 2070.0811, 2045.1442],

[ 526.3024, 633.0677, 669.9554]])

So, we can compute the average mean value (that sounds weird, I know) and the

average standard deviation, per channel. Better yet, let’s make it a method that

takes a data loader and returns an instance of the Normalize() transform,

statistics and all:

422 | Chapter 6: Rock, Paper, Scissors

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

Saved successfully!

Ooh no, something went wrong!