Python caffe2.python.workspace.RunNetOnce() Examples

The following are 30 code examples of caffe2.python.workspace.RunNetOnce(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module caffe2.python.workspace , or try the search function .
Example #1
Source File: test_engine.py    From DetectAndTrack with Apache License 2.0 6 votes vote down vote up
def initialize_model_from_cfg():
    def create_input_blobs(net_def):
        for op in net_def.op:
            for blob_in in op.input:
                if not workspace.HasBlob(blob_in):
                    workspace.CreateBlob(blob_in)

    model = model_builder.create(
        cfg.MODEL.TYPE, train=False,
        init_params=cfg.TEST.INIT_RANDOM_VARS_BEFORE_LOADING)
    model_builder.add_inputs(model)
    if cfg.TEST.INIT_RANDOM_VARS_BEFORE_LOADING:
        workspace.RunNetOnce(model.param_init_net)
    net_utils.initialize_from_weights_file(
        model, cfg.TEST.WEIGHTS, broadcast=False)
    create_input_blobs(model.net.Proto())
    workspace.CreateNet(model.net)
    workspace.CreateNet(model.conv_body_net)
    if cfg.MODEL.MASK_ON:
        create_input_blobs(model.mask_net.Proto())
        workspace.CreateNet(model.mask_net)
    if cfg.MODEL.KEYPOINTS_ON:
        create_input_blobs(model.keypoint_net.Proto())
        workspace.CreateNet(model.keypoint_net)
    return model 
Example #2
Source File: model_utils.py    From models with Apache License 2.0 6 votes vote down vote up
def load_model_pb(net_file, init_file=None, is_run_init=True, is_create_net=True):
    net = core.Net("net")
    if net_file is not None:
        net.Proto().ParseFromString(open(net_file, "rb").read())

    if init_file is None:
        fn, ext = os.path.splitext(net_file)
        init_file = fn + "_init" + ext

    init_net = caffe2_pb2.NetDef()
    init_net.ParseFromString(open(init_file, "rb").read())

    if is_run_init:
        workspace.RunNetOnce(init_net)
        create_blobs_if_not_existed(net.external_inputs)
        if net.Proto().name == "":
            net.Proto().name = "net"
    if is_create_net:
        workspace.CreateNet(net)

    return (net, init_net) 
Example #3
Source File: test_ops_nn.py    From ngraph-python with Apache License 2.0 6 votes vote down vote up
def test_AveragedLoss():
    workspace.ResetWorkspace()
    shape = (32,)

    net = core.Net("net")
    X = net.GivenTensorFill([], "Y", shape=shape, values=np.random.uniform(-1, 1, shape))
    X.AveragedLoss([], ["loss"])

    # Execute via Caffe2
    workspace.RunNetOnce(net)

    # Import caffe2 network into ngraph
    importer = C2Importer()
    importer.parse_net_def(net.Proto(), verbose=False)

    # Get handle
    f_ng = importer.get_op_handle("loss")

    # Execute
    with ExecutorFactory() as ex:
        f_result = ex.executor(f_ng)()

        assert(np.allclose(f_result, workspace.FetchBlob("loss"), equal_nan=False)) 
Example #4
Source File: test_ops_nn.py    From ngraph-python with Apache License 2.0 6 votes vote down vote up
def test_SquaredL2Distance():
    workspace.ResetWorkspace()
    shape = (10, 10)

    net = core.Net("net")
    Y = net.GivenTensorFill([], "Y", shape=shape, values=np.random.uniform(-1, 1, shape))
    T = net.GivenTensorFill([], "T", shape=shape, values=np.random.uniform(-1, 1, shape))
    net.SquaredL2Distance([Y, T], "dist")

    # Execute via Caffe2
    workspace.RunNetOnce(net)

    # Import caffe2 network into ngraph
    importer = C2Importer()
    importer.parse_net_def(net.Proto(), verbose=False)

    # Get handle
    f_ng = importer.get_op_handle("dist")

    # Execute
    with ExecutorFactory() as ex:
        f_result = ex.executor(f_ng)()

        assert(np.allclose(f_result, workspace.FetchBlob("dist"), equal_nan=False)) 
Example #5
Source File: dlrm_s_caffe2.py    From dlrm with MIT License 6 votes vote down vote up
def create_model(self, X, S_lengths, S_indices, T):
        #setup tril indices for the interactions
        offset = 1 if self.arch_interaction_itself else 0
        num_fea = len(self.emb_l) + 1
        tril_indices = np.array([j + i * num_fea
                                 for i in range(num_fea) for j in range(i + offset)])
        self.FeedBlobWrapper(self.tint + "_tril_indices", tril_indices)

        # create compute graph
        if T is not None:
            # WARNING: RunNetOnce call is needed only if we use brew and ConstantFill.
            # We could use direct calls to self.model functions above to avoid it
            workspace.RunNetOnce(self.model.param_init_net)
            workspace.CreateNet(self.model.net)
            if self.test_net is not None:
                workspace.CreateNet(self.test_net) 
Example #6
Source File: model_utils.py    From inference with Apache License 2.0 6 votes vote down vote up
def load_model_pb(net_file, init_file=None, is_run_init=True, is_create_net=True):
    net = core.Net("net")
    if net_file is not None:
        net.Proto().ParseFromString(open(net_file, "rb").read())

    if init_file is None:
        fn, ext = os.path.splitext(net_file)
        init_file = fn + "_init" + ext

    init_net = caffe2_pb2.NetDef()
    init_net.ParseFromString(open(init_file, "rb").read())

    if is_run_init:
        workspace.RunNetOnce(init_net)
        create_blobs_if_not_existed(net.external_inputs)
        if net.Proto().name == "":
            net.Proto().name = "net"
    if is_create_net:
        workspace.CreateNet(net)

    return (net, init_net) 
Example #7
Source File: test_ops_constant.py    From ngraph-python with Apache License 2.0 6 votes vote down vote up
def test_constant():
    workspace.ResetWorkspace()

    shape = [10, 10]
    val = random.random()
    net = core.Net("net")
    net.ConstantFill([], ["Y"], shape=shape, value=val, run_once=0, name="Y")

    # Execute via Caffe2
    workspace.RunNetOnce(net)

    # Import caffe2 network into ngraph
    importer = C2Importer()
    importer.parse_net_def(net.Proto(), verbose=False)

    # Get handle
    f_ng = importer.get_op_handle("Y")

    # Execute
    with ExecutorFactory() as ex:
        f_result = ex.executor(f_ng)()

        # compare Caffe2 and ngraph results
        assert(np.ma.allequal(f_result, workspace.FetchBlob("Y")))
        assert(np.isclose(f_result[0][0], val, atol=1e-6, rtol=0)) 
Example #8
Source File: test_ops_unary.py    From ngraph-python with Apache License 2.0 5 votes vote down vote up
def run_all_close_compare_initiated_with_random_gauss(c2_op_name,
                                                      shape=None,
                                                      data=None,
                                                      expected=None):
    workspace.ResetWorkspace()
    if not shape:
        shape = [2, 7]
    if not data:
        data = [random.gauss(mu=0, sigma=10) for i in range(np.prod(shape))]

    net = core.Net("net")
    net.GivenTensorFill([], "X", shape=shape, values=data, name="X")
    getattr(net, c2_op_name)(["X"], ["Y"], name="Y")

    # Execute via Caffe2
    workspace.RunNetOnce(net)

    # Import caffe2 network into ngraph
    importer = C2Importer()
    importer.parse_net_def(net.Proto(), verbose=False)

    # Get handle
    f_ng = importer.get_op_handle("Y")

    # Execute
    with ExecutorFactory() as ex:
        f_result = ex.executor(f_ng)()

        c2_y = workspace.FetchBlob("Y")

        # compare Caffe2 and ngraph results
        assert(np.allclose(f_result, c2_y, atol=1e-4, rtol=0, equal_nan=False))

        # compare expected results and ngraph results
        if expected:
            assert(np.allclose(f_result, expected, atol=1e-3, rtol=0, equal_nan=False)) 
Example #9
Source File: test_loader.py    From Clustered-Object-Detection-in-Aerial-Image with Apache License 2.0 5 votes vote down vote up
def run_net(net):
    workspace.RunNetOnce(net)
    gpu_dev = core.DeviceOption(caffe2_pb2.CUDA, 0)
    name_scope = 'gpu_{}'.format(0)
    with core.NameScope(name_scope):
        with core.DeviceScope(gpu_dev):
            data = workspace.FetchBlob(core.ScopedName('data'))
            return data 
Example #10
Source File: test_ops_nn.py    From ngraph-python with Apache License 2.0 5 votes vote down vote up
def test_avgpool():
    workspace.ResetWorkspace()

    # shape is in NCHW format
    # [[shape], kernel, stride, caffe_padding_type]
    param_list = [[[1, 3, 10, 10], 2, 2, caffe2_legacy_pb2.NOTSET],
                  [[2, 3, 5, 5], 1, 1, caffe2_legacy_pb2.NOTSET],
                  [[2, 2, 7, 7], 3, 2, caffe2_legacy_pb2.NOTSET],
                  [[8, 5, 8, 8], 4, 4, caffe2_legacy_pb2.NOTSET],
                  [[8, 3, 4, 4], 3, 3, caffe2_legacy_pb2.VALID],
                  [[12, 6, 5, 5], 4, 3, caffe2_legacy_pb2.VALID],
                  [[8, 3, 4, 4], 3, 3, caffe2_legacy_pb2.SAME],
                  [[12, 6, 5, 5], 4, 3, caffe2_legacy_pb2.SAME]]

    for param_iter in param_list:
        shape, kernel, stride, pad_type = param_iter
        data1 = [random.gauss(mu=0, sigma=10) for i in range(np.prod(shape))]

        net = core.Net("net")
        net.GivenTensorFill([], ["X"], shape=shape, values=data1, name="X")
        net.AveragePool('X', 'Y', kernel=kernel, stride=stride, legacy_pad=pad_type)

        # Execute via Caffe2
        workspace.RunNetOnce(net)

        # Import caffe2 network into ngraph
        importer = C2Importer()
        importer.parse_net_def(net.Proto(), verbose=False)

        # Get handle
        f_ng = importer.get_op_handle("Y")

        # Execute
        with ExecutorFactory() as ex:
            f_result = ex.executor(f_ng)()

            # compare Caffe2 and ngraph results
            assert(np.allclose(f_result, workspace.FetchBlob("Y"),
                               atol=1e-4, rtol=1e-3, equal_nan=False)) 
Example #11
Source File: test_ops_nn.py    From ngraph-python with Apache License 2.0 5 votes vote down vote up
def test_convolution_nhwc_no_pad_no_bias():
    workspace.ResetWorkspace()

    # shape is in NCHW format
    # [batch, input_feature_map, spatial, output_feature_map, kernel, stride]
    n, ifm, spatial, ofm, kernel, stride = [2, 3, 8, 1, 2, 2]

    shape_x = (n, spatial, spatial, ifm)
    shape_w = (ofm, kernel, kernel, ifm)
    shape_b = (ofm, )

    data_x = [random.gauss(mu=0, sigma=10) for i in range(np.prod(shape_x))]
    data_w = [random.gauss(mu=0, sigma=10) for i in range(np.prod(shape_w))]
    data_b = [0. for i in range(np.prod(shape_b))]

    net = core.Net("net")
    X = net.GivenTensorFill([], ["X"], shape=shape_x, values=data_x, name="X")
    W = net.GivenTensorFill([], ["W"], shape=shape_w, values=data_w, name="W")
    B = net.GivenTensorFill([], ["B"], shape=shape_b, values=data_b, name="B")

    net.Conv([X, W, B], 'Y', kernel=kernel, stride=stride, order='NHWC')

    # Execute via Caffe2
    workspace.RunNetOnce(net)

    # Import caffe2 network into ngraph
    importer = C2Importer()
    importer.parse_net_def(net.Proto(), verbose=False)

    # Get handle
    f_ng = importer.get_op_handle("Y")

    # Execute
    with ExecutorFactory() as ex:
        f_result = ex.executor(f_ng)()

        # compare Caffe2 and ngraph results
        assert(np.allclose(f_result, workspace.FetchBlob("Y"), atol=1e-4, rtol=1e-3,
                           equal_nan=False)) 
Example #12
Source File: test_ops_nn.py    From ngraph-python with Apache License 2.0 5 votes vote down vote up
def test_convolution_nchw_no_pad_no_bias():
    workspace.ResetWorkspace()

    # shape is in NCHW format
    # [batch, input_feature_map, spatial, output_feature_map, kernel, stride]
    n, ifm, spatial, ofm, kernel, stride = [2, 3, 8, 1, 2, 2]

    shape_x = (n, ifm, spatial, spatial)
    shape_w = (ofm, ifm, kernel, kernel)
    shape_b = (ofm,)

    data_x = [random.gauss(mu=0, sigma=10) for i in range(np.prod(shape_x))]
    data_w = [random.gauss(mu=0, sigma=10) for i in range(np.prod(shape_w))]
    data_b = [0. for i in range(np.prod(shape_b))]

    net = core.Net("net")
    X = net.GivenTensorFill([], ["X"], shape=shape_x, values=data_x, name="X")
    W = net.GivenTensorFill([], ["W"], shape=shape_w, values=data_w, name="W")
    B = net.GivenTensorFill([], ["B"], shape=shape_b, values=data_b, name="B")

    net.Conv([X, W, B], 'Y', kernel=kernel, stride=stride, order='NCHW')

    # Execute via Caffe2
    workspace.RunNetOnce(net)

    # Import caffe2 network into ngraph
    importer = C2Importer()
    importer.parse_net_def(net.Proto(), verbose=False)

    # Get handle
    f_ng = importer.get_op_handle("Y")

    # Execute
    with ExecutorFactory() as ex:
        f_result = ex.executor(f_ng)()

        # compare Caffe2 and ngraph results
        assert (np.allclose(f_result, workspace.FetchBlob("Y"), atol=1e-4, rtol=1e-3,
                            equal_nan=False)) 
Example #13
Source File: common_caffe2.py    From optimized-models with Apache License 2.0 5 votes vote down vote up
def OptimizeTorchModel(init_def, predict_def, model_info, device_opts):
    ws.RunNetOnce(init_def)
    RemoveOutputInBN(predict_def)
    ClipToRelu(predict_def)
    FusePadConv(predict_def, model_info)
    MergeConvAdd(init_def, predict_def, model_info, ws, device_opts)
    MergeConvMulAdd(init_def, predict_def, model_info, ws, device_opts) 
Example #14
Source File: dlrm_s_caffe2.py    From optimized-models with Apache License 2.0 5 votes vote down vote up
def create_model(self, X, S_lengths, S_indices, T):
        #setup tril indices for the interactions
        offset = 1 if self.arch_interaction_itself else 0
        num_fea = len(self.emb_l) + 1
        tril_indices = np.array([j + i * num_fea
                                 for i in range(num_fea) for j in range(i + offset)])
        self.FeedBlobWrapper(self.tint + "_tril_indices", tril_indices)

        # create compute graph
        if T is not None:
            # WARNING: RunNetOnce call is needed only if we use brew and ConstantFill.
            # We could use direct calls to self.model functions above to avoid it
            workspace.RunNetOnce(self.model.param_init_net)
            workspace.CreateNet(self.model.net) 
Example #15
Source File: test_restore_checkpoint.py    From Clustered-Object-Detection-in-Aerial-Image with Apache License 2.0 5 votes vote down vote up
def init_weights(model):
    # init weights in gpu_id = 0 and then broadcast
    workspace.RunNetOnce(model.param_init_net)
    nu.broadcast_parameters(model) 
Example #16
Source File: test_loader.py    From seg_every_thing with Apache License 2.0 5 votes vote down vote up
def run_net(net):
    workspace.RunNetOnce(net)
    gpu_dev = core.DeviceOption(caffe2_pb2.CUDA, 0)
    name_scope = 'gpu_{}'.format(0)
    with core.NameScope(name_scope):
        with core.DeviceScope(gpu_dev):
            data = workspace.FetchBlob(core.ScopedName('data'))
            return data 
Example #17
Source File: test_restore_checkpoint.py    From seg_every_thing with Apache License 2.0 5 votes vote down vote up
def init_weights(model):
    # init weights in gpu_id = 0 and then broadcast
    workspace.RunNetOnce(model.param_init_net)
    nu.broadcast_parameters(model) 
Example #18
Source File: test_loader.py    From masktextspotter.caffe2 with Apache License 2.0 5 votes vote down vote up
def run_net(net):
    workspace.RunNetOnce(net)
    gpu_dev = core.DeviceOption(caffe2_pb2.CUDA, 0)
    name_scope = 'gpu_{}'.format(0)
    with core.NameScope(name_scope):
        with core.DeviceScope(gpu_dev):
            data = workspace.FetchBlob(core.ScopedName('data'))
            return data 
Example #19
Source File: train_net.py    From masktextspotter.caffe2 with Apache License 2.0 5 votes vote down vote up
def create_model():
    """Build the model and look for saved model checkpoints in case we can
    resume from one.
    """
    logger = logging.getLogger(__name__)
    start_iter = 0
    checkpoints = {}
    output_dir = get_output_dir(training=True)
    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, {'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
            cfg.TRAIN.WEIGHTS = os.path.join(output_dir, resume_weights_file)
            logger.info(
                '========> Resuming from checkpoint {} at start iter {}'.
                format(cfg.TRAIN.WEIGHTS, start_iter)
            )

    logger.info('Building model: {}'.format(cfg.MODEL.TYPE))
    model = model_builder.create(cfg.MODEL.TYPE, train=True)
    if cfg.MEMONGER:
        optimize_memory(model)
    # Performs random weight initialization as defined by the model
    workspace.RunNetOnce(model.param_init_net)
    return model, start_iter, checkpoints, output_dir 
Example #20
Source File: test_loader.py    From Detectron-Cascade-RCNN with Apache License 2.0 5 votes vote down vote up
def run_net(net):
    workspace.RunNetOnce(net)
    gpu_dev = core.DeviceOption(caffe2_pb2.CUDA, 0)
    name_scope = 'gpu_{}'.format(0)
    with core.NameScope(name_scope):
        with core.DeviceScope(gpu_dev):
            data = workspace.FetchBlob(core.ScopedName('data'))
            return data 
Example #21
Source File: test_restore_checkpoint.py    From Detectron-Cascade-RCNN with Apache License 2.0 5 votes vote down vote up
def init_weights(model):
    # init weights in gpu_id = 0 and then broadcast
    workspace.RunNetOnce(model.param_init_net)
    nu.broadcast_parameters(model) 
Example #22
Source File: test_loader.py    From Detectron with Apache License 2.0 5 votes vote down vote up
def run_net(net):
    workspace.RunNetOnce(net)
    gpu_dev = core.DeviceOption(caffe2_pb2.CUDA, 0)
    name_scope = 'gpu_{}'.format(0)
    with core.NameScope(name_scope):
        with core.DeviceScope(gpu_dev):
            data = workspace.FetchBlob(core.ScopedName('data'))
            return data 
Example #23
Source File: test_restore_checkpoint.py    From Detectron with Apache License 2.0 5 votes vote down vote up
def init_weights(model):
    # init weights in gpu_id = 0 and then broadcast
    workspace.RunNetOnce(model.param_init_net)
    nu.broadcast_parameters(model) 
Example #24
Source File: test_loader.py    From Detectron-DA-Faster-RCNN with Apache License 2.0 5 votes vote down vote up
def run_net(net):
    workspace.RunNetOnce(net)
    gpu_dev = core.DeviceOption(caffe2_pb2.CUDA, 0)
    name_scope = 'gpu_{}'.format(0)
    with core.NameScope(name_scope):
        with core.DeviceScope(gpu_dev):
            data = workspace.FetchBlob(core.ScopedName('data'))
            return data 
Example #25
Source File: test_restore_checkpoint.py    From Detectron-DA-Faster-RCNN with Apache License 2.0 5 votes vote down vote up
def init_weights(model):
    # init weights in gpu_id = 0 and then broadcast
    workspace.RunNetOnce(model.param_init_net)
    nu.broadcast_parameters(model) 
Example #26
Source File: test_loader.py    From CBNet with Apache License 2.0 5 votes vote down vote up
def run_net(net):
    workspace.RunNetOnce(net)
    gpu_dev = core.DeviceOption(caffe2_pb2.CUDA, 0)
    name_scope = 'gpu_{}'.format(0)
    with core.NameScope(name_scope):
        with core.DeviceScope(gpu_dev):
            data = workspace.FetchBlob(core.ScopedName('data'))
            return data 
Example #27
Source File: test_restore_checkpoint.py    From CBNet with Apache License 2.0 5 votes vote down vote up
def init_weights(model):
    # init weights in gpu_id = 0 and then broadcast
    workspace.RunNetOnce(model.param_init_net)
    nu.broadcast_parameters(model) 
Example #28
Source File: test_loader.py    From NucleiDetectron with Apache License 2.0 5 votes vote down vote up
def run_net(net):
    workspace.RunNetOnce(net)
    gpu_dev = core.DeviceOption(caffe2_pb2.CUDA, 0)
    name_scope = 'gpu_{}'.format(0)
    with core.NameScope(name_scope):
        with core.DeviceScope(gpu_dev):
            data = workspace.FetchBlob(core.ScopedName('data'))
            return data 
Example #29
Source File: train_net.py    From NucleiDetectron with Apache License 2.0 5 votes vote down vote up
def create_model():
    """Build the model and look for saved model checkpoints in case we can
    resume from one.
    """
    logger = logging.getLogger(__name__)
    start_iter = 0
    checkpoints = {}
    output_dir = get_output_dir(training=True)
    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, {'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
            cfg.TRAIN.WEIGHTS = os.path.join(output_dir, resume_weights_file)
            logger.info(
                '========> Resuming from checkpoint {} at start iter {}'.
                format(cfg.TRAIN.WEIGHTS, start_iter)
            )

    logger.info('Building model: {}'.format(cfg.MODEL.TYPE))
    model = model_builder.create(cfg.MODEL.TYPE, train=True)
    if cfg.MEMONGER:
        optimize_memory(model)
    # Performs random weight initialization as defined by the model
    workspace.RunNetOnce(model.param_init_net)
    return model, start_iter, checkpoints, output_dir 
Example #30
Source File: test_loader.py    From DetectAndTrack with Apache License 2.0 5 votes vote down vote up
def run_net(net):
    workspace.RunNetOnce(net)
    gpu_dev = core.DeviceOption(caffe2_pb2.CUDA, 0)
    name_scope = 'gpu_{}'.format(0)
    with core.NameScope(name_scope):
        with core.DeviceScope(gpu_dev):
            data = workspace.FetchBlob(core.ScopedName('data'))
            return data