def _accuracy(track, gt): if len(track) < 2: return [], [], [] abs_acc = [] rel_acc = [] ious = [] st_frame = track[0]['frame'] end_frame = track[-1]['frame'] assert end_frame - st_frame + 1 == len(track) gt_seg = select_gt_segment(gt['track'], st_frame, end_frame) assert len(gt_seg) <= len(track) track_bbox1 = np.asarray([track[0]['bbox']]) gt_bbox1 = np.asarray([gt_seg[0]]) for track_box, gt_bbox in zip(track[1:len(gt_seg)], gt_seg[1:]): # current track box track_bbox = np.asarray([track_box['bbox']]) # gt motion gt_delta = bbox_transform(gt_bbox1, np.asarray([gt_bbox])) # target is the first track_bbox with gt motion track_bbox_target = bbox_transform_inv(track_bbox1, gt_delta) abs_diff = np.abs(track_bbox - track_bbox_target) cur_iou = iou(track_bbox, track_bbox_target) width = track_bbox_target[0,2] - track_bbox_target[0,0] height = track_bbox_target[0,3] - track_bbox_target[0,1] rel_diff = abs_diff / (np.asarray([width, height, width, height]) + np.finfo(float).eps) abs_acc.extend(abs_diff.flatten().tolist()) rel_acc.extend(rel_diff.flatten().tolist()) ious.extend(cur_iou.flatten().tolist()) return abs_acc, rel_acc, ious
def positive_tracks(tracks, annot_proto, overlap_thres, box_key): grouped = {} sampled = [] for track in tracks: start_frame = track[0]['frame'] if start_frame not in grouped: grouped[start_frame] = [] grouped[start_frame].append(track) for start_frame, group_tracks in grouped.iteritems(): init_boxes = [track[0][box_key] for track in group_tracks] gt_boxes = annot_boxes_at_frame(annot_proto, start_frame) overlaps = iou(init_boxes, gt_boxes) max_overlaps = np.max(overlaps, axis=1) sample_idx = np.where(max_overlaps > overlap_thres)[0] sampled.extend([group_tracks[i] for i in sample_idx]) return sampled
os.path.join(args.vid_root, cur_vid_name + '.vid')) if args.annot_root: annot_proto = proto_load( os.path.join(args.annot_root, cur_vid_name + '.annot')) # process boxes frame_idx = path_to_index(vid_proto, frame_name) if args.annot_root: annot_boxes = annot_boxes_at_frame(annot_proto, frame_idx) for box in boxes: bbox = box[0:4].tolist() if args.annot_root: # with GT if len(annot_boxes) == 0: overlaps = 0. else: overlaps = iou([bbox], annot_boxes) box_proto['boxes'].append({ "frame": frame_idx, "bbox": box[0:4].tolist(), "positive": True if np.any(overlaps >= 0.5) else False }) else: # no GT box_proto['boxes'].append({ "frame": frame_idx, "bbox": box[0:4].tolist(), }) # save last proto
#!/usr/bin/env python import sys from vdetlib.utils.common import iou, quick_args import scipy.io as sio import numpy as np if __name__ == '__main__': args = quick_args(['gt_list', 'save_file']) with open(args.gt_list) as f: gt_list = [line.strip() for line in f.readlines()] tot_overlaps = [] for ind, gt_file in enumerate(gt_list, start=1): gt_boxes = sio.loadmat(gt_file)['boxes'] num_gt = len(gt_boxes) if ind % 1000 == 0: print "{:.2%}: Processed {} files.".format(1. * ind / len(gt_list), ind) if num_gt <= 1: continue o_ind = np.triu_indices(num_gt, 1) overlaps = iou(gt_boxes, gt_boxes) valid_overlaps = overlaps[o_ind] tot_overlaps += valid_overlaps.tolist() if ind % 1000 != 0: print "100 %: Processed {} files.".format(ind) sio.savemat(args.save_file, {'gt_overlaps': np.asarray(tot_overlaps)})
parser.add_argument('box_file') args = parser.parse_args() vid_proto = proto_load(args.vid_file) annot_proto = proto_load(args.annot_file) box_proto = proto_load(args.box_file) gt_count = 0 recall_count = 0 for frame in vid_proto['frames']: frame_id = frame['frame'] # annot boxes annot_boxes = [track_box_at_frame(annot_track['track'], frame_id) \ for annot_track in annot_proto['annotations']] annot_boxes = [box for box in annot_boxes if box is not None] if len(annot_boxes) == 0: continue gt_count += len(annot_boxes) # proposal boxes proposal_boxes = boxes_at_frame(box_proto, frame_id) proposal_boxes = [box['bbox'] for box in proposal_boxes] if len(proposal_boxes) == 0: continue overlaps = iou(np.asarray(annot_boxes), np.asarray(proposal_boxes)) max_overlaps = overlaps.max(axis=1) recall_count += np.count_nonzero(max_overlaps >= 0.5) print "{} {} {} {}".format(vid_proto['video'], gt_count, recall_count, float(recall_count) / gt_count)
parser.add_argument('--overlap', type=float, default=0.5, help='GT overlap for considering positive samples.') args = parser.parse_args() vid_proto = proto_load(args.vid_file) track_proto = proto_load(args.track_file) annot_proto = proto_load(args.annot_file) acc = {'abs_acc':[], 'rel_acc':[], 'ious':[]} for track in track_proto['tracks']: frame1_id = track[0]['frame'] annots = annots_at_frame(annot_proto, frame1_id) annot_boxes = [annot[0]['bbox'] for annot in annots] if len(annot_boxes) == 0: continue gt_overlaps = iou([track[0]['roi']], annot_boxes) max_overlap = np.max(gt_overlaps, axis=1) if max_overlap < args.overlap: continue max_gt = np.argmax(gt_overlaps, axis=1)[0] gt_idx = annots[max_gt][1] gt_annot = annot_by_id(annot_proto, gt_idx) try: abs_acc, rel_acc, ious = _accuracy(track, gt_annot) except: import pdb pdb.set_trace() print vid_proto['video'] raise acc['abs_acc'].extend(abs_acc) acc['rel_acc'].extend(rel_acc) acc['ious'].extend(ious)
args = parser.parse_args() vid_proto = proto_load(args.vid_file) annot_proto = proto_load(args.annot_file) track_proto = proto_load(args.track_file) gt_count = 0 recall_count = 0 for frame in vid_proto['frames']: frame_id = frame['frame'] # annot boxes annot_boxes = [track_box_at_frame(annot_track['track'], frame_id) \ for annot_track in annot_proto['annotations']] annot_boxes = [box for box in annot_boxes if box is not None] if len(annot_boxes) == 0: continue gt_count += len(annot_boxes) # track boxes track_boxes = [track_box_at_frame(tracklet, frame_id) \ for tracklet in track_proto['tracks']] track_boxes = [box for box in track_boxes if box is not None] if len(track_boxes) == 0: continue overlaps = iou(np.asarray(annot_boxes), np.asarray(track_boxes)) max_overlaps = overlaps.max(axis=1) recall_count += np.count_nonzero(max_overlaps >= 0.5) print "{} {} {} {}".format(vid_proto['video'], gt_count, recall_count, float(recall_count) / gt_count)