算法工具-1.torch Pt模型转onnx(torch.onnx.export(m, d, onnx_path))

2021/8/19 1:06:05

本文主要是介绍算法工具-1.torch Pt模型转onnx(torch.onnx.export(m, d, onnx_path)),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

使用torch.onnx.export来进行模型的构造

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.onnx

import netron


class model(nn.Module):
    def __init__(self):
        super(model, self).__init__()
        self.block1 = nn.Sequential(
            nn.Conv2d(64, 64, 3, padding=1, bias=False),
            nn.BatchNorm2d(64),
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 32, 1, bias=False),
            nn.BatchNorm2d(32),
            nn.ReLU(inplace=True),
            nn.Conv2d(32, 64, 3, padding=1, bias=False),
            nn.BatchNorm2d(64)
        )

        self.conv1 = nn.Conv2d(3, 64, 3, padding=1, bias=False)
        self.output = nn.Sequential(
            nn.Conv2d(64, 1, 3, padding=1, bias=True),
            nn.Sigmoid()
        )

    def forward(self, x):
        x = self.conv1(x)
        identity = x
        x = F.relu(self.block1(x) + identity)
        x = self.output(x)
        return x


d = torch.rand(1, 3, 416, 416)
m = model()
o = m(d)

onnx_path = "onnx_model.onnx"
torch.onnx.export(m, d, onnx_path)

netron.start(onnx_path)

 



这篇关于算法工具-1.torch Pt模型转onnx(torch.onnx.export(m, d, onnx_path))的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程