How to remodel a caffe network using pycaffe

I want that after booting the network, I decompose some specific layers and save the new network. for example

Origen network:

data → conv1 → conv2 → fc1 → fc2 → softmax;

New network:

data → conv1_1 → conv1_2 → conv2_1 → conv2_2 → fc1 → fc2 → softmax

Therefore, during this process, I was stuck in the following situation:
1. How is a new defined layer with the specified layer parameters in pycaffe?
2. How to copy layer parameters from existing layers (for example, fc1and fc2above)?

I know, using caffe::net_spec, we can define a new network manually. But caffe::net_specit cannot specify a layer from an existing one (for example:) fc1.

+4
source share
1 answer

I have not seen how to upload to previous networks using net_spec, but you can always use protobuf objects directly. (I use your network structure as an example)

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

Also see here as a guide to protocol buffers in python and here for current caffe message formats.

+6
source

Source: https://habr.com/ru/post/1628918/


All Articles