深度学习平台框架

简介

分类

模型转换

网络参数转到MAT 文件

keras权重到mat

可知直接用matlab读取hdf5文件, 也可以通过如下脚本 keras2mat.py 转换.

#!/usr/bin/env python2
#-*- coding: utf-8 -*-

# Author:
# ---------<Zhi Liu>
# ---------<Xidian University>
# ---------<zhiliu.mind@gmail.com>
# ---------<https://iridescent.blog.csdn.net/>
# ---------<https://www.linkedin.com/in/zhiliuln>
# ---------<2019/08/16>

import os
import scipy.io as sio
from ssd import SSD300

modelW = '[YOUR WEIGHT PATH in hdf5]'
input_shape = (6, 4000, 1)
NUM_CLASSES = 11
model = SSD300(input_shape, num_classes=NUM_CLASSES)
model.load_weights(modelW, by_name=True)
output_dir = './'
netpars = {}

for w in model.get_weights():
    print(w.shape)

for layer in model.layers:
	wb = model.get_layer(layer.name).get_weights()
	print(layer.name, len(wb))
	if len(wb) == 0:
		netpars[layer.name + 'v'] = wb
	if len(wb) > 0:
		netpars[layer.name + 'w'] = wb[0]
	if len(wb) > 1:
		netpars[layer.name + 'b'] = wb[1]

sio.savemat(output_dir + 'keras_ssd.mat', {'netpars': netpars})

caffe 到 mat

可以使用caffe的matlab接口, 也可以使用如下脚本 caffe2mat 转换


#!/usr/bin/env python2
#-*- coding: utf-8 -*-

# Author:
# ---------<Zhi Liu>
# ---------<Xidian University>
# ---------<zhiliu.mind@gmail.com>
# ---------<https://iridescent.blog.csdn.net/>
# ---------<https://www.linkedin.com/in/zhiliuln>
# ---------<2017/10/06>

import os
import scipy.io as sio
import caffe

# specify model file -->'deploy.prototxt' and weights file '*.caffemodel'
# net_model = '../../model/alexnet/deploy.prototxt'
# net_weights = '../../model/alexnet/bvlc_alexnet.caffemodel'
output_dir = './mat/'
net_model = '../../model/ssd/deploy.prototxt'
net_weights = '../../model/ssd/VGG_INST25_SSD_300x300_iter_50000.caffemodel'


def GetFileNameAndExt(filename):
    import os
    (filepath, tempfilename) = os.path.split(filename)
    (shotname, extension) = os.path.splitext(tempfilename)
    return shotname, extension


def load(net_model, net_weights, output_dir):
    name, ext = GetFileNameAndExt(net_weights)
    # Load the net
    caffe.set_mode_cpu()
    # You may need to train this caffemodel first
    # There should be script to help you do the training
    net = caffe.Net(net_model, net_weights, caffe.TEST)

    netpars = {}
    # 遍历每一层
    for param_name in net.params.keys():
        # 权重参数
        weight = net.params[param_name][0].data
        netpars[param_name + 'w'] = weight

        if len(net.params[param_name]) > 1:
            # 偏置参数
            bias = net.params[param_name][1].data
            netpars[param_name + 'b'] = bias
            biasshape = bias.shape
        else:
            biasshape = []

        print(param_name, len(net.params[param_name]),
              'weights:', weight.shape, 'bias:', biasshape)

    sio.savemat(output_dir + name + '.mat', {'netpars': netpars})


if __name__ == "__main__":

    if os.path.exists(output_dir) == False:
        os.mkdir(output_dir)
    load(net_model, net_weights, output_dir)
    print("========Done!=========")
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页