caffe2

1、

workspace用来存储blob,这个例子展示了如何将blob存进workspace,然后又是怎样把它拿出来的

from caffe2.python import workspace, model_helper
import numpy as np
# Create random tensor of three dimensions
x = np.random.rand(4, 3, 2)
print(x)
print(x.shape)

workspace.FeedBlob("my_x", x)

x2 = workspace.FetchBlob("my_x")
print(x2)

2、Net


from caffe2.python import workspace, model_helper
import numpy as np
# Create random tensor of three dimensions


# Create the input data
data = np.random.rand(16, 100).astype(np.float32)


# Create labels for the data as integers [0, 9].
label = (np.random.rand(16) * 10).astype(np.int32)


workspace.FeedBlob("data", data)
workspace.FeedBlob("label", label)
m = model_helper.ModelHelper(name="my first net")
weight = m.param_init_net.XavierFill([], 'fc_w', shape=[10, 100])
bias = m.param_init_net.ConstantFill([], 'fc_b', shape=[10, ])
fc_1 = m.net.FC(["data", "fc_w", "fc_b"], "fc1")
pred = m.net.Sigmoid(fc_1, "pred")
softmax, loss = m.net.SoftmaxWithLoss([pred, "label"], ["softmax", "loss"])
#print(m.param_init_net.Proto())
m.AddGradientOperators([loss])                     #加上损失会进行反传
print(m.net.Proto())
workspace.RunNetOnce(m.param_init_net)


workspace.CreateNet(m.net)
for _ in range(100):
    data = np.random.rand(16, 100).astype(np.float32)
    label = (np.random.rand(16) * 10).astype(np.int32)


    workspace.FeedBlob("data", data)
    workspace.FeedBlob("label", label)


    workspace.RunNet(m.name, 10)   # run for 10 times
print(workspace.FetchBlob("softmax"))
print(workspace.FetchBlob("loss"))

model_helper用来创建两个网络,一个是init网络还有一个是net,第一个仅仅用来初始化参数,第二个用于真正的训练里面


还可以使用core来构建Net

net = core.Net("my_first_net")
print("Current network proto:\n\n{}".format(net.Proto()))

X = net.GaussianFill([], ["X"], mean=0.0, std=1.0, shape=[2, 3], run_once=0)        #创建了一个blob X
print("New network proto:\n\n{}".format(net.Proto()))
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)
Y = X.FC([W, b], ["Y"])
print("Current network proto:\n\n{}".format(net.Proto()))

以上只是定义了网络,接下来开始构建运行,一共有两种方式,RunNetOnce 和RunNet。RunNetOnce耗费的时间要比RunNet时间长 

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.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)))

3、

operators类似于函数般的存在。

from matplotlib import pyplot
import numpy as np
import time

# These are the droids you are looking for.
from caffe2.python import core, workspace
from caffe2.proto import caffe2_pb2
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
)                                                                        #创建一个operator
print("Type of the created op is: {}".format(type(op)))
print("Content:\n")
print(str(op))
workspace.FeedBlob("X", np.random.randn(2, 3).astype(np.float32))
workspace.RunOperatorOnce(op)                                            #向operator传入参数然后运行

operator也会选择性的选择输入的参数,输入是一个Tensor,然后用高斯将其填充

op = core.CreateOperator(
    "GaussianFill",
    [], # GaussianFill does not need any parameters.
    ["Z"],
    shape=[100, 100], # shape argument as a list of ints.
    mean=1.0,  # mean as a single float
    std=1.0, # std as a single float
)
print("Content of op:\n")
print(str(op))

workspace.RunOperatorOnce(op)
temp = workspace.FetchBlob("Z")
pyplot.hist(temp.flatten(), bins=50)
pyplot.title("Distribution of Z")


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值