Example #1
0
  cfg.TRAIN.USE_FLIPPED = False
  _, valroidb = combined_roidb(args.imdbval_name)
  print('{:d} validation roidb entries'.format(len(valroidb)))
  cfg.TRAIN.USE_FLIPPED = orgflip

  # load network
  if args.net == 'vgg16':
    net = vgg16()
  elif args.net == 'res50':
    net = resnetv1(num_layers=50)
  elif args.net == 'res101':
    net = resnetv1(num_layers=101)
  elif args.net == 'res152':
    net = resnetv1(num_layers=152)
  elif args.net == 'mobile':
    net = mobilenetv1()
  else:
    raise NotImplementedError
  # some statistic record
  al_num = 0
  ss_num = 0
  initial_num = len(imdb[imdb.item_name(0)].roidb)
  print ('All VOC2007 images use for initial train after flipped, imagenumbers:%d'%(initial_num))
  for i in range(initialnum):                        
      bitmapImdb.set(i)
  train_roidb = imdb[imdb.item_name(0)].roidb
  # pretrained-model
  pretrained_model_name = args.weight
  
  # some parameters
  tao = args.max_iters
Example #2
0
  tfconfig = tf.ConfigProto(allow_soft_placement=True)
  tfconfig.gpu_options.allow_growth=True

  # init session
  sess = tf.Session(config=tfconfig)
  # load network
  if args.net == 'vgg16':
    net = vgg16()
  elif args.net == 'res50':
    net = resnetv1(num_layers=50)
  elif args.net == 'res101':
    net = resnetv1(num_layers=101)
  elif args.net == 'res152':
    net = resnetv1(num_layers=152)
  elif args.net == 'mobile':
    net = mobilenetv1()
  else:
    raise NotImplementedError

  # load model
  net.create_architecture("TEST", imdb.num_classes, tag='default',
                          anchor_scales=cfg.ANCHOR_SCALES,
                          anchor_ratios=cfg.ANCHOR_RATIOS)

  if args.model:
    print(('Loading model check point from {:s}').format(args.model))
    saver = tf.train.Saver()
    saver.restore(sess, args.model)
    print('Loaded.')
  else:
    print(('Loading initial weights from {:s}').format(args.weight))
Example #3
0
    def __init__(self,
                 net_name,
                 model_path,
                 cfg_file,
                 num_classes=2,
                 max_object_per_image=15,
                 conf_thresh=0.3,
                 nms_thresh=0.5,
                 iou_thresh=0.5):
        self.net_name = net_name
        # self.sess = sess
        self.model_path = model_path
        self.cfg_file = cfg_file
        self.num_images = 1
        self.num_classes = num_classes
        self.conf_thresh = conf_thresh
        self.nms_thresh = nms_thresh
        self.iou_thresh = iou_thresh
        self.max_object_per_image = max_object_per_image

        # set config
        tfconfig = tf.ConfigProto(allow_soft_placement=True)
        tfconfig.gpu_options.allow_growth = True
        # tfconfig=tf.ConfigProto(log_device_placement=False,allow_soft_placement=True)
        # init session
        self.sess = tf.Session(config=tfconfig)

        if not os.path.isfile(self.model_path + '.meta'):
            raise IOError((
                '{:s} not found.\nDid you download the proper networks from '
                'our server and place them properly?').format(self.model_path +
                                                              '.meta'))

        # load network configuration
        cfg_from_file(self.cfg_file)
        # pprint.pprint(cfg)

        # load network
        if self.net_name == 'vgg16':
            self.net = vgg16(batch_size=1)
        elif self.net_name == 'res50':
            self.net = resnetv1(batch_size=1, num_layers=50)
        elif self.net_name == 'res101':
            self.net = resnetv1(batch_size=1, num_layers=101)
        elif self.net_name == 'res152':
            self.net = resnetv1(batch_size=1, num_layers=152)
        elif self.net_name == 'mobile':
            self.net = mobilenetv1(batch_size=1)
        else:
            raise NotImplementedError

        with self.sess.as_default():
            self.net.create_architecture(self.sess,
                                         "TEST",
                                         self.num_classes,
                                         tag='default',
                                         anchor_scales=cfg.ANCHOR_SCALES,
                                         anchor_ratios=cfg.ANCHOR_RATIOS)

            saver = tf.train.Saver()
            saver.restore(self.sess, self.model_path)
    # also add the validation set, but with no flipping images
    orgflip = cfg.TRAIN.USE_FLIPPED
    cfg.TRAIN.USE_FLIPPED = False
    _, valroidb = combined_roidb(args.imdbval_name)
    print('{:d} validation roidb entries'.format(len(valroidb)))
    cfg.TRAIN.USE_FLIPPED = orgflip

    # load network
    if args.net == 'vgg16':
        net = vgg16(batch_size=cfg.TRAIN.IMS_PER_BATCH)
    elif args.net == 'res50':
        net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=50)
    elif args.net == 'res101':
        net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=101)
    elif args.net == 'res152':
        net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=152)
    elif args.net == 'mobile':
        net = mobilenetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH)
    else:
        raise NotImplementedError

    train_net(net,
              imdb,
              roidb,
              valroidb,
              output_dir,
              tb_dir,
              pretrained_model=args.weight,
              max_iters=args.max_iters)
