def main(args): vid_proto = proto_load(args.vid_file) frame_to_det = load_frame_to_det(vid_proto, args.det_dir) if not os.path.isdir(args.save_dir): try: os.makedirs(args.save_dir) except: pass track_files = glob(osp.join(args.track_dir, '*.track*')) if len(track_files) == 0: print "Warning: {} has no tracks".format(args.track_dir) return for track_file in track_files: track_proto = proto_load(track_file) vid_name = vid_proto['video'] assert vid_name == track_proto['video'] cls_name = osp.basename(track_file).split('.')[1] cls_index = imagenet_vdet_class_idx[cls_name] # spatial max pooling score_proto = raw_dets_spatial_max_pooling( vid_proto, track_proto, frame_to_det, cls_index, overlap_thres=args.overlap_thres) # temporal max pooling temporal_max_score_proto = score_proto_temporal_maxpool( score_proto, args.window) # interpolation interpolated_score_proto = score_proto_interpolation( temporal_max_score_proto, vid_proto) # save score proto save_file = osp.join(args.save_dir, '{}.{}.score'.format(vid_name, cls_name)) if osp.isfile(save_file): print "{} already exists.".format(save_file) continue proto_dump(interpolated_score_proto, save_file) print "{} created.".format(save_file)
def main(args): vid_proto = proto_load(args.vid_file) frame_to_det = load_frame_to_det(vid_proto, args.det_dir) if not os.path.isdir(args.save_dir): try: os.makedirs(args.save_dir) except: pass track_files = glob(osp.join(args.track_dir, '*.track*')) if len(track_files) == 0: print "Warning: {} has no tracks".format(args.track_dir) return for track_file in track_files: track_proto = proto_load(track_file) vid_name = vid_proto['video'] assert vid_name == track_proto['video'] cls_name = osp.basename(track_file).split('.')[1] cls_index = imagenet_vdet_class_idx[cls_name] # spatial max pooling score_proto = raw_dets_spatial_max_pooling(vid_proto, track_proto, frame_to_det, cls_index, overlap_thres=args.overlap_thres) # temporal max pooling temporal_max_score_proto = score_proto_temporal_maxpool(score_proto, args.window) # interpolation interpolated_score_proto = score_proto_interpolation(temporal_max_score_proto, vid_proto) # save score proto save_file = osp.join(args.save_dir, '{}.{}.score'.format(vid_name, cls_name)) if osp.isfile(save_file): print "{} already exists.".format(save_file) continue proto_dump(interpolated_score_proto, save_file) print "{} created.".format(save_file)
#!/usr/bin/env python import argparse import os import sys sys.path.insert(1, os.path.join(os.path.dirname(__file__), '../..')) from vdetlib.utils.common import stem from vdetlib.utils.protocol import vid_proto_from_dir, proto_dump if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('vid_name') parser.add_argument('root_dir') parser.add_argument('out_file') args = parser.parse_args() if os.path.isfile(args.out_file): print "{} already exists.".format(args.out_file) sys.exit(0) vid = vid_proto_from_dir(args.root_dir, args.vid_name) save_dir = os.path.dirname(args.out_file) if not os.path.isdir(save_dir): os.makedirs(save_dir) proto_dump(vid, args.out_file)
assert vid_name == track_proto["video"] cls_index = imagenet_vdet_class_idx[args.cls] net = caffe_net(args.net_file, args.param_file, args.job - 1) rcnn_sc = lambda vid_proto, track_proto, net, class_idx: rcnn_sampling_dets_scoring( vid_proto, track_proto, det_proto, net, class_idx, args.rcnn_model, args.overlap_thres, save_feat=args.save_feat, save_all_sc=args.save_all_sc, ) rcnn_sc.__name__ = "rcnn_sampling_dets_{}".format(os.path.splitext(os.path.basename(args.param_file))[0]) score_proto = scoring_tracks(vid_proto, track_proto, annot_proto, rcnn_sc, net, cls_index) # ground truth scores, only save gt class scores if args.track_file == "None": for tubelet in score_proto["tubelets"]: if tubelet["gt"] == 0: del tubelet if not score_proto["tubelets"]: sys.exit(0) # save score proto or gt score proto save_dir = os.path.dirname(args.save_file) if not os.path.isdir(save_dir): os.makedirs(save_dir) proto_dump(score_proto, args.save_file)
#!/usr/bin/env python import argparse import sys import cv2 sys.path.insert(1, '.') from vdetlib.vdet.proposal import vid_proposals from vdetlib.utils.protocol import proto_load, proto_dump if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('vid_file') parser.add_argument('save_file') args = parser.parse_args() vid_proto = proto_load(args.vid_file) box_proto = vid_proposals(vid_proto) proto_dump(box_proto, args.save_file)
except: pass track_files = glob(osp.join(args.track_dir, '*.track*')) if len(track_files) == 0: print "Warning: {} has no tracks".format(args.track_dir) sys.exit(0) for track_file in track_files: track_proto = proto_load(track_file) vid_name = vid_proto['video'] assert vid_name == track_proto['video'] cls_name = osp.basename(track_file).split('.')[1] cls_index = imagenet_vdet_class_idx[cls_name] # spatial max pooling score_proto = anchor_propagate(vid_proto, track_proto, det_proto, cls_index) # interpolation interpolated_score_proto = score_proto_interpolation(score_proto, vid_proto) # save score proto save_file = osp.join(args.save_dir, '{}.{}.score'.format(vid_name, cls_name)) if osp.isfile(save_file): print "{} already exists.".format(save_file) continue proto_dump(interpolated_score_proto, save_file) print "{} created.".format(save_file)
with h5py.File(score_file) as d: # skip empty boxes boxes = d['boxes'].value.T.astype('float32') zs = d['zs'].value.T.astype('float32') if boxes.ndim == 1: continue assert boxes.shape[0] == zs.shape[0] for box, scores in zip(boxes, zs): det = {} bbox = box.tolist() det['frame'] = frame_id det['bbox'] = bbox det['hash'] = bbox_hash(vid_name, frame_id, bbox) scores_proto = [] for class_id, (cls_name, score) in \ enumerate(zip(imagenet_vdet_classes[1:], scores), start=1): scores_proto.append({ "class": cls_name, "class_index": class_id, "score": float(score) }) det['scores'] = scores_proto det_proto['detections'].append(det) save_dir = os.path.dirname(args.save_det) if not os.path.isdir(save_dir): try: os.makedirs(save_dir) except: raise proto_dump(det_proto, args.save_det)
def save_if_not_exist(proto, path): if not os.path.isfile(path): proto_dump(box_proto, path)
args = parser.parse_args() if not os.path.isdir(args.save_dir): os.makedirs(args.save_dir) vid_proto = proto_load(args.vid_file) det_proto = proto_load(args.det_file) vid_name = vid_proto['video'] assert vid_name == det_proto['video'] print "Video: {}".format(vid_proto['video']) eng = matlab.engine.start_matlab('-nodisplay -nojvm -nosplash -nodesktop') opts = options({'engine': eng, 'max_tracks': args.num, 'thres': args.thres, 'gpu': args.job - 1, 'max_frames': args.max_frames, 'step': args.step, 'nms_thres': args.nms_thres}) for cls_name in imagenet_vdet_class_idx: if cls_name == '__background__': continue save_name = os.path.join(args.save_dir, '.'.join([vid_name, cls_name, 'track.gz'])) if os.path.isfile(save_name): print "{} already exists.".format(save_name) continue print "Tracking {}...".format(cls_name) cls_idx = imagenet_vdet_class_idx[cls_name] track_proto = greedily_track_from_det(vid_proto, det_proto, fcn_tracker, lambda x:det_score(x, cls_idx), opts) if not track_proto['tracks']: continue proto_dump(track_proto, save_name)
print "Video: {}".format(vid_proto['video']) eng = matlab.engine.start_matlab('-nodisplay -nojvm -nosplash -nodesktop') opts = options({ 'engine': eng, 'max_tracks': args.num, 'thres': args.thres, 'gpu': args.job - 1, 'max_frames': args.max_frames, 'step': args.step, 'nms_thres': args.nms_thres }) for cls_name in imagenet_vdet_class_idx: if cls_name == '__background__': continue save_name = os.path.join(args.save_dir, '.'.join([vid_name, cls_name, 'track.gz'])) if os.path.isfile(save_name): print "{} already exists.".format(save_name) continue print "Tracking {}...".format(cls_name) cls_idx = imagenet_vdet_class_idx[cls_name] track_proto = greedily_track_from_det(vid_proto, det_proto, fcn_tracker, lambda x: det_score(x, cls_idx), opts) if not track_proto['tracks']: continue proto_dump(track_proto, save_name)
# skip empty boxes boxes = d['boxes'].value.T.astype('float32') zs = d['zs'].value.T.astype('float32') if boxes.ndim == 1: continue assert boxes.shape[0] == zs.shape[0] for box, scores in zip(boxes, zs): det = {} bbox = box.tolist() det['frame'] = frame_id det['bbox'] = bbox det['hash'] = bbox_hash(vid_name, frame_id, bbox) scores_proto = [] for class_id, (cls_name, score) in \ enumerate(zip(imagenet_vdet_classes[1:], scores), start=1): scores_proto.append({ "class": cls_name, "class_index": class_id, "score": float(score) }) det['scores'] = scores_proto det_proto['detections'].append(det) save_dir = os.path.dirname(args.save_det) if not os.path.isdir(save_dir): try: os.makedirs(save_dir) except: raise proto_dump(det_proto, args.save_det)
#!/usr/bin/env python import sys import os.path as osp this_dir = osp.dirname(__file__) sys.path.insert(0, osp.join(this_dir, '../../external/')) from vdetlib.utils.protocol import proto_load, proto_dump import argparse import numpy as np import scipy.ndimage if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('gt_file') parser.add_argument('save_file') parser.add_argument('--window', type=int, default=11) args = parser.parse_args() annot_proto = proto_load(args.gt_file) for annot in annot_proto['annotations']: boxes = np.asarray([box['bbox'] for box in annot['track']], dtype=np.float) smoothed = scipy.ndimage.filters.gaussian_filter1d(boxes, args.window / 6., axis=0, mode='nearest') for box, pred_bbox in zip(annot['track'], smoothed): box['bbox'] = pred_bbox.tolist() proto_dump(annot_proto, args.save_file)
try: save_file = osp.join(args.save_dir, vid+'.track') if args.zip: save_file += '.zip' else: save_file += '.pkl' if osp.isfile(save_file): print "{} already exists.".format(save_file) continue vid_file = osp.join(args.vid_dir, vid+'.vid') track_file = osp.join(args.track_dir, vid+'.track') vid_proto = proto_load(vid_file) track_proto = proto_load(track_file) new_track_proto = track_propagation(vid_proto, track_proto, net, im_detect, keep_feat=args.keep_feat, batch_size=args.boxes_num_per_batch) # add ground truth targets if annotation file is given if args.annot_dir is not None: annot_file = osp.join(args.annot_dir, vid+'.annot') annot_proto = proto_load(annot_file) add_track_targets(track_proto, annot_proto) if args.zip: save_track_proto_to_zip(new_track_proto, save_file) else: proto_dump(new_track_proto, save_file) except BaseException as e: print "Error: {} for {}".format(e, vid)
# load feature model caffe.set_mode_gpu() caffe.set_device(args.job_id - 1) feature_net = caffe.Net(args.def_file, args.param, caffe.TEST) print 'Loaded feature network from {:s}.'.format(args.def_file) return feature_net, rnn_net, session if __name__ == '__main__': args = parse_args() if args.cfg_file is not None: cfg_from_file(args.cfg_file) cfg.DEDUP_BOXES = 0.0 # Load models feature_net, rnn_net, session = load_models(args) # Load protocols vid_proto = proto_load(args.vid_file) box_proto = proto_load(args.box_file) # End-to-end testing track_proto = tpn_test(vid_proto, box_proto, feature_net, rnn_net, session, im_detect, scheme=args.scheme, length=args.length, sample_rate=args.sample_rate, offset=args.offset, batch_size=args.boxes_num_per_batch) proto_dump(track_proto, args.save_file)
from vdetlib.utils.protocol import proto_load, proto_dump from vdetlib.utils.common import caffe_net from vdetlib.vdet.tubelet_cls import score_conv_cls from vdetlib.utils.visual import plot_track_scores import matplotlib.pyplot as plt if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('score_file') parser.add_argument('net_file') parser.add_argument('param_file') parser.add_argument('save_file') parser.add_argument('--job', type=int, default=1) parser.add_argument('--save_dir', default=None) args = parser.parse_args() score_proto = proto_load(args.score_file) net = caffe_net(args.net_file, args.param_file, args.job - 1) new_score_proto = score_conv_cls(score_proto, net) save_root = os.path.dirname(args.save_file) if not os.path.isdir(save_root): os.makedirs(save_root) proto_dump(new_score_proto, args.save_file) if args.save_dir is not None: plots = plot_track_scores(new_score_proto) if not os.path.isdir(args.save_dir): os.makedirs(args.save_dir) for ind, plot in enumerate(plots, start=1): plot.savefig(os.path.join(args.save_dir, "{}.png".format(ind)))
#!/usr/bin/env python import argparse import os import sys sys.path.insert(1, '.') from vdetlib.utils.protocol import merge_score_protos, proto_dump, proto_load if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('score_file1') parser.add_argument('score_file2') parser.add_argument('save_file') parser.add_argument('--scheme', required=True, choices=['combine', 'max']) args = parser.parse_args() if os.path.isfile(args.save_file): print '{} already exists.'.format(args.save_file) sys.exit(0) score_proto1 = proto_load(args.score_file1) score_proto2 = proto_load(args.score_file2) new_proto = merge_score_protos(score_proto1, score_proto2, scheme=args.scheme) save_dir = os.path.dirname(args.save_file) if save_dir is not '' and not os.path.isdir(save_dir): os.makedirs(save_dir) proto_dump(new_proto, args.save_file)
# rpn model rpn_pt = os.path.join(model_dir, args.rpn, NETS[args.rpn][1]) rpn_model = os.path.join(model_dir, args.rpn, NETS[args.rpn][2]) # fast_rcnn model fast_rcnn_pt = os.path.join(model_dir, args.feature_net, NETS[args.feature_net][1]) fast_rcnn_model = os.path.join(model_dir, args.feature_net, NETS[args.feature_net][2]) if args.cpu_mode: caffe.set_mode_cpu() else: caffe.set_mode_gpu() gpu_id = args.job - 1; caffe.set_device(gpu_id) cfg.GPU_ID = gpu_id print '\n\nLoaded network {:s}'.format(rpn_model) rpn_net = caffe.Net(rpn_pt, rpn_model, caffe.TEST) print '\n\nLoaded network {:s}'.format(fast_rcnn_model) fast_rcnn_net = caffe.Net(fast_rcnn_pt, fast_rcnn_model, caffe.TEST) vid_proto = proto_load(args.vid_file) gen_rois = lambda net, im: im_proposals(net, im)[0] det_proto = rpn_fast_rcnn_det_vid(rpn_net, fast_rcnn_net, vid_proto, gen_rois, im_detect, CLS_IDX[args.feature_net]) save_dir = os.path.dirname(args.save_file) if not os.path.isdir(save_dir): os.makedirs(save_dir) proto_dump(det_proto, args.save_file)