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.

Updating the training script for segmentation

395

Our approach will cheat a little by adding together various images and masks to get

data in the range 0 to 2, and then multiplying the entire array by 0.5 to get it back into

the right range.

Listing 13.29

training.py:346, .logImages

CT intensity is assigned to all RGB channels

to provide a grayscale base image.

ct_t[:-1,:,:] /= 2000

ct_t[:-1,:,:] += 0.5

ctSlice_a = ct_t[dl.dataset.contextSlices_count].numpy()

image_a = np.zeros((512, 512, 3), dtype=np.float32)

image_a[:,:,:] = ctSlice_a.reshape((512,512,1))

image_a[:,:,0] += prediction_a & (1 - label_a)

image_a[:,:,0] += (1 - prediction_a) & label_a

image_a[:,:,1] += ((1 - prediction_a) & label_a) * 0.5

False positives are flagged as

red and overlaid on the image.

False negatives

are orange.

image_a[:,:,1] += prediction_a & label_a

image_a *= 0.5

image_a.clip(0, 1, image_a)

True positives

are green.

Our goal is to have a grayscale CT at half intensity, overlaid with predicted-nodule (or,

more correctly, nodule-candidate) pixels in various colors. We’re going to use red for

all pixels that are incorrect (false positives and false negatives). This will mostly be

false positives, which we don’t care about too much (since we’re focused on recall).

1 - label_a inverts the label, and that multiplied by the prediction_a gives us only

the predicted pixels that aren’t in a candidate nodule. False negatives get a halfstrength

mask added to green, which means they will show up as orange (1.0 red and

0.5 green renders as orange in RGB). Every correctly predicted pixel inside a nodule

is set to green; since we got those pixels right, no red will be added, and so they will

render as pure green.

After that, we renormalize our data to the 0…1 range and clamp it (in case we start

displaying augmented data here, which would cause speckles when the noise was outside

our expected CT range). All that remains is to save the data to TensorBoard.

Listing 13.30

training.py:361, .logImages

writer = getattr(self, mode_str + '_writer')

writer.add_image(

f'{mode_str}/{series_ndx}_prediction_{slice_ndx}',

image_a,

self.totalTrainingSamples_count,

dataformats='HWC',

)

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

Saved successfully!

Ooh no, something went wrong!