Example #5
0
    def __init__(self, dir_model, network, gpu_id):
        self.net = None

        if network == 'res101':
            self.CLASSES = ('__background__', 'aeroplane', 'bicycle', 'bird',
                            'boat', 'bottle', 'bus', 'car', 'cat', 'chair',
                            'cow', 'diningtable', 'dog', 'horse', 'motorbike',
                            'person', 'pottedplant', 'sheep', 'sofa', 'train',
                            'tvmonitor')
        if network == 'mobilenet':
            self.CLASSES = (
                'background', 'person', 'bicycle', 'car', 'motorcycle',
                'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
                'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
                'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
                'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
                'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
                'kite', 'baseball bat', 'baseball glove', 'skateboard',
                'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
                'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
                'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
                'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
                'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
                'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
                'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
                'teddy bear', 'hair drier', 'toothbrush')
        NETS = {
            'mobilenet': ('mobile_faster_rcnn_iter_1190000.ckpt', ),
            'vgg16': ('vgg16_faster_rcnn_iter_70000.ckpt', ),
            'res101': ('res101_faster_rcnn_iter_110000.ckpt', )
        }
        os.environ["CUDA_VISIBLE_DEVICES"] = gpu_id
        # demonet = 'mobilenet'#'res101'
        tfmodel = os.path.join(dir_model, 'person_detection/faster_rcnn',
                               NETS[network][0])
        if not os.path.isfile(tfmodel + '.meta'):
            raise IOError(
                ('{:s} not found.\nDid you download the proper networks from '
                 'our server and place them properly?').format(tfmodel +
                                                               '.meta'))

        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8)
        # config.gpu_options.per_process_gpu_memory_fraction = 0.8

        self.sess = tf.Session(config=tf.ConfigProto(
            gpu_options=gpu_options, allow_soft_placement=True))
        # tfconfig = tf.ConfigProto(allow_soft_placement=True)
        # tfconfig.gpu_options.allow_growth = True
        #
        # # init session
        # sess = tf.Session(config=tfconfig)
        if network == 'vgg16':
            self.net = vgg16()
        elif network == 'res101':
            self.net = resnetv1(num_layers=101)
        elif network == 'mobilenet':
            self.net = mobilenetv1()
        else:
            raise NotImplementedError

        if network == 'mobilenet':
            self.net.create_architecture("TEST",
                                         81,
                                         tag='default',
                                         anchor_scales=[4, 8, 16, 32])
        if network == 'res101':
            self.net.create_architecture("TEST",
                                         21,
                                         tag='default',
                                         anchor_scales=[8, 16, 32])
        saver = tf.train.Saver()
        saver.restore(self.sess, tfmodel)
        print('Loaded network {:s}'.format(tfmodel))

        self.CONF_THRESH = 0.8
        self.NMS_THRESH = 0.3
