本篇主要参考了jiarenyf的
Caffe学习:使用pycaffe读取caffemodel参数
但他博客中的只将数据写入了txt,由于自己工程需要使用.bat格式各层的网络参数,因此对其做了一些追加。
输入deploy和预先训练好的caffemodel。
输出:每层一个dat文件存放该层的网络参数。
由于不通工程需求不同,因此下面的代码还需要针对你需要的数据排列方式进行修改。以下仅供参考。
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 27 18:13:51 2017
@author: fuwenyan
"""
import caffe
import struct
import numpy as np
# 使输出的参数完全显示
# 若没有这一句,因为参数太多,中间会以省略号“……”的形式代替
np.set_printoptions(threshold='nan')
# deploy文件
MODEL_FILE = 'deploy.prototxt'
# 预先训练好的caffe模型
PRETRAIN_FILE = 'model.caffemodel'
# 保存参数的文件
params_txt = 'params.txt'
pf_txt = open(params_txt, 'w')
# 让caffe以测试模式读取网络参数
net = caffe.Net(MODEL_FILE, PRETRAIN_FILE, caffe.TEST)
# 遍历每一层
for param_name in net.params.keys():
# 权重参数
weight = net.params[param_name][0].data
# 偏置参数
bias = net.params[param_name][1].data
# print param_name
# print 'weight.shape',weight.shape
# print 'weight.dtype',weight.dtype
# print 'bias.shape',bias.shape
pf_dat = open('./weight/'+param_name+'.dat', 'wb')
len_w=len(weight.shape)
if (len_w==4):##conv layer
byte1=struct.pack('i',weight.shape[3])
byte3=struct.pack('i',weight.shape[1])
byte4=struct.pack('i',weight.shape[0])
pf_dat.write(byte1)
pf_dat.write(byte3)
pf_dat.write(byte4)
elif(len_w==2):##fc layer
byte1=struct.pack('i',weight.shape[1])
byte2=struct.pack('i',weight.shape[0])
pf_dat.write(byte1)
pf_dat.write(byte2)
# 该层在prototxt文件中对应“top”的名称
pf_txt.write(param_name)
pf_txt.write('\n')
# 写权重参数
pf_txt.write('\n' + param_name + '_weight:\n\n')
# 权重参数是多维数组,为了方便输出,转为单列数组
weight.shape = (-1, 1)
# print 'weight.shape after:',weight.shape
for w in weight:
pf_txt.write('%f, ' % w)
pf_dat.write(w)
# 写偏置参数
pf_txt.write('\n\n' + param_name + '_bias:\n\n')
# 偏置参数是多维数组,为了方便输出,转为单列数组
bias.shape = (-1, 1)
for b in bias:
pf_txt.write('%f, ' % b)
pf_dat.write(b)
pf_dat.close
pf_txt.write('\n\n')
pf_txt.close