zoukankan      html  css  js  c++  java
  • 算法工具-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)
  • 相关阅读:
    Log4Net使用
    4月博文
    论坛题目练习
    职场冷笑话两则
    初识管理的一些心得
    Project中分清楚挣值项
    预留规划项
    小感触
    好事多磨,好事成双
    忧郁
  • 原文地址:https://www.cnblogs.com/my-love-is-python/p/15159281.html
Copyright © 2011-2022 走看看