当前位置: 首页>>代码示例>>Python>>正文


Python workspace.RunNetOnce方法代码示例

本文整理汇总了Python中caffe2.python.workspace.RunNetOnce方法的典型用法代码示例。如果您正苦于以下问题:Python workspace.RunNetOnce方法的具体用法?Python workspace.RunNetOnce怎么用?Python workspace.RunNetOnce使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在caffe2.python.workspace的用法示例。


在下文中一共展示了workspace.RunNetOnce方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: load_model_pb

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
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) 
开发者ID:facebookarchive,项目名称:models,代码行数:23,代码来源:model_utils.py

示例2: test_constant

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
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)) 
开发者ID:NervanaSystems,项目名称:ngraph-python,代码行数:27,代码来源:test_ops_constant.py

示例3: test_SquaredL2Distance

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
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)) 
开发者ID:NervanaSystems,项目名称:ngraph-python,代码行数:26,代码来源:test_ops_nn.py

示例4: test_AveragedLoss

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
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)) 
开发者ID:NervanaSystems,项目名称:ngraph-python,代码行数:25,代码来源:test_ops_nn.py

示例5: create_model

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
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) 
开发者ID:facebookresearch,项目名称:dlrm,代码行数:18,代码来源:dlrm_s_caffe2.py

示例6: initialize_model_from_cfg

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
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 
开发者ID:facebookresearch,项目名称:DetectAndTrack,代码行数:27,代码来源:test_engine.py

示例7: LoadModel

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
def LoadModel(path, dbtype='minidb'):
    '''
    Load pretrained model from file
    '''
    log.info("Loading path: {}".format(path))
    meta_net_def = pred_exp.load_from_db(path, dbtype)
    init_net = core.Net(pred_utils.GetNet(
        meta_net_def, predictor_constants.GLOBAL_INIT_NET_TYPE))
    predict_init_net = core.Net(pred_utils.GetNet(
        meta_net_def, predictor_constants.PREDICT_INIT_NET_TYPE))

    predict_init_net.RunAllOnGPU()
    init_net.RunAllOnGPU()
    assert workspace.RunNetOnce(predict_init_net)
    assert workspace.RunNetOnce(init_net) 
开发者ID:facebookresearch,项目名称:VMZ,代码行数:17,代码来源:model_helper.py

示例8: ConvertModel

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
def ConvertModel(args):
    meta_net_def = pred_exp.load_from_db(args.load_model_path, args.db_type)
    net = core.Net(
        pred_utils.GetNet(meta_net_def, predictor_constants.PREDICT_NET_TYPE)
    )
    init_net = core.Net(
        pred_utils.
        GetNet(meta_net_def, predictor_constants.GLOBAL_INIT_NET_TYPE)
    )
    init_net.RunAllOnGPU()
    assert workspace.RunNetOnce(init_net)

    pred_params = list(set(net.Proto().external_input) - set(['gpu_0/data']))

    save_params = [str(param) for param in pred_params]
    save_blobs = {}
    for param in save_params:
        scoped_blob_name = str(param)
        unscoped_blob_name = unscope_name(scoped_blob_name)
        if unscoped_blob_name not in save_blobs:
            save_blobs[unscoped_blob_name] = workspace.FetchBlob(
                scoped_blob_name)
            log.info(
                '{:s} -> {:s}'.format(scoped_blob_name, unscoped_blob_name))
    log.info('saving weights to {}'.format(args.save_model_path))
    with open(args.save_model_path, 'w') as fwrite:
        pickle.dump(dict(blobs=save_blobs), fwrite, pickle.HIGHEST_PROTOCOL) 
开发者ID:facebookresearch,项目名称:VMZ,代码行数:29,代码来源:minidb_to_pickle.py

示例9: main

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
def main():
    args = parser.parse_args()
    args.gpu_id = 0

    model = model_helper.ModelHelper(name="le_net", init_params=False)

    # Bring in the init net from init_net.pb
    init_net_proto = caffe2_pb2.NetDef()
    with open(args.c2_init, "rb") as f:
        init_net_proto.ParseFromString(f.read())
    model.param_init_net = core.Net(init_net_proto)  # model.param_init_net.AppendNet(core.Net(init_net_proto)) #

    # bring in the predict net from predict_net.pb
    predict_net_proto = caffe2_pb2.NetDef()
    with open(args.c2_predict, "rb") as f:
        predict_net_proto.ParseFromString(f.read())
    model.net = core.Net(predict_net_proto)  # model.net.AppendNet(core.Net(predict_net_proto))

    # CUDA performance not impressive
    #device_opts = core.DeviceOption(caffe2_pb2.PROTO_CUDA, args.gpu_id)
    #model.net.RunAllOnGPU(gpu_id=args.gpu_id, use_cudnn=True)
    #model.param_init_net.RunAllOnGPU(gpu_id=args.gpu_id, use_cudnn=True)

    input_blob = model.net.external_inputs[0]
    model.param_init_net.GaussianFill(
        [],
        input_blob.GetUnscopedName(),
        shape=(args.batch_size, 3, args.img_size, args.img_size),
        mean=0.0,
        std=1.0)
    workspace.RunNetOnce(model.param_init_net)
    workspace.CreateNet(model.net, overwrite=True)
    workspace.BenchmarkNet(model.net.Proto().name, 5, 20, True) 
开发者ID:rwightman,项目名称:gen-efficientnet-pytorch,代码行数:35,代码来源:caffe2_benchmark.py

示例10: run_net

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
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 
开发者ID:yihui-he,项目名称:KL-Loss,代码行数:10,代码来源:test_loader.py

