MXNET保存模型结构(.json文件)

阅读: 评论:0

MXNET保存模型结构(.json文件)

MXNET保存模型结构(.json文件)

今天被这个问题搞得焦头烂额,因为要把mxnet的模型转成caffe必须要有结构文件。

方法就是先要把普通的Block变成HybridBlock,也就是静态化,改写的方法看官方文档:HybridBlocks.
HybridBlock:mxnet.gluon中的一个Block基础类,支持混合模式,即同时支持动态图+静态图。
其具有以下特点:

  • 其所有的children层都必须是HybridBlock。
  • 只有 NDArray和Symbol同时支持的操作才能使用。
  • 运行过程中,操作不能改变。也即操作只有一条路径,不能在执行过程中发生变化。

注意,forward(self, x)函数要改写成hybrid_forward(self, F, x),这个F是什么意思呢?

The hybrid_forward function takes an additional input, F, which stands for a backend. This exploits one awesome feature of MXNet. MXNet has both a symbolic API (mxnet.symbol) and an imperative API (mxnet.ndarray). In this book, so far, we’ve only focused on the latter. Owing to fortuitous historical reasons, the imperative and symbolic interfaces both support roughly the same API. They have many of same functions (currently about 90% overlap) and when they do, they support the same arguments in the same order. When we define hybrid_forward, we pass in F. When running in imperative mode, hybrid_forward is called with F as mxnet.ndarray and x as some ndarray input. When we compile with hybridize, F will be mxnet.symbol and x will be some placeholder or intermediate symbolic value. Once we call hybridize, the net is compiled, so we’ll never need to call hybrid_forward again.

简单来说F可以根据情况在mxnet.ndarray和mxnet.symbol两种数据类型间切换。(普通Block里是mxnet.ndarry)
然后就可以愉快的输出了,可以用下面这段代码也可以用export。

 from mxnet import symimport jsonx = sym.var('data')print('=== input data holder ===')print(x)y = model(x)print('n=== the symbolic program of net===')print(y)y_json = y.tojson()print('n=== the according json definition===')print(y_json)filename='model.json'with open(filename,'w') as file_obj:json.dump(y_json,file_obj)

本文发布于:2024-02-01 08:09:36,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170674617635110.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:模型   结构   文件   MXNET   json
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23