for i in range(pr_masks.shape[0]): pr_map_cls = [(pr_masks[i, :, :, k] > thres_cls[key]) for k, key in enumerate(thres_cls.keys())] pr_map = pr_map_cls[0] * 1.0 for k in range(1, len(classes)): top = pr_map_cls[k].copy() * (k + 1) top[np.where(np.logical_and(top > 0, pr_map > 0))] = pr_map[np.where( np.logical_and(top > 0, pr_map > 0))] pr_map[np.where(top > 0)] = top[np.where(top > 0)] pr_maps_roc.append(pr_map) pr_maps_roc = np.stack(pr_maps_roc) pr_masks_roc = np.stack([pr_maps_roc == k for k in range(1, len(classes) + 1)], axis=-1) ## IoU and dice coefficient iou_classes, mIoU, dice_classes, mDice = iou_calculate(gt_masks, pr_masks_roc) print('iou_classes: {:.4f},{:.4f},{:.4f},{:.4f}; mIoU: {:.4f}'.format( iou_classes[-1], iou_classes[0], iou_classes[1], iou_classes[2], mIoU)) print('dice_classes: {:.4f},{:.4f},{:.4f},{:.4f}; mDice: {:.4f}'.format( dice_classes[-1], dice_classes[0], dice_classes[1], dice_classes[2], mDice)) def plot(file_name, pr_masks, pr_masks_roc, gt_masks, index, classes=['G1', 'S', 'G2', 'BK']): import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg
for i in test_indices: _, gt_mask = test_dataset[i];gt_masks.append(gt_mask) gt_masks = np.stack(gt_masks);gt_maps = np.argmax(gt_masks,axis=-1) ## reshape # offset = 8 # pr_masks = pr_masks[:,:,offset:-offset] # gt_masks = gt_masks[:,:,offset:-offset,:] # print(pr_masks.shape); print(gt_masks.shape) ## IoU and dice coefficient pr_save_mask = np.concatenate([pr_masks[:,:,:,-1:],pr_masks[:,:,:,:-1]], axis = -1) gt_save_mask = np.concatenate([gt_masks[:,:,:,-1:],gt_masks[:,:,:,:-1]], axis = -1) test_ids = [test_dataset.ids[index] for index in test_indices] ## IoU and dice coefficient iou_classes, mIoU, dice_classes, mDice = iou_calculate(gt_save_mask, pr_save_mask) print('iou_classes: {:.4f},{:.4f},{:.4f}; mIoU: {:.4f}'.format(iou_classes[0],iou_classes[1],iou_classes[2], mIoU)) print('dice_classes: {:.4f},{:.4f},{:.4f}; mDice: {:.4f}'.format(dice_classes[0],dice_classes[1],dice_classes[2], mDice)) # save the prediction pr_save_map = np.argmax(pr_save_mask,axis=-1) gt_save_map = np.argmax(gt_save_mask,axis=-1) save = True if save: pred_dir = os.path.join(model_folder, 'pred_{}'.format(subset)) generate_folder(pred_dir) for pi, fn in enumerate(test_ids): imsave(pred_dir+'/{}'.format(fn), pr_save_map[pi,:,:]) io.imsave(model_folder+'/pr_{}.png'.format(fn.split('.')[0]), pr_save_map[pi,:,:]) y_true=gt_save_map.flatten(); y_pred = pr_save_map.flatten() cf_mat = confusion_matrix(y_true, y_pred)
## Label map prediction pr_masks = model.predict(images_pad, batch_size=1) ## probability maps [N(num of images) x H x W x C(class)] for 0: G1, 1: S, 2: G2, 3: background/M phase # crop images back pr_masks = pr_masks[:, pad_widths[1][0]:-pad_widths[1][1], pad_widths[2][ 0]:-pad_widths[2][ 1], :] if dataset == 'cyc2_1488x1512' else pr_masks[:, :, pad_widths[ 2][0]:-pad_widths[2][1], :] pr_masks_ = np.zeros(pr_masks.shape, dtype=np.float32) pr_masks_[:, :, :, 0] = pr_masks[:, :, :, 3] pr_masks_[:, :, :, 1:] = pr_masks[:, :, :, :3] # 0:background/M, 1:G1, 2:S, 3:G2 pr_maps = np.argmax(pr_masks_, axis=-1) # predicted label map ## calculate the performance metrics on the testing images iou_classes, mIoU, dice_classes, mDice = iou_calculate(gt_maps, pr_maps) print('iou_classes: {:.4f},{:.4f},{:.4f},{:.4f}; mIoU: {:.4f}'.format( iou_classes[0], iou_classes[1], iou_classes[2], iou_classes[-1], mIoU)) print('dice_classes: {:.4f},{:.4f},{:.4f},{:.4f}; mDice: {:.4f}'.format( dice_classes[0], dice_classes[1], dice_classes[2], dice_classes[-1], mDice)) # confusion matrix, precision, recall, and F1 score y_true = gt_maps.flatten() y_pred = pr_maps.flatten() cf_mat = confusion_matrix(y_true, y_pred) print('Confusion matrix:') print(cf_mat) prec_scores = [] recall_scores = [] f1_scores = []