用mmdnn实现模型转换

参考链接:https://www.twblogs.net/a/5ca4cadbbd9eee5b1a0713af

  1. 安装mmdnn

    pip install mmdnn
  2. 准备好mxnet模型的.json文件和.params文件, 以InsightFace MxNet r50为例        https://github.com/deepinsight/insightface
  3. 用mmdnn运行命令行
    python -m mmdnn.conversion._script.convertToIR -f mxnet -n model-symbol.json -w model-.params -d resnet50 --inputShape ,, 

     会生成resnet50.json(可视化文件) resnet50.npy(权重参数) resnet50.pb(网络结构)三个文件。

  4. 用mmdnn运行命令行
    python -m mmdnn.conversion._script.IRToCode -f tensorflow --IRModelPath resnet50.pb --IRWeightPath resnet50.npy --dstModelPath tf_resnet50.py 

     生成tf_resnet50.py文件,可以调用tf_resnet50.py中的KitModel函数加载npy权重参数重新生成原网络框架。

  5. 打开tf_resnet.py文件,修改load_weights()中的代码 (tensorflow=1.14.0报错) 

     try:
    weights_dict = np.load(weight_file).item()
    except:
    weights_dict = np.load(weight_file, encoding='bytes').item()

    改为

     try:
    weights_dict = np.load(weight_file, allow_pickle=True).item()
    except:
    weights_dict = np.load(weight_file, allow_pickle=True, encoding='bytes').item()
  6. 基于resnet50.npy和tf_resnet50.py文​​件,固化参数,生成PB文件:

    import tensorflow as tf
    import tf_resnet50 as tf_fun
    def netWork():
    model=tf_fun.KitModel("./resnet50.npy")
    return model
    def freeze_graph(output_graph):
    output_node_names = "output"
    data,fc1=netWork()
    fc1=tf.identity(fc1,name="output") graph = tf.get_default_graph() # 獲得默認的圖
    input_graph_def = graph.as_graph_def() # 返回一個序列化的圖代表當前的圖
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
    sess.run(init)
    output_graph_def = tf.graph_util.convert_variables_to_constants( # 模型持久化,將變量值固定
    sess=sess,
    input_graph_def=input_graph_def, # 等於:sess.graph_def
    output_node_names=output_node_names.split(",")) # 如果有多個輸出節點,以逗號隔開 with tf.gfile.GFile(output_graph, "wb") as f: # 保存模型
    f.write(output_graph_def.SerializeToString()) # 序列化輸出 if __name__ == '__main__':
    freeze_graph("frozen_insightface_r50.pb")
    print("finish!")
  7. 采用tensorflow的post-train quantization离线量化方法(有一定的精度损失)转换成tflite模型,从而完成端侧的模型部署:
    import tensorflow as tf
    
    convert=tf.lite.TFLiteConverter.from_frozen_graph("frozen_insightface_r50.pb",input_arrays=["data"],output_arrays=["output"],
    input_shapes={"data":[1,112,112,3]})
    convert.post_training_quantize=True
    tflite_model=convert.convert()
    open("quantized_insightface_r50.tflite","wb").write(tflite_model)
    print("finish!")

