用python提取caffemodel网络参数并保存为二进制文件.bat

来源:互联网 发布:js 数组 sort 编辑:程序博客网 时间:2024/06/10 02:49

本篇主要参考了jiarenyf的 

Caffe学习:使用pycaffe读取caffemodel参数

但他博客中的只将数据写入了txt,由于自己工程需要使用.bat格式各层的网络参数,因此对其做了一些追加。

输入deploy和预先训练好的caffemodel。

输出:每层一个dat文件存放该层的网络参数。


由于不通工程需求不同,因此下面的代码还需要针对你需要的数据排列方式进行修改。以下仅供参考。

# -*- coding: utf-8 -*-"""Created on Thu Apr 27 18:13:51 2017@author: fuwenyan"""import caffeimport structimport 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



0 0
原创粉丝点击