def main():
    network_generators = {
        'ZF': ZFGenerator
    }

    args = parse_args(network_generators.keys())

    if args.cfg is not None:
        cfg_from_file(args.cfg)

    apollocaffe.set_random_seed(cfg.RNG_SEED)
    np.random.seed(cfg.RNG_SEED)

    if args.gpu_id >= 0:
        apollocaffe.set_device(args.gpu_id)

    apollocaffe.set_cpp_loglevel(3)

    train_roidb = None
    if args.train_imdb is not None:
        train_imdb =  get_imdb(args.train_imdb)
        train_imdb.set_proposal_method(cfg.TRAIN.PROPOSAL_METHOD)
        train_roidb = get_training_roidb(train_imdb)

    test_roidb = None
    if args.test_imdb is not None:
        test_imdb = get_imdb(args.test_imdb)
        test_imdb.set_proposal_method('gt')
        prepare_roidb(test_imdb)
        test_roidb = test_imdb.roidb

    faster_rcnn = FasterRCNN(args, network_generators, train_roidb=train_roidb, test_roidb=test_roidb)
    faster_rcnn.train()

    return 0
Exemplo n.º 2
0
def main(args_list):
    args = parse_args(args_list)

    print('Called with args:')
    print(args)

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    cfg.GPU_ID = args.gpu_id

    print('Using config:')
    pprint.pprint(cfg)

    while not os.path.exists(args.caffemodel) and args.wait:
        print('Waiting for {} to exist...'.format(args.caffemodel))
        time.sleep(10)

    caffe.set_mode_gpu()
    caffe.set_device(args.gpu_id)
    net = caffe.Net(args.prototxt, args.caffemodel, caffe.TEST)
    net.name = os.path.splitext(os.path.basename(args.caffemodel))[0]

    imdb = get_imdb(args.imdb_name)
    imdb.competition_mode(args.comp_mode)
    if not cfg.TEST.HAS_RPN:
        imdb.set_proposal_method(cfg.TEST.PROPOSAL_METHOD)

    test_net(net, imdb, max_per_image=args.max_per_image, vis=args.vis)
Exemplo n.º 3
0
def main(args_list):
    args = parse_args(args_list)

    print('Called with args:')
    print(args)

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    cfg.GPU_ID = args.GPU_ID

    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)
        caffe.set_random_seed(cfg.RNG_SEED)

    print 'Setting GPU device %d for training' % cfg.GPU_ID
    caffe.set_mode_gpu()
    caffe.set_device(cfg.GPU_ID)

    imdb, roidb = combined_roidb(args.imdb_name)
    print '{:d} roidb entries'.format(len(roidb))

    output_dir = get_output_dir(imdb)
    print 'Output will be saved to `{:s}`'.format(output_dir)

    train_net(args.solver, roidb, output_dir,
              pretrained_model=args.pretrained_model,
              max_iters=args.max_iters)