MxNet 模型转Tensorflow pb模型的更多相关文章

  1. 查看tensorflow pb模型文件的节点信息

    查看tensorflow pb模型文件的节点信息: import tensorflow as tf with tf.Session() as sess: with open('./quantized_ ...

  2. Problem after converting keras model into Tensorflow pb - 将keras模型转换为Tensorflow pb后的问题

    I'm using keras 2.1.* with tensorflow 1.13.* backend. I save my model during training with .h5 forma ...

  3. 查看tensorflow Pb模型所有层的名字

    代码如下: import tensorflow as tf def get_all_layernames(): """get all layers name"& ...

  4. TensorFlow-Bitcoin-Robot:一个基于 TensorFlow LSTM 模型的 Bitcoin 价格预测机器人

    简介 TensorFlow-Bitcoin-Robot:一个基于 TensorFlow LSTM 模型的 Bitcoin 价格预测机器人. 文章包括一下几个部分: 1.为什么要尝试做这个项目? 2.为 ...

  5. TensorFlow-Bitcoin-Robot:一个基于 TensorFlow LSTM 模型的 Bitcoin 价格预测机器人。

    简介 TensorFlow-Bitcoin-Robot:一个基于 TensorFlow LSTM 模型的 Bitcoin 价格预测机器人. 文章包括一下几个部分: 1.为什么要尝试做这个项目? 2.为 ...

  6. tensorflow模型ckpt转pb以及其遇到的问题

    使用tensorflow训练模型,ckpt作为tensorflow训练生成的模型,可以在tensorflow内部使用.但是如果想要永久保存,最好将其导出成pb的形式. tensorflow已经准备好c ...

  7. TensorFlow 自定义模型导出:将 .ckpt 格式转化为 .pb 格式

    本文承接上文 TensorFlow-slim 训练 CNN 分类模型(续),阐述通过 tf.contrib.slim 的函数 slim.learning.train 训练的模型,怎么通过人为的加入数据 ...

  8. tensorflow c++ API加载.pb模型文件并预测图片

    tensorflow  python创建模型,训练模型,得到.pb模型文件后,用c++ api进行预测 #include <iostream> #include <map> # ...

  9. tflearn 中文汉字识别,训练后模型存为pb给TensorFlow使用——模型层次太深,或者太复杂训练时候都不会收敛

    tflearn 中文汉字识别,训练后模型存为pb给TensorFlow使用. 数据目录在data,data下放了汉字识别图片: data$ ls0  1  10  11  12  13  14  15 ...

随机推荐

  1. HDU - 4734 F(x) (2013成都网络游戏,数字DP)

    意甲冠军:求0-B见面<=F[A]所有可能的 思维:数字DP,内存搜索 #include <iostream> #include <cstring> #include & ...

  2. 【C语言学习】C语言功能

    代码,功能为了更好地实现模块化编程.那么,什么是函数的性质?在函数中定义的变量(全局变量.局部变量.静态变量)如何存储?为什么范围和全局变量和局部变量的寿命是不一样的?只是有一个更深入的了解的功能.能 ...

  3. 入骨三分的“我们是谁”IT行业版

    这组漫画用简单的台词.粗犷的线条,把很多人心中对甲方(客户)的不满彻底地发泄了一通,并且出现了很多变种版本,引发了传播热潮. http://news.sina.com.cn/c/nd/2017-08- ...

  4. ThreadPoolExecutor原理和使用

    大家先从ThreadPoolExecutor的整体流程入手: 针对ThreadPoolExecutor代码.我们来看下execute方法: public void execute(Runnable c ...

  5. .NET Core 3.0 Preview 5 亮点之一:发布单文件可执行程序

    在阅读 Announcing .NET Core 3.0 Preview 5 时发现了 .NET Core 3.0 Preview 5 的一个新特性 —— Publishing Single EXEs ...

  6. 简单说说Delphi中线程的释放

    线程的释放方式有两种:一种是线程在运行完成后自动释放,一种是手动释放. 无论是那种释放,都应该在线程停止后进行释放. 然而线程的停止也有两种情况:一种是不需要设置标志位,直接完成:一种是由于execu ...

  7. wcf 代理实例

    通过过代理调用 wcf服务 using Microsoft.Extensions.Options; using System; using System.Collections.Generic; us ...

  8. NOPI 基本读写

    //获取cell的数据,并设置为对应的数据类型 public object GetCellValue(ICell cell) { object value = null; try { if (cell ...

  9. SVG路径动画解密

    原文:SVG路径动画解密 原文链接:http://www.gbtags.com/gb/share/5581.htm SVG路径动画效果现在貌似越来越多网站都使用了,给我的感觉就像是一段时间的流行而已, ...

  10. 快速写入Xml文件

    我们在做一些操作的时候会需要生成日志,Xml文件就是我们常用的一种日志文件. 普通操作Xml文件的代码遇到大数据量的话就很慢了. 用这个生成Xml文件的话,即使数据量很大,也很快 private vo ...