在caffe中有时候会涉及到通道的间的运算,比如通道的拆分、合并、相互运算等,下面介绍这几种分别在prototxt文件和py文件中的实现方法。
# Sclice层实现拆分合并
layer
{
name:"data_s"
type:"Slice"
top:"a"
top:"b"
top:"c"
slice_param
{
axis:0
slice_point:20 # 第一个拆分的通道数
slice_point:30 # 第一个与第二个的和
}
}
layer
{
name:"data_all"
type:"Concat"
bottom:"a"
bottom:"b"
bottom:"c"
top:"d"
concat_param
{
axis:0
}
}
# Eltwise实现拆分合并
# c = L.Eltwise(a, b)
layer
{
name:"eltwise1"
type:"Eltwise"
bottom:"A"
bottom:"B"
top:"C"
eltwise_param{
operation:SUM # PRODUCT, MAX
coeff: 1
coeff: -1
}
}
from caffe import params as P
import caffe.draw
from caffe.proto import caffe_pb2
from google.protobuf import text_format
# 配置文件路径和文件名
path = ""
net_file = path + "concat_slice_py.prototxt"
def concat_slice_net():
n = caffe.NeeSpec()
n.data = L.DummyData(
dummy_data_param=dict(num=20, channels=50, height=64, width=64, data_filter=dict(type="gaussian")))
n.a, n.b, n.c = L.Slice(n.data, ntop=2, slice_point=[20, 30], axis=0)
n.d = L.Concat(n.a, n.b, axis=0)
n.e = L.Eltwise(n.a, n.c)
return n.to_proto()
def draw_net(net_file, jpg_file):
net = caffe_pb2.NetParameter()
text_format.Merge(open(net_file).read(), net)
caffe.draw.draw_net_to_file(net, jpg_file, "BT")
with open(net_file, 'w') as f:
f.write(str(concat_slice_net()))
draw_net(net_file, "a.jpg")