今天被这个问题搞得焦头烂额,因为要把mxnet的模型转成caffe必须要有结构文件。
方法就是先要把普通的Block变成HybridBlock,也就是静态化,改写的方法看官方文档:HybridBlocks.
HybridBlock:mxnet.gluon中的一个Block基础类,支持混合模式,即同时支持动态图+静态图。
其具有以下特点:
注意,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小时内删除。
留言与评论(共有 0 条评论) |