caffe通过python创建网络 [英] caffe create networks by python

查看:102
本文介绍了caffe通过python创建网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道这段python代码可以创建caffe网络:

We all know this python code can create caffe networks:

    n = caffe.NetSpec()
    n.data, n.label = L.Data(batch_size=batch_size,
                             backend=P.Data.LMDB, source=lmdb,
                             transform_param=dict(scale=1. / 255), ntop=2)
    n.conv1 = L.Convolution(n.data, kernel_size=5,
                            num_output=20, weight_filler=dict(type='xavier'))
    n.pool1 = L.Pooling(n.conv1, kernel_size=2,
                        stride=2, pool=P.Pooling.MAX)

图层名称在n的右侧.例如:"n.data",该层的名称为"data".

The layer's name is on the right of n. for example:"n.data",this layer's name is "data".

  1. 编写简单的代码

如果我要创建更多的图层,则图层的名称除编号外均相同.例如,所有图层的名称均为{conv1,conv2,conv3,...,conv100}. 我想确定一个字符串s_name = conv%s,然后循环遍历一次即可,而不必写几乎相同的代码100次.我该怎么办?

If I want to create much more layers ,and the layers' name are the same except the number. For example, all layers' names are {conv1,conv2,conv3,...,conv100}. I want to definite a string s_name = conv%s and just loop the number to do the same thing for once ,don't need to write almost the same code 100 times.How should I do?

  1. 在名称中添加'/'吗? 图层的名称为"conv1/dw",如何确定名称?
  1. add '/' in name? The layer's name is "conv1/dw",how could I definite the name ?

推荐答案

您可以使用__setattr__进行操作:

s_name = 'conv{:03d}'.format(i)
l = L.Convolution( # ...
n.__setattr__(s_name, l)

可以在此处

或者,您可以使用__getitem__属性:

Alternatively, you can use the __getitem__ attribute:

n[s_name] = L.Convolution( # ...

有关更多信息,请参见此答案.

See this answer for more information.

这篇关于caffe通过python创建网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