JunMa11 / SegWithDistMap

How Distance Transform Maps Boost Segmentation CNNs: An Empirical Study
https://openreview.net/forum?id=hM4pNbXWst
Apache License 2.0
375 stars 67 forks source link

Why for loop c in range(out_shape[1]) in the compute_sdf()? #6

Closed cuicathy closed 3 years ago

cuicathy commented 3 years ago

Hi,

Thanks for the great work of the surface losses.

May I ask why "for c in range(1, out_shape[1])" is necessary in the def compute_sdf1_1(img_gt, out_shape)? Based on my understanding, the sdf is the distmap of each sample with the shape(x,y,z), so there is no need to for loop the c and "normalized_sdf[b] = sdf" will be enough (b is the number of samples in a batch).

If I misunderstand anything, please let me know. Thank you very much!

Regards, Cathy


The following is the function I mentioned.

def compute_sdf1_1(img_gt, out_shape): """ compute the normalized signed distance map of binary mask input: segmentation, shape = (batch_size, x, y, z) output: the Signed Distance Map (SDM) sdf(x) = 0; x in segmentation boundary -inf|x-y|; x in segmentation +inf|x-y|; x out of segmentation normalize sdf to [-1, 1] """

img_gt = img_gt.astype(np.uint8)

normalized_sdf = np.zeros(out_shape)

for b in range(out_shape[0]): # batch size
        # ignore background
    for c in range(1, out_shape[1]):
        posmask = img_gt[b].astype(np.bool)
        if posmask.any():
            negmask = ~posmask
            posdis = distance(posmask)
            negdis = distance(negmask)
            boundary = skimage_seg.find_boundaries(posmask, mode='inner').astype(np.uint8)
            sdf = (negdis-np.min(negdis))/(np.max(negdis)-np.min(negdis)) - (posdis-np.min(posdis))/(np.max(posdis)-np.min(posdis))
            sdf[boundary==1] = 0
            normalized_sdf[b][c] = sdf

return normalized_sdf
JunMa11 commented 3 years ago

Hi @cuicathy ,

Sorry for my late reply.

The goal of for c in range(1, out_shape[1]): is to separately compute the distance transform map for each class, and we do not consider the background class.

cuicathy commented 3 years ago

Oh, I see. Thanks for your reply @JunMa11.