print('Using config:') pprint.pprint(cfg) while not os.path.exists(args.model) and args.wait: print('Waiting for {} to exist...'.format(args.model)) time.sleep(10) weights_filename = os.path.splitext(os.path.basename(args.model))[0] imdb = get_imdb(args.imdb_name) imdb.competition_mode(args.comp_mode) cfg.GPU_ID = args.gpu_id device_name = '/gpu:{:d}'.format(args.gpu_id) print device_name network = get_network(args.network_name, args.pretrained_model) print 'Use network `{:s}` in training'.format(args.network_name) # build the network network.data = tf.placeholder(tf.float32, shape=[None, None, None, 3]) network.build(network.data, train=False, num_classes=imdb.num_classes) # start a session saver = tf.train.Saver() sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) saver.restore(sess, args.model) print ('Loading model weights from {:s}').format(args.model) test_net(sess, network, imdb, weights_filename)
# the map from dataset name to the dataset is set and store in the /datasets/factory.py # guess that factory.py should run first imdb = get_imdb(args.imdb_name) print 'Loaded dataset `{:s}` for training'.format(imdb.name) #************************************** # load original images and groundtruth #************************************** roidb = get_training_roidb(imdb) if cfg.DEBUG: for key in roidb[0]: print key # output_dir = get_output_dir(imdb, None) output_dir = "/z/home/chsiyuan/Documents/542FinalProject/experiments/results/train" if not os.path.exists(output_dir): os.makedirs(output_dir) print 'Output will be saved to `{:s}`'.format(output_dir) device_name = '/{}:{:d}'.format(args.device,args.device_id) print device_name network = get_network(args.network_name) print 'Use network `{:s}` in training'.format(args.network_name) train_net(network, imdb, roidb, output_dir, pretrained_model=args.pretrained_model, max_iters=args.max_iters)
return args if __name__ == '__main__': cfg.TEST.HAS_RPN = True # Use RPN for proposals args = parse_args() if args.model == ' ': raise IOError(('Error: Model not found.\n')) # init session sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # load network net = get_network(args.demo_net) # load model saver = tf.train.Saver(write_version=tf.train.SaverDef.V1) saver.restore(sess, args.model) #sess.run(tf.initialize_all_variables()) print '\n\nLoaded network {:s}'.format(args.model) # Warmup on a dummy image im = 128 * np.ones((300, 300, 3), dtype=np.uint8) for i in xrange(2): _, _ = im_detect(sess, net, im) im_names = [ '000456.jpg', '000542.jpg', '001150.jpg', '001763.jpg', '004545.jpg'
output_dir = '../data/test/result' if not os.path.exists(output_dir): os.makedirs(output_dir) print 'Output will be saved to `{:s}`'.format(output_dir) #******************* # Need to change #******************* input_dir = '../data/test/images/' if not os.path.exists(input_dir): raise IOError(('Error: Input not found.\n')) # init session sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # load network net = get_network(args.demo_net) # load model saver = tf.train.Saver(write_version=tf.train.SaverDef.V1) saver.restore(sess, args.model) #sess.run(tf.initialize_all_variables()) print '\n\nLoaded network {:s}'.format(args.model) # Warmup on a dummy image # im = 128 * np.ones((300, 300, 3), dtype=np.uint8) # for i in xrange(2): # _, _= im_detect(sess, net, im) images = os.listdir(input_dir) deformed_mask_name = '../data/test/masks/deformation_train_000000000030.png'
return args if __name__ == '__main__': cfg.TEST.HAS_RPN = True # Use RPN for proposals args = parse_args() if args.model == ' ': raise IOError(('Error: Model not found.\n')) # init session sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # load network net = get_network(args.demo_net, state='TEST') # load model saver = tf.train.Saver(write_version=tf.train.SaverDef.V1) saver.restore(sess, args.model) #sess.run(tf.initialize_all_variables()) print '\n\nLoaded network {:s}'.format(args.model) im_names = ['000.jpg', '001.jpg', '002.jpg', '010.jpg'] # Let's see if we can overfit for im_name in im_names: print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print 'Demo for data/demo/{}'.format(im_name) demo(sess, net, im_name)
cfg_from_list(args.set_cfgs) print('Using config:') pprint.pprint(cfg) if not args.randomize: # fix the random seeds (numpy and caffe) for reproducibility np.random.seed(cfg.RNG_SEED) imdb = get_imdb(args.imdb_name) # everything ok so far - no data twice print 'Loaded dataset `{:s}` for training'.format(imdb.name) print(len(imdb.image_index)) # 178 which is correct roidb = get_training_roidb(imdb) output_dir = get_output_dir(imdb, None) print 'Output will be saved to `{:s}`'.format(output_dir) device_name = '/gpu:{:d}'.format(args.gpu_id) print device_name network = get_network(args.network_name) print 'Use network `{:s}` in training'.format(args.network_name) train_net(network, imdb, roidb, output_dir, pretrained_model=args.pretrained_model, max_iters=args.max_iters)
if not args.randomize: # fix the random seeds (numpy and caffe) for reproducibility np.random.seed(cfg.RNG_SEED) imdb = get_imdb(args.imdb_name) print 'Loaded dataset `{:s}` for training'.format(imdb.name) roidb = get_training_roidb(imdb) output_dir = get_output_dir(imdb, None) print 'Output will be saved to `{:s}`'.format(output_dir) device_name = '/{}:{:d}'.format(args.device,args.device_id) print device_name with tf.variable_scope("build") as scope: network = get_network(args.network_name) scope.reuse_variables() network1 = get_network(args.network_name) scope.reuse_variables() network2 = get_network(args.network_name) scope.reuse_variables() network3 = get_network(args.network_name) scope.reuse_variables() network4 = get_network(args.network_name) scope.reuse_variables() network5 = get_network(args.network_name) scope.reuse_variables() network6 = get_network(args.network_name) scope.reuse_variables() network7 = get_network(args.network_name) scope.reuse_variables()
return args if __name__ == '__main__': cfg.TEST.HAS_RPN = True # Use RPN for proposals args = parse_args() if args.model == ' ': raise IOError(('Error: Model not found.\n')) # init session sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # load network path = "/home/surajit/Desktop/Faster-RCNN-COCO_TF/lib/networks/VGGnet_test.py" net = get_network(path) print("name", net) # load model saver = tf.train.Saver() saver.restore(sess, args.model) print('\n\nLoaded network {:s}'.format(args.model)) im_names = [ '000456.jpg', '000542.jpg', '001150.jpg', '001763.jpg', '004545.jpg' ] img_path = args.img_path img_paths = glob.glob(img_path + "/*") for img in img_paths:
def get_context_profiles(set_dirs, sub_dirs, p_scale=1): train_set_07 = 'voc_2007_train' val_set_07 = 'voc_2007_val' train_set_12 = 'voc_2012_train' val_set_12 = 'voc_2012_val' test_set = "voc_2007_test" net_name = "VGGnet" net_final = '../output/faster_rcnn_end2end/voc_2007_trainval+voc_2012_trainval/VGGnet_wt_context/VGGnet_wt_context.ckpt' # some configuration extra_config = "../experiments/cfgs/faster_rcnn_end2end.yml" cfg_from_file(extra_config) print('[build the graph...]') net = get_network(net_name + "_test") saver = tf.train.Saver() fetch_list = [ net.relation, net.get_output('rois'), net.get_output('cls_prob'), net.get_output('bbox_pred') ] print('[build the session...]') config = tf.ConfigProto(allow_soft_placement=True) sess = tf.Session(config=config) print( '[initialize the graph parameter & restore from pre-trained file...]') sess.run(tf.global_variables_initializer()) saver.restore(sess, net_final) print('Loading model weights from {:s}'.format(net_final)) # Note that is is not necessary to collect all the context profiles, just stop the running # if you have got enough training/testing samples. im_set = list( open('../data/VOCdevkit/VOC2007/ImageSets/Main/train.txt', 'r')) im_set = [int(idx.strip()) for idx in im_set] get_clean_nodes_helper(im_set, train_set_07, sess, net, fetch_list, set_dirs[0], sub_dirs) im_set = list(open('../data/VOCdevkit/VOC2007/ImageSets/Main/val.txt', 'r')) im_set = [int(idx.strip()) for idx in im_set] get_clean_nodes_helper(im_set, val_set_07, sess, net, fetch_list, set_dirs[0], sub_dirs) im_set = list( open('../data/VOCdevkit/VOC2012/ImageSets/Main/train.txt', 'r')) im_set = [idx.strip() for idx in im_set] get_clean_nodes_helper(im_set, train_set_12, sess, net, fetch_list, set_dirs[0], sub_dirs) im_set = list(open('../data/VOCdevkit/VOC2012/ImageSets/Main/val.txt', 'r')) im_set = [idx.strip() for idx in im_set] get_clean_nodes_helper(im_set, val_set_12, sess, net, fetch_list, set_dirs[0], sub_dirs) im_set = list( open('../data/VOCdevkit/VOC2007/ImageSets/Main/test.txt', 'r')) im_set = [int(idx.strip()) for idx in im_set] get_clean_nodes_helper(im_set, test_set, sess, net, fetch_list, set_dirs[1], sub_dirs) p = 1 p_list_dir = '../attack_detector/script_extract_files' p_list_name = 'digital_miscls.txt' im_set = list( open('../data/VOCdevkit/VOC2007/ImageSets/Main/test.txt', 'r')) im_set = [int(idx.strip()) for idx in im_set] get_p_nodes_helper(im_set, test_set, sess, net, fetch_list, set_dirs[2], sub_dirs, p_list_dir, p_list_name, p=p) p_list_dir = '../attack_detector/script_extract_files' p_list_name = 'digital_hiding.txt' im_set = list( open('../data/VOCdevkit/VOC2007/ImageSets/Main/test.txt', 'r')) im_set = [int(idx.strip()) for idx in im_set] get_p_nodes_helper(im_set, test_set, sess, net, fetch_list, set_dirs[3], sub_dirs, p_list_dir, p_list_name, p=p) p_list_dir = '../attack_detector/script_extract_files' p_list_name = 'digital_appear.txt' im_set = list( open('../data/VOCdevkit/VOC2007/ImageSets/Main/test.txt', 'r')) im_set = [int(idx.strip()) for idx in im_set] get_p_nodes_helper_appear(im_set, test_set, sess, net, fetch_list, set_dirs[4], sub_dirs, p_list_dir, p_list_name, p=p) p_list_dir = '../attack_detector/script_extract_files' p_list_name = 'physical_miscls.txt' im_set = list( open('../data/VOCdevkit/VOC2007/ImageSets/Main/test.txt', 'r')) im_set = [int(idx.strip()) for idx in im_set] get_p_nodes_helper(im_set, test_set, sess, net, fetch_list, set_dirs[5], sub_dirs, p_list_dir, p_list_name, p=p) p_list_dir = '../attack_detector/script_extract_files' p_list_name = 'physical_hiding.txt' im_set = list( open('../data/VOCdevkit/VOC2007/ImageSets/Main/test.txt', 'r')) im_set = [int(idx.strip()) for idx in im_set] get_p_nodes_helper(im_set, test_set, sess, net, fetch_list, set_dirs[6], sub_dirs, p_list_dir, p_list_name, p=p) p_list_dir = '../attack_detector/script_extract_files' p_list_name = 'physical_appear.txt' im_set = list( open('../data/VOCdevkit/VOC2007/ImageSets/Main/test.txt', 'r')) im_set = [int(idx.strip()) for idx in im_set] get_p_nodes_helper_appear(im_set, test_set, sess, net, fetch_list, set_dirs[7], sub_dirs, p_list_dir, p_list_name, p=p)
if __name__ == '__main__': cfg.TEST.HAS_RPN = True # Use RPN for proposals os.environ['CUDA_VISIBLE_DEVICES'] = '8' args = parse_args() if args.model == ' ': raise IOError(('Error: Model not found.\n')) # init session sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # load network #net = get_network(args.demo_net) with tf.variable_scope("build") as scope: net = get_network('VGG_test') scope.reuse_variables() network1 = get_network('VGG_test') scope.reuse_variables() network2 = get_network('VGG_test') scope.reuse_variables() network3 = get_network('VGG_test') scope.reuse_variables() network4 = get_network('VGG_test') scope.reuse_variables() network5 = get_network('VGG_test') scope.reuse_variables() network6 = get_network('VGG_test') scope.reuse_variables() network7 = get_network('VGG_test') scope.reuse_variables()
cfg.USE_GPU_NMS = False if not args.randomize: # fix the random seeds (numpy and caffe) for reproducibility np.random.seed(cfg.RNG_SEED) target_imdb = get_imdb(args.target_imdb_name) target_imdb.competition_mode(args.comp_mode) print 'Loaded dataset `{:s}` for training'.format(target_imdb.name) target_roidb = get_training_roidb(target_imdb) source_imdb = get_imdb(args.source_imdb_name) source_imdb.competition_mode(args.comp_mode) print 'Loaded dataset `{:s}` for training'.format(source_imdb.name) source_roidb = get_training_roidb(source_imdb) target_network = get_network(args.target_network_name) print 'Use network `{:s}` in training'.format(args.target_network_name) target_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='target') source_network = get_network(args.source_network_name) print 'Use network `{:s}` in training'.format(args.source_network_name) source_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='source') target_roidb = filter_roidb(target_roidb) source_roidb = filter_roidb(source_roidb) target_restorer = tf.train.Saver(var_list=target_vars, write_version=tf.train.SaverDef.V1) source_restorer = tf.train.Saver(var_list=source_vars,
cfg.TEST.HAS_RPN = True # Use RPN for proposals args = parse_args() if args.model == ' ': raise IOError(('Error: Model not found.\n')) if args.jpg == '': raise IOError(('Error: No image for detection')) imdb = get_imdb(args.imdb) # init session sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) # load network net = get_network(args.demo_net, len(CLASSES)) # load model saver = tf.train.Saver(write_version=tf.train.SaverDef.V1) saver.restore(sess, args.model) #sess.run(tf.initialize_all_variables()) print '\n\nLoaded network {:s}'.format(args.model) # Warmup on a dummy image im = 128 * np.ones((300, 300, 3), dtype=np.uint8) for i in xrange(2): _, _ = im_detect(sess, net, im) # im_names = ['000456.jpg', '000542.jpg', '001150.jpg', # '001763.jpg', '004545.jpg']
def viz_net(net_name, weight_name, imdb, viz_mode='viz_cls'): sess = tf.Session() # set up testing mode rois = tf.placeholder(dtype=tf.float32, shape=[None, 5], name='rois') rel_rois = tf.placeholder(dtype=tf.float32, shape=[None, 5], name='rois') ims = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3], name='ims') relations = tf.placeholder(dtype=tf.int32, shape=[None, 2], name='relations') inputs = { 'rois': rois, 'rel_rois': rel_rois, 'ims': ims, 'relations': relations, 'num_roi': tf.placeholder(dtype=tf.int32, shape=[]), 'num_rel': tf.placeholder(dtype=tf.int32, shape=[]), 'num_classes': imdb.num_classes, 'num_predicates': imdb.num_predicates, 'rel_mask_inds': tf.placeholder(dtype=tf.int32, shape=[None]), 'rel_segment_inds': tf.placeholder(dtype=tf.int32, shape=[None]), 'rel_pair_mask_inds': tf.placeholder(dtype=tf.int32, shape=[None, 2]), 'rel_pair_segment_inds': tf.placeholder(dtype=tf.int32, shape=[None]), 'quadric_rois': tf.placeholder(dtype=tf.float32, shape=[None, 25]), 'n_iter': cfg.TEST.INFERENCE_ITER } net = get_network(net_name)(inputs) net.setup() print('Loading model weights from {:s}').format(weight_name) saver = tf.train.Saver() saver.restore(sess, weight_name) roidb = imdb.roidb if cfg.TEST.USE_RPN_DB: imdb.add_rpn_rois(roidb, make_copy=False) prepare_roidb(roidb) num_images = len(imdb.image_index) if net.iterable: inference_iter = net.n_iter - 1 else: inference_iter = 0 print('=======================VIZ INFERENCE Iteration = '), print(inference_iter) print('=======================VIZ MODES = '), print(viz_mode) rec = 0 for im_i in range(num_images): print('processing image ' + str(im_i) + '/' + str(num_images)) im = imdb.im_getter(im_i) bbox_reg = True if viz_mode == 'viz_cls': # use ground truth bounding boxes bbox_reg = False box_proposals = gt_rois(roidb[im_i]) elif viz_mode == 'viz_det': # use RPN-proposed object locations box_proposals, roi_scores = non_gt_rois(roidb[im_i]) roi_scores = np.expand_dims(roi_scores, axis=1) nms_keep = cpu_nms( np.hstack((box_proposals, roi_scores)).astype(np.float32), cfg.TEST.PROPOSAL_NMS) nms_keep = np.array(nms_keep) num_proposal = min(cfg.TEST.NUM_PROPOSALS, nms_keep.shape[0]) keep = nms_keep[:num_proposal] box_proposals = box_proposals[keep, :] else: raise NotImplementedError( 'Incorrect visualization mode. Choose between [cls] and [det]') if box_proposals.size == 0 or box_proposals.shape[0] < 2: print 'skipping image', im_i continue #import pdb; pdb.set_trace() quadric_rois = np.vstack( [roidb[im_i]['quadric_rois'], roidb[im_i]['quadric_rois']] ) # this duplication, I don't know why, but the boxes are duplicated, so I did the same, maybe it is to evaluate different aspect quadric_rois = np.hstack( [np.zeros((quadric_rois.shape[0], 1)), quadric_rois] ) #this is because in the training phase, the image number is appended out_dict = im_detect(sess, net, inputs, im, box_proposals, bbox_reg, [inference_iter], quadric_rois) sg_entry = out_dict[inference_iter] # ground predicted graphs to ground truth annotations gt_to_pred = ground_predictions(sg_entry, roidb[im_i], 0.5) gt_rel = roidb[im_i]['gt_relations'] pred_rel = np.argmax(sg_entry['relations'], 2) if roidb[im_i]['gt_relations'][0][2] == pred_rel[ roidb[im_i]['gt_relations'][0][0], roidb[im_i]['gt_relations'][0][1]]: rec += 0.5 if roidb[im_i]['gt_relations'][1][2] == pred_rel[ roidb[im_i]['gt_relations'][1][0], roidb[im_i]['gt_relations'][1][1]]: rec += 0.5 #my_draw_graph_pred(im, sg_entry['boxes'], sg_entry['scores'], sg_entry['relations'], gt_to_pred, roidb[im_i], im_i) print 'recall', rec / num_images