MCPcopy Create free account
hub / github.com/ImprintLab/Medical-SAM2 / eval_seg

Function eval_seg

func_3d/utils.py:139–203  ·  view source on GitHub ↗

threshold: a int or a tuple of int masks: [b,2,h,w] pred: [b,2,h,w]

(pred,true_mask_p,threshold)

Source from the content-addressed store, hash-verified

137 return np.array([y0, x0, y1, x1])
138
139def eval_seg(pred,true_mask_p,threshold):
140 '''
141 threshold: a int or a tuple of int
142 masks: [b,2,h,w]
143 pred: [b,2,h,w]
144 '''
145 b, c, h, w = pred.size()
146 if c == 2:
147 iou_d, iou_c, disc_dice, cup_dice = 0,0,0,0
148 for th in threshold:
149
150 gt_vmask_p = (true_mask_p > th).float()
151 vpred = (pred > th).float()
152 vpred_cpu = vpred.cpu()
153 disc_pred = vpred_cpu[:,0,:,:].numpy().astype('int32')
154 cup_pred = vpred_cpu[:,1,:,:].numpy().astype('int32')
155
156 disc_mask = gt_vmask_p [:,0,:,:].squeeze(1).cpu().numpy().astype('int32')
157 cup_mask = gt_vmask_p [:, 1, :, :].squeeze(1).cpu().numpy().astype('int32')
158
159 '''iou for numpy'''
160 iou_d += iou(disc_pred,disc_mask)
161 iou_c += iou(cup_pred,cup_mask)
162
163 '''dice for torch'''
164 disc_dice += dice_coeff(vpred[:,0,:,:], gt_vmask_p[:,0,:,:]).item()
165 cup_dice += dice_coeff(vpred[:,1,:,:], gt_vmask_p[:,1,:,:]).item()
166
167 return iou_d / len(threshold), iou_c / len(threshold), disc_dice / len(threshold), cup_dice / len(threshold)
168 elif c > 2: # for multi-class segmentation > 2 classes
169 ious = [0] * c
170 dices = [0] * c
171 for th in threshold:
172 gt_vmask_p = (true_mask_p > th).float()
173 vpred = (pred > th).float()
174 vpred_cpu = vpred.cpu()
175 for i in range(0, c):
176 pred = vpred_cpu[:,i,:,:].numpy().astype('int32')
177 mask = gt_vmask_p[:,i,:,:].squeeze(1).cpu().numpy().astype('int32')
178
179 '''iou for numpy'''
180 ious[i] += iou(pred,mask)
181
182 '''dice for torch'''
183 dices[i] += dice_coeff(vpred[:,i,:,:], gt_vmask_p[:,i,:,:]).item()
184
185 return tuple(np.array(ious + dices) / len(threshold)) # tuple has a total number of c * 2
186 else:
187 eiou, edice = 0,0
188 for th in threshold:
189
190 gt_vmask_p = (true_mask_p > th).float()
191 vpred = (pred > th).float()
192 vpred_cpu = vpred.cpu()
193 disc_pred = vpred_cpu[:,0,:,:].numpy().astype('int32')
194
195 disc_mask = gt_vmask_p [:,0,:,:].squeeze(1).cpu().numpy().astype('int32')
196

Callers 1

validation_samFunction · 0.90

Calls 2

iouFunction · 0.70
dice_coeffFunction · 0.70

Tested by

no test coverage detected