Exemplo n.º 4
0
  def __init__(self, _yaml_file=None, _gpu_id=None):
    assert(_yaml_file)
    self.yaml_file = _yaml_file
    self.mean_value = np.zeros((1, 1, 3)).astype(np.float32)
    self.mean_value[0,0,0] = 102.9801
    self.mean_value[0,0,1] = 115.9465
    self.mean_value[0,0,2] = 122.7717
    if _gpu_id == None:
      self.use_gpu = False
      self.gpu_id = -1
    else:
      self.use_gpu = True
      self.gpu_id = _gpu_id

    cfg_from_file(self.yaml_file)

    cfg.USE_GPU= self.use_gpu
    cfg.GPU_ID = self.gpu_id
    """
    cfg.CLASSES = ('__background__',
                   'aeroplane', 'bicycle', 'bird', 'boat',
                   'bottle', 'bus', 'car', 'cat', 'chair',
                   'cow', 'diningtable', 'dog', 'horse',
                   'motorbike', 'person', 'pottedplant',
                   'sheep', 'sofa', 'train', 'tvmonitor')
    """
    """
    cfg.CLASSES = ('__background__', # always index 0
                     'pant', 'leggings/stocking', 'hat/cap', 'shoe',
                     'blouse', 'jumper', 'panty', 'knit', 'swimsuit',
                     'shirt', 'coat', 'one-piece', 'tshirt', 'cardigan',
                     'skirt', 'jacket')
    """
    cfg.CLASSES = ('__background__', # always index 0
      'bag', 'bra', 'jacket_coat', 'onepiece', 
      'pants', 'panty', 'shoes', 'skirt', 'swimwear', 
      'tshirts_shirts_blouse_hoody', 'vest', 'knit_cardigan')


    """
Exemplo n.º 5
0
def main():
    parser = argparse.ArgumentParser("Traing ALISC Clasification Network")
    parser.add_argument('--network_type', help='VGG16 or ZF', required=True)
    parser.add_argument('--model', help="Pretrained Model, could be empty")
    parser.add_argument('--gpu', help="GPU ID to use for training",
                        default=0)
    c = vars(parser.parse_args())

    network_type = c['network_type']

    print("Loading Configuration File...")
    cfg = cfg_from_file(osp.join(
        ROOT_DIR,
        'experiments', 'cfgs', 'alisc_classification.yml'))

    solver_fn = osp.join(
        ROOT_DIR,
        'models', 'alisc', network_type, 'solver.pt')

    caffe.set_mode_gpu()
    caffe.set_device(int(c['gpu']))
    solver = caffe.SGDSolver(solver_fn)

    if c['model']:
        weights_fn = c['model']
    else:
        weights_fn = osp.join(
            ROOT_DIR, 'data', 'faster_rcnn_models',
            '{}_faster_rcnn_final.caffemodel'.format(network_type))
    if not osp.exists(weights_fn):
        print("Pretrained Model {} does not exists".format(weights_fn))
        raise

    print("Loading pretrained model from {}".format(weights_fn))
    solver.net.copy_from(weights_fn)

    if DEBUG:
        code.interact(local=locals())
    print('Start Solving...')
    solver.solve()
def main():
    args = parse_args()

    print('Called with args:')
    print(args)

    cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    netdir,net_name = os.path.split(args.net)
    # --------------------------------------------------------------------------
    # Pycaffe doesn't reliably free GPU memory when instantiated nets are
    # discarded (e.g. "del net" in Python code). To work around this issue, each
    # training stage is executed in a separate process using
    # multiprocessing.Process.
    # --------------------------------------------------------------------------

    # queue for communicated results between processes
    mp_queue = mp.Queue()
    # solves, iters, etc. for each training stage
    solvers, max_iters, rpn_test_prototxt = get_solvers(args.net)

    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    print 'Stage 1 RPN, init from ImageNet model'
    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

    cfg.TRAIN.SNAPSHOT_INFIX = 'stage1'
    mp_kwargs = dict(
            queue=mp_queue,
            imdb_name=args.imdb_name,
            init_model=args.pretrained_model,
            solver=solvers[0],
            max_iters=max_iters[0],
            cfg=cfg)
    p = mp.Process(target=train_rpn, kwargs=mp_kwargs)
    p.start()
    rpn_stage1_out = mp_queue.get()
    p.join()

    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    print 'Stage 1 RPN, generate proposals'
    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

    mp_kwargs = dict(
            queue=mp_queue,
            imdb_name=args.imdb_name,
            rpn_model_path=str(rpn_stage1_out['model_path']),
            cfg=cfg,
            rpn_test_prototxt=rpn_test_prototxt)
    p = mp.Process(target=rpn_generate, kwargs=mp_kwargs)
    p.start()
    rpn_stage1_out['proposal_path'] = mp_queue.get()['proposal_path']
    p.join()

    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    print 'Stage 1 Fast R-CNN using RPN proposals, init from ImageNet model'
    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

    cfg.TRAIN.SNAPSHOT_INFIX = 'stage1'
    mp_kwargs = dict(
            queue=mp_queue,
            imdb_name=args.imdb_name,
            init_model=args.pretrained_model,
            solver=solvers[1],
            max_iters=max_iters[1],
            cfg=cfg,
            rpn_file=rpn_stage1_out['proposal_path'])
    p = mp.Process(target=train_fast_rcnn, kwargs=mp_kwargs)
    p.start()
    fast_rcnn_stage1_out = mp_queue.get()
    p.join()

    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    print 'Stage 2 RPN, init from stage 1 Fast R-CNN model'
    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

    cfg.TRAIN.SNAPSHOT_INFIX = 'stage2'
    mp_kwargs = dict(
            queue=mp_queue,
            imdb_name=args.imdb_name,
            init_model=str(fast_rcnn_stage1_out['model_path']),
            solver=solvers[2],
            max_iters=max_iters[2],
            cfg=cfg)
    p = mp.Process(target=train_rpn, kwargs=mp_kwargs)
    p.start()
    rpn_stage2_out = mp_queue.get()
    p.join()

    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    print 'Stage 2 RPN, generate proposals'
    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

    mp_kwargs = dict(
            queue=mp_queue,
            imdb_name=args.imdb_name,
            rpn_model_path=str(rpn_stage2_out['model_path']),
            cfg=cfg,
            rpn_test_prototxt=rpn_test_prototxt)
    p = mp.Process(target=rpn_generate, kwargs=mp_kwargs)
    p.start()
    rpn_stage2_out['proposal_path'] = mp_queue.get()['proposal_path']
    p.join()

    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    print 'Stage 2 Fast R-CNN, init from stage 2 RPN R-CNN model'
    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

    cfg.TRAIN.SNAPSHOT_INFIX = 'stage2'
    mp_kwargs = dict(
            queue=mp_queue,
            imdb_name=args.imdb_name,
            init_model=str(rpn_stage2_out['model_path']),
            solver=solvers[3],
            max_iters=max_iters[3],
            cfg=cfg,
            rpn_file=rpn_stage2_out['proposal_path'])
    p = mp.Process(target=train_fast_rcnn, kwargs=mp_kwargs)
    p.start()
    fast_rcnn_stage2_out = mp_queue.get()
    p.join()

    # Create final model (just a copy of the last stage)
    final_path = os.path.join(
            os.path.dirname(fast_rcnn_stage2_out['model_path']),
            net_name + '_faster_rcnn_final.caffemodel')
    print 'cp {} -> {}'.format(
            fast_rcnn_stage2_out['model_path'], final_path)
    shutil.copy(fast_rcnn_stage2_out['model_path'], final_path)
    print 'Final model: {}'.format(final_path)
Exemplo n.º 7
0
    textdetector = TextDetector()
    boxes = textdetector.detect(boxes, scores[:, np.newaxis], img.shape[:2])
    draw_boxes(img, image_name, boxes, scale)
    timer.toc()
    print(('Detection took {:.3f}s for '
           '{:d} object proposals').format(timer.total_time, boxes.shape[0]))



if __name__ == '__main__':
    if os.path.exists("data/results/"):
        shutil.rmtree("data/results/")
    os.makedirs("data/results/")

    cfg_from_file('ctpn/text.yml')

    # init session
    print ('Begin')
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)
    print ('aaaaaaaaaaaaa')
    # load network
    net = get_network("VGGnet_test")
    print ('bbbbbbbbbbbbb')
    # load model
    print(('Loading network {:s}... '.format("VGGnet_test")), end=' ')
    saver = tf.train.Saver()

    try:
        ckpt = tf.train.get_checkpoint_state(cfg.TEST.checkpoints_path)
Exemplo n.º 8
0
                                (i[2] - i[0]) / im_scale,
                                (i[3] - i[1]) / im_scale,
                                scores[id]))
    return [i + '\n' for i in lines]

if __name__ == '__main__':

    args = parse_args()

    caffemodel = caffe_model_dir

    if not os.path.isfile(caffemodel):
        raise IOError(('{:s} not found.').format(caffemodel))

    if cfg_file is not None:
        cfg_from_file(os.path.join(cfg.ROOT_DIR, cfg_file))
    cfg.GPU_ID = args.gpu_id

    if cpu:
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()
        caffe.set_device(args.gpu_id)
        cfg.GPU_ID = args.gpu_id
    print prototxt
    net = caffe.Net(prototxt, caffemodel, caffe.TEST)

    print '\n\nLoaded network {:s}'.format(caffemodel)

    fold_num=0
    # set up caffe
    caffe.set_mode_gpu()
    caffe.set_device(cfg.GPU_ID)


if __name__ == '__main__':

    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    print 'Stage 1 Fast R-CNN using RPN proposals, init from ImageNet model'
    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

    os.chdir(_init_paths.faster_rcnn_root)
    args = parse_args()
    scenario = Scenario().load(args.scenario_file)
    if scenario.config_path is not None:
        print 'loading config from ', scenario.config_path
        cfg_from_file(scenario.config_path)

    cfg.GPU_ID = scenario.gpu_id
    cfg.TRAIN.SNAPSHOT_INFIX = 'stage1'
    _init_caffe(cfg)

    output_dir = os.path.join(scenario.scen_dir, 'output')
    dir_exists_or_create(output_dir)
    train_fast_rcnn(imdb_name=scenario.train_imdb,
                    init_model=args.weights,
                    solver=scenario.models['stage1_fast_rcnn_solver'],
                    cfg=cfg,
                    rpn_file=args.roi_pkl,
                    output_dir=output_dir,
                    max_iters=scenario.max_iters[1])
        print 'Please the config file for training! (--cfg)'
        sys.exit()

    # load trainging config file
    train_cfg = parse_cfg_file(args.cfg_file)

    print('Using config:')
    pprint.pprint(train_cfg)

    # set conf
    if train_cfg.common.rpn_cfg:
        rpn_config.cfg_from_file(train_cfg.common.rpn_cfg)
        print('RPN using config:')
        pprint.pprint(rpn_config.cfg)
    if train_cfg.common.fast_rcnn_cfg:
        fast_rcnn_config.cfg_from_file(train_cfg.common.fast_rcnn_cfg)
        print('Fast-RCNN using config:')
        pprint.pprint(fast_rcnn_config.cfg)

    # set up caffe
    if not args.randomize:
        # fix the random seeds (numpy and caffe) for reproducibility
        np.random.seed(rpn_config.cfg.RNG_SEED)
        caffe.set_random_seed(rpn_config.cfg.RNG_SEED)

    if args.cpu_mode:
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()
        caffe.set_device(args.gpu_id)
        dropout_lstm=_CONFIG['dropout_lstm'],
        dropout_word_embedding=_CONFIG['dropout_word_embedding'],
        l2_weight=_CONFIG['l2_weight'],
        block_unnecessary_tokens=_CONFIG['block_unnecessary_tokens'],
        device=_CONFIG['device'])

    if _CONFIG['load_path_cg'] is not None:
        print("Starting from PATH", _CONFIG['load_path_cg'])
        cg.load(checkpoint_path=_CONFIG['load_path_cg'],
                load_optimizer=_CONFIG['load_cg_optimizer'])

    return cg


if __name__ == '__main__':
    _CONFIG = parse_parameters(sys.argv[1:])

    # Load the Bottom-Up net
    cfg_from_file(_CONFIG['bunet_config_path'])
    bunet = BottomUp()
    bunet.load_net(layout_path=_CONFIG['bunet_layout_path'],
                   weights_path=_CONFIG['bunet_weights_path'],
                   device=_CONFIG['device'])

    batch = bunet.preprocess_inputs(examples_dir=_CONFIG['examples_dir'])

    cg = _create_caption_generator()
    cg.set_mode(mode='inference')

    generate_captions(caption_generator=cg, batch=batch)
Exemplo n.º 12
0
def test(args=None):
	parser = argparse.ArgumentParser(description='Simple testing script.')
	parser.add_argument('--net_final', help='the pretrained model', type=str, default='../output/faster_rcnn_end2end/voc_2007_trainval+voc_2012_trainval/VGGnet_wt_context/VGGnet_wt_context.ckpt')
	parser.add_argument('--net_name', help='net_name', type=str, default="VGGnet")
	parser.add_argument('--test_set', help='train set', type=str, default="voc_2007_test")
	parser = parser.parse_args(args)
	test_data = parser.test_set 
	net_name = parser.net_name
	net_final = parser.net_final

	# get dataset
	extra_config = "../experiments/cfgs/faster_rcnn_end2end.yml"
	cfg_from_file(extra_config)
	imdb = get_imdb(test_data)
	imdb.competition_mode(True) # use_salt: False; cleanup: False
	weights_filename = os.path.splitext(os.path.basename(net_final))[0]
	output_dir = get_output_dir(imdb,weights_filename)

	# start a session
	net = get_network(net_name+"_test")
	saver = tf.train.Saver()
	config = tf.ConfigProto(allow_soft_placement=True)
	sess = tf.Session(config=config)
	saver.restore(sess, net_final)
	print('Loading model weights from {:s}'.format(net_final))
	fetch_list = [net.get_output('cls_prob'),
				net.get_output('bbox_pred'),
				net.get_output('rois')]

	assert len(cfg.TEST.SCALES) == 1
	target_size = cfg.TEST.SCALES[0]
	max_per_image = 300
	thresh = 0.05

	num_images = len(imdb.image_index)
	all_boxes = [[[] for _ in xrange(num_images)]
					for _ in xrange(imdb.num_classes)]
	# all detections are collected into:
	# all_boxes[cls][image] = N*5 (x1,y1,x2,y2,score)

	_t = {'im_detect':Timer(), 'misc':Timer()}
	for i in xrange(num_images):

		im_cv = cv2.imread(imdb.image_path_at(i))
		im = im_cv.astype(np.float32, copy=True) 
		im -= cfg.PIXEL_MEANS
		im_size_min = np.min(im.shape[0:2])
		im_size_max = np.max(im.shape[0:2])
		im_scale = min([float(target_size) / im_size_min,
						float(cfg.TEST.MAX_SIZE) / im_size_max])
		# the biggest axis should not be more than MAX_SIZE
		im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale,
					interpolation=cv2.INTER_LINEAR)
		im_info = np.array([im.shape[0],im.shape[1],im_scale],
						dtype=np.float32)
		
		_t['im_detect'].tic()
		feed_dict = {net.data: np.expand_dims(im, axis=0),
					net.im_info: np.expand_dims(im_info, axis=0),
					#net.keep_prob: 1.0}
					net.appearance_drop_rate: 0.0000000001}

		cls_prob, box_deltas, rois = sess.run(fetch_list,
					feed_dict=feed_dict)

		scores = cls_prob
		boxes = rois[:,1:5] / im_scale # scale first??
		boxes = bbox_transform_inv(boxes, box_deltas)
		boxes = clip_boxes(boxes, im_cv.shape)
		_t['im_detect'].toc()

		_t['misc'].tic()
		# skip j = 0, since it is the background class
		for j in xrange(1, imdb.num_classes):
			inds = np.where(scores[:,j] > thresh)[0] 
			# which boxes has object belongs to class j
			cls_scores = scores[inds,j] # [num_box]
			cls_boxes = boxes[inds,j*4:(j+1)*4] # [num_box,4]
			cls_dets = np.hstack( (cls_boxes, cls_scores[:,np.newaxis])) \
						.astype(np.float32, copy=False) #[num_box, 4 + score]
			keep = nms(cls_dets, cfg.TEST.NMS)
			cls_dets = cls_dets[keep,:]
			all_boxes[j][i] = cls_dets 
			# j class, exists on image i, and have one or more boxes
		# limt to max_per_image detection *over all classes*
		image_scores = np.hstack([all_boxes[j][i][:,-1]
						for j in xrange(1, imdb.num_classes)])
		if len(image_scores) > max_per_image:
			image_thresh = np.sort(image_scores)[-max_per_image]
			for j in xrange(1, imdb.num_classes):
				keep = np.where(all_boxes[j][i][:,-1] >= image_thresh)[0]
				all_boxes[j][i] = all_boxes[j][i][keep,:]
		_t['misc'].toc()
		print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s'\
			.format(i+1, num_images, _t['im_detect'].average_time, 
				_t['misc'].average_time))

	det_file = os.path.join(output_dir, "detections.pkl")
	with open(det_file, 'wb') as f:
		cPickle.dump(all_boxes, f, cPickle.HIGHEST_PROTOCOL)
	print 'Evaluating detections'
	imdb.evaluate_detections(all_boxes, output_dir)
		
	print(net_final)
        return roidb

    roidbs = [get_roidb(s) for s in imdb_names.split('+')]
    roidb = roidbs[0]
    if len(roidbs) > 1:
        for r in roidbs[1:]:
            roidb.extend(r)
        imdb = datasets.imdb.imdb(imdb_names)
    else:
        imdb = get_imdb(imdb_names)
    return imdb, roidb

if __name__ == '__main__':

    if CFG_FILE is not None:
        cfg_from_file(CFG_FILE)
    if SET_CFGS is not None:
        cfg_from_list(SET_CFGS)

    print('Using config:')
    pprint.pprint(cfg)

    if not RANDOMIZE:
        # fix the random seeds (numpy and caffe) for reproducibility
        np.random.seed(cfg.RNG_SEED)
        caffe.set_random_seed(cfg.RNG_SEED)

    # set up caffe
    caffe.set_mode_cpu()

    imdb, roidb = combined_roidb(IMDB_NAME)
Exemplo n.º 14
0
def train(args=None):
    parser = argparse.ArgumentParser(description='Simple training script.')
    parser.add_argument('--net_name',
                        help='net_name',
                        type=str,
                        default="VGGnet_wo_context")
    parser.add_argument('--train_set',
                        help='train set',
                        type=str,
                        default="voc_2007_trainval+voc_2012_trainval")
    parser.add_argument('--net_pretrained',
                        help='the pretrained model',
                        type=str,
                        default='../data/pretrain_model/VGG_imagenet.npy')
    parser.add_argument(
        '--iter_start',
        help='skip the first few iterations, relates to checkpoint',
        type=int,
        default=0)
    parser.add_argument('--max_iters',
                        help='max number of iterations to run',
                        type=int,
                        default=350000)

    parser = parser.parse_args(args)

    train_set = parser.train_set
    net_name = parser.net_name
    net_pretrained = parser.net_pretrained
    iter_start = parser.iter_start
    max_iters = parser.max_iters

    # some configuration
    extra_config = "../experiments/cfgs/faster_rcnn_end2end.yml"
    cfg_from_file(extra_config)
    np.random.seed(cfg.RNG_SEED)

    print("[preparing the dataset...]")
    cache_path = './db_cache/' + train_set
    if not os.path.exists(cache_path):
        os.makedirs(cache_path)
    cache_file_1 = os.path.join(cache_path, 'roidb.pkl')

    if os.path.exists(cache_file_1):
        with open(cache_file_1, 'rb') as fid:
            roidb = cPickle.load(fid)
            print 'roidb loaded from {}'.format(cache_file_1)
        imdb = datasets.imdb(train_set)

    else:
        imdb, roidb = combined_roidb(train_set)
        """
		each entry rois[index] =
			{image: image_path,
			width: scaler
			height: scaler
			boxes: [num_box, 4], absolute pixel, 0-based ! no background boxes
			gt_classes: [num_box], the ground-truth class for each box
			gt_overlaps: [num_box, num_class] one-hot verson of gt_classes 
			flipped: True/False
			max_classes: exactly the gt_class [num_box] why we have this?
			max_overlaps:  all one vector [num_box] why we have this?
			}
		"""
        roidb = filter_roidb(roidb)
        with open(cache_file_1, 'wb') as fid:
            cPickle.dump(roidb, fid, cPickle.HIGHEST_PROTOCOL)
            print 'wrote roidb to {}'.format(cache_file_1)
    if 'coco' in train_set:
        num_classes = 81
    elif 'voc' in train_set:
        num_classes = 21
    if cfg.TRAIN.BBOX_REG:
        # calculate roidb.roidb[im_id].['bbox_target'] [label, tx, ty, tw, th]
        # calculate mean and std, and apply on (tx, ty, tw, th)
        print(" computing bounding-box regression targets ...")
        bbox_means, bbox_stds = add_bbox_regression_targets(roidb)
        print("Done")

    data_layer = get_data_layer(roidb, num_classes)  # for db+db, num_class=0!
    # a layer used to get next mini batch

    print("[build the graph...]")
    net = get_network(net_name +
                      "_train")  # chang n_classes at VGGnet_train.py
    saver = tf.train.Saver(max_to_keep=10, write_version=saver_pb2.SaverDef.V2)
    rpn_cls_loss = get_rpn_cls_loss(net)
    rpn_box_loss = get_rpn_box_loss(net)
    RCNN_cls_loss = get_RCNN_cls_loss(net)
    RCNN_box_loss = get_RCNN_box_loss(net)
    loss = rpn_cls_loss + rpn_box_loss + RCNN_cls_loss + RCNN_box_loss
    global_step = tf.Variable(iter_start, trainable=False)
    lr = tf.train.exponential_decay(cfg.TRAIN.LEARNING_RATE,
                                    global_step,
                                    cfg.TRAIN.STEPSIZE,
                                    0.1,
                                    staircase=True)  # /2 change
    momentum = cfg.TRAIN.MOMENTUM
    optimizer = tf.train.MomentumOptimizer(lr, momentum)
    train_op = optimizer.minimize(loss, global_step=global_step)
    fetch_list = [
        lr, rpn_cls_loss, rpn_box_loss, RCNN_cls_loss, RCNN_box_loss, train_op
    ]
    gvs = optimizer.compute_gradients(loss)
    summary_op = summary(gvs, loss, RCNN_cls_loss, RCNN_box_loss, rpn_cls_loss,
                         rpn_box_loss)

    print("trainable variables are:")
    for var in tf.trainable_variables():
        print(var)

    print("[build the session and set model&summary save helper]")
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)
    output_dir = get_output_dir(imdb, net_name)
    tb_dir = get_output_tb_dir(imdb, net_name)
    train_writer = tf.summary.FileWriter(tb_dir, sess.graph)

    print("[initalize the graph paremater & restore from pre-trained file")
    sess.run(tf.global_variables_initializer())
    if net_pretrained is not None:
        print(
            'loding pretrained model weights from {:s}'.format(net_pretrained))
        try:
            net.load(net_pretrained, sess, saver, True)
        except:
            optimistic_restore(sess, net_pretrained)
    """
	print("variables in the pretrained file are:")
	print_tensors_in_checkpoint_file(file_name=net_pretrained, 
									tensor_name='',
									all_tensors = False,
									all_tensor_names = True)
	#saver.restore(sess, net_pretrained)
	"""

    for iter in xrange(iter_start, max_iters):
        try:
            blobs = data_layer.forward()
            feed_dict = {
                net.data:
                blobs['data'],  # [num_image, max_height, max_width, 3]
                net.im_info: blobs['im_info'],  # [height, width, im_scale]
                net.gt_boxes: blobs['gt_boxes']
            }  # [x1, y1, x2, y2, cls]

            if (iter + 1) % 1000 == 0:
                summary_w = sess.run(summary_op, feed_dict)
                train_writer.add_summary(summary_w, iter)

                filename = (net_name + '_iter_{:d}'.format(iter + 1) + '.ckpt')
                filename = os.path.join(output_dir, filename)
                snapshot(sess, saver, filename, net, bbox_means, bbox_stds)
                print("Wrote snapshot to: {:s}".format(filename))

            cur_lr, rpn_cls_loss_value, rpn_box_loss_value, RCNN_cls_loss_value, RCNN_box_loss_value, _ = \
             sess.run(fetch_list, feed_dict=feed_dict)
            total_loss = rpn_cls_loss_value + rpn_box_loss_value + RCNN_cls_loss_value + RCNN_box_loss_value

            if (iter + 1) % cfg.TRAIN.DISPLAY == 0:
                print(('iter %d/%d with lr %.6f: total loss is %.4f.       (rpn_cls_loss: %.4f, rpn_box_loss: %.4f, ' +
                    'RCNN_cls_loss: %.4f, RCNN_box_loss: %.4f)') % (iter + 1, max_iters, cur_lr, total_loss, \
                                rpn_cls_loss_value, rpn_box_loss_value,
                                RCNN_cls_loss_value, RCNN_box_loss_value))

        except:  # ZeroDivisionError as err:
            # print('Handling run-time error:', err)
            print('ignore current iteration')
Exemplo n.º 15
0
    args = parser.parse_args()

    return args


if __name__ == '__main__':

    cfg.TEST.HAS_RPN = True  # Use RPN for proposals

    args = parse_args()

    import glob
    cfg_file = glob.glob('*.yml')

    assert len(cfg_file) == 1, 'Too many .cfg files.'
    cfg_from_file(cfg_file[0])

    CLASSES = cfg.CLASSES

    print 'Classes: ', CLASSES

    prototxt = args.demo_net
    caffemodels = glob.glob('snapshots/*.caffemodel')

    import re
    try:
        caffemodel = [
            model for model in caffemodels
            if int(re.search('iter_(\d+).caffemodel', model).group(1)) ==
            args.demo_iter
        ]
Exemplo n.º 16
0
def load_net(cfg_file, net_pt, net_weight):
    cfg_from_file(cfg_file)
    net = caffe.Net(net_pt, net_weight, caffe.TEST)
    return net
Exemplo n.º 17
0
def main(params):

    sys.path.insert(0, os.path.join(params.bottomup_path, 'lib'))
    from fast_rcnn.config import cfg, cfg_from_file
    from fast_rcnn.test import im_detect, _get_blobs
    from fast_rcnn.nms_wrapper import nms

    ###########################
    # CNN : Faster-RCNN setting
    data_path = os.path.join(params.bottomup_path, 'data/genome/1600-400-20')

    # Load classes
    classes = ['__background__']
    with open(os.path.join(data_path, 'objects_vocab.txt')) as f:
        for object in f.readlines():
            classes.append(object.split(',')[0].lower().strip())

    # Load attributes
    attributes = ['__no_attribute__']
    with open(os.path.join(data_path, 'attributes_vocab.txt')) as f:
        for att in f.readlines():
            attributes.append(att.split(',')[0].lower().strip())

    GPU_ID = params.gpu_id  # if we have multiple GPUs, pick one
    caffe.init_log()
    caffe.set_device(GPU_ID)
    caffe.set_mode_gpu()
    net = None
    cfg_from_file(
        os.path.join(params.bottomup_path,
                     'experiments/cfgs/faster_rcnn_end2end_resnet.yml'))

    weights = os.path.join(
        params.bottomup_path,
        'data/faster_rcnn_models/resnet101_faster_rcnn_final.caffemodel')
    prototxt = os.path.join(
        params.bottomup_path,
        'models/vg/ResNet-101/faster_rcnn_end2end_final/test.prototxt')

    net = caffe.Net(prototxt, caffe.TEST, weights=weights)

    conf_thresh = 0.4
    min_boxes = params.num_objects
    max_boxes = params.num_objects
    ###########################

    ###########################
    # RNN : Caption generation setting
    # load json file
    label_info = json.load(open(params.input_labels))
    word_to_idx = label_info['word_to_idx']

    # load h5 file
    caps_info = h5py.File(params.input_caps, 'r', driver='core')
    seq_length = caps_info['labels'].shape[1]

    # GPU options
    config = tf.ConfigProto(allow_soft_placement=True)
    config.gpu_options.allow_growth = True

    # build a graph to sample captions
    graph_gen_cap = tf.Graph()
    sess_gen_cap = tf.Session(graph=graph_gen_cap, config=config)
    with graph_gen_cap.as_default():
        model = CaptionGenerator(word_to_idx,
                                 num_features=params.num_objects,
                                 dim_feature=params.dim_features,
                                 dim_embed=params.dim_word_emb,
                                 dim_hidden=params.rnn_hid_size,
                                 dim_attention=params.att_hid_size,
                                 n_time_step=seq_length - 1)
        alphas, sampled_captions = model.build_sampler(max_len=params.max_len)
        saver1 = tf.train.Saver()
        saver1.restore(sess_gen_cap, params.test_model)
    tf.reset_default_graph()
    ############################

    ###########################
    # Face : Replacer
    name_replacer = NameReplacer(model.idx_to_word, params.score_thr)
    ############################

    ###########################
    # Run Image Captioning with face detection

    while True:
        full_fname = raw_input("Enter the image path and name:")
        if full_fname == 'Exit':
            break
        if not os.path.exists(full_fname):
            print("Not Exist File : {}".format(full_fname))
            continue

        ###########################
        # Object Detection
        im = cv2.imread(full_fname)
        scores, boxes, attr_scores, rel_scores = im_detect(net, im)

        # Keep the original boxes, don't worry about the regression bbox outputs
        rois = net.blobs['rois'].data.copy()
        # unscale back to raw image space
        blobs, im_scales = _get_blobs(im, None)

        cls_boxes = rois[:, 1:5] / im_scales[0]
        cls_prob = net.blobs['cls_prob'].data
        attr_prob = net.blobs['attr_prob'].data
        pool5 = net.blobs['pool5_flat'].data

        # Keep only the best detections
        max_conf = np.zeros((rois.shape[0]))
        for cls_ind in range(1, cls_prob.shape[1]):
            cls_scores = scores[:, cls_ind]
            dets = np.hstack(
                (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
            keep = np.array(nms(dets, cfg.TEST.NMS))
            max_conf[keep] = np.where(cls_scores[keep] > max_conf[keep],
                                      cls_scores[keep], max_conf[keep])

        keep_boxes = np.where(max_conf >= conf_thresh)[0]
        if len(keep_boxes) < min_boxes:
            keep_boxes = np.argsort(max_conf)[::-1][:min_boxes]
        elif len(keep_boxes) > max_boxes:
            keep_boxes = np.argsort(max_conf)[::-1][:max_boxes]

        feats = pool5[keep_boxes]
        ############################

        ###########################
        # Caption generation using CNN features
        feed_dict = {model.features: [feats]}
        alps, sam_cap = sess_gen_cap.run([alphas, sampled_captions], feed_dict)
        decoded = decode_captions(sam_cap, model.idx_to_word)
        ############################

        ###########################
        # Name replacer
        name_list, conf_list, roi_list = vtt_face_recognize(
            full_fname, params.url, params.post_data)
        replace_decoded, words = name_replacer.name_replace_caps(
            sam_cap, alps, cls_boxes, name_list, conf_list, roi_list)
        print("Original caption : %s" % decoded[0])
        print("Replaced caption : %s" % replace_decoded[0])
        ############################

        ###########################
        # Showing
        img = skimage.io.imread(full_fname)
        img = skimage.img_as_float(img)
        boxes = cls_boxes[keep_boxes]
        boxes = boxes.astype(int)

        # draw attention map
        fig = plt.figure(figsize=(16, 8))
        ax = fig.add_subplot(3, 6, 1)
        ax.imshow(img)
        plt.axis('off')

        # Plot images with attention weights
        words = words[0]
        for t in range(len(words)):
            if t > 16:
                break
            if words[t] == '<BLANK>':
                continue
            alphamap = np.zeros((img.shape[0], img.shape[1]))
            for b in range(boxes.shape[0]):
                alphamap[boxes[b, 1]:boxes[b, 3],
                         boxes[b, 0]:boxes[b, 2]] += alps[0, t, b]
            max_idx = np.argmax(alps[0, t, :])
            att_img = np.dstack((img, alphamap))
            ax = fig.add_subplot(3, 6, t + 2)
            plt.text(0,
                     1,
                     '%s' % (words[t]),
                     color='black',
                     backgroundcolor='white',
                     fontsize=8)
            ax.imshow(att_img)
            ax.add_patch(
                patches.Rectangle((boxes[max_idx, 0], boxes[max_idx, 1]),
                                  boxes[max_idx, 2] - boxes[max_idx, 0],
                                  boxes[max_idx, 3] - boxes[max_idx, 1],
                                  linewidth=1,
                                  edgecolor='r',
                                  facecolor='none'))
            plt.axis('off')

        fig.tight_layout()
        plt.show()
Exemplo n.º 18
0
def get_p_set(im_set,
              im_list,
              save_dir,
              attack_type='miscls',
              net_name="VGGnet_wo_context"):
    if not os.path.isdir(save_dir):
        os.makedirs(save_dir)
    if attack_type == 'appear':
        perturbation_name = '/'.join([
            save_dir,
            'im{:d}_box({:1.0f}-{:1.0f}-{:1.0f}-{:1.0f})_iou{:1.3f}_t{:d}'
        ])
    else:
        perturbation_name = '/'.join([save_dir, 'im{:d}_box{:d}_f{:d}_t{:d}'])

    # some configuration
    extra_config = "../experiments/cfgs/faster_rcnn_end2end.yml"
    cfg_from_file(extra_config)

    # prepare data
    imdb = prepare_dataset(im_set, cfg)
    # prepare graph and sess
    sess, net, grad = build_digital_adv_graph(net_name, im_set)

    num_images = len(im_list)
    _t = Timer()
    for idx, i in enumerate(im_list):
        im_cv, im, im_info, gt_boxes = get_image_prepared(cfg, imdb.roidb[idx])
        num_gt_boxes = len(gt_boxes)
        _t.tic()
        for box_id in range(num_gt_boxes):
            valid = is_valid_wo_context(
                im_cv,
                im,
                im_info,
                gt_boxes[box_id],
                f_id=int(gt_boxes[box_id][-1]))  # changed here
            if not valid:
                break
        if not valid:
            print(
                "ignore the image since at least one object is not detected correctly"
            )
            continue
        if attack_type == 'appear':
            ori_gt_boxes = gt_boxes
            new_gt_boxes, iou_list = generate_appear_box(
                im_info, gt_boxes)  # (x1, y1, x2, y2, gt_cls=0)
            num_iter = len(new_gt_boxes)
        else:
            num_iter = num_gt_boxes

        for box_id in range(num_iter):
            new_box_id = box_id
            if attack_type == 'appear':
                box_id = -1
                gt_boxes = np.concatenate([
                    ori_gt_boxes,
                    np.expand_dims(new_gt_boxes[new_box_id], axis=0)
                ])
            gt_cls = int(gt_boxes[box_id, -1])
            gt_cls_name = imdb._classes[gt_cls]
            for target_cls, target_cls_name in enumerate(imdb._classes):
                if attack_type == 'hiding' and target_cls != 0:
                    continue
                elif target_cls == int(gt_boxes[box_id, -1]):
                    continue
                elif attack_type != 'hiding' and target_cls == 0:
                    continue
                mask = create_mask(im_info[:2], gt_boxes[box_id, :4])
                original_id = gt_boxes[box_id, -1]
                gt_boxes[box_id, -1] = target_cls
                p = get_p_box_IFGSM(net, im_cv, im, im_info, gt_boxes,
                                    original_id, box_id, mask, sess, grad)
                # p = get_p_box_FGSM(net,im_cv, im, im_info, gt_boxes, original_id, box_id, mask, sess, grad)

                gt_boxes[box_id, -1] = gt_cls
                if p is not None:
                    if attack_type == 'appear':
                        save_name = perturbation_name.format(
                            i, gt_boxes[box_id, 0], gt_boxes[box_id, 1],
                            gt_boxes[box_id, 2], gt_boxes[box_id, 3],
                            iou_list[box_id], target_cls)
                    else:
                        save_name = perturbation_name.format(
                            i, box_id, gt_cls, target_cls)
                    p = np.int32(p)

                    block_matrix.save(save_name, p, im_info[:2],
                                      gt_boxes[box_id, :4])
                    print("{:s} --> {:s} succeed." \
                          .format(gt_cls_name, imdb._classes[target_cls]))
                else:
                    print("{:s} --> {:s} failed." \
                          .format(gt_cls_name, imdb._classes[target_cls]))
        _t.toc()
        print('perturbation_generation: {:d}/{:d} {:.3f}s' \
              .format(idx + 1, num_images, _t.average_time))
attributes = ['__no_attribute__']
with open(os.path.join(data_path, 'attributes_vocab.txt')) as f:
    for att in f.readlines():
        attributes.append(att.split(',')[0].lower().strip())

# Check object extraction
from fast_rcnn.config import cfg, cfg_from_file
from fast_rcnn.test import im_detect, _get_blobs
from fast_rcnn.nms_wrapper import nms
import cv2

GPU_ID = 0  # if we have multiple GPUs, pick one
caffe.set_device(GPU_ID)
caffe.set_mode_gpu()
net = None
cfg_from_file('experiments/cfgs/faster_rcnn_end2end_resnet.yml')

weights = 'data/faster_rcnn_models/resnet101_faster_rcnn_final.caffemodel'
prototxt = 'models/vg/ResNet-101/faster_rcnn_end2end_final/test.prototxt'

net = caffe.Net(prototxt, caffe.TEST, weights=weights)

conf_thresh = 0.4
min_boxes = 16
max_boxes = 16

data_dir = '../data/test'
data_classes = ['test']
save_dir = data_dir + '_new_attentions'

#im_file = 'data/group/pos_34.jpg'
Exemplo n.º 20
0
                        dest='cfg_path',
                        help='config file to use for demo',
                        default='../experiments/cfgs/faster_rcnn_end2end.yml')

    args = parser.parse_args()

    return args


if __name__ == '__main__':
    cfg.TEST.HAS_RPN = True  # Use RPN for proposals

    args = parse_args()

    # Load config file
    cfg_from_file(args.cfg_path)

    # Load category IDs from the config file
    category_ids = cfg.TRAIN.CAT_IDS

    # Amount of random test images per category
    sample_img_count = 3

    # list up all classes that you want the demo run on. Do not forget to include __background__ as additional class.
    # If you do not know the correct string of your categories, check data/MSCOCO_API_categories.py or better
    # use create_class_tuple() from tools/customToolbox.py
    # CLASSES = ('__background__', 'person', 'car', 'dog', 'horse')

    CLASSES = customToolbox.create_class_tuple(category_ids)
    print "Classes: " + str(CLASSES)
Exemplo n.º 21
0
    parser.add_argument("--snapshot_dir", help="caffe model params file", type=str)
    parser.add_argument("--save_dir", help="location to save weight files", type=str)
    args = parser.parse_args()
    os.chdir(args.snapshot_dir)
    if not osp.exists(args.save_dir):
        os.makedirs(args.save_dir)

    # Objects for logging solver training
    _train_loss = []
    _weight_params = {}

    start = args.start
    stop = args.stop
    timestr = 'snaps_{0}_{1}'.format(start, stop)
    
    cfg_from_file('/ltmp/gustavo-2951t/dd_cv/py-faster-rcnn/experiments/cfgs/faster_rcnn_end2end.yml')

    if (cfg.TRAIN.HAS_RPN and cfg.TRAIN.BBOX_REG and cfg.TRAIN.BBOX_NORMALIZE_TARGETS):
        # RPN can only use precomputed normalization because there are n
	# fixed statistics to compute a priori
        assert cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED


    import numpy as np
    import matplotlib.pyplot as plt

    imdb, roidb = combined_roidb('voc_2007_test')
    for itt in range(start, stop+1, args.iter):
        # TODO: change this to the name of your default solver file and shapshot file
	
	if(cfg.TRAIN.HAS_RPN and cfg.TRAIN.BBOX_REG and cfg.TRAIN.BBOX_NORMALIZE_TARGETS):
Exemplo n.º 22
0
 def getDetectionData(self,
                      modelName,
                      prototxtName,
                      cls_order,
                      cls_mapper,
                      gpu_id,
                      testSets,
                      datasetName="openImages_v4"):
     imgIdx = []
     GT = []
     CLASSES = self.processClass(cls_order, cls_mapper)
     name = "eval"
     for target in testSets:
         dataset = openImageData(name, cls_order, cls_mapper, sets=target)
         for i in dataset._image_index:
             target_img = "/root/data/data-{}/{}/{}.jpg".format(
                 datasetName, target, i)
             imgIdx += [target_img]
             label = dataset._load_boxes(i)['gt_classes']
             bbox = dataset._load_boxes(i)['boxes']
             for j, k in zip(label, bbox):
                 GT.append(
                     ([target_img, k[0], k[1], k[2], k[3], CLASSES[j]]))
     GTdata = {}
     gtCounterPerCls = {}
     for re in GT:
         if gtCounterPerCls.has_key(re[5]):
             gtCounterPerCls[re[5]] += 1
         else:
             gtCounterPerCls[re[5]] = 1
         hit = {"label": re[5], "bbox": [x for x in re[1:5]], "used": False}
         if GTdata.has_key(re[0]):
             GTdata.get(re[0]).append(hit)
         else:
             GTdata[re[0]] = []
             GTdata.get(re[0]).append(hit)
     weightPath = modelName  #"/root/pva-faster-rcnn/models/output/{}/{}_iter_{}.caffemodel".format(modelName,modelName,str(iteration))
     modelPath = prototxtName  #"/root/pva-faster-rcnn/models/pvanet/lite/{}_test.prototxt".format(modelName)
     print weightPath
     print modelPath
     caffe.set_mode_gpu()
     caffe.set_device(gpu_id)
     cfg_from_file("models/pvanet/cfgs/submit_0716.yml")
     cfg.GPU_ID = gpu_id
     det = {}
     totalImg = len(imgIdx)
     if not os.path.isfile(weightPath):
         raise IOError(('Caffemodel: {:s} not found').format(weightPath))
     net = caffe.Net(modelPath, weightPath, caffe.TEST)
     print '\n\nLoaded network {:s}'.format(modelPath)
     print "Total testing images: {}".format(len(imgIdx))
     for idx, targetImg in enumerate(imgIdx):
         timer = Timer()
         timer.tic()
         _t = {
             'im_preproc': Timer(),
             'im_net': Timer(),
             'im_postproc': Timer(),
             'misc': Timer()
         }
         res = self.detect(CLASSES, net, targetImg, 15)
         timer.toc()
         print "Processing: {}/{},time usage is {:.3f}s".format(
             idx, totalImg, timer.average_time)
         for re in res:
             hit = {
                 "confidence": re[5],
                 "fileName": re[0],
                 "bbox": [x for x in re[1:5]]
             }
             if det.has_key(re[6]):
                 det.get(re[6]).append(hit)
             else:
                 det[re[6]] = []
                 det.get(re[6]).append(hit)
     for i in CLASSES:
         if det.has_key(i) == False:
             det[i] = []
     return det, GTdata, gtCounterPerCls
Exemplo n.º 23
0
import time, os, sys, pprint, cPickle
from unrealcv import unrealcv

__sets = dict()
for el in range(0, 61, 30):
    for az in range(90, 271, 45):
        __sets['unrealcv_%d_%d' % (el, az)] = (lambda el=el, az=az: unrealcv('RealisticRendering', '%d_%d' % (el, az)))

def get_imdb(name):
    """Get an imdb (image database) by name."""
    if not __sets.has_key(name):
        raise KeyError('Unknown dataset: {}'.format(name))
    return __sets[name]()

cfg_file = '../experiments/cfgs/faster_rcnn_alt_opt.yml'
cfg_from_file(cfg_file)
cfg.GPU_ID = 0

max_per_image = 100

print('Using config:')
pprint.pprint(cfg)

caffemodel = '../data/faster_rcnn_models/VGG16_faster_rcnn_final.caffemodel'

wait = 1
while not os.path.exists(caffemodel) and wait:
    print('Waiting for {} to exist...'.format(args.caffemodel))
    time.sleep(10)

        print 'Please the config file for training! (--cfg)'
        sys.exit()

    # load trainging config file
    train_cfg = parse_cfg_file(args.cfg_file)

    print('Using config:')
    pprint.pprint(train_cfg)

    # set conf
    if train_cfg.common.rpn_cfg:
        rpn_config.cfg_from_file(train_cfg.common.rpn_cfg)
        print('RPN using config:')
        pprint.pprint(rpn_config.cfg)
    if train_cfg.common.fast_rcnn_cfg:
        fast_rcnn_config.cfg_from_file(train_cfg.common.fast_rcnn_cfg)
        print('Fast-RCNN using config:')
        pprint.pprint(fast_rcnn_config.cfg)

    # set up caffe
    if not args.randomize:
        # fix the random seeds (numpy and caffe) for reproducibility
        np.random.seed(rpn_config.cfg.RNG_SEED)
        caffe.set_random_seed(rpn_config.cfg.RNG_SEED)
    
    if args.cpu_mode:
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()
        caffe.set_device(args.gpu_id)
Exemplo n.º 25
0
    parser.add_argument('--cpu',
                        dest='cpu_mode',
                        help='Use CPU mode (overrides --gpu)',
                        action='store_true')
    args = parser.parse_args()

    return args


if __name__ == '__main__':
    cfg.TEST.HAS_RPN = True  # Use RPN for proposals

    args = parse_args()
    net = None
    cfg_from_file(
        '/media/sadaf/e4da0f25-29be-4c9e-a432-3193ff5f5baf/Code/bottom-up-attention/experiments/cfgs/faster_rcnn_end2end_resnet.yml'
    )

    caffemodel = '/media/sadaf/e4da0f25-29be-4c9e-a432-3193ff5f5baf/Code/bottom-up-attention/data/faster_rcnn_models/resnet101_faster_rcnn_final.caffemodel'
    prototxt = '/media/sadaf/e4da0f25-29be-4c9e-a432-3193ff5f5baf/Code/bottom-up-attention/models/vg/ResNet-101/faster_rcnn_end2end_final/test.prototxt'

    if not os.path.isfile(caffemodel):
        raise IOError(('{:s} not found.\nDid you run ./data/script/'
                       'fetch_faster_rcnn_models.sh?').format(caffemodel))

    if args.cpu_mode:
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()
        caffe.set_device(args.gpu_id)
        cfg.GPU_ID = args.gpu_id
Exemplo n.º 26
0
def __load_net(net_path):
    global model_path, cfg_path
    cfg_from_file(cfg_path)
    caffe.set_mode_gpu()
    caffe.set_device(0)
    return caffe.Net(str(net_path), str(model_path), caffe.TEST)
Exemplo n.º 27
0
    args = parser.parse_args()

    return args


if __name__ == '__main__':
    cfg.TEST.HAS_RPN = True  # Use RPN for proposals

    args = parse_args()

    prototxt = args.test_prototxt
    caffemodel = args.demo_net
    cfg_path = args.cfg_path
    if cfg_path is not None:
        cfg_from_file(cfg_path)

    if not os.path.isfile(caffemodel):
        raise IOError(('{:s} not found.\nDid you run ./data/script/'
                       'fetch_faster_rcnn_models.sh?').format(caffemodel))

    if args.cpu_mode:
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()
        # caffe.set_device(args.gpu_id)
        # cfg.GPU_ID = args.gpu_id
    net = caffe.Net(prototxt, caffemodel, caffe.TEST)

    print '\n\nLoaded network {:s}'.format(caffemodel)
Exemplo n.º 28
0
rand_seed = 1024
save_name = '{}_{}'
max_per_image = 300
thresh = 0.0001
visualize = False

# ------------

if rand_seed is not None:
    np.random.seed(rand_seed)

if rand_seed is not None:
    np.random.seed(rand_seed)

# load config
cfg_from_file(cfg_file)


def vis_detections(im, class_name, dets, thresh=0.1):
    """Visual debugging of detections."""
    for i in range(np.minimum(10, dets.shape[0])):
        bbox = tuple(int(np.round(x)) for x in dets[i, :4])
        score = dets[i, -1]
        if score > thresh:
            cv2.rectangle(im, bbox[0:2], bbox[2:4], (0, 204, 0), 2)
            cv2.putText(im,
                        '%s: %.3f' % (class_name, score),
                        (bbox[0], bbox[1] + 15),
                        cv2.FONT_HERSHEY_PLAIN,
                        1.0, (0, 0, 255),
                        thickness=1)
Exemplo n.º 29
0
def main(args):
    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    # Setup caffe
    if args.gpu >= 0:
        caffe.mpi_init()
        caffe.set_mode_gpu()
        caffe.set_device(cfg.GPU_ID)
    else:
        caffe.mpi_init()
        caffe.set_mode_cpu()

    # Get query image and roi
    query_img = 'demo/query.jpg'
    query_roi = [0, 0, 466, 943]  # [x1, y1, x2, y2]

    # Extract feature of the query person
    net = caffe.Net(args.probe_def, args.caffemodel, caffe.TEST)
    query_feat = demo_exfeat(net, query_img, query_roi)
    del net  # Necessary to release cuDNN conv static workspace

    # Get gallery images
    gallery_imgs = sorted(glob('demo/gallery*.jpg'))

    # Detect and extract feature of persons in each gallery image
    net = caffe.Net(args.gallery_def, args.caffemodel, caffe.TEST)

    # Necessary to warm-up the net, otherwise the first image results are wrong
    # Don't know why. Possibly a bug in caffe's memory optimization.
    # Nevertheless, the results are correct after this warm-up.
    demo_detect(net, query_img)

    for gallery_img in gallery_imgs:
        print gallery_img, '...'
        boxes, features = demo_detect(net, gallery_img,
                                      threshold=args.det_thresh)
        if boxes is None:
            print gallery_img, 'no detections'
            continue
        # Compute pairwise cosine similarities,
        #   equals to inner-products, as features are already L2-normed
        similarities = features.dot(query_feat)

        # Visualize the results
        fig, ax = plt.subplots(figsize=(16, 9))
        ax.imshow(plt.imread(gallery_img))
        plt.axis('off')
        for box, sim in zip(boxes, similarities):
            x1, y1, x2, y2, _ = box
            ax.add_patch(
                plt.Rectangle((x1, y1), x2 - x1, y2 - y1,
                              fill=False, edgecolor='#4CAF50', linewidth=3.5))
            ax.add_patch(
                plt.Rectangle((x1, y1), x2 - x1, y2 - y1,
                              fill=False, edgecolor='white', linewidth=1))
            ax.text(x1 + 5, y1 - 18, '{:.2f}'.format(sim),
                    bbox=dict(facecolor='#4CAF50', linewidth=0),
                    fontsize=20, color='white')
        plt.tight_layout()
        fig.savefig(gallery_img.replace('gallery', 'result'))
        plt.show()
        plt.close(fig)
    del net
Exemplo n.º 30
0
import pprint

gpu_id = 0
solver = '/home/zyb/VirtualDisk500/exhdd/Recognition-master/models/UniqueDET/solver_debug.prototxt'
max_iters = 100000
size = 224
imdb_name = 'UniqueDET'
out = 'out'
cfg = '/home/zyb/VirtualDisk500/exhdd/Recognition-master/experiments/cfgs/faster_rcnn_end2end.yml'
pretrained_model = '/home/zyb/VirtualDisk500/exhdd/Recognition-master/pretrained_models/VGG_CNN_M_1024.v2.caffemodel'

if __name__ == '__main__':

    def_cfg('UDET')

    cfg_from_file(cfg)
    pprint.pprint(fconfig.cfg)

    caffe.set_mode_gpu()
    caffe.set_device(gpu_id)
    # setup the dataset's path
    dataset = os.path.join('..', 'data', imdb_name)
    # load pixel mean
    pixel_means = None
    if os.path.exists(os.path.join(dataset, 'mean.npy')):
        pixel_means = np.load(os.path.join(dataset, 'mean.npy'))
        fconfig.cfg.PIXEL_MEANS = pixel_means
        print 'Loaded mean.npy: {}'.format(pixel_means)
    else:
        print 'Cannot find mean.npy and we will use default mean.'
Exemplo n.º 31
0
    args = parser.parse_args()
    return args


if __name__ == '__main__':
    #args = parse_args()

    # print('Called with args:')
    # print(args)

    # if args.cfg_file is not None:
    #     cfg_from_file(args.cfg_file)
    # if args.set_cfgs is not None:
    #     cfg_from_list(args.set_cfgs)

    cfg_from_file("models/pvanet/cfgs/submit_160715.yml")

    cfg.GPU_ID = 0

    print('Using config:')
    pprint.pprint(cfg)

    # while not os.path.exists(args.caffemodel) and args.wait:
    #     print('Waiting for {} to exist...'.format(args.caffemodel))
    #     time.sleep(10)

    caffe.set_mode_gpu()
    caffe.set_device(0)
    net = caffe.Net("output/test_traffic_light.prototxt",
                    "rcnn/traf/pvanet_frcnn_384_iter_190000.caffemodel",
                    caffe.TEST)
Exemplo n.º 32
0
def main():
    args = parse_args()
    ins_id = args.id

    instance = getTrainingInstance(ins_id)
    assert instance != None

    generateCFG(instance)
    cfg_from_file(instance.cfg_file)
    if instance.set_cfgs is not None:
        cfg_from_list(instance.set_cfgs)

    #generate model if it not exists yet

    if instance.net == '': 
        instance.setDefaultNet()
        models_path = os.path.join(instance.devkit, 'models')
        if os.path.exists(models_path):
            os.popen('rm -rf {}'.format(models_path))
        print 'Generating model ' + models_path
        generate_custom_net.main(instance.cls_num,models_path,instance.steps,instance.lr)

    #if not os.path.exists(instance.net):
    #    generate_custom_net.main(instance.cls_num,instance.netdir,instance.steps,instance.lr)

    #copy net_def to devkit
    net_src = os.path.join(instance.devkit,instance.net, "faster_rcnn_alt_opt", \
                           "faster_rcnn_test.pt")
    net_dst = os.path.join(instance.devkit, "results")
    if (not os.path.exists(net_dst)):
        os.makedirs(net_dst)
    print 'Copying {} to {}'.format(net_src, net_dst)
    shutil.copy(net_src, net_dst)

    #generate factory.py
    generateFactory(instance)

    #generate train.sh
    import generate_train_sh
    generate_train_sh.main(instance)

    #make symbolic link to VOCCode
    generateVOCCode(instance.devkit)

    if instance.validate() == False:
        print 'Error in training instance.'
        exit(1)

    dbconn = sql.MySQLConnection('192.168.1.90','test','test','zb_label')
    dbconn.connect()
    sqlstr = 'update zb_train set status = 2 where id = {}'.format(ins_id)
    dbconn.query(sqlstr)
    dbconn.commit()
    dbconn.close()

    #start training
    try:
        os.system('experiments/scripts/train.sh')
        acc_rate = getAccuracy(ins_id)
        acc_str = json.dumps(acc_rate)
        #sqlstr = 'update zb_train set status = 3 , accuracy = {} where id = {}'.format(json.dumps(acc_str), ins_id)
        #prev dbconn may be time-out and closed by the server.
        sqlstr = 'update zb_train set status = 3 where id = {}'.format( ins_id)
        dbconn = sql.MySQLConnection('192.168.1.90','test','test','zb_label')
        dbconn.connect()
        dbconn.query(sqlstr)
        dbconn.commit()
        dbconn.close()
    except Exception,e:
        sqlerrstr = 'update zb_train set status = -1 where id = {}'.format(ins_id)
        dbconn = sql.MySQLConnection('192.168.1.90','test','test','zb_label')
        dbconn.connect()
        dbconn.query(sqlstr)
        dbconn.commit()
        dbconn.close()
        print e
Exemplo n.º 33
0
    """

    import caffe
    # fix the random seeds (numpy and caffe) for reproducibility
    np.random.seed(cfg.RNG_SEED)
    caffe.set_random_seed(cfg.RNG_SEED)
    # set up caffe
    caffe.set_mode_gpu()
    caffe.set_device(cfg.GPU_ID)


