diff --git a/eval/tt/Deteval.py b/eval/tt/Deteval.py index ccc3680..6f35b0d 100644 --- a/eval/tt/Deteval.py +++ b/eval/tt/Deteval.py @@ -1,6 +1,7 @@ from os import listdir from scipy import io import numpy as np + # from skimage.draw import polygon # from polygon_wrapper import iou # from polygon_wrapper import iod @@ -36,34 +37,34 @@ def get_intersection(pD, pG): def input_reading_mod(input_dir, input): """This helper reads input from txt files""" - with open('%s/%s' % (input_dir, input), 'r') as input_fid: + with open("%s/%s" % (input_dir, input), "r") as input_fid: pred = input_fid.readlines() - det = [x.strip('\n') for x in pred] + det = [x.strip("\n") for x in pred] return det def gt_reading_mod(gt_dir, gt_id): """This helper reads groundtruths from mat files""" - gt_id = gt_id.split('.')[0] - gt = io.loadmat('%s/poly_gt_%s.mat' % (gt_dir, gt_id)) - gt = gt['polygt'] + gt_id = gt_id.split(".")[0] + gt = io.loadmat("%s/poly_gt_%s.mat" % (gt_dir, gt_id)) + gt = gt["polygt"] return gt def detection_filtering(detections, groundtruths, threshold=0.5): for gt_id, gt in enumerate(groundtruths): - if (gt[5] == '#') and (gt[1].shape[1] > 1): - gt_x = map(int, np.squeeze(gt[1])) - gt_y = map(int, np.squeeze(gt[3])) + if (gt[5] == "#") and (gt[1].shape[1] > 1): + gt_x = np.squeeze(gt[1]).astype(int) + gt_y = np.squeeze(gt[3]).astype(int) gt_p = np.concatenate((np.array(gt_x), np.array(gt_y))) gt_p = gt_p.reshape(2, -1).transpose() gt_p = plg.Polygon(gt_p) for det_id, detection in enumerate(detections): - detection = detection.split(',') - # detection = map(int, detection[0:-1]) - detection = map(int, detection) + detection = detection.split(",") + # detection = detection[0:-1].astype(int) + detection = np.array(detection, dtype=np.int) det_y = detection[0::2] det_x = detection[1::2] @@ -95,6 +96,7 @@ def detection_filtering(detections, groundtruths, threshold=0.5): # """ # return np.round((area_of_intersection(det_x, det_y, gt_x, gt_y) / area(det_x, det_y)), 2) + def sigma_calculation(det_p, gt_p): """ sigma = inter_area / gt_area @@ -124,13 +126,15 @@ def tau_calculation(det_p, gt_p): for input_id in allInputs: - if (input_id != '.DS_Store'): - print('input_id', input_id) + if input_id != ".DS_Store": + print("input_id", input_id) detections = input_reading_mod(input_dir, input_id) # from IPython import embed; groundtruths = gt_reading_mod(gt_dir, input_id) - detections = detection_filtering(detections, groundtruths) # filters detections overlapping with DC area - dc_id = np.where(groundtruths[:, 5] == '#') + detections = detection_filtering( + detections, groundtruths + ) # filters detections overlapping with DC area + dc_id = np.where(groundtruths[:, 5] == "#") groundtruths = np.delete(groundtruths, (dc_id), (0)) local_sigma_table = np.zeros((groundtruths.shape[0], len(detections))) @@ -139,17 +143,17 @@ def tau_calculation(det_p, gt_p): for gt_id, gt in enumerate(groundtruths): if len(detections) > 0: for det_id, detection in enumerate(detections): - detection = detection.split(',') + detection = detection.split(",") # print (len(detection)) # detection = map(int, detection[:-1]) - detection = map(int, detection) + detection = np.array(detection, dtype=np.int) # print (len(detection)) # from IPython import embed;embed() # detection = list(detection) - gt_x = map(int, np.squeeze(gt[1])) - gt_y = map(int, np.squeeze(gt[3])) + gt_x = np.squeeze(gt[1]).astype(int) + gt_y = np.squeeze(gt[3]).astype(int) gt_p = np.concatenate((np.array(gt_x), np.array(gt_y))) gt_p = gt_p.reshape(2, -1).transpose() @@ -183,16 +187,25 @@ def tau_calculation(det_p, gt_p): total_num_det = 0 -def one_to_one(local_sigma_table, local_tau_table, local_accumulative_recall, - local_accumulative_precision, global_accumulative_recall, global_accumulative_precision, - gt_flag, det_flag): - for gt_id in xrange(num_gt): +def one_to_one( + local_sigma_table, + local_tau_table, + local_accumulative_recall, + local_accumulative_precision, + global_accumulative_recall, + global_accumulative_precision, + gt_flag, + det_flag, +): + for gt_id in range(num_gt): qualified_sigma_candidates = np.where(local_sigma_table[gt_id, :] > tr) num_qualified_sigma_candidates = qualified_sigma_candidates[0].shape[0] qualified_tau_candidates = np.where(local_tau_table[gt_id, :] > tp) num_qualified_tau_candidates = qualified_tau_candidates[0].shape[0] - if (num_qualified_sigma_candidates == 1) and (num_qualified_tau_candidates == 1): + if (num_qualified_sigma_candidates == 1) and ( + num_qualified_tau_candidates == 1 + ): global_accumulative_recall = global_accumulative_recall + 1.0 global_accumulative_precision = global_accumulative_precision + 1.0 local_accumulative_recall = local_accumulative_recall + 1.0 @@ -201,13 +214,27 @@ def one_to_one(local_sigma_table, local_tau_table, local_accumulative_recall, gt_flag[0, gt_id] = 1 matched_det_id = np.where(local_sigma_table[gt_id, :] > tr) det_flag[0, matched_det_id] = 1 - return local_accumulative_recall, local_accumulative_precision, global_accumulative_recall, global_accumulative_precision, gt_flag, det_flag - - -def one_to_many(local_sigma_table, local_tau_table, local_accumulative_recall, - local_accumulative_precision, global_accumulative_recall, global_accumulative_precision, - gt_flag, det_flag): - for gt_id in xrange(num_gt): + return ( + local_accumulative_recall, + local_accumulative_precision, + global_accumulative_recall, + global_accumulative_precision, + gt_flag, + det_flag, + ) + + +def one_to_many( + local_sigma_table, + local_tau_table, + local_accumulative_recall, + local_accumulative_precision, + global_accumulative_recall, + global_accumulative_precision, + gt_flag, + det_flag, +): + for gt_id in range(num_gt): # skip the following if the groundtruth was matched if gt_flag[0, gt_id] > 0: continue @@ -217,12 +244,15 @@ def one_to_many(local_sigma_table, local_tau_table, local_accumulative_recall, if num_non_zero_in_sigma >= k: ####search for all detections that overlaps with this groundtruth - qualified_tau_candidates = np.where((local_tau_table[gt_id, :] >= tp) & (det_flag[0, :] == 0)) + qualified_tau_candidates = np.where( + (local_tau_table[gt_id, :] >= tp) & (det_flag[0, :] == 0) + ) num_qualified_tau_candidates = qualified_tau_candidates[0].shape[0] if num_qualified_tau_candidates == 1: - if ((local_tau_table[gt_id, qualified_tau_candidates] >= tp) and ( - local_sigma_table[gt_id, qualified_tau_candidates] >= tr)): + if (local_tau_table[gt_id, qualified_tau_candidates] >= tp) and ( + local_sigma_table[gt_id, qualified_tau_candidates] >= tr + ): # became an one-to-one case global_accumulative_recall = global_accumulative_recall + 1.0 global_accumulative_precision = global_accumulative_precision + 1.0 @@ -231,23 +261,41 @@ def one_to_many(local_sigma_table, local_tau_table, local_accumulative_recall, gt_flag[0, gt_id] = 1 det_flag[0, qualified_tau_candidates] = 1 - elif (np.sum(local_sigma_table[gt_id, qualified_tau_candidates]) >= tr): + elif np.sum(local_sigma_table[gt_id, qualified_tau_candidates]) >= tr: gt_flag[0, gt_id] = 1 det_flag[0, qualified_tau_candidates] = 1 global_accumulative_recall = global_accumulative_recall + fsc_k - global_accumulative_precision = global_accumulative_precision + num_qualified_tau_candidates * fsc_k + global_accumulative_precision = ( + global_accumulative_precision + num_qualified_tau_candidates * fsc_k + ) local_accumulative_recall = local_accumulative_recall + fsc_k - local_accumulative_precision = local_accumulative_precision + num_qualified_tau_candidates * fsc_k - - return local_accumulative_recall, local_accumulative_precision, global_accumulative_recall, global_accumulative_precision, gt_flag, det_flag - - -def many_to_many(local_sigma_table, local_tau_table, local_accumulative_recall, - local_accumulative_precision, global_accumulative_recall, global_accumulative_precision, - gt_flag, det_flag): - for det_id in xrange(num_det): + local_accumulative_precision = ( + local_accumulative_precision + num_qualified_tau_candidates * fsc_k + ) + + return ( + local_accumulative_recall, + local_accumulative_precision, + global_accumulative_recall, + global_accumulative_precision, + gt_flag, + det_flag, + ) + + +def many_to_many( + local_sigma_table, + local_tau_table, + local_accumulative_recall, + local_accumulative_precision, + global_accumulative_recall, + global_accumulative_precision, + gt_flag, + det_flag, +): + for det_id in range(num_det): # skip the following if the detection was matched if det_flag[0, det_id] > 0: continue @@ -257,12 +305,15 @@ def many_to_many(local_sigma_table, local_tau_table, local_accumulative_recall, if num_non_zero_in_tau >= k: ####search for all detections that overlaps with this groundtruth - qualified_sigma_candidates = np.where((local_sigma_table[:, det_id] >= tp) & (gt_flag[0, :] == 0)) + qualified_sigma_candidates = np.where( + (local_sigma_table[:, det_id] >= tp) & (gt_flag[0, :] == 0) + ) num_qualified_sigma_candidates = qualified_sigma_candidates[0].shape[0] if num_qualified_sigma_candidates == 1: - if ((local_tau_table[qualified_sigma_candidates, det_id] >= tp) and ( - local_sigma_table[qualified_sigma_candidates, det_id] >= tr)): + if (local_tau_table[qualified_sigma_candidates, det_id] >= tp) and ( + local_sigma_table[qualified_sigma_candidates, det_id] >= tr + ): # became an one-to-one case global_accumulative_recall = global_accumulative_recall + 1.0 global_accumulative_precision = global_accumulative_precision + 1.0 @@ -271,19 +322,30 @@ def many_to_many(local_sigma_table, local_tau_table, local_accumulative_recall, gt_flag[0, qualified_sigma_candidates] = 1 det_flag[0, det_id] = 1 - elif (np.sum(local_tau_table[qualified_sigma_candidates, det_id]) >= tp): + elif np.sum(local_tau_table[qualified_sigma_candidates, det_id]) >= tp: det_flag[0, det_id] = 1 gt_flag[0, qualified_sigma_candidates] = 1 - global_accumulative_recall = global_accumulative_recall + num_qualified_sigma_candidates * fsc_k + global_accumulative_recall = ( + global_accumulative_recall + num_qualified_sigma_candidates * fsc_k + ) global_accumulative_precision = global_accumulative_precision + fsc_k - local_accumulative_recall = local_accumulative_recall + num_qualified_sigma_candidates * fsc_k + local_accumulative_recall = ( + local_accumulative_recall + num_qualified_sigma_candidates * fsc_k + ) local_accumulative_precision = local_accumulative_precision + fsc_k - return local_accumulative_recall, local_accumulative_precision, global_accumulative_recall, global_accumulative_precision, gt_flag, det_flag + return ( + local_accumulative_recall, + local_accumulative_precision, + global_accumulative_recall, + global_accumulative_precision, + gt_flag, + det_flag, + ) -for idx in xrange(len(global_sigma)): +for idx in range(len(global_sigma)): print(allInputs[idx]) local_sigma_table = global_sigma[idx] local_tau_table = global_tau[idx] @@ -300,26 +362,62 @@ def many_to_many(local_sigma_table, local_tau_table, local_accumulative_recall, det_flag = np.zeros((1, num_det)) #######first check for one-to-one case########## - local_accumulative_recall, local_accumulative_precision, global_accumulative_recall, global_accumulative_precision, \ - gt_flag, det_flag = one_to_one(local_sigma_table, local_tau_table, - local_accumulative_recall, local_accumulative_precision, - global_accumulative_recall, global_accumulative_precision, - gt_flag, det_flag) + ( + local_accumulative_recall, + local_accumulative_precision, + global_accumulative_recall, + global_accumulative_precision, + gt_flag, + det_flag, + ) = one_to_one( + local_sigma_table, + local_tau_table, + local_accumulative_recall, + local_accumulative_precision, + global_accumulative_recall, + global_accumulative_precision, + gt_flag, + det_flag, + ) #######then check for one-to-many case########## - local_accumulative_recall, local_accumulative_precision, global_accumulative_recall, global_accumulative_precision, \ - gt_flag, det_flag = one_to_many(local_sigma_table, local_tau_table, - local_accumulative_recall, local_accumulative_precision, - global_accumulative_recall, global_accumulative_precision, - gt_flag, det_flag) + ( + local_accumulative_recall, + local_accumulative_precision, + global_accumulative_recall, + global_accumulative_precision, + gt_flag, + det_flag, + ) = one_to_many( + local_sigma_table, + local_tau_table, + local_accumulative_recall, + local_accumulative_precision, + global_accumulative_recall, + global_accumulative_precision, + gt_flag, + det_flag, + ) #######then check for many-to-many case########## - local_accumulative_recall, local_accumulative_precision, global_accumulative_recall, global_accumulative_precision, \ - gt_flag, det_flag = many_to_many(local_sigma_table, local_tau_table, - local_accumulative_recall, local_accumulative_precision, - global_accumulative_recall, global_accumulative_precision, - gt_flag, det_flag) - # for det_id in xrange(num_det): + ( + local_accumulative_recall, + local_accumulative_precision, + global_accumulative_recall, + global_accumulative_precision, + gt_flag, + det_flag, + ) = many_to_many( + local_sigma_table, + local_tau_table, + local_accumulative_recall, + local_accumulative_precision, + global_accumulative_recall, + global_accumulative_precision, + gt_flag, + det_flag, + ) + # for det_id in range(num_det): # # skip the following if the detection was matched # if det_flag[0, det_id] > 0: # continue @@ -352,7 +450,7 @@ def many_to_many(local_sigma_table, local_tau_table, local_accumulative_recall, # local_accumulative_recall = local_accumulative_recall + num_qualified_sigma_candidates * fsc_k # local_accumulative_precision = local_accumulative_precision + fsc_k - fid = open(fid_path, 'a+') + fid = open(fid_path, "a+") try: local_precision = local_accumulative_precision / num_det except ZeroDivisionError: @@ -363,7 +461,11 @@ def many_to_many(local_sigma_table, local_tau_table, local_accumulative_recall, except ZeroDivisionError: local_recall = 0 - temp = ('%s______/Precision:_%s_______/Recall:_%s\n' % (allInputs[idx], str(local_precision), str(local_recall))) + temp = "%s______/Precision:_%s_______/Recall:_%s\n" % ( + allInputs[idx], + str(local_precision), + str(local_recall), + ) fid.write(temp) fid.close() try: @@ -381,11 +483,15 @@ def many_to_many(local_sigma_table, local_tau_table, local_accumulative_recall, except ZeroDivisionError: f_score = 0 -fid = open(fid_path, 'a') +fid = open(fid_path, "a") hmean = 2 * precision * recall / (precision + recall) -temp = ('Precision:_%s_______/Recall:_%s/Hmean:_%s\n' % (str(precision), str(recall), str(hmean))) +temp = "Precision:_%s_______/Recall:_%s/Hmean:_%s\n" % ( + str(precision), + str(recall), + str(hmean), +) print(temp) fid.write(temp) fid.close() -print('pb') +print("pb")