最终解决方案是建立了一个新的虚拟环境,对默认的代码做了一些修改。
报错或警告的内容包括:
torch._C.Value object has no attribute 'uniqueName'
ONNX's Upsample /Resize operator did not match until opset 11 Pytorch's Interpolation until opset 11
_optimize_trace_missing 1 required positional argument''
module torch.onnx has no attribute set_training
module 'torch.jit' has no attribute '_script_if_tracing'
_optimize_trace_missing 1 required positional argument
针对以上问题,见缝插针,逐一解决。我的个人配置版本如下:
pytorch=1.5.1(貌似1.6以上就出现了一些新的问题,1.6以上可能影响tensorwatch的使用,我忘记在哪里看到了)
torchvision=0.6.1
tensorwatch=0.8.0(该版本以后如0.9.1中的__init__函数中的draw_model与0.8.0明显不同,很奇怪)
tensorboard=2.1.0
tensorboardX=1.8 2.2好像都可以 好像不是特别关键
可能主要就牵涉到这几个包
做了一些改动
your envs/python3.7/site-packages/tensorwatch/model_graph/hiddenlayer/pytorch_builder.py中
修改
trace,out=torch.jit.get_trace_graph(models,args)
改为:
trace,out=torch.jit._get_trace_graph()models,args)
torch_graph=trace.graph()
改为
torch_graph=trace(如果不改,就提示 has no attribute graph,好像是这样,具体信息我忘记了)
your envs/python3.7/site-packages/torch/onnx中symbolic_helper.py
修改_default_onnx_opset_version=9为_default_onnx_opset_version=11(这涉及到Upsample...)
我有点怀疑做这件事的意义了,修改完了,能跑通了,但是可视化的结果还是怪怪的,所以修改的意义在哪里呢?pytorch各个版本库的兼容性真是让人头大!
RuntimeError: Failed to export an ONNX attribute, since it's not constant, please try to make thinkgs (e.g., kernel size) static if possible
如何解决?亲测可用的办法来自:https://github.com/onnx/tutorials/issues/137,即定位到onnx/ymbolic.py对其进行代码级的修改,删除红色部分代码,新增绿色部分代码https://github.com/pytorch/pytorch/commit/11845cf2440c1abb0cb117bcee9532b26573e9c9
修改的时候需要细心检查,保持自己所做的修改与其保持一致。说一个自己差点漏掉的点,即首行@parse_args('v','is')就差点被我漏掉了!