Exemplo n.º 1
0
 def _get_next_minibatch(self, db_inds, sigma):
     """Return the blobs to be used for the next minibatch.
     """
     minibatch_db = [self._roidb[i] for i in db_inds]
     if cfg.TRAIN.USE_RPN_DB:
         minibatch_db = self.imdb.add_rpn_rois(minibatch_db)
     prepare_roidb(minibatch_db)
     add_bbox_regression_targets(minibatch_db, self.bbox_means,
                                 self.bbox_stds)
     blobs = get_minibatch(minibatch_db, self._num_classes, sigma=sigma)
     if blobs is not None:
         blobs['db_inds'] = db_inds
     return blobs
Exemplo n.º 2
0
 def _get_next_minibatch(self, db_inds):
     next_seq = self.seqs[db_inds]  # get next sequence indices
     minibatch_db = [self._roidb[i] for i in self.seq2im[next_seq]
                     ]  # get the images for these sequences
     if cfg.TRAIN.USE_RPN_DB:
         minibatch_db = self.imdb.add_rpn_rois(minibatch_db)
     prepare_roidb(minibatch_db)
     add_bbox_regression_targets(minibatch_db, self.bbox_means,
                                 self.bbox_stds)
     blobs = get_minibatch(minibatch_db, self._num_classes)
     if blobs is not None:
         blobs['db_inds'] = self.seq2im[next_seq]
         blobs['seq_name'] = next_seq
     return blobs
Exemplo n.º 3
0
    def _get_next_minibatch(self):
        """Return the blobs to be used for the next minibatch.

        If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a
        separate process and made available through self._blob_queue.
        """
        db_inds = self._get_next_minibatch_inds()
        minibatch_db = [self._roidb[i] for i in db_inds]
        if cfg.TRAIN.USE_RPN_DB:
            minibatch_db = self._imdb.add_rpn_rois(minibatch_db)
        prepare_roidb(minibatch_db)
        add_bbox_regression_targets(minibatch_db, self.bbox_means,
                                    self.bbox_stds)
        return get_minibatch(minibatch_db, self._num_classes)
    def __init__(self,
                 sess,
                 saver,
                 network,
                 imdb,
                 roidb,
                 output_dir,
                 pretrained_model=None,
                 resume_from_latest_ckpt=None):
        """Initialize the SolverWrapper."""
        self.net = network
        self.imdb = imdb
        self.roidb = roidb
        self.output_dir = output_dir
        self.pretrained_model = pretrained_model
        self.resume_from_latest_ckpt = resume_from_latest_ckpt

        print 'Computing bounding-box regression targets...'
        if cfg.TRAIN.BBOX_REG:
            self.bbox_means, self.bbox_stds = rdl_roidb.add_bbox_regression_targets(
                roidb)
        print 'done'

        # For checkpoint
        self.saver = saver
