예제 #1
0
def create(model_type_func, train=False, gpu_id=0):
    """Generic model creation function that dispatches to specific model
    building functions.

    By default, this function will generate a data parallel model configured to
    run on cfg.NUM_GPUS devices. However, you can restrict it to build a model
    targeted to a specific GPU by specifying gpu_id. This is used by
    optimizer.build_data_parallel_model() during test time.
    """
    model = DetectionModelHelper(name=model_type_func,
                                 train=train,
                                 num_classes=cfg.MODEL.NUM_CLASSES,
                                 init_params=train)
    model.only_build_forward_pass = False
    model.target_gpu_id = gpu_id

    model = get_func(model_type_func)(model)

    if cfg.CASCADE_RCNN.SCALE_GRAD:
        # replace the grad op's "scale" by "scale_grad"
        # so the fprop and bprop scale can be different
        for o in model.net.Proto().op:
            if hasattr(o, "arg") and o.is_gradient_op:
                value = None
                for a in o.arg:
                    if a.name == "scale_grad":
                        value = a.f
                if value is None:
                    continue
                for a in o.arg:
                    if a.name == "scale":
                        a.f = value
    return model
예제 #2
0
def create(model_type_func, train=False, gpu_id=0):
    """Generic model creation function that dispatches to specific model
    building functions.

    By default, this function will generate a data parallel model configured to
    run on cfg.NUM_GPUS devices. However, you can restrict it to build a model
    targeted to a specific GPU by specifying gpu_id. This is used by
    optimizer.build_data_parallel_model() during test time.
    """
    model = DetectionModelHelper(
        name=model_type_func,
        train=train,
        num_classes=cfg.MODEL.NUM_CLASSES,
        init_params=(train or cfg.VIS_NET)
    )
    model.only_build_forward_pass = False
    model.target_gpu_id = gpu_id
    model = get_func(model_type_func)(model)
    # Stop gradient at specified nodes
    if len(cfg.TRAIN.FREEZE_BLOBS):
        blob_references = []
        for gpu_id in range(cfg.NUM_GPUS):
            with c2_utils.NamedCudaScope(gpu_id):
                for blob_name in cfg.TRAIN.FREEZE_BLOBS:
                    try:
                        blob_references.append(model.net.GetBlobRef(core.ScopedName(blob_name)))
                    except KeyError, e:
                        logger.warn('Failed to freeze blob. {}'.format(e))
                        if not cfg.CONTINUE_ON_ERROR:
                            raise
        for blob_ref in blob_references:
            logger.info('Freezing blob. {}'.format(blob_ref))
            model.StopGradient(blob_ref, blob_ref)
예제 #3
0
 def _add_track_outputs(self, X, track_n_rois):
     model = DetectionModelHelper(train=False, num_classes=1)
     X = model.GivenTensorFill([], 'X', values=X, shape=X.shape)
     add_track_outputs(model, X, cfg.TRCNN.MLP_HEAD_DIM)
     workspace.FeedBlob('track_n_rois', track_n_rois)
     workspace.FeedBlob('track_n_rois_one', np.array([track_n_rois[0]]))
     workspace.FeedBlob('track_n_rois_two', np.array([track_n_rois[1]]))
     workspace.RunNetOnce(model.net)
     return workspace.FetchBlob('track_similarity')
예제 #4
0
def create(model_type_func, train=False, gpu_id=0):
    """Generic model creation function that dispatches to specific model
    building functions.

    By default, this function will generate a data parallel model configured to
    run on cfg.NUM_GPUS devices. However, you can restrict it to build a model
    targeted to a specific GPU by specifying gpu_id. This is used by
    optimizer.build_data_parallel_model() during test time.
    """
    model = DetectionModelHelper(name=model_type_func,
                                 train=train,
                                 num_classes=cfg.MODEL.NUM_CLASSES,
                                 init_params=train)
    model.only_build_forward_pass = False
    model.target_gpu_id = gpu_id
    return get_func(model_type_func)(model)
예제 #5
0
 def _add_track_losses(self, X, X_gt):
     model = DetectionModelHelper(train=False, num_classes=1)
     add_track_losses(model)
     workspace.FeedBlob('track_similarity', X)
     workspace.FeedBlob('track_int32', X_gt)
     workspace.RunNetOnce(model.net)
     return workspace.FetchBlob('loss_track')
