import io
import numpy as np
import torch
import torch.onnx
from model.nets.yolo4 import YoloBody
from conf.my_conf import anchors_path, classes_path, model_path_train
weight_file = '../data/weights/Epoch75-Total_Loss1.3492-Val_Loss3.9141.pth'
image_path = '../data/img/wastebag/0004.jpg'
onnx_file_name = '../data/weights/Yolo.onnx'
batch_size = 1
num_anchors, num_classes = 3, 2
IN_IMAGE_H = 416
IN_IMAGE_W = 416
model = YoloBody(num_anchors, num_classes)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# 加快模型训练的效率
model_dict = model.state_dict()
pretrained_dict = torch.load(weight_file, map_location=device)
pretrained_dict = {k: v for k, v in pretrained_dict.items() if np.shape(model_dict[k]) == np.shape(v)}
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
dummy_input1 = torch.randn((1, 3, IN_IMAGE_H, IN_IMAGE_W), requires_grad=True)
input_names = ["input"] # onnx输入接口的名字,需要与模型输入结果对应
output_names = ["output0", "output1", "output2"] # onnx输出接口的名字,需要与模型输出结果对应
dynamic_axes = {"input": {0: "batch_size"}, "output0": {0: "batch_size"}, "output1": {0: "batch_size"}, "output2": {0: "batch_size"}}
torch.onnx.export(model,
dummy_input1,
onnx_file_name,
export_params=True,
opset_version=11,
do_constant_folding=True,
input_names=input_names,
output_names=output_names,
dynamic_axes=dynamic_axes)
Pytorch模型转onnx模型2
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 我首先尝试一下keras-yolo3的可靠性,我首先下载了keras-yolo3的官方训练好的权重文件,附链接:h...
- 操作系统:Windows 10 IDE:Pycharm Python: 3.6.2 且已安装好tensorflow...
- 目录faster rcnn论文备注caffe代码框架简介faster rcnn代码分析后记 faster rcnn...