caffe2--Nets(四)

来源:互联网 发布:macbook air 卸载软件 编辑:程序博客网 时间:2024/05/18 03:30

Nets

nets本质上是计算图。为了保持前后一致性保留名称Net(同时也向神经网络致敬)。网络由多个operators组成,类似于一段程序由一系列命令组成。

当我们讨论nets时候,我们通常也讨论BlobReference,它是一个对象,包装了一个字符串,所以我们可以做简单的运算符链接操作。

创建一个基本上等同于下面python数学的网络。

X=np.random.randn(2,3)W=np.random.randn(5,3)b=np.ones(5)Y=X*W^T+b

我们将一步一步的展示这个过程,caffe2的core.Net是NetDef协议缓冲区的包装类。

当创建一个网络时,其底层protocol buffer基本上为空,创建网络并展示proto内容。

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

Results:

Current network proto:name:"my_first_net"

创建一个blob,命名X,使用GaussianFill填充一些随机数据。
X=net.GaussianFill( [], [“x”], mean=0.0, std=1.0, shape=[2,3], run_once=0)
print(“New network proto:\n\n{}”.format(net.proto()))

New network proto:name: "my_first_net"op {  output: "X"  name: ""  type: "GaussianFill"  arg {    name: "std"    f: 1.0  }  arg {    name: "run_once"    i: 0  }  arg {    name: "shape"    ints: 2    ints: 3  }  arg {    name: "mean"    f: 0.0  }}

你可能已经观察到这和之前的core.CreateOperator调用有一些不同。基本上,当我们有一个网络,你可以直接创建一个运算符并将它添加到网络里。本质上,如果你调用net.SomeOp,SomeOp是一个注册的运算符类型的字符串,可以翻译成:

op = core.CreateOperator("SomeOp", ...)net.Proto().op.append(op)

你可能会想X是什么,X是一个BlobReference,它基本上记录了两件事情:

通过str(X)查看X的名字
_from_net记录net的创建,但是大多是时候你不需要它。

我们来验证一下 另外,请记住,我们实际上并没有运行任何东西,所以X只包含一个符号。 不要指望现在得到任何数值。

print("Type of X is:{}".format(type(X)))print("The blob name is: {}".format(str(X)))
Type of X is: <class 'caffe2.python.core.BlobReference'>The blob name is: X

继续创建W和b。

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对象知道从哪个网络生成,除了从网络创建运算符之外,还可以从BlobReferences创建运算符,我们以这种方式创建FC运算符。

Y=X.FC([W,b],["Y"])

等价于:

Y=net.FC([X,W,b],["Y"])

看一下目前的网络

print ("Current network proto:\n\n{}".format(net.Proto))
Current network proto:name: "my_first_net"op {  output: "X"  name: ""  type: "GaussianFill"  arg {    name: "std"    f: 1.0  }  arg {    name: "run_once"    i: 0  }  arg {    name: "shape"    ints: 2    ints: 3  }  arg {    name: "mean"    f: 0.0  }}op {  output: "W"  name: ""  type: "GaussianFill"  arg {    name: "std"    f: 1.0  }  arg {    name: "run_once"    i: 0  }  arg {    name: "shape"    ints: 5    ints: 3  }  arg {    name: "mean"    f: 0.0  }}op {  output: "b"  name: ""  type: "ConstantFill"  arg {    name: "run_once"    i: 0  }  arg {    name: "shape"    ints: 5  }  arg {    name: "value"    f: 1.0  }}op {  input: "X"  input: "W"  input: "b"  output: "Y"  name: ""  type: "FC"}

太冗长了吗? 我们尝试将其可视化为图形。 Caffe2为此提供了极小的图形可视化工具。 我们来看看在ipython中。

from caffe2.python import net_drawerfrom IPython import displaygraph=net_drawer.GetPydotGraph(net,rankdir="LR")display.Image(graph.create_png(),width=800)

这里写图片描述
我们定义一个Net,但是没有任何执行。上面的网络本质上就是一个保留网络定义的protobuf。实际运行网路时,发生了下面的情况:

从protobuf实例化一个C++网络对象;
调用实例网络的Run()函数。

在做任何操作之前,我们应该调用ResetWorkspace()先把工作区变量清除掉。

使用python有两种方式运行网路,下面的例子展示第一种方式。

  1. Using workspace.RunNetOnce(), which instantiates, runs and immediately destructs the network.
  2. A little bit more complex and involves two steps: (a) call workspace.CreateNet() to create the C++ net object owned by the workspace, and (b) use workspace.RunNet() by passing the name of the network to it.
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 blobsfor name in workspace.Blobs():    print("{}:\n{}".format(name, workspace.FetchBlob(name)))
Current blobs in the workspace: []Blobs in the workspace after execution: ['W', 'X', 'Y', 'b']W:[[-0.96537346  0.42591459  0.66788739] [-0.47695673  2.25724339 -0.10370601] [-0.20327474 -3.07469416  0.47715324] [-1.62159526  0.73711687 -1.42365313] [ 0.60718107 -0.50448036 -1.17132831]]X:[[-0.99601173 -0.61438894  0.10042733] [ 0.23359862  0.15135486  0.77555442]]Y:[[ 1.76692021  0.07781416  3.13944149  2.01927781  0.58755434] [ 1.35693741  1.14979863  0.85720366 -0.37135673  0.15705228]]b:[ 1.  1.  1.  1.  1.]

第二种方式创建网络并运行它。

  1. ResetWorkspace();
  2. CreateNet(net_object);
  3. RunNet(net_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)))
Current blobs in the workspace: []Blobs in the workspace after execution: ['W', 'X', 'Y', 'b']W:[[-0.29295802  0.02897477 -1.25667715] [-1.82299471  0.92877913  0.33613944] [-0.64382178 -0.68545657 -0.44015241] [ 1.10232282  1.38060772 -2.29121733] [-0.55766547  1.97437167  0.39324901]]X:[[-0.47522315 -0.40166432  0.7179445 ] [-0.8363331  -0.82451206  1.54286408]]Y:[[ 0.22535783  1.73460138  1.2652775  -1.72335696  0.7543118 ] [-0.71776152  2.27745867  1.42452145 -4.59527397  0.4452306 ]]b:[ 1.  1.  1.  1.  1.]

RunNetOnce和RunNet之间有一些区别,但主要区别在于计算时间开销。 由于RunNetOnce涉及到将protobuf序列化以在Python和C之间传递并实例化网络,所以运行可能需要更长时间。 在这种情况下,我们看看什么是开销

# It seems that %timeit magic does not work well with# C++ extensions so we'll basically do for loopsstart=time.time()for i in range(1000):    workspace.RunNetOnce(net)end=time.time()print("Run time per RunNetOnce:{}".format((end-start)/1000))start = time.time()workspace.CreateNet(net)for i in range(1000):    workspace.RunNet(net.Proto().name)end = time.time()print('Run time per RunNet: {}'.format((end - start) / 1000))

Results:

Run time per RunNetOnce: 0.000364284992218Run time per RunNet: 4.42600250244e-06

Nets这部分就ok了,翻译水平有限,如有翻译不恰当之处,请留言。(^__^) 嘻嘻……

原创粉丝点击