if __name__ == '__main__':
    args = parse_args()

    if args.path_cfg is not None:
        cfg_from_file(args.path_cfg)
    cfg.GPU_ID = args.gpu_id

    print 'RPN model: {}'.format(args.path_net_weights)
    print('Using config:')
    pprint.pprint(cfg)

    import caffe
    _init_caffe(cfg)

    imdb = get_imdb(args.imdb_name)
    print 'Loaded dataset `{:s}` for proposal generation'.format(imdb.name)

    # Load RPN and configure output directory
    rpn_net = caffe.Net(args.path_net_proto, args.path_net_weights, caffe.TEST)
    output_dir = get_output_dir(imdb)
Exemplo n.º 34
0
MODEL = '/home/ryan/vision/py-faster-rcnn/output/faster_rcnn_end2end/ir_train/vgg16_faster_rcnn_iter_40000.caffemodel'
CFG = './experiments/cfgs/faster_rcnn_end2end.yml'
GPU = 0
PROTOTXT = './models/imagenet/VGG16/faster_rcnn_end2end/test.prototxt'
WAIT = True
DATASET = 'ir'
COMP = True
VIS = False 
NUM_DETS = 100
IMDB = 'ir_test'


if __name__ == '__main__':

    # Set config and caffe parameters
    cfg_from_file(CFG)
    cfg.GPU_ID = GPU
    caffe.set_mode_gpu()
    caffe.set_device(GPU)
    
    # Initialize net
    net = caffe.Net(PROTOTXT, MODEL, caffe.TEST)
    net.name = os.path.splitext(os.path.basename(MODEL))[0]

    # Get the dataset
    imdb = get_imdb(IMDB)
    imdb.competition_mode(True)
    if not cfg.TEST.HAS_RPN:
        imdb.set_proposal_method(cfg.TEST.PROPOSAL_METHOD)
    
    # Call test function
