如何使用pycaffe重建caffe网

时间:2016-02-16 03:44:32

标签: python neural-network deep-learning caffe pycaffe

我想要的是,在加载网后,我将分解某些图层并保存新网。例如

Orignial net:

  

数据 - > conv1 - > conv2 - > fc1 - > fc2 - > SOFTMAX;

新网:

  

数据 - > conv1_1 - > conv1_2 - > conv2_1 - > conv2_2 - > fc1 - > fc2 - > SOFTMAX

因此,在此过程中,我陷入了以下情况:
1.如何在pycaffe中新建一个具有指定图层参数的图层? 2.如何从现有图层(例如上面的fc1fc2)复制图层参数?

我知道使用caffe::net_spec,我们可以手动定义新网络。但caffe::net_spec无法指定现有图层(例如:fc1)。

1 个答案:

答案 0 :(得分:10)

我没有看到如何使用net_spec加载以前的网络,但你总是可以直接使用protobuf对象。 (我以您的网络结构为例)

import caffe.proto.caffe_pb2 as caffe_pb2
import google.protobuf as pb
from caffe import layers as L

net = caffe_pb2.NetParameter()
with open('net.prototxt', 'r') as f:
    pb.text_format.Merge(f.read(), net)

#example of modifing the network:
net.layer[1].name = 'conv1_1'
net.layer[1].top[0] = 'conv1_1'
net.layer[2].name = 'conv1_2'
net.layer[2].top[0] = 'conv1_2'
net.layer[2].bottom[0] = 'conv1_1'

net.layer[3].bottom[0] = 'conv2_2'

#example of adding new layers (using net_spec):
conv2_1 = net.layer.add()
conv2_1.CopyFrom(L.Convolution(kernel_size=7, stride=1, num_output=48, pad=0).to_proto().layer[0])
conv2_1.name = 'conv2_1'
conv2_1.top[0] = 'conv2_1'
conv2_1.bottom.add('conv1_2')

conv2_2 = net.layer.add()
conv2_2.CopyFrom(L.Convolution(kernel_size=7, stride=1, num_output=48, pad=0).to_proto().layer[0])
conv2_2.name = 'conv2_2'
conv2_2.top[0] = 'conv2_2'
conv2_2.bottom.add('conv2_1')

# then write back out:
with open('net2.prototxt, 'w') as f:
    f.write(pb.text_format.MessageToString(net))

另请参阅here作为python中协议缓冲区的指南,并查看当前caffe消息格式的here