参考教程:官方教程https://zhuanlan.zhihu.com/p/34700709
code:
# -*- coding: utf-8 -*-
import numpy as np
from caffe2.python import core, workspace
print("blob in workspace now:{}".format(workspace.Blobs())) #查看Blobs变量 Blobs()
print("workspase has blob 'x'?{}".format(workspace.HasBlob("x"))) #检查是否有x变量 HasBlob(变量名), Blob没有接收过输入,因此为false
x = np.random.randn(1,3).astype(np.float32)
print("generate x as {}".format(x))
workspace.FeedBlob("x", x) # 将x输入给workspace,FeedBlob(变量名, 变量)x
print("feeded x to network, now, workspase has blob x?{}".format(workspace.HasBlob("x"))) # 检查blob内是否有x变量,上一行输入了,因此是true
print("Fetched X:\n{}".format(workspace.FetchBlob("x")))
print("current workspace: {}".format(workspace.CurrentWorkspace())) # workspace的名称
workspace.FeedBlob("xx",x)
print("current blob in workspace: {}".format(workspace.Blobs())) # 查看所有blob变量,输入过两次,输入应该有两个
workspace.SwitchWorkspace("validate", True) # 切换工作空间,在训练切换验证阶段十分有用。SwitchWorkspace(空间名, 是否创建)
print('current workspace is :{}'.format(workspace.CurrentWorkspace()))
print('current blobs is :{}'.format(workspace.Blobs()))
#workspace.SwitchWorkspace("defalt")尝试打开不存在的空间
# 报错RuntimeError: [enforce fail at ..\caffe2\python\pybind_state.cc:175] create_if_missing.(no backtrace available)
workspace.SwitchWorkspace("default")
print('current blobs is :{}'.format(workspace.Blobs()))
workspace.ResetWorkspace() # 清空当前空间,blob清空
print('current blobs is :{}'.format(workspace.Blobs()))
输出:
blob in workspace now:[]
workspase has blob 'x'?False
generate x as [[-0.698431 0.4617593 -1.0960009]]
feeded x to network, now, workspase has blob x?True
Fetched X:
[[-0.698431 0.4617593 -1.0960009]]
current workspace: default
current blob in workspace: ['x', 'xx']
current workspace is :validate
current blobs is :[]
current blobs is :['x', 'xx']
current blobs is :[]
code:operator
# operator
# 可理解为函数;创建一个操作,必须有“操作类型”、“输出变量名”两个参数,可选参数与操作类型相关
# 运行一次操作,使用workspace.RunOperatorOnce(操作名),输出自动保存到“输出变量名”中
op = core.CreateOperator("Relu", ["X"], ["Y"], "op") #创建
print("Type of the created op is: {}".format(type(op)))
print("what's in op: {}".format(str(op)))
X = np.random.randn(1,3).astype(np.float32)
workspace.FeedBlob("X", X)
workspace.RunOperatorOnce(op)
temp = workspace.FetchBlob("Y")
print(X)
print(temp)
op2 = core.CreateOperator("GaussianFill", [], ["Z"], shape=[100,100], mean = 1.0, std = 1.0)
print("what's in op2 now:{}".format(str(op2)))
workspace.RunOperatorOnce(op2)
temp2 = workspace.FetchBlob("Z")
pyplot.hist(temp2.flatten(), bins=50)
pyplot.title("distribute of Z")
pyplot.show()
输出
Type of the created op is: <class 'caffe2.proto.caffe2_pb2.OperatorDef'>
what's in op: input: "X"
output: "Y"
name: "op"
type: "Relu"
[[-0.20640971 -0.19312549 -0.80844414]]
[[0. 0. 0.]]
what's in op2 now:output: "Z"
name: ""
type: "GaussianFill"
arg {
name: "shape"
ints: 100
ints: 100
}
arg {
name: "mean"
f: 1.0
}
arg {
name: "std"
f: 1.0
}
[[ 8.45763087e-01 8.98109317e-01 8.84111941e-01 ... 1.82099056e+00
1.39330113e+00 2.00439072e+00]
[ 7.27447271e-01 2.29497623e+00 -3.36288333e-01 ... 3.20352650e+00
1.07577443e+00 1.68419456e+00]
[ 5.27375937e-03 -4.66272593e-01 6.12424374e-01 ... 4.53812957e-01
1.51060951e+00 1.09457970e-03]
...
[-6.32846355e-02 1.32404649e+00 1.31898379e+00 ... 2.28813887e+00
-5.58585405e-01 1.17822886e-01]
[-5.43288946e-01 -3.44863415e-01 2.20968103e+00 ... -6.26695156e-03
1.10627568e+00 -2.96685815e-01]
[ 6.48198128e-01 4.71401572e-01 2.05487394e+00 ... 6.44312918e-01
5.47430336e-01 1.88929129e+00]]