예제 #6
0
def create(model_type_func, train=False, gpu_id=0):
    """Generic model creation function that dispatches to specific model
    building functions.

    By default, this function will generate a data parallel model configured to
    run on cfg.NUM_GPUS devices. However, you can restrict it to build a model
    targeted to a specific GPU by specifying gpu_id. This is used by
    optimizer.build_data_parallel_model() during test time.
    """
    model = DetectionModelHelper(
        name=model_type_func,
        train=train,
        num_classes=cfg.MODEL.NUM_CLASSES,
        init_params=train
    )
    model.only_build_forward_pass = False
    model.target_gpu_id = gpu_id
    return get_func(model_type_func)(model)
예제 #7
0
def create_model(train, cfg, output_dir):

    logger = logging.getLogger(__name__)
    start_iter = 0
    checkpoints = {}
    weights_file = None
    if cfg.TRAIN.AUTO_RESUME:
        # Check for the final model (indicates training already finished)
        final_path = os.path.join(output_dir, 'model_final.pkl')
        if os.path.exists(final_path):
            logger.info('model_final.pkl exists; no need to train!')
            return None, None, None, {'final': final_path}, output_dir

        # Find the most recent checkpoint (highest iteration number)
        files = os.listdir(output_dir)
        for f in files:
            iter_string = re.findall(r'(?<=model_iter)\d+(?=\.pkl)', f)
            if len(iter_string) > 0:
                checkpoint_iter = int(iter_string[0])
                if checkpoint_iter > start_iter:
                    # Start one iteration immediately after the checkpoint iter
                    start_iter = checkpoint_iter + 1
                    resume_weights_file = f

        if start_iter > 0:
            # Override the initialization weights with the found checkpoint
            weights_file = os.path.join(output_dir, resume_weights_file)
            logger.info(
                '========> Resuming from checkpoint {} at start iter {}'.
                format(weights_file, start_iter))

    logger.info('Building model: {}'.format(cfg.MODEL.TYPE))
    model = DetectionModelHelper(name="fusion",
                                 train=train,
                                 num_classes=cfg.MODEL.NUM_CLASSES,
                                 init_params=train)
    model.only_build_forward_pass = False
    model.target_gpu_id = gpu_id

    def _single_gpu_build_func(model):
        loss = {"loss": None}
        p = model.Conv("data_stage2",
                       'conv1_stage2',
                       40,
                       20,
                       kernel=3,
                       pad=1,
                       stride=1,
                       no_bias=1,
                       weight_init=(cfg.MRCNN.CONV_INIT, {
                           'std': 0.001
                       }))
        p = model.AffineChannel(p, 'conv1_bn_stage2', dim=20, inplace=True)
        p = model.Relu(p, p)
        human_fc = model.Conv(p,
                              'conv2_stage2',
                              20,
                              20,
                              kernel=1,
                              pad=0,
                              stride=1,
                              weight_init=(cfg.MRCNN.CONV_INIT, {
                                  'std': 0.001
                              }))
        if not model.train:
            model.net.NCHW2NHWC(human_fc, 'seg_score_NHWC_stage2')
            model.net.Softmax('seg_score_NHWC_stage2',
                              'probs_human_NHWC_stage2',
                              axis=3)
            model.net.NHWC2NCHW('probs_human_NHWC_stage2',
                                'probs_human_NCHW_stage2')
        loss_gradient = None
        if model.train:
            model.net.NCHW2NHWC(human_fc, 'seg_score_NHWC_stage2')
            model.Reshape(
                'seg_score_NHWC_stage2',
                ['seg_score_reshape_stage2', 'seg_score_old_shape_stage2'],
                shape=[-1, model.num_classes])
            model.Reshape('gt_label_stage2',
                          ['gt_label_reshape_stage2', 'gt_label_shape_stage2'],
                          shape=[
                              -1,
                          ])

            probs_human, loss_human = model.net.SoftmaxWithLoss(
                ['seg_score_reshape_stage2', 'gt_label_reshape_stage2'],
                ['probs_human_stage2', 'loss_human_stage2'],
                scale=1. / cfg.NUM_GPUS)
            loss_gradient = blob_utils.get_loss_gradients(model, [loss_human])
            model.AddLosses('loss_human_stage2')
        loss['loss'] = loss_gradient

        if model.train:
            loss_gradients = {}
            for lg in loss.values():
                if lg is not None:
                    loss_gradients.update(lg)
            return loss_gradients
        else:
            return None

    optim.build_data_parallel_model(model, _single_gpu_build_func)

    # Performs random weight initialization as defined by the model
    workspace.RunNetOnce(model.param_init_net)
    return model, weights_file, start_iter, checkpoints