示例11: init_weights

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
def init_weights(model):
    # init weights in gpu_id = 0 and then broadcast
    workspace.RunNetOnce(model.param_init_net)
    nu.broadcast_parameters(model) 
开发者ID:yihui-he,项目名称:KL-Loss,代码行数:6,代码来源:test_restore_checkpoint.py

示例12: train

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
def train(INIT_NET, PREDICT_NET, epochs, batch_size, device_opts) :

    data, gt_segmentation = get_data(batch_size)
    workspace.FeedBlob("data", data, device_option=device_opts)
    workspace.FeedBlob("gt_segmentation", gt_segmentation, device_option=device_opts)

    train_model= model_helper.ModelHelper(name="train_net", arg_scope = {"order": "NHWC"})
    output_segmentation = create_unet_model(train_model, device_opts=device_opts, is_test=0)
    add_training_operators(output_segmentation, train_model, device_opts=device_opts)
    with core.DeviceScope(device_opts):
        brew.add_weight_decay(train_model, 0.001)

    workspace.RunNetOnce(train_model.param_init_net)
    workspace.CreateNet(train_model.net)

    print '\ntraining for', epochs, 'epochs'
    for j in range(0, epochs):
        data, gt_segmentation = get_data(batch_size, 4)

        workspace.FeedBlob("data", data, device_option=device_opts)
        workspace.FeedBlob("gt_segmentation", gt_segmentation, device_option=device_opts)

        workspace.RunNet(train_model.net, 1)   # run for 10 times
        print str(j) + ': ' + str(workspace.FetchBlob("avg_loss"))

    print 'training done'
    test_model= model_helper.ModelHelper(name="test_net", arg_scope = {"order": "NHWC"}, init_params=False)
    create_unet_model(test_model, device_opts=device_opts, is_test=1)
    workspace.RunNetOnce(test_model.param_init_net)
    workspace.CreateNet(test_model.net, overwrite=True)

    print '\nsaving test model'
    save_net(INIT_NET, PREDICT_NET, test_model) 
开发者ID:peterneher,项目名称:peters-stuff,代码行数:35,代码来源:segmentation_no_db_example.py

示例13: load_net

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
def load_net(INIT_NET, PREDICT_NET, device_opts):

    init_def = caffe2_pb2.NetDef()
    with open(INIT_NET, 'r') as f:
        init_def.ParseFromString(f.read())
        init_def.device_option.CopyFrom(device_opts)
        workspace.RunNetOnce(init_def.SerializeToString())

    net_def = caffe2_pb2.NetDef()
    with open(PREDICT_NET, 'r') as f:
        net_def.ParseFromString(f.read())
        net_def.device_option.CopyFrom(device_opts)
        workspace.CreateNet(net_def.SerializeToString(), overwrite=True) 
开发者ID:peterneher,项目名称:peters-stuff,代码行数:15,代码来源:segmentation_no_db_example.py

示例14: sum_example

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
def sum_example():
    # Caffe2 - network creation
    net = core.Net("net")
    shape = (2, 2, 2)

    A = net.GivenTensorFill([], "A", shape=shape, values=np.random.uniform(-5, 5, shape), name="A")
    B = net.GivenTensorFill([], "B", shape=shape, values=np.random.uniform(-5, 5, shape), name="B")
    C = net.GivenTensorFill([], "C", shape=shape, values=np.random.uniform(-5, 5, shape), name="C")
    A.Sum([B, C], ["Y"], name="Y")

    # Execute via Caffe2
    workspace.ResetWorkspace()
    workspace.RunNetOnce(net)

    # Execute in numpy
    a = workspace.FetchBlob("A")
    b = workspace.FetchBlob("B")
    c = workspace.FetchBlob("C")

    np_result = np.sum([a, b, c], axis=0)

    # 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 in ngraph
    f_result = ngt.make_transformer().computation(f_ng)()

    # compare numpy, Caffe2 and ngraph results
    print("Caffe2 result: \n{}\n".format(workspace.FetchBlob("Y")))
    print("ngraph result: \n{}\n".format(f_result))
    print("numpy result: \n{}\n".format(np_result))

    assert(np.allclose(f_result, workspace.FetchBlob("Y")))
    assert(np.allclose(f_result, np_result)) 
开发者ID:NervanaSystems,项目名称:ngraph-python,代码行数:40,代码来源:sum.py

示例15: fc_example

# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunNetOnce [as 别名]
def fc_example():
    # Caffe2 - network creation
    net = core.Net("net")

    X = net.GivenTensorFill([], "X", shape=[2, 2], values=[1.0, 2.0, 3.0, 4.0], name="X")
    W = net.GivenTensorFill([], "W", shape=[1, 2], values=[5.0, 6.0], name="W")
    b = net.ConstantFill([], ["b"], shape=[1, ], value=0.5, run_once=0, name="b")
    X.FC([W, b], ["Y"], name="Y")

    # Execute via Caffe2
    workspace.ResetWorkspace()
    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
    f_result = ngt.make_transformer().computation(f_ng)()

    # compare Caffe2 and ngraph results
    print("Caffe2 result: {}:\n{}".format("Y", workspace.FetchBlob("Y")))
    print("ngraph result: {}:\n{}".format("Y", f_result))
    assert(np.array_equal(f_result, workspace.FetchBlob("Y"))) 
开发者ID:NervanaSystems,项目名称:ngraph-python,代码行数:29,代码来源:fc.py


注:本文中的caffe2.python.workspace.RunNetOnce方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。