caffe:BN层转batchnorm+scale修改prototxt

#coding=UTF-8
import sys
sys.path.insert(0,'/home/cdli/ECO2/caffe_3d/python')
import copy
from caffe.proto import caffe_pb2
from google.protobuf import text_format
import google

def create_layer(base_name_,type_,bottom_,top_):
	layer = caffe_pb2.LayerParameter()
	layer.name = base_name_+'/'+type_
	layer.type = type_
	if type_ == 'batchnorm':
		layer.bottom.append(bottom)
		layer.top.append(top)
		temp = caffe_pb2.ParamSpec()
		temp.lr_mult = 0
		temp.decay_mult = 0
		layer.param.append(temp)
		layer.param.append(temp)
		layer.param.append(temp)
		layer.batch_norm_param.use_global_stats = False
		layer.batch_norm_param.eps = 0.00001
	elif type_ == 'scale':
		layer.bottom.append(top)
		layer.top.append(top)
		temp = caffe_pb2.ParamSpec()
		temp.lr_mult = 0.2
		temp.decay_mult = 0.2
		layer.param.append(temp)
		layer.param.append(temp)
		layer.scale_param.filler.value = 1
		layer.scale_param.bias_filler.value = 0
	return layer

net1 = caffe_pb2.NetParameter()
net2 = copy.copy(net1)
deploy = 'deploy-pool3.prototxt'
text_format.Merge(open(deploy).read(), net1) #把文本内容读进


layers = net1.layer
for i, l in enumerate(layers):
	if str(l.type)!='BN':
		continue
	name = str(l.name)
	print name
	bottom = str(l.bottom[0])
	top = str(l.top[0])
	
	batchnorm = create_layer(name,'batchnorm',bottom,top)
	scale = create_layer(name,'scale',bottom,top)
	layers.pop(i)
	layers.insert(i, batchnorm)
	layers.insert(i, scale)
with open(deploy.split('.')[0]+'-batchnorm.prototxt','w') as f:
	f.write(str(net1))


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值