Exemplo n.º 35
0
def _unmap(data, count, inds, fill=0):
    """ Unmap a subset of item (data) back to the original set of items (of
    size count) """
    if len(data.shape) == 1:
        ret = np.empty((count, ), dtype=np.float32)
        ret.fill(fill)
        ret[inds] = data
    else:
        ret = np.empty((count, ) + data.shape[1:], dtype=np.float32)
        ret.fill(fill)
        ret[inds, :] = data
    return ret

if __name__ == '__main__':
	
	cfg_from_file('experiments/cfgs/faster_rcnn_end2end_kitti.yml')

	# Load dataset	
	from datasets.kitti import kitti
	imdb = kitti('train', '2012')
	roidb = imdb.roidb

	
	im_scale = float(576) / float(375)	  

	# Load anchors
	from rpn.generate_anchors import generate_anchors
	anchors = generate_anchors(scales=np.array(range(1,10)), ratios=[0.5, 1., 1.5, 2., 2.5, 3.])
	anchors = anchors * im_scale

	num_anchors = anchors.shape[0]
Exemplo n.º 36
0
    def __init__(self, args):
        logging.info('Loading net and associated files...')

        gpu_id = args['gpu_id']
        if gpu_id < 0:
            caffe.set_mode_cpu()
            print "use cpu mode"
        else:
            caffe.set_mode_gpu()
            caffe.set_device(gpu_id)
            cfg.GPU_ID = gpu_id
            print "use gpu mode"
        self.gpu_id = gpu_id
        logging.info('\n\ngpu id: {:s}'.format(str(gpu_id)))
        time.sleep(1)

        cfg_file = args['cfg_file'].strip()
        if os.path.exists(cfg_file) and os.path.isfile(cfg_file):
            cfg_from_file(cfg_file)
        logging.info('\n\ncfg_file: {:s}'.format(cfg_file))

        cls_filepath = args['cls_filepath'].strip()
        if not os.path.exists(cls_filepath) or not os.path.isfile(
                cls_filepath):
            raise IOError(('{:s} not found.\n').format(cls_filepath))
        with open(cls_filepath) as f:
            classes = [x.strip().lower() for x in f.readlines()]
        self.classes = tuple(classes)
        logging.info('\n\cls_filepath: {:s}'.format(cls_filepath))
        logging.info('\n\nclasses: {:s}'.format(",".join(classes)))

        pt_cls = args['pt_cls'].strip().split(",")
        self.pt_cls = [cls.strip() for cls in pt_cls]
        logging.info('\n\pt_cls: {:s}'.format(pt_cls))

        self.pt_choice = args['pt_choice']
        logging.info('\n\pt_choice: {:s}'.format(str(self.pt_choice)))

        pt_def = args['pt_def'].strip()
        if not os.path.exists(pt_def) or not os.path.isfile(pt_def):
            raise IOError(('{:s} not found.\n').format(pt_def))
        logging.info('\n\nLoaded pt_def: {:s}'.format(pt_def))

        person_caffemodel = args['person_caffemodel'].strip()
        if not os.path.exists(person_caffemodel) or not os.path.isfile(
                person_caffemodel):
            raise IOError(('{:s} not found.\n').format(person_caffemodel))
        self.person_caffemodel = caffe.Net(pt_def, person_caffemodel,
                                           caffe.TEST)
        logging.info(
            '\n\nLoaded person network: {:s}'.format(person_caffemodel))

        torso_caffemodel = args['torso_caffemodel'].strip()
        if not os.path.exists(torso_caffemodel) or not os.path.isfile(
                torso_caffemodel):
            raise IOError(('{:s} not found.\n').format(torso_caffemodel))
        self.torso_caffemodel = caffe.Net(pt_def, torso_caffemodel, caffe.TEST)
        logging.info('\n\nLoaded torso network: {:s}'.format(torso_caffemodel))

        init_net(self.person_caffemodel, self.torso_caffemodel)
        logging.info('\n\ninit_net of pt done!')

        time.sleep(1)
