from caffe2.python import core, workspace, model_helper from caffe2.proto import caffe2_pb2 import numpy as np
op = core.CreateOperator( "Relu", # The type of operator that we want to run ["X"], # A list of input blobs by their names ["Y"], # A list of output blobs by their names )
# op的本质就是protobuf object print("Type of the created op is: {}".format(type(op))) print("Content:\n") print(str(op))
# net创建Operator的,返回值是BlobReference # BlobReference中保存了blob name以及创建该BlobReference的net print("Type of X is: {}".format(type(X))) print("The blob name is: {}".format(str(X))) W = net.GaussianFill([], ["W"], mean=0.0, std=1.0, shape=[5, 3], run_once=0) b = net.ConstantFill([], ["b"], shape=[5,], value=1.0, run_once=0)
# 也可以通过BlobReference直接创建Operator,其中Blob为输入 # 以下操作等价于 Y = net.FC([X, W, b], ["Y"]) Y = X.FC([W, b], ["Y"])
# 要运行Net可以通过workspace # 方法一:workspace.RunNetOnce() workspace.ResetWorkspace() print("Current blobs in the workspace: {}".format(workspace.Blobs())) workspace.RunNetOnce(net) print("Blobs in the workspace after execution: {}".format(workspace.Blobs()))
# Let's dump the contents of the blobs for name in workspace.Blobs(): print("{}:\n{}".format(name, workspace.FetchBlob(name)))
# 方法二:先workspace.CreateNet() 再workspace.RunNet() workspace.ResetWorkspace() print("Current blobs in the workspace: {}".format(workspace.Blobs())) workspace.CreateNet(net) workspace.RunNet(net.Proto().name) print("Blobs in the workspace after execution: {}".format(workspace.Blobs())) for name in workspace.Blobs(): print("{}:\n{}".format(name, workspace.FetchBlob(name)))
voidrun(){ // >>> data = np.random.rand(16, 100).astype(np.float32) std::vector<float> data(16 * 100); for (auto& v : data) { v = (float)rand() / RAND_MAX; }
// >>> label = (np.random.rand(16) * 10).astype(np.int32) std::vector<int> label(16); for (auto& v : label) { v = 10 * rand() / RAND_MAX; }
// >>> workspace.FeedBlob("data", data) { auto tensor = workspace.CreateBlob("data")->GetMutable<TensorCPU>(); auto value = TensorCPU({16, 100}, data, NULL); tensor->ResizeLike(value); tensor->ShareData(value); }
// >>> workspace.FeedBlob("label", label) { auto tensor = workspace.CreateBlob("label")->GetMutable<TensorCPU>(); auto value = TensorCPU({16}, label, NULL); tensor->ResizeLike(value); tensor->ShareData(value); }
// >>> m = model_helper.ModelHelper(name="my first net") NetDef initModel; initModel.set_name("my first net_init"); NetDef predictModel; predictModel.set_name("my first net");
// >>> weight = m.param_initModel.XavierFill([], 'fc_w', shape=[10, 100]) { auto op = initModel.add_op(); op->set_type("XavierFill"); auto arg = op->add_arg(); arg->set_name("shape"); arg->add_ints(10); arg->add_ints(100); op->add_output("fc_w"); }
// >>> bias = m.param_initModel.ConstantFill([], 'fc_b', shape=[10, ]) { auto op = initModel.add_op(); op->set_type("ConstantFill"); auto arg = op->add_arg(); arg->set_name("shape"); arg->add_ints(10); op->add_output("fc_b"); } std::vector<OperatorDef*> gradient_ops;
// >>> fc_1 = m.net.FC(["data", "fc_w", "fc_b"], "fc1") { auto op = predictModel.add_op(); op->set_type("FC"); op->add_input("data"); op->add_input("fc_w"); op->add_input("fc_b"); op->add_output("fc1"); gradient_ops.push_back(op); }
// >>> pred = m.net.Sigmoid(fc_1, "pred") { auto op = predictModel.add_op(); op->set_type("Sigmoid"); op->add_input("fc1"); op->add_output("pred"); gradient_ops.push_back(op); }