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.

StepByStep Method

@staticmethod

def make_normalizer(loader):

total_samples, total_means, total_stds = \

StepByStep.loader_apply(

loader,

StepByStep.statistics_per_channel

)

norm_mean = total_means / total_samples

norm_std = total_stds / total_samples

return Normalize(mean=norm_mean, std=norm_std)

setattr(StepByStep, 'make_normalizer', make_normalizer)

IMPORTANT: Always use the training set to compute statistics

for standardization!

Now, we can use this method to create a transformation that standardizes our

dataset:

Creating Normalizer Transform

1 normalizer = StepByStep.make_normalizer(temp_loader)

2 normalizer

Output

Normalize(mean=tensor([0.8502, 0.8215, 0.8116]),

std=tensor([0.2089, 0.2512, 0.2659]))

Remember that PyTorch converts the pixel values into the [0, 1] range. The

average mean value of a pixel for the red (first) channel is 0.8502, while its average

standard deviation is 0.2089.

In the next chapter, we’ll use pre-computed statistics to

standardize the inputs when using a pre-trained model.

Data Preparation | 423

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

Saved successfully!

Ooh no, something went wrong!