Exemplo n.º 37
0
def main():
    args = parse_args()

    print('Called with args:')
    print(args)

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)
    cfg.GPU_ID = args.gpu_id

    # fix the random seeds (numpy and caffe) for reproducibility
    np.random.seed(cfg.RNG_SEED)
    caffe.set_random_seed(cfg.RNG_SEED)
    # set up caffe
    caffe.set_mode_gpu()
    caffe.set_device(cfg.GPU_ID)

    # queue for communicated results between processes
    mp_queue = mp.Queue()

    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    print 'Stage 1 RPN, init from ImageNet model'
    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

    cfg.TRAIN.SNAPSHOT_INFIX = 'stage1'
    rpn_stage1_out = train_rpn(imdb_names=args.imdb_name,
                               init_model=args.pretrained_model,
                               solver=args.solver,
                               max_iters=args.max_iters,
                               cfg=cfg)
    #  mp_kwargs = dict(
    #          queue=mp_queue,
    #          imdb_names=args.imdb_name,
    #          init_model=args.pretrained_model,
    #          solver=args.solver,
    #          max_iters=args.max_iters,
    #          cfg=cfg)
    #  p = mp.Process(target=train_rpn, kwargs=mp_kwargs)
    #  p.start()
    #  rpn_stage1_out = mp_queue.get()
    #  p.join()
    #  rpn_stage1_out = \
    #          {'model_path': '/home/leoyolo/research/py-faster-rcnn-another/output/rpn_small_obj/voc_2007_trainval/vgg_cnn_m_1024_rpn_small_obj_stage1_iter_80000.caffemodel'}

    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    print 'Stage 1 RPN, generate proposals for the test set'
    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

    rpn_stage1_out['proposal_path'] = \
        rpn_generate(imdb_name=args.test_imdb_name,
                     rpn_model_path=str(rpn_stage1_out['model_path']),
                     cfg=cfg,
                     rpn_test_prototxt=args.rpn_test_prototxt
        )['proposal_path']

    #  mp_kwargs = dict(
    #          queue=mp_queue,
    #          imdb_name=args.test_imdb_name,
    #          rpn_model_path=str(rpn_stage1_out['model_path']),
    #          cfg=cfg,
    #          rpn_test_prototxt=args.rpn_test_prototxt)
    #  p = mp.Process(target=rpn_generate, kwargs=mp_kwargs)
    #  p.start()
    #  rpn_stage1_out['proposal_path'] = mp_queue.get()['proposal_path']
    #  p.join()

    for area in ['all', 'small', 'medium', 'large']:

        print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
        print 'Stage 1 RPN, eval recall with area {}'.format(area)
        print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'

        rpn_test(imdb_name=args.test_imdb_name,
                 rpn_proposal_path=rpn_stage1_out['proposal_path'],
                 area=area)
