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 dataset for segmentation

373

We’ll skip showing the search in the third dimension. Our final bounding box is

five voxels wide and seven voxels tall. Here’s what that looks like in code, for the index

direction.

Listing 13.3

dsets.py:131, Ct.buildAnnotationMask

center_irc = xyz2irc(

candidateInfo_tup.center_xyz,

self.origin_xyz,

self.vxSize_xyz,

self.direction_a,

)

ci = int(center_irc.index)

cr = int(center_irc.row)

cc = int(center_irc.col)

candidateInfo_tup here is the same as

we’ve seen previously: as returned by

getCandidateInfoList.

Gets the center voxel

indices, our starting point

index_radius = 2

try:

while self.hu_a[ci + index_radius, cr, cc] > threshold_hu and \

self.hu_a[ci - index_radius, cr, cc] > threshold_hu:

index_radius += 1

except IndexError:

index_radius -= 1

The safety net for indexing

beyond the size of the tensor

The search

described

previously

We first grab the center data and then do the search in a while loop. As a slight complication,

our search might fall off the boundary of our tensor. We are not terribly

concerned about that case and are lazy, so we just catch the index exception. 7

Note that we stop incrementing the very approximate radius values after the density

drops below threshold, so our bounding box should contain a one-voxel border of lowdensity

tissue (at least on one side; since nodules can be adjacent to regions like the lung

wall, we have to stop searching in both directions when we hit air on either side). Since

we check both center_index + index_radius and center_index - index_radius

against that threshold, that one-voxel boundary will only exist on the edge closest to our

nodule location. This is why we need those locations to be relatively centered. Since

some nodules are adjacent to the boundary between the lung and denser tissue like muscle

or bone, we can’t trace each direction independently, as some edges would end up

incredibly far away from the actual nodule.

We then repeat the same radius-expansion process with row_radius and col

_radius (this code is omitted for brevity). Once that’s done, we can set a box in our

bounding-box mask array to True (we’ll see the definition of boundingBox_ary in just

a moment; it’s not surprising).

OK, let’s wrap all this up in a function. We loop over all nodules. For each nodule,

we perform the search shown earlier (which we elide from listing 13.4). Then, in a

Boolean tensor boundingBox_a, we mark the bounding box we found.

7

The bug here is that the wraparound at 0 will go undetected. It does not matter much to us. As an exercise,

implement proper bounds checking.

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

Saved successfully!

Ooh no, something went wrong!