当前位置:网站首页>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
边栏推荐
- AUTOSAR从入门到精通100讲(八十一)-AUTOSAR基础篇之FiM
- Solution to slow compilation speed of Xcode
- [dynamic programming] Yang Hui triangle
- [point cloud series] a rotation invariant framework for deep point cloud analysis
- 基于open mv 搭配stm32循迹
- 机器视觉系列(02)---TensorFlow2.3 + win10 + GPU安装
- 基于51单片机的温湿度监测+定时报警系统(c51源码)
- EMMC/SD学习小记
- 直观理解 torch.nn.Unfold
- 《Multi-modal Visual Tracking:Review and Experimental Comparison》翻译
猜你喜欢
Chapter 3 pytoch neural network toolbox
Summary of image classification white box anti attack technology
项目文件“ ”已被重命名或已不在解决方案中、未能找到与解决方案关联的源代码管理提供程序——两个工程问题
AUTOSAR从入门到精通100讲(五十一)-AUTOSAR网络管理
GIS实战应用案例100篇(五十二)-ArcGIS中用栅格裁剪栅格,如何保持行列数量一致并且对齐?
使用proteus仿真STM32超声波SRF04测距!Code+Proteus
imx6ull-qemu 裸机教程2:USDHC SD卡
Detailed explanation of device tree
【点云系列】点云隐式表达相关论文概要
[point cloud series] a rotation invariant framework for deep point cloud analysis
随机推荐
Proteus 8.10安装问题(亲测稳定不闪退!)
swin transformer 转 onnx
Write a wechat double open gadget to your girlfriend
【点云系列】 场景识别类导读
PyTorch 20. PyTorch技巧(持续更新)
【Tensorflow】共享机制
如何利用qemu搭建SOC protoype:80行代码实现一个Cortex M4 模拟器
【點雲系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation
直观理解 torch.nn.Unfold
x509解析
【点云系列】点云隐式表达相关论文概要
MySQL installation and configuration - detailed tutorial
ARMCC/GCC下的stack protector
吴恩达编程作业——Logistic Regression with a Neural Network mindset
Detailed explanation of device tree
Chapter 2 pytoch foundation 1
机器视觉系列(02)---TensorFlow2.3 + win10 + GPU安装
Five methods are used to obtain the parameters and calculation of torch network model
Systrace 解析
AUTOSAR从入门到精通100讲(五十二)-诊断和通信管理功能单元