对prototxt文件的处理
对prototxt的处理的一个例子为
#-*-coding:utf-8-*- ''' @Author dezan zhao @Date 2018-01-22 ''' import re import pdb #对output文件进行处理 fid_output = open('output.txt') lines = fid_output.readlines() fid_output.close() dict = {} for i in range(3,len(lines)): line = lines[i].strip('\r\n') line = line.split(' ') layer_name = line[0] layer_type = line[1] if layer_type=='Convolution': q_in = line[2] q_rst = line[3] q_alpha = line[5] q_alpha_mul = line[6] dict[layer_name] = q_in + ','+q_rst+','+q_alpha+','+q_alpha_mul elif layer_type=='Eltwise': Q_A = line[2] Q_rst = line[3] Q_B = line[4] dict[layer_name] = Q_A +',' + Q_rst +','+Q_B #对prototxt文件进行处理 fid_deploy = open('deploy_72_merge_bn.prototxt') deploy_str = fid_deploy.read() fid_deploy.close() #去掉注释# deploy_str = deploy_str.replace('#', '') #去掉空格 deploy_str = deploy_str.replace(' ','') #分离layer layer_list = deploy_str.split('layer{') new_proto_txt='' for layer in layer_list: if 'type' in layer: layer = 'layer{' + layer #提取name layer_name = re.search(r'name:".*?"',layer) layer_name = layer_name.group(0).split('"')[1] #提取type layer_type = re.search(r'type:".*?"',layer) layer_type = layer_type.group(0).split('"')[1] print layer_type if layer_type=='Convolution': q_param = dict[layer_name].split(',') #提取convolution_param convolution_param_new = 'convolution_param{\n' + '\nbiconv_input_qparam:'+q_param[0] +'\n' +'biconv_sum_qparam:'+q_param[1]+'\n'+'biconv_alpha_qparam:'+q_param[2]+'\n'+'biconv_alpha_mul_qparam:'+q_param[3]+'\n' layer = layer.replace(r'convolution_param{',convolution_param_new) layer = layer.replace(r'type:"Convolution"','type:"FixBinaryConvolution"') elif layer_type=='Eltwise': q_param = dict[layer_name].split(',') #提取eltwise_param eltwise_param_new = 'eltwise_param {' +'\n' +\ 'q_a:'+ q_param[0]+'\n' + \ 'q_b:'+ q_param[1]+'\n' + \ 'q_rst:'+q_param[2] + '\n' #组成新的 layer = layer.replace(r'eltwise_param{',eltwise_param_new) layer = layer.replace(r'type:"Eltwise"','type:"FixEltwise"') #pdb.set_trace() new_proto_txt = new_proto_txt + layer ''' else layer_type=='Innerproduct': print 'innerproduct' new_proto_txt = new_proto_txt+layer ''' #保存 fid_result = open('deploy_result.prototxt','w+') fid_result.write(new_proto_txt) fid_result.close() print 'done'