在进行Unet模型转onnx时发生报错

问题遇到的现象和发生背景

在进行Unet转onnx时发生报错 NameError: name 'useopset_vers_external_data_format' is not defined

convert torch format to onnx

input_names = ["input"]
output_names = ["output"]
torch.onnx.export(model,
                  dummy_input,
                  "unet_deconv.onnx",
                  verbose=True,
                  input_names=input_names,
                  output_names=output_names)
print("convert torch format model to onnx ...")
# [4] confirm the onnx file
net = onnx.load("unet_deconv.onnx")
# check that the IR is well formed
onnx.checker.check_model(net)
# print a human readable representation of the graph
onnx.helper.printable_graph(net.graph)
运行结果及详细报错内容

Traceback (most recent call last):
File "/home/dl/桌面/Pytorch-UNet-1.0/to_onnx.py", line 74, in
output_names=output_names)
File "/home/dl/anaconda3/envs/nano/lib/python3.6/site-packages/torch/onnx/init.py", line 276, in export
custom_opsets, enable_onnx_checker, useopset_vers_external_data_format)
NameError: name 'useopset_vers_external_data_format' is not defined

到276行那里看看吧~没有useopset_vers_external_data_format这个变量,看看有没有打错

你可以参考官方资料调整一下你的export

# Input to the model
x = torch.randn(batch_size, 1, 224, 224, requires_grad=True)
torch_out = torch_model(x)

# Export the model
torch.onnx.export(torch_model,               # model being run
                  x,                         # model input (or a tuple for multiple inputs)
                  "super_resolution.onnx",   # where to save the model (can be a file or file-like object)
                  export_params=True,        # store the trained parameter weights inside the model file
                  opset_version=10,          # the ONNX version to export the model to
                  do_constant_folding=True,  # whether to execute constant folding for optimization
                  input_names = ['input'],   # the model's input names
                  output_names = ['output'], # the model's output names
                  dynamic_axes={'input' : {0 : 'batch_size'},    # variable length axes
                                'output' : {0 : 'batch_size'}})
NameError: name 'useopset_vers_external_data_format' is not defined
名称错误:未定义名称“useopset_vers_external_data_format”
出现该情况,常见的因素排查:
情况一:要加双引号(" ")或者(’ ')而没加
情况二:字符缩进格式的问题
情况三:导入问题【路径、方式】
例:
import sys
sys.path.append('../')
import util.Reader as Reader
具体原因,可先自查下。

错误NameError: name ‘xxx’ is not defined总结

情况一:要加双引号(" ")或者(’ ')而没加

情况二:字符缩进格式的问题

情况三:if name=='main' : 没有和class类进行对齐

情况四:NameError: name ‘file’ is not defined

情况五:NameError: name ‘模块’ is not defined

情况六:NameError: name ‘reload’ is not defined

情况七:全局变量的问题

情况八:两个.py文件的函数或类调用问题(一个.py文件要调用另一个.py文件中的函数或者类时,需要添加该代码文件所在路径,否则会报“NameError: name ‘XXX’ is not defined”的错误。)

Pytorch模型转ONNX遇到Unexpected node type: onnx:: Sub
借鉴下
https://blog.csdn.net/qq_42022920/article/details/120350188

这个错误通常是由于使用了不存在的 ONNX 版本常量引起的。

解决方案是检查是否正确导入了 ONNX 库,并检查您使用的 ONNX 版本常量是否存在。

如果您正在使用的是 ONNX 1.6.0 或更高版本,可以尝试使用以下常量:

import onnx

# For ONNX 1.6.0 and higher
onnx.data_format.GOOGLENET_ONNX
onnx.data_format.NCHW
onnx.data_format.NHWC


正在使用的是更低版本的 ONNX,则可能需要使用以下常量:

import onnx

# For ONNX 1.5.0 and lower
onnx.TensorProto.GOOGLENET_ONNX
onnx.TensorProto.NCHW
onnx.TensorProto.NHWC


/home/dl/桌面/Pytorch-UNet-1.0/to_onnx.py", line 74 这里报错了,useopset_vers_external_data_format未定义
可能的原因:
1、变量未提前定义
2、定义位置不准确
3、检查一下这个名称是否为import过来的

这是一个 Python 程序中的异常信息,其中报告了在执行 torch.onnx.export 函数时发生了 NameError 错误。

NameError 错误通常是由于在代码中使用了未定义的变量或函数名导致的。在这个错误信息中,可以看到程序在调用 torch.onnx.export 时传入了一个未定义的参数 useopset_vers_external_data_format。

为了解决这个错误,需要检查代码中调用 torch.onnx.export 的位置,并确保传入的所有参数都是正确定义的。如果你不确定某个参数的意义,可以查看 PyTorch 文档或者参考其他资料来了解。