Exemplo n.º 38
0
        print 'Set proposal method: {:s}'.format(cfg.TRAIN.PROPOSAL_METHOD)
        roidb = get_training_roidb(imdb)
        return roidb

    roidbs = [get_roidb(s) for s in imdb_names.split('+')]
    roidb = roidbs[0]
    if len(roidbs) > 1:
        for r in roidbs[1:]:
            roidb.extend(r)
        imdb = datasets.imdb.imdb(imdb_names)
    else:
        imdb = get_imdb(imdb_names)
    return imdb, roidb


cfg_from_file('experiments/cfgs/rfcn_end2end.yml')

imdb, roidb = combined_roidb('voc_0712_test')

import cv2

ann = roidb[9]
im = cv2.imread(ann['image'])
idx = 0
for bb in ann['boxes']:
    cv2.rectangle(im,(bb[0],bb[1]),(bb[2],bb[3]),(0,255,0),1)
cv2.imwrite('/home/zawlin/data/all.jpg',im)
cv2.imshow('im2',im)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
Exemplo n.º 39
0
def main(args):
    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    # Setup caffe
    if args.gpu >= 0:
        caffe.mpi_init()
        caffe.set_mode_gpu()
        caffe.set_device(cfg.GPU_ID)
    else:
        caffe.mpi_init()
        caffe.set_mode_cpu()

    # Get query image and roi
    query_img = 'demo/query.jpg'
    query_roi = [0, 0, 466, 943]  # [x1, y1, x2, y2]

    # Extract feature of the query person
    net = caffe.Net(args.probe_def, args.caffemodel, caffe.TEST)
    query_feat = demo_exfeat(net, query_img, query_roi)
    del net  # Necessary to release cuDNN conv static workspace

    # Get gallery images
    gallery_imgs = sorted(glob('demo/gallery*.jpg'))

    # Detect and extract feature of persons in each gallery image
    net = caffe.Net(args.gallery_def, args.caffemodel, caffe.TEST)

    # Necessary to warm-up the net, otherwise the first image results are wrong
    # Don't know why. Possibly a bug in caffe's memory optimization.
    # Nevertheless, the results are correct after this warm-up.
    demo_detect(net, query_img)

    for gallery_img in gallery_imgs:
        print gallery_img, '...'
        boxes, features = demo_detect(net,
                                      gallery_img,
                                      threshold=args.det_thresh)
        if boxes is None:
            print gallery_img, 'no detections'
            continue
        # Compute pairwise cosine similarities,
        #   equals to inner-products, as features are already L2-normed
        similarities = features.dot(query_feat)

        # Visualize the results
        fig, ax = plt.subplots(figsize=(16, 9))
        ax.imshow(plt.imread(gallery_img))
        plt.axis('off')
        for box, sim in zip(boxes, similarities):
            x1, y1, x2, y2, _ = box
            ax.add_patch(
                plt.Rectangle((x1, y1),
                              x2 - x1,
                              y2 - y1,
                              fill=False,
                              edgecolor='#4CAF50',
                              linewidth=3.5))
            ax.add_patch(
                plt.Rectangle((x1, y1),
                              x2 - x1,
                              y2 - y1,
                              fill=False,
                              edgecolor='white',
                              linewidth=1))
            ax.text(x1 + 5,
                    y1 - 18,
                    '{:.2f}'.format(sim),
                    bbox=dict(facecolor='#4CAF50', linewidth=0),
                    fontsize=20,
                    color='white')
        plt.tight_layout()
        fig.savefig(gallery_img.replace('gallery', 'result'))
        plt.show()
        plt.close(fig)
    del net
