当前位置:网站首页>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
边栏推荐
- excel实战应用案例100讲(八)-Excel的报表连接功能
- [point cloud series] pnp-3d: a plug and play for 3D point clouds
- 主流 RTOS 评估
- swin transformer 转 onnx
- UEFI学习01-ARM AARCH64编译、ArmPlatformPriPeiCore(SEC)
- 多机多卡训练时的错误
- 基于openmv的无人机Apriltag动态追踪降落完整项目资料(labview+openmv+apriltag+正点原子四轴)
- 【Tensorflow】共享机制
- Résolution du système
- 网络层重要知识(面试、复试、期末)
猜你喜欢
随机推荐
【点云系列】Learning Representations and Generative Models for 3D pointclouds
GIS实用小技巧(三)-CASS怎么添加图例?
吴恩达编程作业——Logistic Regression with a Neural Network mindset
【Tensorflow】共享机制
PyTorch 17. GPU并发
unhandled system error, NCCL version 2.7.8
画 ArcFace 中的 margin 曲线
GIS实战应用案例100篇(五十一)-ArcGIS中根据指定的范围计算nc文件逐时次空间平均值的方法
ArcGIS license server administrator cannot start the workaround
[8] Assertion failed: dims.nbDims == 4 || dims.nbDims == 5
【点云系列】FoldingNet:Point Cloud Auto encoder via Deep Grid Deformation
PyTorch 21. PyTorch中nn.Embedding模块
机器学习——朴素贝叶斯
pth 转 onnx 时出现的 gather、unsqueeze 等算子
GIS实战应用案例100篇(五十三)-制作三维影像图用以作为城市空间格局分析的底图
Unwind 栈回溯详解
面试总结之特征工程
【无标题】PID控制TT编码器电机
rearrange 和 einsum 真的优雅吗
AUTOSAR从入门到精通100讲(八十一)-AUTOSAR基础篇之FiM









