海思AI芯片(HI35xx):tensorflow转caffemodel之模型参数转换

摘要:
要把自己的模型进行移植,之前是后端的移植,最近前端也提了需求,前端一般都是用海思芯片(海思HI3516DV300),只支持caffe,所以为了先测试时间得把tf的模型转成caffemodel。这里是将tf1.x转为caffemode,后续补全darknet转为caffemode

一、转换ckpt转caffemodel
转换代码:

#  coding=utf-8
#  Author      : AnnSun
#  Created date: 2020-06-23
#  

from __future__ import print_function, division
caffe_root = '/home/qif/smf/caffe/'   # 给为你自己的caffe路径
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
import numpy as np
import tensorflow as tf

deploy_proto = r"./yolov3_deploy_tf.prototxt"
caffe_model = r"./yolov3_helmet_tf.caffemodel"
# net = caffe.Net(deploy_proto, caffe_model, caffe.TEST)

# checkpoint为模型保存地址
checkpoint_path = './models'
def tf_ckpt2caffemodel(deploy_proto, caffe_model, checkpoint_path):
    # 定义自己的net
    net = caffe.Net(deploy_proto, caffe.TEST)

    # 得到checkpoint文件中所有的参数(名字,形状)元组
    for var_name, varshape in tf.contrib.framework.list_variables(checkpoint_path):
        # 得到上述参数的值
        var = tf.contrib.framework.load_variable(checkpoint_path, var_name)
        # var_name为变量的name scope
        # var 是该name scope对应的值
        # conv layer
        ndim = var.ndim
        str_var_name = str(var_name)
        layerName_split = str_var_name.split("/")
        print(layerName_split)
        if str_var_name.endswith("weights"):
            layer_name = "_".join(layerName_split[1:-1])
            if ndim == 4:
                v_4d = np.transpose(var, [3, 2, 0, 1])
                net.params[layer_name][0].data[...] = v_4d
                print(var_name)
                print("conv weights {} succefully".format(layer_name))
                print(v_4d)

            else:
                v_2d = np.transpose(var, [1, 0])
                net.params[layer_name][0].data[...] = v_2d
                print("fc weights {} succefully".format(layer_name))
                print(v_2d)

        elif str_var_name.endswith('biases'):  # .strip("/biases:0")
            layer_name = "_".join(layerName_split[1:-1])
            net.params[layer_name][1].data[...] = var
            print("{} biases succefully".format(layer_name))
            print(var)


        elif layerName_split[-2] == "BatchNorm":
            # BN
            bn_name = "_".join(layerName_split[1:-1])
            if str_var_name.endswith('moving_mean'):
                net.params[bn_name][0].data[...] = var
                print("{} BN moving_mean succefully".format(bn_name))
                print(var)


            elif str_var_name.endswith('moving_variance'):
                net.params[bn_name][1].data[...] = var + 1e-3
                net.params[bn_name][2].data[...] = np.array([1.0])
                print("{} BN moving_variance succefully".format(bn_name))
                print(var)

            # scale
            elif str_var_name.endswith('beta'): # offset
                layer_name = "_".join(layerName_split[1:-2]) + "_scale"
                net.params[layer_name][1].data[...] = var
                print("{} Scale beta succefully".format(layer_name))
                print(var)


            elif str_var_name.endswith('gamma'):
                layer_name = "_".join(layerName_split[1:-2]) + "_scale"
                net.params[layer_name][0].data[...] = var
                print("{} Scale gamma succefully".format(layer_name))
                print(var)
    # 保存caffemodel
    net.save(caffe_model)
    print("\n -- caffeModel Finished. -- \n")


def show_TF_param(checkpoint_path):
    with open("tf1_yolov3_prama.txt", "w") as fw:
        # 得到checkpoint文件中所有的参数(名字,形状)元组
        for var_name, varshape in tf.contrib.framework.list_variables(checkpoint_path):
            # 得到上述参数的值
            var = tf.contrib.framework.load_variable(checkpoint_path, var_name)
            # var_name为变量的name scope
            # var 是该name scope对应的值
            info_str = str(var_name) + ":" + str(varshape) + "\n"
            fw.write(info_str)
            fw.write(str(var) + "\n" + "\n")
            print(var.ndim)
            print(var_name, " : ", varshape)
            print(var)

def show_caffe_param(deploy_proto, caffe_model):
    net = caffe.Net(deploy_proto, caffe_model, caffe.TEST)
    with open("caffe1_yolov3_prama.txt", "w") as fw:
        for layer_name, param in net.params.items():
            # print(layer_name + ": " + str(len(param)))
            for i in range(len(param)):
                info_str = layer_name + ': ' + str(i) + "/" + str(len(param)) + str(param[i].data.shape) + str(
                    param[i].data.ndim)
                print(info_str)
                fw.write(info_str + "\n")
                fw.write(str(param[i].data))
                fw.write("\n\n")

if __name__ == '__main__':
    # caffe
    deploy_proto = r"./yolov3_deploy_tf.prototxt"
    caffe_model = r"./yolov3_helmet_tf.caffemodel"
    # tf113(slim)
    # checkpoint为模型保存地址
    checkpoint_path = './models'

    tfckpt_to_caffemodel = True
    isShowCaffe = True
    isShowTF = False

    if tfckpt_to_caffemodel:
        tf_ckpt2caffemodel(deploy_proto, caffe_model, checkpoint_path)
    elif isShowCaffe:
        show_caffe_param(deploy_proto, caffe_model)
    elif isShowTF:
        show_TF_param(checkpoint_path)

函数解析说明:
tf_ckpt2caffemodel():将tf的参数转为caffemodel的参数
show_caffe_param():转换后的caffemodel的参数
show_TF_param():需要转换的TF的模型中的参数

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值