예제 #1
0
	def run(self, img, step=None, debug=False, print_time=False, filename='unknown'):
		self.debug_out_list = []
		self.source = img
		funcs_to_run = self.functions[:step]
		output = self.source
		stopwatch = Stopwatch()
		log_string = '{}\t{: <16}\t{:.04f}s   {:.02f} fps'
		for func in funcs_to_run:
			if debug:
				output, output_debug = func.run(debug, input=output)
				self.debug_out_list.append(output_debug)
			else:
				output = func.run(debug, input=output)
			t = stopwatch.round()
			if print_time:
				fps = 1 / t if t != 0 else 999
				print(log_string.format(filename, str(func), t, fps))
			
		t = stopwatch.total()
		fps = 1 / t if t != 0 else 999
		print(Fore.YELLOW + log_string.format(filename, 'TOTAL', t, fps) + Fore.RESET)
		print() if print_time else None

		if debug:
			return self.debug_out_list[-1]
		else:
			return output
def evaluate(function, eval_func, test_perc=1.0, seed=0, **kwargs):
    masks_path = 'data_test/paintings_gt/masks'
    imgs_path = 'data_test/paintings_gt/imgs'
    random.seed(seed)

    all_imgs = all_files_in(imgs_path)
    all_masks = all_files_in(masks_path)

    dice_vals = []
    tversky_vals = []
    specificity_vals = []
    presicion_vals = []
    recall_vals = []
    iou_vals = []
    filenames = []

    if test_perc == 1.0:
        pairs = list(zip(all_imgs, all_masks))
    else:
        pairs = random.sample(list(zip(all_imgs, all_masks)),
                              int(len(all_imgs) * test_perc))

    watch = Stopwatch()
    for i, (paint_path, mask_path) in enumerate(pairs):
        sys.stdout.write("\033[K")
        filename = os.path.basename(paint_path)
        mask = xml2img(mask_path)
        if mask is None:
            print(f'  [{i + 1}/{len(pairs)}] ERROR "{filename}"', end='\r')
            continue
        paint = cv2.imread(paint_path)
        out = function(paint, **kwargs)
        # out = np.zeros_like(mask) + 255
        dice, tversky, specificity, presicion, recall, iou = eval_func(
            out, mask)
        dice_vals.append(dice)
        tversky_vals.append(tversky)
        specificity_vals.append(specificity)
        presicion_vals.append(presicion)
        recall_vals.append(recall)
        iou_vals.append(iou)
        print(
            f'  [{i + 1}/{len(pairs)}] dice={mean(dice_vals):0.4f} tversky={mean(tversky_vals):0.4f} specificity={mean(specificity_vals):0.4f} presicion={mean(presicion_vals):0.4f} recall={mean(recall_vals):0.4f} iou={mean(iou_vals):0.4f} time={watch.total():.0f}s of "{filename}"',
            end='\r')
    sys.stdout.write("\033[K")
    time = watch.total()
    print(' '.join([f'({i}, {v:0.3f})' for i, v in enumerate(dice_vals, 1)]))
    return dice_vals, filenames, mean(dice_vals), mean(tversky_vals), mean(
        specificity_vals), mean(presicion_vals), mean(recall_vals), mean(
            iou_vals), time