当前位置:网站首页>Swin transformer to onnx
Swin transformer to onnx
2022-04-23 07:27:00 【wujpbb7】
swin transformer Code : Unofficial implementation , But it's easy to understand .
Will be trained pth turn onnx Code :
import torch
from swin_transformer_pytorch import swin_t
pth_filename = './demo.pth' # Trained weights
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')
Errors occurred :
1、“Exporting the operator roll to ONNX opset version 12 is not supported.”
modify roll by 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'.”
hold “ Slice self addition subtraction ” Replace with 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')
...
Reference resources :
Pytorch turn ONNX- Actual combat 2( Summary of actual combat stepping on the pit )
版权声明
本文为[wujpbb7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230611550496.html
边栏推荐
- Some common data type conversion methods in pytorch are similar to list and NP Conversion method of ndarray
- PyTorch 13. 嵌套函数和闭包(狗头)
- GIS实用小技巧(三)-CASS怎么添加图例?
- 【点云系列】FoldingNet:Point Cloud Auto encoder via Deep Grid Deformation
- ECDSA 签名验证原理及C语言实现
- “Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated
- AUTOSAR从入门到精通100讲(八十一)-AUTOSAR基础篇之FiM
- 《Multi-modal Visual Tracking:Review and Experimental Comparison》翻译
- 带低压报警的51单片机太阳能充电宝设计与制作(完整代码资料)
- Chapter 4 pytoch data processing toolbox
猜你喜欢

【点云系列】Pointfilter: Point Cloud Filtering via Encoder-Decoder Modeling

【点云系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation

RISCV MMU 概述

【点云系列】Neural Opacity Point Cloud(NOPC)

rearrange 和 einsum 真的优雅吗

1.1 pytorch and neural network

【点云系列】Multi-view Neural Human Rendering (NHR)

【点云系列】Unsupervised Multi-Task Feature Learning on Point Clouds

Device Tree 详解

机器视觉系列(02)---TensorFlow2.3 + win10 + GPU安装
随机推荐
《Multi-modal Visual Tracking:Review and Experimental Comparison》翻译
基于Labview上位机的51单片机步进电机控制系统(上位机代码+下位机源码+ad原理图+51完整开发环境)
【点云系列】点云隐式表达相关论文概要
Device Tree 详解
SSL / TLS application example
Minesweeping games
scons 搭建嵌入式arm编译
Chapter 1 numpy Foundation
Common regular expressions
安装 pycuda 出现 PEP517 的错误
WinForm scroll bar beautification
Compression and acceleration technology of deep learning model (I): parameter pruning
PyTorch 20. PyTorch技巧(持续更新)
Detailed explanation of unwind stack backtracking
. net encountered failed to decode downloaded font while loading font:
[point cloud series] a rotation invariant framework for deep point cloud analysis
How keras saves and loads the keras model
【点云系列】DeepMapping: Unsupervised Map Estimation From Multiple Point Clouds
GIS实战应用案例100篇(五十一)-ArcGIS中根据指定的范围计算nc文件逐时次空间平均值的方法
Mysql database installation and configuration details