Exemplo n.º 40
0
               for j in xrange(0, 28, 2)]

        ploygon_test = Polygon(pts)
        if not ploygon_test.is_valid:
            print('non-ploygon detected')
            delete_inds.append(i)
        if int(ploygon_test.area) < 10:
            print('neg-ploygon')
            delete_inds.append(i)
    dets = np.delete(dets, delete_inds, 0)
    cdets = np.delete(cdets, delete_inds, 0)
    return dets, cdets


if __name__ == "__main__":
    cfg_from_file(cofig_file)
    net = caffe.Net(net_prototxt, model, caffe.TEST)

    for image in images:
        im = cv2.imread(image)
        scores, boxes, infos_h, infos_w = im_detect(net, im, None)

        assert (scores.shape[0] == infos_h.shape[0] ==
                infos_w.shape[0]), 'length mismatch'
        inds = np.where(scores[:, 1] > 0.5)[0]

        cls_scores = scores[inds, 1]

        cls_boxes = boxes[inds, 4:8]
        ## curve
        cls_infos_h = infos_h[inds, :14]
Exemplo n.º 41
0
            performance['rocauc'] += [obj['rocauc']]
            performance['recs'] += [obj['rec']]
            performance['precs'] += [obj['prec']]
            performance['prauc'] += [obj['prauc']]
    return (classes, performance)


