20.03.2021 Views

Deep-Learning-with-PyTorch

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

350 CHAPTER 12 Improving training with metrics and augmentation

ROTATING

Rotation is the first augmentation technique we’re going to use where we have to carefully

consider our data to ensure that we don’t break our sample with a conversion that

causes it to no longer be representative. Recall that our CT slices have uniform spacing

along the rows and columns (X- and Y-axes), but in the index (or Z) direction, the voxels

are non-cubic. That means we can’t treat those axes as interchangeable.

One option is to resample our data so that our resolution along the index-axis is

the same as along the other two, but that’s not a true solution because the data along

that axis would be very blurry and smeared. Even if we interpolate more voxels, the

fidelity of the data would remain poor. Instead, we’ll treat that axis as special and confine

our rotations to the X-Y plane.

Listing 12.16

dsets.py:181, def getCtAugmentedCandidate

if 'rotate' in augmentation_dict:

angle_rad = random.random() * math.pi * 2

s = math.sin(angle_rad)

c = math.cos(angle_rad)

rotation_t = torch.tensor([

[c, -s, 0, 0],

[s, c, 0, 0],

[0, 0, 1, 0],

[0, 0, 0, 1],

])

transform_t @= rotation_t

NOISE

Our final augmentation technique is different from the others in that it is actively

destructive to our sample in a way that flipping or rotating the sample is not. If we add

too much noise to the sample, it will swamp the real data and make it effectively

impossible to classify. While shifting and scaling the sample would do something similar

if we used extreme input values, we’ve chosen values that will only impact the edge

of the sample. Noise will have an impact on the entire image.

Listing 12.17

dsets.py:208, def getCtAugmentedCandidate

if 'noise' in augmentation_dict:

noise_t = torch.randn_like(augmented_chunk)

noise_t *= augmentation_dict['noise']

augmented_chunk += noise_t

The other augmentation types have increased the effective size of our dataset. Noise

makes our model’s job harder. We’ll revisit this once we see some training results.

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

Saved successfully!

Ooh no, something went wrong!