当前位置:网站首页>Problems related to pytorch to onnx
Problems related to pytorch to onnx
2022-07-27 06:57:00 【Mr_ health】
These problems are in spectral normalization spectral_norm Meet in .
The first thing I met was torch.mv Operator and torch.dot Problems not supported by operators .
at present pytorch Spectral normalization has been officially realized :spectral_norm, Which includes torch.mv、 torch.dot operator , turn onnx There will be errors
terms of settlement : take torch.mv and torch.dot use torch.matmul Instead of , But you may need to change yourself tensor Dimensions .( adopt unsqueeze And so on. )
After I solve the above two operators , Be able to run torch.onnx.export function , But when transforming inference :
RuntimeError: invalid argument 0: Tensors must have same number of dimensions: got 2 and 1It seems that there is something wrong with the dimension , But I searched for hours and couldn't find the problem .
The solution later was , In turn onnx Before , remove spectral_norm.
Refer to :https://github.com/pytorch/pytorch/issues/27723
The official has realized how to remove spectral_norm Function of :
def remove_spectral_norm(module, name='weight'):
r"""Removes the spectral normalization reparameterization from a module.
Args:
module (Module): containing module
name (str, optional): name of weight parameter
Example:
>>> m = spectral_norm(nn.Linear(40, 10))
>>> remove_spectral_norm(m)
"""
for k, hook in module._forward_pre_hooks.items():
if isinstance(hook, SpectralNorm) and hook.name == name:
hook.remove(module)
del module._forward_pre_hooks[k]
break
else:
raise ValueError("spectral_norm of '{}' not found in {}".format(
name, module))
for k, hook in module._state_dict_hooks.items():
if isinstance(hook, SpectralNormStateDictHook) and hook.fn.name == name:
del module._state_dict_hooks[k]
break
for k, hook in module._load_state_dict_pre_hooks.items():
if isinstance(hook, SpectralNormLoadStateDictPreHook) and hook.fn.name == name:
del module._load_state_dict_pre_hooks[k]
break
return moduleThe specific steps are :
1. Build the model according to the training model( At this time, it still contains spectral_norm), And load pretrained model, This pretrained model contains spectral_norm Related parameters of :weight_orig、weight_u as well as weight_v.
2. Then use the following function , The input of this function is built model, What is completed is the structure of recursive model , When I meet spectral_norm when , Will call the above remove_spectral_norm remove spectral_norm.
def remove_all_spectral_norm(item):
if isinstance(item, nn.Module):
try:
remove_spectral_norm(item)
except Exception:
pass
for child in item.children():
remove_all_spectral_norm(child)
if isinstance(item, nn.ModuleList):
for module in item:
remove_all_spectral_norm(module)
if isinstance(item, nn.Sequential):
modules = item.children()
for module in modules:
remove_all_spectral_norm(module)3. Finally run torch.onnx.export
Here's a little explanation , Ordinary convolution operations are followed by spectral_norm after , The training parameters will be convoluted weight Will turn into weight_orig、weight_u、weight_v These three categories , That is, these parameters are saved when the model is saved .
Through the above removal operation , From weight_orig、weight_u、weight_v Recover weight.
边栏推荐
- Soul continues to make efforts to list its social channels in Hong Kong. Why is "soul style social" popular?
- MangoDB
- Pruning - quantification - turn to onnx Chinese series tutorials
- Account management and authority
- What if the website server is attacked? Sunflower tips that preventing loopholes is the key
- Basic concepts of program, process, thread, coprocess, single thread and multi thread
- Ftx.us launched stock and ETF trading services to make trading more transparent
- C语言怎么学?这篇文章给你完整答案
- 银行业客户体验管理现状与优化策略分析
- Redis operation of Linux Installation
猜你喜欢

EasyCVR平台播放设备录像时,拖动时间轴播放无效是什么原因?

NFS introduction and configuration

C语言怎么学?这篇文章给你完整答案

齐岳:巯基修饰寡聚DNA|DNA修饰CdTe/CdS核壳量子点|DNA偶联砷化铟InAs量子点InAs-DNA QDs

regular expression

Esxi virtual machine starts, and the module "monitorloop" fails to power on

Event capture and bubbling - what is the difference between them?

DNA偶联PbSe量子点|近红外硒化铅PbSe量子点修饰脱氧核糖核酸DNA|PbSe-DNA QDs

含有偶氮苯单体的肽核酸寡聚体(NH2-TNT4,N-PNAs)齐岳生物定制

Auto encoder (AE), denoising auto encoder (DAE), variable auto encoder (VAE) differences
随机推荐
Ftx.us launched stock and ETF trading services to make trading more transparent
Speech and language processing (3rd ed. draft) Chapter 2 - regular expression, text normalization, editing distance reading notes
Px4 source code compilation to establish its own program module
Relevant preparation materials for system design
Sunflower teaches you how to prevent denial of service attacks?
Account management and authority
关于卡尔曼滤波的协方差如何影响deepsort的跟踪效果的考虑
How to avoid loopholes? Sunflower remote explains the safe use methods in different scenarios
Li Hongyi 2020 deep learning and human language processing dlhlp core resolution-p21
CentOS上使用Docker安装和部署Redis
关于ES6的新特性
goLang的一个跨域问题
Create a container that does not depend on any underlying image
GoLand writes Go program
Is it feasible to fix the vulnerability with one click? Sunflower to tell you that one click fix vulnerability is feasible? Sunflower to tell you that one click fix vulnerability is feasible? Sunflowe
About the new features of ES6
deepsort源码解读(三)
程序、进程、线程、协程以及单线程、多线程基本概念
含有偶氮苯单体的肽核酸寡聚体(NH2-TNT4,N-PNAs)齐岳生物定制
deepsort源码解读(六)