Example #6
0
  tfconfig = tf.ConfigProto(allow_soft_placement=True)
  tfconfig.gpu_options.allow_growth=True

  # init session
  sess = tf.Session(config=tfconfig)
  # load network
  if args.net == 'vgg16':
    net = vgg16(batch_size=1)
  elif args.net == 'res50':
    net = resnetv1(batch_size=1, num_layers=50)
  elif args.net == 'res101':
    net = resnetv1(batch_size=1, num_layers=101)
  elif args.net == 'res152':
    net = resnetv1(batch_size=1, num_layers=152)
  elif args.net == 'mobile':
    net = mobilenetv1(batch_size=1)
  else:
    raise NotImplementedError

  # load model
  net.create_architecture("TEST", imdb.num_classes, tag='default',
                          anchor_scales=cfg.ANCHOR_SCALES,
                          anchor_ratios=cfg.ANCHOR_RATIOS)

  if args.model:
    print(('Loading model check point from {:s}').format(args.model))
    saver = tf.train.Saver()
    saver.restore(sess, args.model)
    print('Loaded.')
  else:
    print(('Loading initial weights from {:s}').format(args.weight))
Example #7
0
  tfconfig = tf.ConfigProto(allow_soft_placement=True)
  tfconfig.gpu_options.allow_growth=True

  # init session
  sess = tf.Session(config=tfconfig)
  # load network
  if args.net == 'vgg16':
    net = vgg16(batch_size=1)
  elif args.net == 'res50':
    net = resnetv1(batch_size=1, num_layers=50)
  elif args.net == 'res101':
    net = resnetv1(batch_size=1, num_layers=101)
  elif args.net == 'res152':
    net = resnetv1(batch_size=1, num_layers=152)
  elif args.net == 'mobile':
    net = mobilenetv1(batch_size=1)
  else:
    raise NotImplementedError

  # load model
  net.create_architecture(sess, "TEST", imdb.num_classes, tag='default',
                          anchor_scales=cfg.ANCHOR_SCALES,
                          anchor_ratios=cfg.ANCHOR_RATIOS)

  if args.model:
    print(('Loading model check point from {:s}').format(args.model))
    saver = tf.train.Saver()
    saver.restore(sess, args.model)
    print('Loaded.')
  else:
    print(('Loading initial weights from {:s}').format(args.weight))
    def launch_train(self, conf):
        '''
        
        '''
        args = {}
        args['cfg_file'] = conf.frcnn_cfg
        args['weight'] = conf.starting_weights
        args['imdb_name'] = conf.train_set
        args['imdbval_name'] = conf.valid_set
        args['max_iters'] = conf.iters
        args['tag'] = conf.frcnn_tag
        args['net'] = conf.frcnn_net
        args['set_cfgs'] = None

        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'])

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

        np.random.seed(cfg.RNG_SEED)

        # train set
        imdb, roidb = combined_roidb(args['imdb_name'], conf)
        print('{:d} roidb entries'.format(len(roidb)))

        # output directory where the models are saved
        output_dir = conf.backup_folder  #get_output_dir(imdb, args.tag)
        print('Output will be saved to `{:s}`'.format(output_dir))

        # tensorboard directory where the summaries are saved during training
        tb_dir = conf.backup_folder  # get_output_tb_dir(imdb, args.tag)
        print('TensorFlow summaries will be saved to `{:s}`'.format(tb_dir))

        # also add the validation set, but with no flipping images
        orgflip = cfg.TRAIN.USE_FLIPPED
        cfg.TRAIN.USE_FLIPPED = False
        _, valroidb = combined_roidb(args['imdbval_name'], conf)
        print('{:d} validation roidb entries'.format(len(valroidb)))
        cfg.TRAIN.USE_FLIPPED = orgflip
        if args['net'] == 'vgg16':
            net = vgg16(batch_size=cfg.TRAIN.IMS_PER_BATCH)
        elif args['net'] == 'res50':
            net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=50)
        elif args['net'] == 'res101':
            net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=101)

        # load network
        elif args['net'] == 'res152':
            net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=152)
        elif args['net'] == 'mobile':
            net = mobilenetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH)
        else:
            raise NotImplementedError

        train_net(net,
                  imdb,
                  roidb,
                  valroidb,
                  output_dir,
                  tb_dir,
                  pretrained_model=args['weight'],
                  max_iters=args['max_iters'])
    def launch_test(self, conf, hash_model):
        '''
        '''
        args = {}
        args['cfg_file'] = conf.frcnn_cfg
        args['weight'] = conf.starting_weights
        args['model'] = hash_model
        args['imdb_name'] = conf.valid_set
        args['comp_mode'] = False
        args['tag'] = conf.frcnn_tag
        args['net'] = conf.frcnn_net
        args['set_cfgs'] = None
        args['max_per_image'] = 5

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

        if args['cfg_file'] is not None:
            cfg_from_file(argsargs['cfg_file'])
        if args['set_cfgs'] is not None:
            cfg_from_list(args['set_cfgs'])

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

        # if has model, get the name from it
        # if does not, then just use the inialization weights
        if args['model']:
            filename = os.path.splitext(os.path.basename(args['model']))[0]
        else:
            filename = os.path.splitext(os.path.basename(args['weight']))[0]

        tag = args['tag']
        tag = tag if tag else 'default'
        filename = tag + '/' + filename

        # TODO This is really bad but it works, I'm sincerely sorry
        conf_copy = copy.deepcopy(conf)
        conf_copy.train_set = conf_copy.valid_set
        imdb = get_imdb(args['imdb_name'], conf_copy)
        print(args['imdb_name'])
        imdb.competition_mode(args['comp_mode'])

        tfconfig = tf.ConfigProto(allow_soft_placement=True)
        tfconfig.gpu_options.allow_growth = True

        # init session
        sess = tf.Session(config=tfconfig)
        # load network
        if args['net'] == 'vgg16':
            net = vgg16(batch_size=1)
        elif args['net'] == 'res50':
            net = resnetv1(batch_size=1, num_layers=50)
        elif args['net'] == 'res101':
            net = resnetv1(batch_size=1, num_layers=101)
        elif args['net'] == 'res152':
            net = resnetv1(batch_size=1, num_layers=152)
        elif args['net'] == 'mobile':
            net = mobilenetv1(batch_size=1)
        else:
            raise NotImplementedError

        # load model
        net.create_architecture(sess,
                                "TEST",
                                imdb.num_classes,
                                tag='default',
                                anchor_scales=cfg.ANCHOR_SCALES,
                                anchor_ratios=cfg.ANCHOR_RATIOS)

        if args['model']:
            print(
                ('Loading model check point from {:s}').format(args['model']))
            saver = tf.train.Saver()
            saver.restore(sess, args['model'])
            print('Loaded.')
        else:
            print(('Loading initial weights from {:s}').format(args['weight']))
            sess.run(tf.global_variables_initializer())
            print('Loaded.')

        test_net(sess,
                 net,
                 imdb,
                 filename,
                 max_per_image=args['max_per_image'])

        sess.close()
Example #10
0
  print('Output will be saved to `{:s}`'.format(output_dir))

  # tensorboard directory where the summaries are saved during training
  tb_dir = get_output_tb_dir(imdb, args.tag)
  print('TensorFlow summaries will be saved to `{:s}`'.format(tb_dir))

  # also add the validation set, but with no flipping images
  orgflip = cfg.TRAIN.USE_FLIPPED
  cfg.TRAIN.USE_FLIPPED = False
  _, valroidb = combined_roidb(args.imdbval_name)
  print('{:d} validation roidb entries'.format(len(valroidb)))
  cfg.TRAIN.USE_FLIPPED = orgflip

  # load network
  if args.net == 'vgg16':
    net = vgg16(batch_size=cfg.TRAIN.IMS_PER_BATCH)
  elif args.net == 'res50':
    net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=50)
  elif args.net == 'res101':
    net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=101)
  elif args.net == 'res152':
    net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=152)
  elif args.net == 'mobile':
    net = mobilenetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH)
  else:
    raise NotImplementedError
    
  train_net(net, imdb, roidb, valroidb, output_dir, tb_dir,
            pretrained_model=args.weight,
            max_iters=args.max_iters)