首页 文章

使用Dev Pytorch 1.0将Pytorch模型加载到C中

提问于
浏览
2

Pytorch 1.0具有将模型转换为火炬脚本程序(以某种方式序列化)的功能,以使其能够在没有Python依赖性的情况下在C中执行 .

详细信息在本教程中 . https://pytorch.org/tutorials/advanced/cpp_export.html

这是如何做到的:

import torch
import torchvision

# An instance of your model.
model = A UNET MODEL FROM FASTAI which has hooks as required by UNET

# An example input you would normally provide to your model's forward() method.
example = torch.rand(1, 3, 224, 224)

# Use torch.jit.trace to generate a torch.jit.ScriptModule via tracing.
traced_script_module = torch.jit.trace(model, example)

在我的用例中,我使用UNET模型进行语义分割 . 但是,我使用此方法跟踪模型,我得到以下错误 .

Forward or backward hooks can't be compiled

UNET模型使用钩子来保存在网络中后续层使用的中间特征 . 有办法解决吗?或者这仍然是这种新方法的限制,它不能与使用这种钩子的模型一起使用 .

1 回答

  • -1

    也许你可以在C中重写模型,因为c API与python版本的界面几乎相同 .

相关问题