问题:
TracerWarning: torch.tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.
虽然在转换中使用 dynamic_axes 可以在转换后的 onnx 模型输入任意尺寸的图像,但如果原模型处理过程中直接使用了 torch.tensor() ,导出的onnx模型对于不同尺寸的图片不能得到正确的处理结果
原代码:
def normgrid(self, x, H, W):
""" Normalize coords to [-1,1]. """
return 2. * (x/(torch.tensor([W-1, H-1], device = x.device, dtype = x.dtype))) - 1.
修改后代码:
def normgrid(self, x, H, W):
"" Normalize coords to [-1,1]. ""
# 将W-1和H-1转换为变量
W_var = torch.tensor(W-1, device=x.device, dtype=x.dtype)
H_var = torch.tensor(H-1, device=x.device, dtype=x.dtype)
return 2. * (x / torch.stack([W_var, H_var])) - 1.
前后对比:修改后导出的模型提出的特征点更加准确