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

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

model anyway. We’ll use it to compute statistics only. By the way, we needstatistics for each channel, as required by the Normalize() transform.So, let’s build a function that takes a mini-batch (images and labels) and computesthe mean pixel value and standard deviation per channel of each image, adding upthe results for all images. Better yet, let’s make it a method of our StepByStep classtoo.StepByStep Method@staticmethoddef statistics_per_channel(images, labels):# NCHWn_samples, n_channels, n_height, n_weight = images.size()# Flatten HW into a single dimensionflatten_per_channel = images.reshape(n_samples, n_channels, -1)# Computes statistics of each image per channel# Average pixel value per channel# (n_samples, n_channels)means = flatten_per_channel.mean(axis=2)# Standard deviation of pixel values per channel# (n_samples, n_channels)stds = flatten_per_channel.std(axis=2)# Adds up statistics of all images in a mini-batch# (1, n_channels)sum_means = means.sum(axis=0)sum_stds = stds.sum(axis=0)# Makes a tensor of shape (1, n_channels)# with the number of samples in the mini-batchn_samples = torch.tensor([n_samples]*n_channels).float()# Stack the three tensors on top of one another# (3, n_channels)return torch.stack([n_samples, sum_means, sum_stds], axis=0)setattr(StepByStep, 'statistics_per_channel',statistics_per_channel)Data Preparation | 421

first_images, first_labels = next(iter(temp_loader))StepByStep.statistics_per_channel(first_images, first_labels)Outputtensor([[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 columnrepresents a channel, and the rows are the number of data points, the sum of meanvalues, and the sum of standard deviations, respectively.We can leverage the loader_apply() method we created in the last chapter to getthe sums for the whole dataset:results = StepByStep.loader_apply(temp_loader,StepByStep.statistics_per_channel)resultsOutputtensor([[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 theaverage standard deviation, per channel. Better yet, let’s make it a method thattakes a data loader and returns an instance of the Normalize() transform,statistics and all:422 | Chapter 6: Rock, Paper, Scissors

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!