def extract_auprc(targets, scores, radius, threshold, match_radius=None, pool=None): N = 0 mse = 0 hits = [] preds = [] if pool is not None: process = ExtractMatches(radius, threshold, match_radius) iterator = iterate_score_target_pairs(scores, targets) for assignment,score,this_mse,n in pool.imap_unordered(process, iterator): mse += this_mse hits.append(assignment) preds.append(score) N += n else: for score,target in iterate_score_target_pairs(scores, targets): score,coords = non_maximum_suppression(score, radius, threshold=threshold) if match_radius is None: assignment, dist = match_coordinates(target, coords, radius) else: assignment, dist = match_coordinates(target, coords, match_radius) mse += np.sum(dist[assignment==1]**2) hits.append(assignment) preds.append(score) N += len(target) hits = np.concatenate(hits, 0) preds = np.concatenate(preds, 0) auprc = average_precision(hits, preds, N=N) rmse = np.sqrt(mse/hits.sum()) return auprc, rmse, int(hits.sum()), N
def __call__(self, args): score,target = args score,coords = non_maximum_suppression(score, self.radius, threshold=self.threshold) if self.match_radius is None: assignment, dist = match_coordinates(target, coords, self.radius) else: assignment, dist = match_coordinates(target, coords, self.match_radius) mse = np.sum(dist[assignment==1]**2) return assignment, score, mse, len(target)
def main(args): match_radius = args.assignment_radius targets = pd.read_csv(args.targets, sep='\t') predicts = pd.read_csv(args.predicted, sep='\t', comment='#') if args.images == 'union': image_list = set(targets.image_name.unique()) | set(predicts.image_name.unique()) elif args.images == 'target': image_list = set(targets.image_name.unique()) elif args.images == 'predicted': image_list = set(predicts.image_name.unique()) else: raise Exception('Unknown image argument: ' + args.images) image_list = list(image_list) N = len(targets) matches = [] scores = [] count = 0 mae = 0 for name in image_list: target = targets.loc[targets.image_name == name] predict = predicts.loc[predicts.image_name == name] target_coords = target[['x_coord', 'y_coord']].values predict_coords = predict[['x_coord', 'y_coord']].values score = predict.score.values.astype(np.float32) match,dist = match_coordinates(target_coords, predict_coords, match_radius) this_mae = np.sum(dist[match==1]) count += np.sum(match) delta = this_mae - np.sum(match)*mae mae += delta/count matches.append(match) scores.append(score) matches = np.concatenate(matches, 0) scores = np.concatenate(scores, 0) precision,recall,threshold,auprc = precision_recall_curve(matches, scores, N=N) print('# auprc={}, mae={}'.format(auprc,np.sqrt(mae))) mask = (precision + recall) == 0 f1 = 2*precision*recall f1[mask] = 0 f1[~mask] /= (precision + recall)[~mask] table = pd.DataFrame({'threshold': threshold}) table['precision'] = precision table['recall'] = recall table['f1'] = f1 table.to_csv(sys.stdout, sep='\t', index=False)