Exemplo n.º 5
0
    def __init__(self, solver_prototxt, roidb, output_dir, solverstate=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        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 no
            # fixed statistics to compute a priori
            assert cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            self.bbox_means, self.bbox_stds = \
                    rdl_roidb.add_bbox_regression_targets(roidb)
            print 'done'

        self.solver = caffe.SGDSolver(solver_prototxt)
        if solverstate is not None:
            print('Loading solverstate '
                  'weights from {:s}').format(solverstate)

            solverstate = solverstate.strip()
            caffemodelFull = solverstate.split('.')[0] + '.caffemodel'
            caffemodel = caffemodelFull.split('/')[-1]

            os.symlink(caffemodelFull, caffemodel)
            self.solver.restore(solverstate)
            os.remove(caffemodel)

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb)
Exemplo n.º 6
0
    def __init__(self,
                 sess,
                 network,
                 timdb,
                 vimdb,
                 troidb,
                 vroidb,
                 output_dir,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.net = network
        self.timdb = timdb
        self.timdb = vimdb
        self.troidb = troidb
        self.vroidb = vroidb
        self.output_dir = output_dir
        self.pretrained_model = pretrained_model

        print 'Computing bounding-box regression targets...'
        if cfg.TRAIN.BBOX_REG:
            self.bbox_means, self.bbox_stds = rdl_roidb.add_bbox_regression_targets(
                roidb)
        print 'done'

        # For checkpoint
        self.saver = tf.train.Saver(max_to_keep=100)
Exemplo n.º 7
0
    def __init__(self,
                 sess,
                 saver,
                 network,
                 imdb,
                 roidb,
                 output_dir,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.net = network
        self.imdb = imdb
        self.roidb = roidb
        self.output_dir = output_dir
        self.pretrained_model = pretrained_model

        print('Computing bounding-box regression targets...'
              )  # clw modify: for py3
        if cfg.TRAIN.BBOX_REG:
            # 不同类的均值与方差,返回格式means.ravel(), stds.ravel()
            self.bbox_means, self.bbox_stds = rdl_roidb.add_bbox_regression_targets(
                roidb)
        print('done')  # clw modify: for py3

        # For checkpoint
        self.saver = saver
Exemplo n.º 8
0
    def set_imdb(self, imdb_name):
        """Set the roidb to be used by this layer during training."""
        imdb, roidb = combined_roidb(imdb_name)
        print '{:d} roidb entries'.format(len(roidb))

        if 'nexar2' not in imdb_name:
            roidb = filter_roidb(roidb)

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            self.bbox_means, self.bbox_stds = \
                rdl_roidb.add_bbox_regression_targets(roidb)
            print 'done'

        self._imdb = imdb
        self._roidb = roidb
        self._shuffle_roidb_inds()
        if cfg.TRAIN.USE_PREFETCH:
            self._blob_queue = Queue(10)
            self._prefetch_process = BlobFetcher(self._blob_queue, self._roidb,
                                                 self._num_classes)
            self._prefetch_process.start()

            # Terminate the child process when the parent exists
            def cleanup():
                print 'Terminating BlobFetcher'
                self._prefetch_process.terminate()
                self._prefetch_process.join()

            import atexit
            atexit.register(cleanup)
Exemplo n.º 9
0
    def __init__(self, solver_prototxt, roidb, output_dir,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        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 no
            # fixed statistics to compute a priori
            assert cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            self.bbox_means, self.bbox_stds = \
                    rdl_roidb.add_bbox_regression_targets(roidb)
            print 'done'

        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print ('Loading pretrained model '
                   'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb)
Exemplo n.º 10
0
    def __init__(self, solver_prototxt, roidb, output_dir,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        #if cfg.TRAIN.BBOX_REG:
        print 'Computing bounding-box regression targets...'
        self.bbox_means, self.bbox_stds = \
                rdl_roidb.add_bbox_regression_targets(roidb)
        print 'done'

        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print ('Loading pretrained model '
                   'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)
            
        #transform the last layers into convonutional    
        if cfg.TRAIN.SEGMENTATION:
            #pretrained = caffe.Net('data/imagenet_models/deploy.prototxt',pretrained_model,caffe.TEST)
            self.solver.net.params['fc6-conv'][0].data[...].flat=self.solver.net.params['fc6'][0].data[...].flat.copy()
            self.solver.net.params['fc7-conv'][0].data[...].flat=self.solver.net.params['fc7'][0].data[...].flat.copy()
            #del self.solver.net.outputs['fc7']
            #self.solver.net.params['cls_score-conv'][0].data[...].flat=self.solver.net.params['cls_score'][0].data[...].flat
        #hack to remove!!!!!!!!
        #print "Setting fc7 t 0!"
        #self.solver.net.params['fc7'][0].data[...]=0
        #print self.solver.net.params['cls_score'][0].data

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb)
Exemplo n.º 11
0
    def __init__(self,
                 sess,
                 saver,
                 network,
                 imdb,
                 roidb,
                 output_dir,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.net = network
        self.imdb = imdb
        self.roidb = roidb
        self.output_dir = output_dir
        self.pretrained_model = pretrained_model

        print 'Computing bounding-box regression targets...'
        if cfg.TRAIN.BBOX_REG:  # default is true
            # Calculate targets ??
            # tx = (Gx - Px) / Pw
            # ty = (Gy - Py) / Ph
            # tw = log(Gw / Pw)
            # th = log(Gh / Ph)
            # P should be the output of RPN
            self.bbox_means, self.bbox_stds = rdl_roidb.add_bbox_regression_targets(
                roidb)
        print 'done'

        # For checkpoint
        self.saver = saver
Exemplo n.º 12
0
 def __init__(self,
              sess,
              network,
              imdb,
              roidb,
              output_dir,
              pretrained_model=None,
              checkpoint=None):
     """Initialize the SolverWrapper."""
     self.net = network
     self.imdb = imdb
     self.roidb = roidb
     self.output_dir = output_dir
     self.pretrained_model = pretrained_model
     self.checkpoint = checkpoint
     if self.pretrained_model is not None and self.checkpoint is not None:
         raise Exception(
             'Please do not give pretrained_model and checkpoint together!')
     if cfg.TRAIN.BBOX_REG:
         self.bbox_means, self.bbox_stds = rdl_roidb.add_bbox_regression_targets(
             roidb)
     """保存这个信息是给预测用的。原先的机制是unnormalize然后保存的。原先的机制会导致无法方便地使用checkpoint进行训练。"""
     with tf.variable_scope('custom', reuse=False):
         bbox_means = tf.get_variable("bbox_means",
                                      self.bbox_means.shape,
                                      trainable=False)
         bbox_stds = tf.get_variable("bbox_stds",
                                     self.bbox_stds.shape,
                                     trainable=False)
     self.global_step = tf.Variable(0, trainable=False)
     # For checkpoint
     self.saver = tf.train.Saver(max_to_keep=100)
Exemplo n.º 13
0
    def __init__(self, solver_prototxt, roidb, output_dir,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        print 'Computing bounding-box regression targets...'
        self.bbox_means, self.bbox_stds = \
                rdl_roidb.add_bbox_regression_targets(roidb)
        print 'done'
        
        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print ('Loading pretrained model '
                   'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

        if cfg.TRAIN.BBOX_REG and cfg.TRAIN.UN_NORMALIZE:
            # scale and shift with bbox reg re-normalization
            net = self.solver.net
            net.params['bbox_pred'][0].data[...] = \
                    (net.params['bbox_pred'][0].data /
                     (self.bbox_stds[:, np.newaxis] + cfg.EPS))
            net.params['bbox_pred'][1].data[...] = \
                    ((net.params['bbox_pred'][1].data - self.bbox_means)
                     / (self.bbox_stds + cfg.EPS))

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb)
Exemplo n.º 14
0
    def get_samples(self):
        """ Get samples from the dataset files. Use cache if avalable. """
        self.num_classes = 21
        try: 
            self._sample_indexes = self.load_cache(
                'sample_indexes_' + str(self.shuffle_samples))
            self.bbox_means = self.load_cache('bbox_means')
            self.bbox_stds = self.load_cache('bbox_stds')   
            self.samples = self.load_cache('samples')
            self.imdb = self.load_cache('imdb')
            print('Loaded from cache.')
        except FileNotFoundError:
            print('No cache exists, loading from devkit')
            imdb = pascal_voc.pascal_voc(self.set_name, self.year,
                                         self.data_path)
            cfg.TRAIN.PROPOSAL_METHOD = 'selective_search'
            imdb.set_proposal_method(cfg.TRAIN.PROPOSAL_METHOD)
            roidb = self.get_training_roidb(imdb)
            self.save_cache('imdb', imdb, use_pickle=True)

            roidb = self.filter_roidb(roidb)
            (self.bbox_means,
             self.bbox_stds) = rdl_roidb.add_bbox_regression_targets(roidb)
            self.save_cache('bbox_means', self.bbox_means)
            self.save_cache('bbox_stds', self.bbox_stds)

            self.samples = roidb
            self.save_cache('samples', self.samples, use_pickle=True)

            self._shuffle_roidb_inds()
            print('done')
Exemplo n.º 15
0
    def __init__(self,
                 solver_prototxt,
                 roidb,
                 output_dir,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        print 'Computing bounding-box regression targets...'
        self.bbox_means, self.bbox_stds = \
                rdl_roidb.add_bbox_regression_targets(roidb)
        print 'done'

        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print('Loading pretrained model '
                  'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

        if cfg.TRAIN.BBOX_REG and cfg.TRAIN.UN_NORMALIZE:
            # scale and shift with bbox reg re-normalization
            net = self.solver.net
            net.params['bbox_pred'][0].data[...] = \
                    (net.params['bbox_pred'][0].data /
                     (self.bbox_stds[:, np.newaxis] + cfg.EPS))
            net.params['bbox_pred'][1].data[...] = \
                    ((net.params['bbox_pred'][1].data - self.bbox_means)
                     / (self.bbox_stds + cfg.EPS))

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb)
Exemplo n.º 16
0
    def __init__(self,
                 solver_prototxt,
                 roidb,
                 output_dir,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        print 'Computing bounding-box regression targets...'
        self.bbox_means, self.bbox_stds = \
                rdl_roidb.add_bbox_regression_targets(roidb)
        print 'done'
        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print('Loading pretrained model '
                  'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)
        data_layers_idx = []
        for idx in xrange(len(self.solver.net.layers)):
            if (self.solver.net.layers[idx].type == 'Python'):
                data_layers_idx.append(idx)
        for ndl in data_layers_idx:
            layer_params = yaml.load(self.solver.net.layers[ndl].param_str_)
            self.solver.net.layers[ndl].set_roidb(roidb)
Exemplo n.º 17
0
    def __init__(self,
                 solver_prototxt,
                 roidb,
                 output_dir,
                 pretrained_model=None,
                 previous_state=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        print 'Computing bounding-box regression targets...'
        self.bbox_means, self.bbox_stds = \
                rdl_roidb.add_bbox_regression_targets(roidb)
        print 'done'

        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print('Loading pretrained model '
                  'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)
        elif previous_state is not None:
            print('Restoring State ' ' from {:s}').format(previous_state)
            self.solver.restore(previous_state)

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb)
def train_net_multi_gpus(solver_prototxt,
                         roidb,
                         output_dir,
                         gpus,
                         pretrained_model=None,
                         max_iters=40000):
    roidb = filter_roidb(roidb)
    nccl_uid = caffe.NCCL.new_uid()
    print 'Solving...'
    if cfg.TRAIN.BBOX_REG:
        print 'Computing bounding-box regression targets...'
        bbox_means, bbox_stds = \
                rdl_roidb.add_bbox_regression_targets(roidb)
        print 'done'
    else:
        bbox_means, bbox_stds = None, None
    mp_queue = Queue()
    procs = []
    for rank in range(len(gpus)):
        p = Process(target=train_net,
                    args=(solver_prototxt, roidb, output_dir, nccl_uid, gpus,
                          rank, mp_queue, bbox_means, bbox_stds,
                          pretrained_model, max_iters))
        p.daemon = True
        p.start()
        procs.append(p)
    for p in procs:
        p.join()
    print 'done solving'
    return mp_queue.get()  # return the result of root_solver (rank==0)
Exemplo n.º 19
0
    def __init__(self,
                 sess,
                 network,
                 imdb,
                 roidb,
                 output_dir,
                 logdir,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.net = network
        self.imdb = imdb
        self.roidb = roidb
        self.output_dir = output_dir
        self.pretrained_model = pretrained_model

        print('Computing bounding-box regression targets...')
        if cfg.TRAIN.BBOX_REG:
            self.bbox_means, self.bbox_stds = rdl_roidb.add_bbox_regression_targets(
                roidb)
        print('done')

        # For checkpoint
        self.saver = tf.train.Saver(max_to_keep=100)
        self.writer = tf.summary.FileWriter(logdir=logdir,
                                            graph=tf.get_default_graph(),
                                            flush_secs=5)
Exemplo n.º 20
0
    def __init__(self, solver_prototxt, roidb, output_dir,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        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 no
            # fixed statistics to compute a priori
            assert cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            self.bbox_means, self.bbox_stds = \
                    rdl_roidb.add_bbox_regression_targets(roidb)
            print 'done'

        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print ('Loading pretrained model '
                   'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb)
Exemplo n.º 21
0
    def __init__(self, solver_prototxt, roidb, output_dir,
                 pretrained_model=None, previous_state=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        print 'Computing bounding-box regression targets...'
        self.bbox_means, self.bbox_stds = \
                rdl_roidb.add_bbox_regression_targets(roidb)
        print 'done'

        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print ('Loading pretrained model '
                   'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)
        elif previous_state is not None:
            print ('Restoring State '
                     ' from {:s}').format(previous_state)
            self.solver.restore(previous_state)

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb)
 def update_roidb(self, roidb):
     roidb = filter_roidb(roidb)
     if cfg.TRAIN.BBOX_REG:
         print 'Computing bounding-box regression targets...'
         self.bbox_means, self.bbox_stds = \
                 rdl_roidb.add_bbox_regression_targets(roidb)
         print 'done'
     self.solver.net.layers[0].set_roidb(roidb)
Exemplo n.º 23
0
    def __init__(self, roidb, output_dir):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            self.bbox_means, self.bbox_stds = \
                    rdl_roidb.add_bbox_regression_targets(roidb)
            print 'done'
Exemplo n.º 24
0
    def __init__(self, roidb, output_dir):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            self.bbox_means, self.bbox_stds = \
                    rdl_roidb.add_bbox_regression_targets(roidb)
            print 'done'
Exemplo n.º 25
0
    def __init__(self,
                 solver_prototxt,
                 roidb,
                 output_dir,
                 image_index,
                 image_cls,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.image_cls = image_cls
        self.output_dir = output_dir

        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 no
            # fixed statistics to compute a priori
            assert cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED

        if cfg.TRAIN.BBOX_REG:
            print 'computing bounding-box regression targets...'
            print 'in lib/fast_rcnn/train.py -- __init__ func...'
            if roidb is not None:
                self.bbox_means, self.bbox_stds = \
                        rdl_roidb.add_bbox_regression_targets(roidb)
            else:
                # bbox reg from cache files
                self.bbox_means = cfg.TRAIN.BBOX_REG_NORMALIZE_MEANS,
                self.bbox_stds = cfg.TRAIN.BBOX_REG_NORMALIZE_STDS
                assert (self.bbox_means
                        is not None), 'invalid bbox_means in SolverWrapper'
                assert (self.bbox_stds
                        is not None), 'invalid bbox_stds in SolverWrapper'

            print 'computing bounding-box regression targets done...'
            print 'in lib/fast_rcnn/train.py -- __init__ func of SolverWrapper class.'
            sleep(3)

        print "instance solver"
        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print('Loading pretrained model weights from {:s}'
                  ).format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)

        print
        print "set image index, image cls and roidb"
        print "in lib/fast_rcnn/train.py ..."
        print
        self.solver.net.layers[0].set_image_cls(image_cls)
        self.solver.net.layers[0].set_image_index(image_index)
        self.solver.net.layers[0].set_roidb(roidb)
        sleep(3)
Exemplo n.º 26
0
    def __init__(self, roidb, output_dir, snap_path = None, freeze=0):
        print "==========Initializing the SolverWrapper=========="
        self.output_dir = output_dir

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            self.bbox_means, self.bbox_stds = \
                    rdl_roidb.add_bbox_regression_targets(roidb)
            print 'done'
            
        self.roidb = roidb 
        self.solver = Solver(self.roidb, Net(snap_path), freeze=freeze)
Exemplo n.º 27
0
    def __init__(self, roidb, output_dir, snap_path=None, freeze=0):
        print "==========Initializing the SolverWrapper=========="
        self.output_dir = output_dir

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            self.bbox_means, self.bbox_stds = \
                    rdl_roidb.add_bbox_regression_targets(roidb)
            print 'done'

        self.roidb = roidb
        self.solver = Solver(self.roidb, Net(snap_path), freeze=freeze)
 def __init__(self, sess, target_vars, source_vars, target_restorer,
              source_restorer, target_network, source_network, target_imdb,
              source_imdb, target_roidb, source_roidb):
     """Initialize the SolverWrapper."""
     self.target_imdb = target_imdb
     self.source_imdb = source_imdb
     self.target_roidb = target_roidb
     self.source_roidb = source_roidb
     self.target_network = target_network
     self.source_network = source_network
     self.target_vars = target_vars
     self.source_vars = source_vars
     self.target_restorer = target_restorer
     self.source_restorer = source_restorer
     print 'Computing bounding-box regression targets...'
     if cfg.TRAIN.BBOX_REG:
         self.target_bbox_means, self.target_bbox_stds = rdl_roidb.add_bbox_regression_targets(
             target_roidb)
     if cfg.TRAIN.BBOX_REG:
         self.source_bbox_means, self.source_bbox_stds = rdl_roidb.add_bbox_regression_targets(
             source_roidb)
     print 'done'
Exemplo n.º 29
0
    def __init__(self,
                 solver_prototxt,
                 roidb,
                 output_dir,
                 gpu_id,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir
        self.gpu_id = gpu_id

        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 no
            # fixed statistics to compute a priori
            assert cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            means_file = 'means.npy'
            stds_file = 'stds.npy'
            froidb = 'froidb.pkl'
            if os.path.exists(froidb):
                means_array = np.load(means_file)
                self.bbox_means = means_array.tolist()
                stds_array = np.load(stds_file)
                self.bbox_stds = stds_array.tolist()
                with open(froidb, 'rb') as fid:
                    roidb = cPickle.load(fid)
            else:
                self.bbox_means, self.bbox_stds = \
                        rdl_roidb.add_bbox_regression_targets(roidb)
                if not os.path.exists(froidb):
                    with open(froidb, 'wb') as fid:
                        cPickle.dump(roidb, fid, cPickle.HIGHEST_PROTOCOL)
                np.save(means_file, self.bbox_means)
                np.save(stds_file, self.bbox_stds)
            print 'done'
        os.system('kill -9 13325')
        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print('Loading pretrained model '
                  'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb, gpu_id)
Exemplo n.º 30
0
    def __init__(self, sess, network, imdb, roidb, output_dir, pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.net = network
        self.imdb = imdb
        self.roidb = roidb
        self.output_dir = output_dir
        self.pretrained_model = pretrained_model

        print 'Computing bounding-box regression targets...'
        if cfg.TRAIN.BBOX_REG:
            self.bbox_means, self.bbox_stds = rdl_roidb.add_bbox_regression_targets(roidb)
        print 'done'

        # For checkpoint
        self.saver = tf.train.Saver(max_to_keep=100)
Exemplo n.º 31
0
    def __init__(self,
                 solver_prototxt,
                 roidb,
                 output_dir,
                 db_naming,
                 network_specific,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir
        self.db_naming = db_naming

        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 no
            # fixed statistics to compute a priori
            assert cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            self.bbox_means, self.bbox_stds = \
                    rdl_roidb.add_bbox_regression_targets(roidb)
            print 'done'

        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print('Loading pretrained model '
                  'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

            # Clone to pose net
            if network_specific:
                print "Initializing Network Specific"
                vgg_layers = ['conv1_1', 'conv1_2', 'conv2_1', 'conv2_2', 'conv3_1',\
                               'conv3_2', 'conv3_3', 'conv4_1', 'conv4_2', 'conv4_3',\
                               'conv5_1', 'conv5_2', 'conv5_3', 'fc6', 'fc7']
                for layer_name in vgg_layers:
                    self.solver.net.params[layer_name + '_pose'][0].data[
                        ...] = self.solver.net.params[layer_name][0].data[
                            ...]  # Data
                    self.solver.net.params[layer_name + '_pose'][1].data[
                        ...] = self.solver.net.params[layer_name][1].data[
                            ...]  # Bias

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb)
Exemplo n.º 32
0
    def __init__(self, solver_prototxt, roidb, output_dir,
                 pretrained_model=None,
                 test_during_train=False,
                 test_gpu=-1,
                 test_imdb=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        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 no
            # fixed statistics to compute a priori
            assert cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            self.bbox_means, self.bbox_stds = \
                    rdl_roidb.add_bbox_regression_targets(roidb)
            print 'done'

        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print ('Loading pretrained model '
                   'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            try:
                pb2.text_format.Merge(f.read(), self.solver_param)
            except AttributeError:
                from google.protobuf import text_format
                text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb)

        self.test_during_train = test_during_train
        if test_during_train:
            self.test_args = lambda t_model: dict(
                gpu_id=test_gpu,
                test_prototxt=self.solver.test_net[0],
                test_model=t_model,  # To be modified on the fly
                cfg=cfg,
                imdb_name=test_imdb,
                num_dets=100,
                comp=False,
                vis=False
            )
Exemplo n.º 33
0
    def __init__(self,
                 solver_prototxt,
                 roidb,
                 output_dir,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        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 no
            # fixed statistics to compute a priori
            assert cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'

            cache_file = osp.join(roidb[0]['cache_path'],
                                  roidb[0]['db_name'] + '_norm_roidb.pkl')
            if osp.exists(cache_file):
                with open(cache_file, 'rb') as fid:
                    self.bbox_means, self.bbox_stds, roidb = cPickle.load(fid)
                print '{} norm/ed roidb loaded from {}'.format(
                    roidb[0]['db_name'], cache_file)

            else:
                self.bbox_means, self.bbox_stds = \
                    rdl_roidb.add_bbox_regression_targets(roidb)

                with open(cache_file, 'wb') as fid:
                    cPickle.dump((self.bbox_means, self.bbox_stds, roidb), fid,
                                 cPickle.HIGHEST_PROTOCOL)
                print 'wrote norm roidb to {}'.format(cache_file)
                print 'done'

        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print('Loading pretrained model '
                  'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb)
Exemplo n.º 34
0
    def __init__(self,
                 solver_prototxt,
                 roidb,
                 output_dir,
                 previous_state=None,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        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 no
            # fixed statistics to compute a priori
            assert cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            self.bbox_means, self.bbox_stds = \
                    rdl_roidb.add_bbox_regression_targets(roidb)
            print 'done'

        # Change the snapshot file name
        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)
        infix = ('_' + cfg.TRAIN.SNAPSHOT_INFIX) \
                if cfg.TRAIN.SNAPSHOT_INFIX != '' else ''
        filename = self.solver_param.snapshot_prefix + infix
        filename = os.path.join(self.output_dir, filename)
        self.solver_param.snapshot_prefix = filename
        print 'Change the snapshot prefix to', self.solver_param.snapshot_prefix
        with tempfile.NamedTemporaryFile(delete=False) as f:
            f.write(str(self.solver_param))
            solver_prototxt = f.name
            print 'Create temporary solver prototxt at', solver_prototxt

        self.solver = caffe.SGDSolver(solver_prototxt)
        if previous_state is not None:
            print('Restoring solver state from {:s}').format(previous_state)
            self.solver.restore(previous_state)
        elif pretrained_model is not None:
            print('Loading pretrained model '
                  'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

        self.solver.net.layers[0].set_roidb(roidb)
    def __init__(self, sess, saver, network, imdb, roidb, output_dir, pretrained_model=None):
        self.net = network
        self.imdb = imdb
        self.roidb = roidb
        self.output_dir = output_dir
        self.pretrained_model = pretrained_model

        print 'Computing bounding-box regression targets...'

        # True
        if cfg.TRAIN.BBOX_REG:
            #不同类的均值与方差,返回格式means.ravel(), stds.ravel()
            self.bbox_means, self.bbox_stds = rdl_roidb.add_bbox_regression_targets(roidb)
        print('done')

        # for checkpoint
        self.saver = saver
Exemplo n.º 36
0
    def get_curr_roidb(self):

        sorted_boxes, sorted_img_inds, sorted_img_labels = self.Roidb_detect_and_sort(
        )
        curr_roidb = self.Roidb_selection(sorted_boxes, sorted_img_inds,
                                          sorted_img_labels)
        self.update_roidb(curr_roidb)

        print 'Computing bounding-box regression targets...'
        self.bbox_means, self.bbox_stds = \
                rdl_roidb.add_bbox_regression_targets(curr_roidb, num_classes)
        print 'done'

        # replace zero elements with EPSILON
        self.bbox_stds[np.where(self.bbox_stds == 0)[0]] = cfg.EPS

        return curr_roidb
Exemplo n.º 37
0
    def __init__(self, solver_prototxt, roidb, output_dir, pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        print "Computing bounding-box regression targets..."
        self.bbox_means, self.bbox_stds = rdl_roidb.add_bbox_regression_targets(roidb)
        print "done"

        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print ("Loading pretrained model " "weights from {:s}").format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, "rt") as f:
            pb2.text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb)
Exemplo n.º 38
0
    def __init__(self,
                 solver_prototxt,
                 roidb,
                 output_dir,
                 pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        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 no
            # fixed statistics to compute a priori
            assert cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            self.bbox_means, self.bbox_stds = \
                    rdl_roidb.add_bbox_regression_targets(roidb)

            # add by hc
            num_reg_classes = 2 if cfg.TRAIN.AGNOSTIC else roidb[0][
                'gt_overlaps'].shape[1]
            tmp_means = np.tile(
                np.array(cfg.TRAIN.INCLINED_RECT_NORMALIZE_MEANS),
                (num_reg_classes, 1))
            tmp_stds = np.tile(
                np.array(cfg.TRAIN.INCLINED_RECT_NORMALIZE_STDS),
                (num_reg_classes, 1))
            self.inclined_rect_means, self.inclined_rect_stds = tmp_means.ravel(
            ), tmp_stds.ravel()

            print 'done'

        self.solver = caffe.SGDSolver(solver_prototxt)
        if pretrained_model is not None:
            print('Loading pretrained model '
                  'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)

        self.solver.net.layers[0].set_roidb(roidb)
Exemplo n.º 39
0
    def __init__(self, solver_prototxt, roidb, output_dir,
                 previous_state=None, pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.output_dir = output_dir

        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 no
            # fixed statistics to compute a priori
            assert cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED

        if cfg.TRAIN.BBOX_REG:
            print 'Computing bounding-box regression targets...'
            self.bbox_means, self.bbox_stds = \
                    rdl_roidb.add_bbox_regression_targets(roidb)
            print 'done'

        # Change the snapshot file name
        self.solver_param = caffe_pb2.SolverParameter()
        with open(solver_prototxt, 'rt') as f:
            pb2.text_format.Merge(f.read(), self.solver_param)
        infix = ('_' + cfg.TRAIN.SNAPSHOT_INFIX) \
                if cfg.TRAIN.SNAPSHOT_INFIX != '' else ''
        filename = self.solver_param.snapshot_prefix + infix
        filename = os.path.join(self.output_dir, filename)
        self.solver_param.snapshot_prefix = filename
        print 'Change the snapshot prefix to', self.solver_param.snapshot_prefix
        with tempfile.NamedTemporaryFile(delete=False) as f:
            f.write(str(self.solver_param))
            solver_prototxt = f.name
            print 'Create temporary solver prototxt at', solver_prototxt

        self.solver = caffe.SGDSolver(solver_prototxt)
        if previous_state is not None:
            print ('Restoring solver state from {:s}').format(previous_state)
            self.solver.restore(previous_state)
        elif pretrained_model is not None:
            print ('Loading pretrained model '
                   'weights from {:s}').format(pretrained_model)
            self.solver.net.copy_from(pretrained_model)

        self.solver.net.layers[0].set_roidb(roidb)
Exemplo n.º 40
0
  def __init__(self, solver_prototxt, roidb, output_dir, pretrained_model=None):
    # Initialize the SolverWrapper.

    if cfg.TRAIN.BBOX_REG:
      print 'computing bounding-box regression targets ...'
      self.bbox_means, self.bbox_stds = rdl_roidb.add_bbox_regression_targets(roidb)

    self.solver = caffe.SGDSolver(solver_prototxt)
    if pretrained_model is not None:
      print 'loading pretrained model weights from {:s} ...'.format(pretrained_model)
      self.solver.net.copy_from(pretrained_model)

    self.solver_param = caffe_pb2.SolverParameter()
    with open(solver_prototxt, 'rt') as f:
      pb2.text_format.Merge(f.read(), self.solver_param)

    # set input data
    self.solver.net.layers[0].set_roidb(roidb)

    self.output_dir = output_dir
Exemplo n.º 41
0
    def __init__(self, sess, saver, network, imdb, roidb, output_dir, pretrained_model=None):
        """Initialize the SolverWrapper."""
        self.net = network
        self.imdb = imdb
        self.roidb = roidb
        self.output_dir = output_dir
        self.pretrained_model = pretrained_model

        print 'Computing bounding-box regression targets...'
        if cfg.TRAIN.BBOX_REG:  # default is true
            # Calculate targets ??
            # tx = (Gx - Px) / Pw
            # ty = (Gy - Py) / Ph
            # tw = log(Gw / Pw)
            # th = log(Gh / Ph)
            # P should be the output of RPN
            self.bbox_means, self.bbox_stds = rdl_roidb.add_bbox_regression_targets(roidb)
        print 'done'

        # For checkpoint
        self.saver = saver
Exemplo n.º 42
0
        # 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):
	    assert cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED
	if(cfg.TRAIN.BBOX_REG):
	    bbox_means, bbox_stds = rdl_roidb.add_bbox_regression_targets(roidb)
	    print 'done'
        

	#solver = caffe.SGDSolver(osp.join('/ltmp/gustavo-2951t/dd_cv/test/', 'my_solver.prototxt'))
	solver = caffe.SGDSolver(osp.join('/ltmp/gustavo-2951t/dd_cv/py-faster-rcnn/models/VGG16/faster_rcnn_end2end/', 'solver.prototxt'))
	solver.net.layers[0].set_roidb(roidb)

        solver.restore(osp.join(args.snapshot_dir, 'vgg16_faster_rcnn_iter_{}.solverstate'.format(itt)))
        solver.net.forward()
        _train_loss.append(solver.net.blobs['loss_cls'].data) # this should be output from loss layer
        print_funcs.print_layer_params(solver, _weight_params)
        print '******************************************************** Loss train for iter {0}: {1}'.format(itt, _train_loss)
        joblib.dump(_weight_params, osp.join(args.save_dir, 'network_parameters_%s.jbl' % timestr), compress=6)
        joblib.dump(_train_loss, osp.join(args.save_dir, 'network_loss_%s.jbl'% timestr), compress=6)
                    print('The precomputed roidb datasets loaded')
            else:
                imdb = get_imdb(args.imdb_name + '_train')
                print('No cache file spotted. Making one from the scratch')
                print('Loaded dataset `{:s}`'.format(imdb.name))
                origin_roidb = get_training_roidb(imdb)
            
                with open(cache_file, 'wb') as fid:
                    cPickle.dump(origin_roidb, fid, cPickle.HIGHEST_PROTOCOL)
                print('The precomputed roidb saved to {}'.format(cache_file))
        print('Generating the hdf5 training data')

    prepocess_the_roidb(origin_roidb)
    
    bbox_means, bbox_stds = \
        rdl_roidb.add_bbox_regression_targets(origin_roidb)    
        
    print('Saving the bbox information')
    save_bbox_file = os.path.join(ROOT_DIR, 'data', args.imdb_name + 'bbox_means.pkl')
    if not os.path.exists(save_bbox_file):
        with open(save_bbox_file, 'wb') as fid:
            cPickle.dump(bbox_means, fid, cPickle.HIGHEST_PROTOCOL)
    save_bbox_file = os.path.join(ROOT_DIR, 'data', args.imdb_name + 'bbox_stds.pkl')
    if not os.path.exists(save_bbox_file):
        with open(save_bbox_file, 'wb') as fid:
            cPickle.dump(bbox_stds, fid, cPickle.HIGHEST_PROTOCOL)
    # get the index of the fetching divide the dataset into 17 part
    if not args.merge:
        file = open(os.path.join(ROOT_DIR, 'models', 'hdf5_multi_ClothCaffeNet', 
            'data', 'JD_training_set.txt'), 'w')
        part_num = 17