def test_dice_coefficient(self): # paths image_train_path = "/Users/shiwakaga/Amodel_Data/train/*/images/*.jpg" image_test_path = "/Users/shiwakaga/Amodel_Data/test/*/images/*.png" x_path = "/Users/shiwakaga/Desktop/output/" y_path = "/Users/shiwakaga/Amodel_Data/*/amodel/" # read images import glob xs = glob.glob(x_path) for img in xs: x = imread(y_path + img.split('/')[-1].replace('raw','instrument_instances')) y = imread(img) # make images binary x[x < 0.5] = 0 x[x >= 0.5] = 1 y[y < 0.5] = 0 y[y >= 0.5] = 1 # calculate dice # dice = [] dice = compute_dice_coefficient(x,y) print(dice) with open('dice_sorce.txt','a+') as f: f.write(img.split('/')[-1]+':'+str(dice)+'\n')
def detect_and_color_splash(model, image_path=None, video_path=None, out_path=None): assert image_path or video_path out_path = out_path # Run model detection and generate the color splash effect print("Running on {}".format(image_path)) # Read image image = skimage.io.imread(image_path) # Detect objects r = model.detect([image], verbose=1)[0] #--------- for show -----------# splash,mask_bi = color_splash(image, r['masks']) # get dice score x_path = image_path.replace('raw', 'instrument_instances').replace('img', 'gt') gt = imread(x_path) gt[gt < 0.5] = 0 gt[gt >= 0.5] = 1 dice = compute_dice_coefficient(gt, mask_bi) print(dice) with open('60_flow_dice_sorce.txt', 'a+') as f: f.write(x_path.split('/')[-1] + ':' + str(dice) + '\n') # Save output file_name = out_path+image_path.split('/')[-1] skimage.io.imsave(file_name, splash)
def test_empty_mask_in_gt(self): # test empty mask in ground truth mask_gt = np.zeros((128, 128, 128), np.uint8) mask_pred = np.zeros((128, 128, 128), np.uint8) # mask_gt[50,60,70] = 1 mask_pred[50, 60, 72] = 1 surface_distances = compute_surface_distances(mask_gt, mask_pred, spacing_mm=(3, 2, 1)) average_surface_distance = compute_average_surface_distance(surface_distances) hausdorf_100 = compute_robust_hausdorff(surface_distances, 100) hausdorf_95 = compute_robust_hausdorff(surface_distances, 95) surface_overlap_1_mm = compute_surface_overlap_at_tolerance(surface_distances, 1) surface_dice_1mm = compute_surface_dice_at_tolerance(surface_distances, 1) volumetric_dice = compute_dice_coefficient(mask_gt, mask_pred) print("average surface distance: {} mm".format(average_surface_distance)) print("hausdorff (100%): {} mm".format(hausdorf_100)) print("hausdorff (95%): {} mm".format(hausdorf_95)) print("surface overlap at 1mm: {}".format(surface_overlap_1_mm)) print("surface dice at 1mm: {}".format(surface_dice_1mm)) print("volumetric dice: {}".format(volumetric_dice)) self.assertAlmostEqual(surface_dice_1mm, 0.0, delta=self.delta) self.assertAlmostEqual(volumetric_dice, 0.0, delta=self.delta)
def test_bi_score(self,x_path,mask): gt = imread(x_path) gt[gt < 0.5] = 0 gt[gt >= 0.5] = 1 dice = compute_dice_coefficient(gt, mask) print(dice) with open('60_dice_sorce.txt', 'a+') as f: f.write(x_path.split('/')[-1] + ':' + str(dice) + '\n')
def test_two_cubes(self): # two cubes. cube 1 is 100x100x100 mm^3 and cube 2 is 102x100x100 mm^3 mask_gt = np.zeros((100, 100, 100), np.uint8) mask_pred = np.zeros((100, 100, 100), np.uint8) spacing_mm = (2, 1, 1) mask_gt[0:50, :, :] = 1 mask_pred[0:51, :, :] = 1 surface_distances = compute_surface_distances(mask_gt, mask_pred, spacing_mm) expected_average_distance_gt_to_pred = 0.836145008498 expected_volumetric_dice = 2. * 100 * 100 * 100 / (100 * 100 * 100 + 102 * 100 * 100) surface_dice_1mm = compute_surface_dice_at_tolerance(surface_distances, 1) volumetric_dice = compute_dice_coefficient(mask_gt, mask_pred) print("surface dice at 1mm: {}".format(compute_surface_dice_at_tolerance(surface_distances, 1))) print("volumetric dice: {}".format(compute_dice_coefficient(mask_gt, mask_pred))) self.assertAlmostEqual(surface_dice_1mm, expected_average_distance_gt_to_pred, delta=self.delta) self.assertAlmostEqual(volumetric_dice, expected_volumetric_dice, delta=self.delta)
def test_single_pixels_2mm_away(self): mask_gt = np.zeros((128, 128, 128), np.uint8) mask_pred = np.zeros((128, 128, 128), np.uint8) mask_gt[50, 60, 70] = 1 mask_pred[50, 60, 72] = 1 surface_distances = compute_surface_distances(mask_gt, mask_pred, spacing_mm=(3, 2, 1)) surface_dice_1mm = compute_surface_dice_at_tolerance(surface_distances, 1) volumetric_dice = compute_dice_coefficient(mask_gt, mask_pred) print("surface dice at 1mm: {}".format(surface_dice_1mm)) print("volumetric dice: {}".format(volumetric_dice)) self.assertAlmostEqual(surface_dice_1mm, 0.5, delta=self.delta) self.assertAlmostEqual(volumetric_dice, 0.0, delta=self.delta)