栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

swin transformer 转 onnx

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

swin transformer 转 onnx

swin transformer 代码:非官方实现,但是好理解 。

将训练好的 pth 转 onnx 代码:

import torch
from swin_transformer_pytorch import swin_t

pth_filename = './demo.pth'  # 训练好的权重
onnx_filename = './demo.onnx'

net = swin_t()
weights = torch.load(pth_filename)
#net.load_state_dict(weights)
net.load_state_dict({k.replace('module.', ''): v for k, v in weights['embedding'].items()})
net.eval()

dummy_input = torch.randn(1, 3, 224, 224, device='cpu')
torch.onnx.export(net, dummy_input, onnx_filename,
                  input_names=['input'], output_names=['ouput'],
                  export_params=True, verbose=False, opset_version=12,
                  dynamic_axes={'input':{0:"batch_size"},
                                'output':{0:"batch_size"}})

print('save onnx succ')

出现的错误:

1、“Exporting the operator roll to onNX opset version 12 is not supported.”

修改 roll 为 cat:

class CyclicShift(nn.Module):
    def __init__(self, displacement):
        super().__init__()
        self.displacement = displacement

    def forward(self, x):
        #return torch.roll(x, shifts=(self.displacement, self.displacement), dims=(1, 2))
        x=torch.cat((x[:,self.displacement:,:,:], x[:,:self.displacement,:,:]), dim=1)
        x=torch.cat((x[:,:,self.displacement:,:], x[:,:,:self.displacement,:]), dim=2)
        return x

2、“RuntimeError: Expected node type 'onnx::Constant', got 'onnx::Cast'.”

把 “对切片做自加减法” 替换成 cat:

class WindowAttention(nn.Module):
    ...
    def forward(self, x):
        ...
        #if self.shifted:
            #dots[:, :, -nw_w:] += self.upper_lower_mask
            #dots[:, :, nw_w - 1::nw_w] += self.left_right_mask
        
        if self.shifted:
            dots = rearrange(dots, 'b c (n_h n_w) h w -> b c n_h n_w h w', n_h=nw_h, n_w=nw_w)
            dots = torch.cat((dots[:, :, :-1], dots[:, :, -1:] + self.upper_lower_mask), dim=2)
            dots = dots.permute(0,1,3,2,4,5)
            dots = torch.cat((dots[:, :, :-1], dots[:, :, -1:] + self.left_right_mask), dim=2)
            dots = dots.permute(0,1,3,2,4,5)
            dots = rearrange(dots, 'b c n_h n_w h w -> b c (n_h n_w) h w')
        ...

参考:

Pytorch转ONNX-实战篇2(实战踩坑总结)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/354940.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号