用python提取caffemodel网络参数并保存为二进制文件.bat
2017-04-28 17:21
295 查看
本篇主要参考了jiarenyf的
但他博客中的只将数据写入了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
本文Python 二进制文件读写参考了博客
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
本文Python 二进制文件读写参考了博客
使用Python进行二进制文件读写(转)
廖雪峰的教程http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820066616a77f826d876b46b9ac34cb5f34374f7a000
相关文章推荐
- DeepLearning(基于caffe)实战项目(10)--Python编写网络配置文件
- Python从二进制文件中提取Exponent和Modulus数据(e, n)并构建公钥
- python任务-从文件读取参数来发送json格式的post请求,再讲请求结果保存到文档里
- caffe的python接口学习(8):caffemodel中的参数及特征的抽取
- 【python】提取一个网站带有参数的url,然后随机选取几个保存到本地+源码分析
- caffe源码学习——用python定义网络时,源代码生成prototxt文件的原理
- caffe的python接口学习(8):caffemodel中的参数及特征的抽取
- caffe zoo .caffemodels模型下载\通过网络结构估算caffemodel文件的大小(转载)
- Caffe实例开发学习笔记。——网络配置文件 Layer参数详解——
- caffe 实战系列:proto文件格式以及含义解析:如何定义网络,如何设置网络参数(以AlexNet为例) 2016.3.30
- caffe 07 win10 使用caffe python接口定义网络配置文件
- caffe的python接口学习:caffemodel中的参数及特征的抽取(转载)
- caffe的python接口学习(8):caffemodel中的参数及特征的抽取
- 使用Anaconda安装库文件、控制台运行Python文件、显示Caffe网络结构
- Google地图发布基于python的神经网络学习模型,自动高效高准确度的从带有地理定位信息的图像文件中提取信息
- 读取和修改caffemodel文件里的参数——by 蠢鱼
- caffe的python接口学习(8):caffemodel中的参数及特征的抽取
- python读取caffemodel文件
- 通过网络结构估算caffemodel文件的大小
- python下将文件以二进制形式保存