yolact turn onnx
最编程
2024-03-15 09:54:33
...
利用yolact训练了一波自己的数据,目前效果还可以,准备利用C++调用,这里记录一下模型转为onnx的过程和一些问题。
一、模型转换
1、设置配置文件
依据自己训练的配置,设置配置文件
set_cfg("yolact_resnet50_custom_config")
2、精简yolact.py
依据配置参数,删除一些判断分支语句,yolact只保留初始化、加载权重和推理
3、加载权重
weight_path = "../weights/20200428_754_80000.pth"
net = Yolact()
net.load_weights(weight_path)
net.eval()
net.to(device)
4、转换
inputs = torch.randn(1, 3, 550, 550).to(device)
onnx_model_path = "./yolact.onnx"
print("convert net to ", onnx_model_path, " ... ")
torch.onnx.export(
net,
(inputs,),
onnx_model_path,
verbose=True,
input_names=["img"],
output_names=["loc", "conf", "mask", "proto"],
opset_version=12
)
print("converted successed!")
二、常见问题
1、FPN不是子模块
-
错误信息
RuntimeError: Tried to trace <torch.yolact.FPN object at 0x9c26df60> but it is not part of the active trace. Modules that are called during a trace must be registered as submodules of the thing being traced. -
解决方法
找到yolact.py文件中FPN
类的位置
将
class FPN(ScriptModuleWrapper):
修改为
class FPN(nn.Module):
2、device报错
-
错误信息
RuntimeError: legacy constructor for device type: cpu was passed device type: cuda, but device type must be: cpu. -
解决方法
在yolact开头添加
device = "cuda" if torch.cuda.is_available() else "cpu"
找到yolact.py文件中
self.priors = torch.Tensor(prior_data, device=device).view(-1, 4).detach()
将其更改为
self.priors = torch.Tensor(prior_data).view(-1, 4).detach().to(device)
原文地址:https://www.cnblogs.com/xiaxuexiaoab/p/16249384.html
推荐阅读
-
[亚马逊加入联盟对抗谷歌 ONNX] 微软 Facebook 深度学习开源联盟再添盟友
-
杨佳的新公司 Lepton AI 曝光,ONNX 联合创始人、etcd 作者加入,产品正在内部测试中
-
模型推理加速系列 | 04:BERT 加速方案 TorchScript 与 ONNX 的比较
-
崛起的 amtc 工具量化了 yolov5 的 onnx
-
深度学习模型部署概述(ONNX/NCNN/OpenVINO/TensorRT)(如下) - 参考资料
-
pytorch->onnx->ncnn->Android
-
pytorch 到 ncnn 及其测试 - I. pytorch 模型到 onnx 模型
-
pt转onnx
-
Optimum + ONNX Runtime:更轻松、更快速地训练 "拥抱脸 "模型!
-
很好地利用了轮盘 turn.js 来实现翻书的模拟效果