if __name__ == '__main__':
    DEBUG = 0
    args = parse_args()

    print('Called with args:')
    print(args)

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    cfg.GPU_ID = args.gpu_id

    print('Using config:')
    pprint.pprint(cfg)

    #while not os.path.exists(args.caffemodel) and args.wait:
    #    print('Waiting for {} to exist...'.format(args.caffemodel))
    #    time.sleep(10)

    caffe.set_mode_gpu()
    caffe.set_device(args.gpu_id)
    print "TEST NET creation parameters:"
Exemplo n.º 42
0
    return imdb, roidb


def latest_model(model_pattern):
    searchedfile = glob.glob(model_pattern)
    assert (len(searchedfile) > 0), "0 file matched by %s!" % (model_pattern)
    files = sorted(searchedfile, key=lambda file: os.path.getmtime(file))
    return files[-1]


if __name__ == "__main__":
    args = parse_args()
    path_env = setup_paths(args.net, args.data, args.expid)
    # Get model train config copy it to the output path.
    cfg_config = args.model_config or path_env["cfg"]
    cfg_from_file(cfg_config)
    copyfile(cfg_config, os.path.join(path_env["output"], "config.yml"))
    cfg.GPU_ID = args.GPU_ID
    cfg.DATA_DIR = path_env['data_root']
    cfg.TRAIN.SCALES = args.train_sizes[:-1]
    cfg.TRAIN.MAX_SIZE = args.train_sizes[-1]

    # fix the random seeds (numpy and caffe) for reproducibility
    #np.random.seed(cfg.RNG_SEED)
    #caffe.set_random_seed(cfg.RNG_SEED)

    # redirect output to the LOG file
    print 'Logging output to %s' % path_env['log']
    start = time.time()
    with open(path_env['log'], 'w') as pylog, PyTee(pylog, 'stdout'):
        print 'Setting GPU device %d for training' % cfg.GPU_ID
Exemplo n.º 43
0
def main(args):
    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    # parse gpus
    gpus = map(int, args.gpus.split(','))
    assert len(gpus) >= mpi_size, "Number of GPUs must be >= MPI size"
    cfg.GPU_ID = gpus[mpi_rank]

    # parse feature blob names
    blob_names = args.blob_names.split(',')

    print('Using config:')
    pprint.pprint(cfg)

    while not osp.exists(args.caffemodel) and args.wait:
        print('Waiting for {} to exist...'.format(args.caffemodel))
        time.sleep(10)

    # load imdb
    imdb = get_imdb(args.imdb_name)
    root_dir = imdb._root_dir
    images_dir = imdb._data_path
    output_dir = get_output_dir(imdb.name,
                                osp.splitext(osp.basename(args.caffemodel))[0])

    if args.eval_only:
        def _load(fname):
            fpath = osp.join(output_dir, fname)
            assert osp.isfile(fpath), "Must have extracted detections and " \
                                      "features first before evaluation"
            return unpickle(fpath)
        if mpi_rank == 0:
            gboxes = _load('gallery_detections.pkl')
            gfeatures = _load('gallery_features.pkl')
            pfeatures = _load('probe_features.pkl')
    else:
        # setup caffe
        caffe.mpi_init()
        caffe.set_mode_gpu()
        caffe.set_device(cfg.GPU_ID)

        # 1. Detect and extract features from all the gallery images in the imdb
        start, end = mpi_dispatch(len(imdb.image_index), mpi_size, mpi_rank)
        if args.use_gt:
            net = caffe.Net(args.probe_def, args.caffemodel, caffe.TEST)
            gboxes, gfeatures = usegt_and_exfeat(net, imdb,
                start=start, end=end, blob_names=blob_names)
        else:
            net = caffe.Net(args.gallery_def, args.caffemodel, caffe.TEST)
            gboxes, gfeatures = detect_and_exfeat(net, imdb,
                start=start, end=end, blob_names=blob_names)
        gboxes = mpi_collect(mpi_comm, mpi_rank, gboxes)
        gfeatures = mpi_collect(mpi_comm, mpi_rank, gfeatures)
        del net # to release the cudnn conv static workspace

        # 2. Only extract features from given probe rois
        start, end = mpi_dispatch(len(imdb.probes), mpi_size, mpi_rank)
        net = caffe.Net(args.probe_def, args.caffemodel, caffe.TEST)
        pfeatures = exfeat(net, imdb.probes,
            start=start, end=end, blob_names=blob_names)
        pfeatures = mpi_collect(mpi_comm, mpi_rank, pfeatures)
        del net

        # Save
        if mpi_rank == 0:
            pickle(gboxes, osp.join(output_dir, 'gallery_detections.pkl'))
            pickle(gfeatures, osp.join(output_dir, 'gallery_features.pkl'))
            pickle(pfeatures, osp.join(output_dir, 'probe_features.pkl'))

    # Evaluate
    if mpi_rank == 0:
        imdb.evaluate_detections(gboxes, det_thresh=args.det_thresh)
        imdb.evaluate_detections(gboxes, det_thresh=args.det_thresh,
                                 labeled_only=True)
        imdb.evaluate_search(gboxes, gfeatures['feat'], pfeatures['feat'],
             det_thresh=args.det_thresh,
             gallery_size=args.gallery_size,
             dump_json=osp.join(output_dir, 'results.json'))
Exemplo n.º 44
0
if __name__ == '__main__':
    args = parse_args()

    comm = MPI.COMM_WORLD
    mpi_rank = comm.Get_rank()
    pool_size = comm.Get_size()
    # TODO: wheather to check
    caffe.set_parallel()

    # load config file
    with open(args.val_cfg) as f:
        config = yaml.load(f.read())
    print "Config:\n{}".format(config)

    if args.rcnn_cfg is not None:
        cfg_from_file(args.rcnn_cfg)

    # load data
    provider = load_data(args.val_cfg)

    # get gpu id
    gpus = args.device_id
    assert len(gpus) >= pool_size
    cur_gpu = gpus[mpi_rank]
    cfg.GPU_ID = cur_gpu

    # load solver and nets
    net = load_nets(args, cur_gpu)

    # start training
    iter = mpi_rank
    print(args)
    assert os.path.exists(args.outdir), 'SVM path does not exist'
    sigmoid_cfg = get_sigmoid_configuration()
    #write configuration file to dir
    out_dir = args.outdir
    writeDict(sigmoid_cfg, '{}/sigmoid_cfg.txt'.format(out_dir))
    # Must turn this off to prevent issues when digging into the net blobs to
    # pull out features (tricky!)
    cfg.DEDUP_BOXES = 0
    # Must turn this on because we use the test im_detect() method to harvest
    # hard negatives
    cfg.TEST.SVM = True
    cfg.TRAIN.USE_FLIPPED = sigmoid_cfg.USE_FLIPPED

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)

    print('Using config:')
    pprint.pprint(cfg)

    # set up caffe
    caffe.set_mode_gpu()
    if args.gpu_id is not None:
        caffe.set_device(args.gpu_id)
    net = caffe.Net(args.prototxt, args.caffemodel, caffe.TEST)
    net.name = os.path.splitext(os.path.basename(args.caffemodel))[0]
    #load dataset
    imdb_name = args.imdb_name + '.' + args.image_set
    imdb = get_imdb(imdb_name)
    print 'Loaded dataset `{:s}` for training'.format(imdb.name)
    if args.k_fold: imdb.gen_k_fold(args.k_fold)
Exemplo n.º 46
0
#encoding:utf-8 
import numpy as np 
import cv2 

from rotation.data_extractor import get_rroidb
from rotation.r_roidb import add_rbbox_regression_targets
from rotation.r_minibatch import r_get_minibatch
from fast_rcnn.config import cfg, cfg_from_file, cfg_from_list, get_output_dir
from rotation.rt_train import filter_roidb


cfg_from_file('../../experiments/cfgs/faster_rcnn_end2end.yml')
for c in cfg:
	print c,':',cfg[c]


roidb = get_rroidb()
print 'len',len(roidb)
#print roidb[0]
roidb = filter_roidb(roidb)
print 'len_after',len(roidb)
print roidb[0]
bbox_means,bbox_stds = add_rbbox_regression_targets(roidb)#To test
#print roidb[0]
#print 'bbox_means',bbox_means
#print 'bbox_stds',bbox_stds
blobs = r_get_minibatch(roidb, 2)

im = blobs['data'].transpose(0, 2, 3, 1)[0]
gt_boxes = blobs['gt_boxes']
info = blobs['im_info']