def __init__(self, ): checkpoint_path = CHECKPOINT logging.info('Evaluating {}...'.format(checkpoint_path)) # Read configurations from json self.model_config, _, self.track_config = load_cfgs(checkpoint_path) self.track_config['log_level'] = 1 # Skip verbose logging for speed self.track_config['scale_step'] = 1.025 # 1.025 self.track_config['scale_damp'] = 1. #self.track_config['window_influence'] = 0.125 # 0.176 # Build the inference graph. g = tf.Graph() with g.as_default(): self.model = inference_wrapper.InferenceWrapper() restore_fn = self.model.build_graph_from_config( self.model_config, self.track_config, checkpoint_path) g.finalize() gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) self.sess = tf.Session(graph=g, config=sess_config) # Load the model from checkpoint. restore_fn(self.sess) self.tracker = Tracker(self.model, self.model_config, self.track_config) self.vi = 0
def main(checkpoint, input_files): os.environ['CUDA_VISIBLE_DEVICES'] = auto_select_gpu() model_config, _, track_config = load_cfgs(checkpoint) track_config['log_level'] = 1 track_config["is_video"] = False g = tf.Graph() with g.as_default(): model = inference_wrapper.InferenceWrapper() restore_fn = model.build_graph_from_config(model_config, track_config, checkpoint) g.finalize() if not osp.isdir(track_config['log_dir']): logging.info('Creating inference directory: %s', track_config['log_dir']) mkdir_p(track_config['log_dir']) video_dirs = [] for file_pattern in input_files.split(","): video_dirs.extend(glob(file_pattern)) logging.info("Running tracking on %d videos matching %s", len(video_dirs), input_files) gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(graph=g, config=sess_config) as sess: restore_fn(sess) tracker = Tracker(model, model_config=model_config, track_config=track_config) for video_dir in video_dirs: if not osp.isdir(video_dir): logging.warning( '{} is not a directory, skipping...'.format(video_dir)) continue video_name = osp.basename(video_dir) video_log_dir = osp.join(track_config['log_dir'], video_name) mkdir_p(video_log_dir) filenames = sort_nicely(glob(video_dir + '/img/*.jpg')) first_line = open(video_dir + '/groundtruth_rect.txt').readline() bb = [int(v) for v in first_line.strip().split(',')] # Rectangle: [x,y,width,height] init_bb = Rectangle(bb[0] - 1, bb[1] - 1, bb[2], bb[3]) # 0-index in python trajectory = tracker.track(sess, init_bb, filenames, video_log_dir) with open(osp.join(video_log_dir, 'track_rect.txt'), 'w') as f: for region in trajectory: rect_str = '{},{},{},{}\n'.format(region.x + 1, region.y + 1, region.width, region.height) f.write(rect_str)
def run_SiamRPN_OPF(seq, rp, bSaveImage): os.environ['CUDA_VISIBLE_DEVICES'] = auto_select_gpu() config_name = "SiamRPN_ftall" CHECKPOINT = '/home/lab-xiong.jiangfeng/Projects/SiameseRPN/Logs/%s/track_model_checkpoints/%s' % ( config_name, config_name) logging.info('Evaluating {}...'.format(CHECKPOINT)) # Read configurations from json model_config, _, track_config = load_cfgs(CHECKPOINT) track_config['log_level'] = 0 # Skip verbose logging for speed np.random.seed(1234) tf.set_random_seed(1234) g = tf.Graph() with g.as_default(): model = get_model(model_config['Model'])(model_config=model_config, mode='inference') model.build(reuse=tf.AUTO_REUSE) model.online_net = OnlineNet(online_config, is_training=True, reuse=False) model.online_valnet = OnlineNet(online_config, is_training=False, reuse=True) global_variables_init_op = tf.global_variables_initializer() gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) sess_config.gpu_options.per_process_gpu_memory_fraction = 0.2 with tf.Session(graph=g, config=sess_config) as sess: sess.run(global_variables_init_op) model.restore_weights_from_checkpoint(sess, 605000) tracker = OnlineTracker(sess, model, track_config, online_config, show_video=0) tic = time.clock() frames = seq.s_frames init_rect = seq.init_rect x, y, width, height = init_rect # OTB format init_bb = Rectangle(x - 1, y - 1, width, height) trajectory_py = tracker.track(init_bb, frames, bSaveImage, rp) #print(trajectory_py) trajectory = [ Rectangle(val.x + 1, val.y + 1, val.width, val.height) for val in trajectory_py ] # x, y add one to match OTB format duration = time.clock() - tic result = dict() result['res'] = trajectory result['type'] = 'rect' result['fps'] = round(seq.len / duration, 3) return result
def run_SA_Siam(seq, rp, bSaveImage, epoch=30): iter_ckpt = epoch * 6650 - 1 checkpoint_appearance_path = CHECKPOINT_APPEARANCE.format( iter_ckpt=iter_ckpt) logging.info('Evaluating {}...'.format(checkpoint_appearance_path)) checkpoint_semantic_path = CHECKPOINT_SEMANTIC.format(iter_ckpt=iter_ckpt) logging.info('Evaluating {}...'.format(checkpoint_semantic_path)) # Read configurations from json model_config, _, track_config = load_cfgs(CHECKPOINT_SA_SIAM) track_config['log_level'] = 0 # Skip verbose logging for speed # Build the inference graph. g = tf.Graph() with g.as_default(): model = inference_wrapper.InferenceWrapper() model.build_model(model_config, track_config) saver_loader_semantic = get_saver('', removes=[':0', '_semantic'], excepts=['appearance', 'State']) saver_loader_appearance = get_saver('', removes=[':0', '_appearance'], excepts=['semantic', 'State']) g.finalize() gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(graph=g, config=sess_config) as sess: # Load the model from checkpoint. # restore_fn(sess) saver_loader_semantic.restore(sess, checkpoint_semantic_path) saver_loader_appearance.restore(sess, checkpoint_appearance_path) tracker = Tracker(model, model_config, track_config) tic = time.clock() frames = seq.s_frames init_rect = seq.init_rect x, y, width, height = init_rect # OTB format init_bb = Rectangle(x - 1, y - 1, width, height) trajectory_py = tracker.track(sess, init_bb, frames) trajectory = [ Rectangle(val.x + 1, val.y + 1, val.width, val.height) for val in trajectory_py ] # x, y add one to match OTB format duration = time.clock() - tic result = dict() result['res'] = trajectory result['type'] = 'rect' result['fps'] = round(seq.len / duration, 3) return result
def run_iSiam_otb(seq, rp, bSaveImage): checkpoint_path = CHECKPOINT logging.info('Evaluating {}...'.format(checkpoint_path)) # Read configurations from json model_config, _, track_config = load_cfgs(checkpoint_path) track_config['log_level'] = 1 # Skip verbose logging for speed track_config['scale_step'] = 1.021 # 1.023*, 1.021* track_config['scale_damp'] = 1. track_config['window_influence'] = 0.21 # 0.21* #track_config['x_image_size'] = 273 # Build the inference graph. g = tf.Graph() with g.as_default(): model = inference_wrapper.InferenceWrapper() restore_fn = model.build_graph_from_config(model_config, track_config, checkpoint_path) g.finalize() gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(graph=g, config=sess_config) as sess: # Load the model from checkpoint. restore_fn(sess) tracker = Tracker(model, model_config, track_config) tic = time.time() frames = seq.s_frames init_rect = seq.init_rect x, y, width, height = init_rect # OTB format init_bb = Rectangle(x - 1, y - 1, width, height) #init_bb = Rectangle(x, y, width, height) first_name = frames[0] first_split = first_name.split('/') dir_name = os.path.join( '/home/william/tracker_benchmark/results/samples', first_split[-3]) if not os.path.exists(dir_name): os.mkdir(dir_name) trajectory_py = tracker.track(sess, init_bb, frames, logdir=dir_name) trajectory = [ Rectangle(val.x + 1, val.y + 1, val.width, val.height) for val in trajectory_py ] # x, y add one to match OTB format duration = time.time() - tic result = dict() result['res'] = trajectory result['type'] = 'rect' result['fps'] = round(seq.len / duration, 3) return result
def main(checkpoint, input_files): os.environ['CUDA_VISIBLE_DEVICES'] = auto_select_gpu() model_config, _, track_config = load_cfgs(checkpoint) track_config['log_level'] = 1 g = tf.Graph() with g.as_default(): model = inference_wrapper.InferenceWrapper() restore_fn = model.build_graph_from_config(model_config, track_config, checkpoint) g.finalize() if not osp.isdir(track_config['log_dir']): logging.info('Creating inference directory: %s', track_config['log_dir']) mkdir_p(track_config['log_dir']) video_dirs = [] for file_pattern in input_files.split(","): video_dirs.extend(glob(file_pattern)) logging.info("Running tracking on %d videos matching %s", len(video_dirs), input_files) gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(graph=g, config=sess_config) as sess: restore_fn(sess) tracker = Tracker(model, model_config=model_config, track_config=track_config) for video_dir in video_dirs: if not osp.isdir(video_dir): logging.warning('{} is not a directory, skipping...'.format(video_dir)) continue video_name = osp.basename(video_dir) video_log_dir = osp.join(track_config['log_dir'], video_name) mkdir_p(video_log_dir) filenames = sort_nicely(glob(video_dir + '/img/*.jpg')) first_line = open(video_dir + '/groundtruth_rect.txt').readline() bb = [int(v) for v in first_line.strip().split(',')] init_bb = Rectangle(bb[0] - 1, bb[1] - 1, bb[2], bb[3]) # 0-index in python trajectory = tracker.track(sess, init_bb, filenames, video_log_dir) with open(osp.join(video_log_dir, 'track_rect.txt'), 'w') as f: for region in trajectory: rect_str = '{},{},{},{}\n'.format(region.x + 1, region.y + 1, region.width, region.height) f.write(rect_str)
def __init__(self): self.model_config, _, self.track_config = load_cfgs(checkpoint) with tf.Session() as sess: if osp.isdir(checkpoint): checkpoint_path = tf.train.latest_checkpoint(checkpoint) if not checkpoint: raise ValueError("No checkpoint file found in: {}".format(checkpoint)) saver = tf.train.import_meta_graph(checkpoint_path + '.meta') saver.restore(sess, checkpoint_path) writer = tf.compat.v1.summary.FileWriter(logdir="./training_model", graph=tf.get_default_graph())
def run_MFST(seq, rp, bSaveImage): checkpoint_path = CHECKPOINT logging.info('Evaluating {}...'.format(checkpoint_path)) # Read configurations from json model_config, _, track_config = load_cfgs(checkpoint_path) track_config['log_level'] = 0 # Skip verbose logging for speed # Build the inference graph. g = tf.Graph() with g.as_default(): model = inference_wrapper.InferenceWrapper() restore_fn = model.build_graph_from_config(model_config, track_config, checkpoint_path) #g.finalize() gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(graph=g, config=sess_config) as sess: ## used for initializing alexnet parameters init_global = tf.global_variables_initializer() sess.run(init_global) ## global initalizer must be run before restore # Load the model from checkpoint. restore_fn(sess) tracker = Tracker(model, model_config, track_config) tic = time.clock() frames = seq.s_frames init_rect = seq.init_rect x, y, width, height = init_rect # OTB format init_bb = Rectangle(x - 1, y - 1, width, height) trajectory_py = tracker.track(sess, init_bb, frames) trajectory = [Rectangle(val.x + 1, val.y + 1, val.width, val.height) for val in trajectory_py] # x, y add one to match OTB format duration = time.clock() - tic result = dict() result['res'] = trajectory result['type'] = 'rect' result['fps'] = round(seq.len / duration, 3) return result
def test_load_embedding_from_converted_TF_model(): """Test if the embedding model loaded from converted TensorFlow checkpoint produces the same features as the original implementation""" checkpoint = osp.join( PARENT_DIR, 'Logs/SiamFC/track_model_checkpoints/SiamFC-3s-color-pretrained') test_im = osp.join(CURRENT_DIR, '01.jpg') gt_feat = osp.join(CURRENT_DIR, 'result.mat') if not osp.exists(checkpoint): raise Exception('SiamFC-3s-color-pretrained is not generated yet.') model_config, train_config, track_config = load_cfgs(checkpoint) # Build the model g = tf.Graph() with g.as_default(): model = siamese_model.SiameseModel(model_config, train_config, mode='inference') model.build() with tf.Session() as sess: # Load model here saver = tf.train.Saver(tf.global_variables()) if osp.isdir(checkpoint): model_path = tf.train.latest_checkpoint(checkpoint) else: model_path = checkpoint saver.restore(sess, model_path) # Load image im = imread(test_im) im_batch = np.expand_dims(im, 0) # Feed image feature = sess.run([model.exemplar_embeds], feed_dict={model.examplar_feed: im_batch}) # Compare with features computed from original source code ideal_feature = sio.loadmat(gt_feat)['r']['z_features'][0][0] diff = feature - ideal_feature diff = np.sqrt(np.mean(np.square(diff))) print('Feature computation difference: {}'.format(diff)) print('You should get something like: 0.00892720464617')
def __init__( self, debug=0, checkpoint='Logs/SiamFC/track_model_checkpoints/SiamFC-3s-color-pretrained' ): os.environ['CUDA_VISIBLE_DEVICES'] = auto_select_gpu() # run only on cpu # os.environ['CUDA_VISIBLE_DEVICES'] = '-1' model_config, _, track_config = load_cfgs(checkpoint) track_config['log_level'] = debug g = tf.Graph() with g.as_default(): model = inference_wrapper.InferenceWrapper() restore_fn = model.build_graph_from_config(model_config, track_config, checkpoint) g.finalize() if not osp.isdir(track_config['log_dir']): logging.info('Creating inference directory: %s', track_config['log_dir']) mkdir_p(track_config['log_dir']) gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) sess = tf.Session(graph=g, config=sess_config) # sess.run(tf.global_variables_initializer()) restore_fn(sess) tracker = Tracker(model, model_config=model_config, track_config=track_config) video_name = "demo" video_log_dir = osp.join(track_config['log_dir'], video_name) rmdir(video_log_dir) mkdir_p(video_log_dir) self.tracker = tracker self.sess = sess self.video_log_dir = video_log_dir self.graph = g
def run_SiamRPN(seq, rp, bSaveImage): CHECKPOINT = '/home/lab-xiong.jiangfeng/Projects/SiameseRPN/Logs/%s/track_model_checkpoints/%s' % ( tracker_name, tracker_name) logging.info('Evaluating {}...'.format(CHECKPOINT)) # Read configurations from json model_config, _, track_config = load_cfgs(CHECKPOINT) track_config['log_level'] = 0 # Skip verbose logging for speed g = tf.Graph() with g.as_default(): model = SiamRPN(model_config=model_config, mode='inference') model.build(reuse=tf.AUTO_REUSE) global_variables_init_op = tf.global_variables_initializer() gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(graph=g, config=sess_config) as sess: sess.run(global_variables_init_op) model.restore_weights_from_checkpoint(sess) tracker = Tracker(sess, model, track_config) tic = time.clock() frames = seq.s_frames init_rect = seq.init_rect x, y, width, height = init_rect # OTB format init_bb = Rectangle(x - 1, y - 1, width, height) trajectory_py = tracker.track(init_bb, frames, bSaveImage, rp) trajectory = [ Rectangle(val.x + 1, val.y + 1, val.width, val.height) for val in trajectory_py ] # x, y add one to match OTB format duration = time.clock() - tic result = dict() result['res'] = trajectory result['type'] = 'rect' result['fps'] = round(seq.len / duration, 3) return result
def test_load_embedding_from_converted_TF_model(): """Test if the embedding model loaded from converted TensorFlow checkpoint produces the same features as the original implementation""" checkpoint = osp.join(PARENT_DIR, 'Logs/SiamFC/track_model_checkpoints/SiamFC-3s-color-pretrained') test_im = osp.join(CURRENT_DIR, '01.jpg') gt_feat = osp.join(CURRENT_DIR, 'result.mat') if not osp.exists(checkpoint): raise Exception('SiamFC-3s-color-pretrained is not generated yet.') model_config, train_config, track_config = load_cfgs(checkpoint) # Build the model g = tf.Graph() with g.as_default(): model = siamese_model.SiameseModel(model_config, train_config, mode='inference') model.build() with tf.Session() as sess: # Load model here saver = tf.train.Saver(tf.global_variables()) if osp.isdir(checkpoint): model_path = tf.train.latest_checkpoint(checkpoint) else: model_path = checkpoint saver.restore(sess, model_path) # Load image im = imread(test_im) im_batch = np.expand_dims(im, 0) # Feed image feature = sess.run([model.exemplar_embeds], feed_dict={model.examplar_feed: im_batch}) # Compare with features computed from original source code ideal_feature = sio.loadmat(gt_feat)['r']['z_features'][0][0] diff = feature - ideal_feature diff = np.sqrt(np.mean(np.square(diff))) print('Feature computation difference: {}'.format(diff)) print('You should get something like: 0.00892720464617')
def main(model_config, train_config, track_config): os.environ['CUDA_VISIBLE_DEVICES'] = auto_select_gpu() # Create training directory which will be used to save: configurations, model files, TensorBoard logs train_dir = train_config['train_dir'] if not osp.isdir(train_dir): logging.info('Creating training directory: %s', train_dir) mkdir_p(train_dir) if have_cfgs(train_dir): model_config, train_config, track_config = load_cfgs(train_dir) print("=================== load cfg ") else: save_cfgs(train_dir, model_config, train_config, track_config) print("=================== save default cfg, please modify files in {}".format(train_dir)) return g = tf.Graph() with g.as_default(): # Set fixed seed for reproducible experiments random.seed(train_config['seed']) np.random.seed(train_config['seed']) tf.set_random_seed(train_config['seed']) # Build the training and validation model model = siamese_model.SiameseModel(model_config, train_config, track_config, mode='train') model.build() model_va = siamese_model.SiameseModel(model_config, train_config, track_config, mode='validation') model_va.build(reuse=True) learning_rate = _configure_learning_rate(train_config, model.global_step) optimizer = _configure_optimizer(train_config, learning_rate) tf.summary.scalar('learning_rate', learning_rate) # general way for run train: https://qiita.com/horiem/items/00ec6488b23895cc4fe2 # tensorflow 2.1: https://www.tensorflow.org/tutorials/customization/custom_training_walkthrough # Set up the training ops opt_op = tensorflow.contrib.layers.optimize_loss( loss=model.total_loss, global_step=model.global_step, learning_rate=learning_rate, optimizer=optimizer, clip_gradients=train_config['clip_gradients'], learning_rate_decay_fn=None, summaries=['learning_rate']) with tf.control_dependencies([opt_op]): train_op = tf.no_op(name='train') saver = tf.train.Saver(tf.global_variables(), max_to_keep=train_config['max_checkpoints_to_keep']) summary_writer = tf.summary.FileWriter(train_dir, g) summary_op = tf.summary.merge_all() global_variables_init_op = tf.global_variables_initializer() local_variables_init_op = tf.local_variables_initializer() g.finalize() # Finalize graph to avoid adding ops by mistake # Dynamically allocate GPU memory gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) sess = tf.Session(config=sess_config) model_path = tf.train.latest_checkpoint(train_config['train_dir']) if not model_path: sess.run(global_variables_init_op) sess.run(local_variables_init_op) start_step = 0 if model_config['embed_config']['embedding_checkpoint_file']: model.init_fn(sess) else: logging.info('Restore from last checkpoint: {}'.format(model_path)) sess.run(local_variables_init_op) saver.restore(sess, model_path) start_step = tf.train.global_step(sess, model.global_step.name) + 1 # export if train_config["export"]: # still debugging ''' frozen_graph_def = tf.graph_util.convert_variables_to_constants(sess, tf.get_default_graph().as_graph_def(), ["train/detection/add"]) frozen_graph = tf.Graph() with frozen_graph.as_default(): tf.import_graph_def(frozen_graph_def) save_model_dir = osp.join(train_config['train_dir'], 'models') tf.train.write_graph(frozen_graph_def, save_model_dir, 'quantized_frozen_graph.pb', as_text=False) tf.train.write_graph(frozen_graph_def, save_model_dir, 'quantized_frozen_graph.pbtxt', as_text=True) output_op = sess.graph.get_tensor_by_name("validation/detection/add:0") input1_op = sess.graph.get_tensor_by_name("validation/template_image:0") input2_op = sess.graph.get_tensor_by_name("validation/input_image:0") converter = tf.lite.TFLiteConverter.from_session(sess, [input1_op, input2_op], [output_op]) converter.inference_type = tf.lite.constants.QUANTIZED_UINT8 input_arrays = converter.get_input_arrays() converter.quantized_input_stats = {input_arrays[0] : (0., 1.), input_arrays[1] : (0., 1.)} # mean, std_dev converter.default_ranges_stats = (0, 255) tflite_model = converter.convert() open(osp.join(save_model_dir, 'quantized_frozen_graph.tflite'), "wb").write(tflite_model) ''' return # Training loop data_config = train_config['train_data_config'] total_steps = int(data_config['epoch'] * data_config['num_examples_per_epoch'] / data_config['batch_size']) logging.info('Train for {} steps'.format(total_steps)) save_step = int(data_config['num_examples_per_epoch'] / data_config['batch_size']) print("=========== save_step: {}".format(save_step)) for step in range(start_step, total_steps): start_time = time.time() # no "feed_dict" # has "feed_dict" exmaple (mnist): https://qiita.com/SwitchBlade/items/6677c283b2402d060cd0 _, loss, batch_loss, instances, response = sess.run([train_op, model.total_loss, model.batch_loss, model.instances, model.response]) duration = time.time() - start_time if step % 10 == 0: examples_per_sec = data_config['batch_size'] / float(duration) time_remain = data_config['batch_size'] * (total_steps - step) / examples_per_sec m, s = divmod(time_remain, 60) h, m = divmod(m, 60) format_str = ('%s: step %d, total loss = %.2f, batch loss = %.2f (%.1f examples/sec; %.3f ' 'sec/batch; %dh:%02dm:%02ds remains)') logging.info(format_str % (datetime.now(), step, loss, batch_loss, examples_per_sec, duration, h, m, s)) if step % 100 == 0: summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str, step) if step % save_step == 0 or (step + 1) == total_steps: checkpoint_path = osp.join(train_config['train_dir'], 'model.ckpt') saver.save(sess, checkpoint_path, global_step=step)
def main(_): # load model model_config, _, track_config = load_cfgs(CHECKPOINT) track_config["log_level"] = 0 track_config["is_video"] = True g = tf.Graph() with g.as_default(): model = inference_wrapper.InferenceWrapper() restore_fn = model.build_graph_from_config(model_config, track_config, CHECKPOINT) g.finalize() if not os.path.isdir(track_config['log_dir']): tf.logging.info('Creating inference directory: %s', track_config['log_dir']) mkdir_p(track_config['log_dir']) gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(graph=g, config=sess_config) as sess: restore_fn(sess) tracker = Tracker(model, model_config=model_config, track_config=track_config) video_name = os.path.basename(FLAGS.video_path) video_log_dir = os.path.join(track_config["log_dir"], video_name) mkdir_p(video_log_dir) if str(FLAGS.video_path) in ["0", "1"]: # read from camera video_path = int(FLAGS.video_path) with_camera = True else: # read from video video_path = glob(os.path.join(FLAGS.video_path, "*.mp4"))[0] with_camera = False video_capture = cv2.VideoCapture(video_path) bb = [-1, -1, -1, -1] cv2.namedWindow("template") cv2.setMouseCallback("template", draw_init_box, bb) trajectory = [] f_count = 0 f_rate = 0 start_time = time.time() while True: # capture frame by frame ret_, frame = video_capture.read() if ret_ == False: continue f_width, f_height = [ int(a) for a in FLAGS.video_resolution.split("*") ] try: o_frame = cv2.resize(frame, (f_width, f_height), interpolation=cv2.INTER_CUBIC) except: break i_frame = cv2.cvtColor(o_frame, cv2.COLOR_BGR2RGB) # cv2.imwrite("test.jpg",o_frame) # pdb.set_trace() if f_count == 0: # initialize the tracker # wait for drawing init box while True: init_frame = o_frame.copy() cv2.imshow("template", init_frame) k = cv2.waitKey(0) if k == 32: # space cx = int((bb[0] + bb[2]) / 2) cy = int((bb[1] + bb[3]) / 2) w = int(bb[2] - bb[0]) h = int(bb[3] - bb[1]) # Rectangle: [x,y,width,height] init_bb = Rectangle(cx - 1, cy - 1, w, h) # 0-index in python draw_box(init_frame, init_bb, "exemplar") break first_box = convert_bbox_format(init_bb, "center-based") bbox_feed = [ first_box.y, first_box.x, first_box.height, first_box.width ] input_feed = [i_frame, bbox_feed] frame2crop_scale = tracker.siamese_model.initialize( sess, input_feed) # Storing target state original_target_height = first_box.height original_target_width = first_box.width search_center = np.array([ get_center(tracker.x_image_size), get_center(tracker.x_image_size) ]) current_target_state = TargetState( bbox=first_box, search_pos=search_center, scale_idx=int(get_center(tracker.num_scales))) # setup initialized params current_param = { "original_target_width": original_target_width, "original_target_height": original_target_height, "search_center": search_center, "current_target_state": current_target_state } bbox, current_param = tracker.track_frame(sess, i_frame, current_param, video_log_dir) # add overlays end_time = time.time() f_rate = int(1 / (end_time - start_time)) start_time = time.time() draw_box(o_frame, bbox) cv2.putText(o_frame, str(f_rate) + "fps", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), thickness=2, lineType=2) trajectory.append(bbox) f_count += 1 cv2.imshow("Real-time Ouput", o_frame) cv2.imshow("template", init_frame) # if f_count > 30: # cv2.imwrite("test.jpg",o_frame) # pdb.set_trace() if cv2.waitKey(1) & 0xFF == ord("q"): cv2.imwrite("./assets/instance.jpg", o_frame) cv2.imwrite("./assets/exemplar.jpg", init_frame) break video_capture.release() cv2.destroyAllWindows() # save track results # pdb.set_trace() with open(os.path.join(video_log_dir, "track_rect.txt"), "w") as f: for region in trajectory: rect_str = "{},{},{},{}\n".format(region.x + 1, region.y + 1, region.width, region.height) f.write(rect_str)
h = s * (y2 - y1) + 1 return [cx - (w - 1) // 2, cy - (h - 1) // 2, w, h] if __name__ == "__main__": os.environ['CUDA_VISIBLE_DEVICES'] = auto_select_gpu() MODEL = "SiamRPN" CHECKPOINT = "Logs/%s/track_model_checkpoints/%s" % (MODEL, MODEL) gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(config=sess_config) as sess: #1. init Tracker Model model_config, _, track_config = load_cfgs(CHECKPOINT) model = get_model(model_config['Model'])(model_config=model_config, mode='inference') model.build(reuse=tf.AUTO_REUSE) global_variables_init_op = tf.global_variables_initializer() sess.run(global_variables_init_op) model.restore_weights_from_checkpoint(sess) tracker = Tracker(sess, model, track_config, True) #2. load tracking video tracking_dir = "dataset/demo/bag" gt_file = os.path.join(tracking_dir, "groundtruth.txt") try: first_bbox = loadtxt(gt_file, delimiter=',')[0] except:
CODE_ROOT = '/home/travail/dev/GitRepo/MFST' CHECKPOINT = '/home/travail/dev/GitRepo/MFST/Logs/SiamFC/track_model_checkpoints/siamfc_se' sys.path.insert(0, CODE_ROOT) from utils.misc_utils import auto_select_gpu, load_cfgs from inference import inference_wrapper from inference.tracker import Tracker # Set GPU os.environ['CUDA_VISIBLE_DEVICES'] = auto_select_gpu() checkpoint_path = CHECKPOINT # Read configurations from json model_config, _, track_config = load_cfgs(checkpoint_path) track_config['log_level'] = 0 # Skip verbose logging for speed # Build the inference graph. g = tf.Graph() with g.as_default(): model = inference_wrapper.InferenceWrapper() restore_fn = model.build_graph_from_config(model_config, track_config, checkpoint_path) #g.finalize() gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(graph=g, config=sess_config) as sess:
action="store", help='the specific checkpoint for export', default=-1, type=int) parser.add_argument('--scale', dest='scale', action="store", help='the number of scale to do tracking', default=1, type=int) args, _ = parser.parse_known_args() model_save_dir = osp.join(args.checkpoint_dir, 'models') model_config, train_config, track_config = load_cfgs(args.checkpoint_dir) size_z = model_config['z_image_size'] size_x = track_config['x_image_size'] tf.enable_eager_execution( ) # the position of this is very important!!!!, can not be outside with tf.Session() as sess: template_image = tf.placeholder(tf.float32, shape=[size_z, size_z, 3], name='template_image') input_image = tf.placeholder(tf.float32, shape=[args.scale, size_x, size_x, 3], name='input_image') template_image = tf.expand_dims(template_image, 0)
'Logs/SiamFC/track_model_checkpoints/SiamFC-3s-color-pretrained/models/whole_model_scale1.pb', type=str) parser.add_argument( '--config', dest='config_filepath', action="store", help='the path of tracking config for inference', default= 'Logs/SiamFC/track_model_checkpoints/SiamFC-3s-color-pretrained', type=str) args, _ = parser.parse_known_args() model_save_dir = osp.dirname(args.frozen_graph_model) model_config, train_config, track_config = load_cfgs(args.config_filepath) train_config['validation_data_config']["batch_size"] = 1 train_config['validation_data_config'][ "prefetch_capacity"] = 0 # no need to prefetch print("batch.size: {}".format( train_config['validation_data_config'].get("batch_size"))) tf.enable_eager_execution( ) # the position of this is very important!!!!, can not be outside scale = 1 with tf.gfile.GFile(args.frozen_graph_model, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read())
def main(checkpoint, input_files): os.environ['CUDA_VISIBLE_DEVICES'] = auto_select_gpu() model_config, _, track_config = load_cfgs(checkpoint) track_config['log_level'] = 1 g = tf.Graph() with g.as_default(): model = inference_wrapper.InferenceWrapper() restore_fn = model.build_graph_from_config(model_config, track_config, checkpoint) g.finalize() if not osp.isdir(track_config['log_dir']): logging.info('Creating inference directory: %s', track_config['log_dir']) mkdir_p(track_config['log_dir']) video_dirs = [] for file_pattern in input_files.split(","): video_dirs.extend(glob(file_pattern)) logging.info("Running tracking on %d videos matching %s", len(video_dirs), input_files) gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(graph=g, config=sess_config) as sess: restore_fn(sess) tracker = Tracker(model, model_config=model_config, track_config=track_config) for video_dir in video_dirs: if not osp.isdir(video_dir): logging.warning( '{} is not a directory, skipping...'.format(video_dir)) continue video_name = osp.basename(video_dir) video_log_dir = osp.join(track_config['log_dir'], video_name) mkdir_p(video_log_dir) filenames = sort_nicely( glob(video_dir + '/img/*.jpg') + glob(video_dir + '/img/*.png')) first_line = open(video_dir + '/groundtruth_rect.txt').readline() bb = [int(v) for v in first_line.strip().split(',')] init_bb = Rectangle(bb[0] - 1, bb[1] - 1, bb[2], bb[3]) # 0-index in python trajectory = tracker.track(sess, init_bb, filenames, video_log_dir) with open(osp.join(video_log_dir, 'track_rect.txt'), 'w') as f: for region in trajectory: rect_str = '{},{},{},{}\n'.format(region.x + 1, region.y + 1, region.width, region.height) f.write(rect_str) with open(osp.join(video_log_dir, 'bboxes.json'), 'r') as f: data = json.load(f) final_output = {} for i, fname in enumerate(data.keys()): img = np.array(Image.open(fname).convert('RGB')) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #print(img,img.shape) bboxes = data[fname] bboxes = list( map( lambda x: list( map(lambda y: float(y), x.strip().split(','))), bboxes)) arr = [] for x, y, w, h in bboxes: ymin, xmin, ymax, xmax = int(y), int(x), int(y + h), int(x + w) img = cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0, 255), 2) arr.append([ymin, xmin, ymax, xmax]) final_output[fname] = arr name = osp.basename(fname) name = osp.splitext(name)[0] W, H, _ = img.shape cv2.imshow("Pic", cv2.resize(img, (W // 2, H // 2))) cv2.waitKey(0) out_folder = osp.join(video_log_dir, "Outputs") mkdir_p(out_folder) cv2.imwrite(osp.join(out_folder, f"{name}_bbox.png"), img) with open(osp.join(out_folder, "output.json"), "w") as f: json.dump(final_output, f, indent=4) cv2.destroyAllWindows()