如何将深度学习模型从MATLAB导入PyTorch? [英] How to import deep learning models from MATLAB to PyTorch?
问题描述
我正在尝试将经过DNN训练的模型从MATLAB导入PyTorch.
I’m trying to import a DNN trained model from MATLAB to PyTorch.
我找到了针对相反情况的解决方案(从PyTorch到MATLAB),但是没有关于如何从MATLAB导入训练后的模型到PyTorch的提议解决方案.
I’ve found solutions for the opposite case (from PyTorch to MATLAB), but no proposed solutions on how to import a trained model from MATLAB to PyTorch.
有什么想法吗?
推荐答案
您可以首先 ONNX 加载模型;先决条件是:
You can first export your model to ONNX format, and then load it using ONNX; prerequisites are:
pip install onnx onnxruntime
然后
onnx.load('model.onnx')
# Check that the IR is well formed
onnx.checker.check_model(model)
到目前为止,您仍然没有PyTorch模型.由于它是本机不支持.
Until this point, you still don't have a PyTorch model. This can be done through various ways since it's not natively supported.
一种解决方法(仅通过加载 模型参数)
A workaround (by loading only the model parameters)
import onnx
onnx_model = onnx.load('model.onnx')
graph = onnx_model.graph
initalizers = dict()
for init in graph.initializer:
initalizers[init.name] = numpy_helper.to_array(init)
for name, p in model.named_parameters():
p.data = (torch.from_numpy(initalizers[name])).data
使用 onnx2pytorch
import onnx
from onnx2pytorch import ConvertModel
onnx_model = onnx.load('model.onnx')
pytorch_model = ConvertModel(onnx_model)
注意:消耗时间
Note: Time Consuming
使用 onnx2keras
,然后使用(示例)
这篇关于如何将深度学习模型从MATLAB导入PyTorch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!