Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在train.py中取消注释tb_writer.add_graph(model, imgs) ,会报错。注释掉则正常运行 #2284

Closed
wyuzyf opened this issue Feb 24, 2021 · 6 comments · Fixed by #2758
Labels
question Further information is requested Stale

Comments

@wyuzyf
Copy link

wyuzyf commented Feb 24, 2021

❔Question

在train.py中取消注释tb_writer.add_graph(model, imgs) ,会报错

EEROR如下:

Additional context

module._c._create_method_from_trace(method_name, func, example_inputs, var_lookup_fn, strict, _force_outplace)
D:\zhaoyinfa\code\zdws\models\yolo.py:45: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
if self.grid[i].shape[2:4] != x[i].shape[2:4]:
0/299 0G 0.1403 0.0758 0 0.2161 24 640: 0%| | 0/17 [00:05<?, ?it/s]
Traceback (most recent call last):
File "D:/zhaoyinfa/code/zdws/train.py", line 442, in
train(hyp, opt, device, tb_writer)
File "D:/zhaoyinfa/code/zdws/train.py", line 294, in train
tb_writer.add_graph(model, imgs) # add model to tensorboard
File "D:\ProgramData\Anaconda3\lib\site-packages\torch\utils\tensorboard\writer.py", line 714, in add_graph
self.get_file_writer().add_graph(graph(model, input_to_model, verbose))
File "D:\ProgramData\Anaconda3\lib\site-packages\torch\utils\tensorboard_pytorch_graph.py", line 285, in graph
trace = torch.jit.trace(model, args)
File "D:\ProgramData\Anaconda3\lib\site-packages\torch\jit_init
.py", line 955, in trace
check_tolerance, strict, _force_outplace, module_class)
File "D:\ProgramData\Anaconda3\lib\site-packages\torch\jit_init
.py", line 1119, in trace_module
check_tolerance, strict, _force_outplace, True, module_class)
File "D:\ProgramData\Anaconda3\lib\site-packages\torch\autograd\grad_mode.py", line 15, in decorate_context
return func(*args, **kwargs)
File "D:\ProgramData\Anaconda3\lib\site-packages\torch\jit_init
.py", line 742, in _check_trace
raise TracingCheckError(*diag_info)
torch.jit.TracingCheckError: Tracing failed sanity checks!
ERROR: Graphs differed across invocations!
Graph diff:
graph(%self.1 : torch.models.yolo.Model,
%x : Tensor):
%2 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%3 : torch.models.yolo.Detect = prim::GetAttrname="24"
%4 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%5 : torch.models.common.BottleneckCSP = prim::GetAttrname="23"
%6 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%7 : torch.models.common.Concat = prim::GetAttrname="22"
%8 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%9 : torch.models.common.Conv = prim::GetAttrname="21"
%10 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%11 : torch.models.common.BottleneckCSP = prim::GetAttrname="20"
%12 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%13 : torch.models.common.Concat = prim::GetAttrname="19"
%14 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%15 : torch.models.common.Conv = prim::GetAttrname="18"
%16 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%17 : torch.models.common.BottleneckCSP = prim::GetAttrname="17"
%18 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%19 : torch.models.common.Concat = prim::GetAttrname="16"
%20 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%21 : torch.torch.nn.modules.upsampling.Upsample = prim::GetAttrname="15"
%22 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%23 : torch.models.common.Conv = prim::GetAttrname="14"
%24 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%25 : torch.models.common.BottleneckCSP = prim::GetAttrname="13"
%26 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%27 : torch.models.common.Concat = prim::GetAttrname="12"
%28 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%29 : torch.torch.nn.modules.upsampling.Upsample = prim::GetAttrname="11"
%30 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%31 : torch.models.common.Conv = prim::GetAttrname="10"
%32 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%33 : torch.models.common.BottleneckCSP = prim::GetAttrname="9"
%34 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%35 : torch.models.common.SPP = prim::GetAttrname="8"
%36 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%37 : torch.models.common.Conv = prim::GetAttrname="7"
%38 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%39 : torch.models.common.BottleneckCSP = prim::GetAttrname="6"
%40 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%41 : torch.models.common.Conv = prim::GetAttrname="5"
%42 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%43 : torch.models.common.BottleneckCSP = prim::GetAttrname="4"
%44 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%45 : torch.models.common.Conv = prim::GetAttrname="3"
%46 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%47 : torch.models.common.BottleneckCSP = prim::GetAttrname="2"
%48 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%49 : torch.models.common.Conv = prim::GetAttrname="1"
%50 : torch.torch.nn.modules.container.Sequential = prim::GetAttrname="model"
%51 : torch.models.common.Focus = prim::GetAttrname="0"
%83 : bool = prim::Constantvalue=1, scope: __module.model.0/__module.model.0.conv/__module.model.0.conv.conv # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py:416:0
%84 : bool = prim::Constantvalue=0, scope: __module.model.0/__module.model.0.conv/__module.model.0.conv.conv # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py:416:0
%85 : None = prim::Constant(), scope: __module.model.0/__module.model.0.conv/__module.model.0.conv.conv
%86 : float = prim::Constantvalue=0.001, scope: __module.model.0/__module.model.0.conv/__module.model.0.conv.bn # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\functional.py:2016:0
%87 : float = prim::Constantvalue=0.029999999999999999, scope: __module.model.0/__module.model.0.conv/__module.model.0.conv.bn # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\functional.py:2016:0
%88 : float = prim::Constantvalue=0.10000000000000001, scope: __module.model.0/__module.model.0.conv/__module.model.0.conv.act # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\functional.py:1293:0
%89 : int = prim::Constantvalue=1, scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%90 : int = prim::Constantvalue=3, scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%91 : int = prim::Constantvalue=9223372036854775807, scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%92 : int = prim::Constantvalue=0, scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%93 : int = prim::Constantvalue=2, scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%94 : torch.models.common.Conv = prim::GetAttrname="conv"
%95 : Tensor = aten::slice(%x, %93, %92, %91, %93), scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%96 : Tensor = aten::slice(%95, %90, %92, %91, %93), scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%97 : Tensor = aten::slice(%x, %93, %89, %91, %93), scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%98 : Tensor = aten::slice(%97, %90, %92, %91, %93), scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%99 : Tensor = aten::slice(%x, %93, %92, %91, %93), scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%100 : Tensor = aten::slice(%99, %90, %89, %91, %93), scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%101 : Tensor = aten::slice(%x, %93, %89, %91, %93), scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%102 : Tensor = aten::slice(%101, %90, %89, %91, %93), scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%103 : Tensor[] = prim::ListConstruct(%96, %98, %100, %102), scope: __module.model.0
%input.1 : Tensor = aten::cat(%103, %89), scope: __module.model.0 # D:\zhaoyinfa\code\zdws\models\common.py:88:0
%105 : torch.torch.nn.modules.batchnorm.BatchNorm2d = prim::GetAttrname="bn"
%106 : torch.torch.nn.modules.conv.Conv2d = prim::GetAttrname="conv"
%107 : Tensor = prim::GetAttrname="weight"
%108 : int[] = prim::ListConstruct(%89, %89), scope: __module.model.0/__module.model.0.conv/__module.model.0.conv.conv
%109 : int[] = prim::ListConstruct(%89, %89), scope: __module.model.0/__module.model.0.conv/__module.model.0.conv.conv
%110 : int[] = prim::ListConstruct(%89, %89), scope: __module.model.0/__module.model.0.conv/__module.model.0.conv.conv
%111 : int[] = prim::ListConstruct(%92, %92), scope: __module.model.0/__module.model.0.conv/__module.model.0.conv.conv
%input.2 : Tensor = aten::_convolution(%input.1, %107, %85, %108, %109, %110, %84, %111, %89, %83, %84, %83), scope: __module.model.0/__module.model.0.conv/__module.model.0.conv.conv # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py:416:0
%113 : Tensor = prim::GetAttrname="running_var"
%114 : Tensor = prim::GetAttrname="running_mean"
%115 : Tensor = prim::GetAttrname="bias"
%116 : Tensor = prim::GetAttrname="weight"
%input.3 : Tensor = aten::batch_norm(%input.2, %116, %115, %114, %113, %84, %87, %86, %83), scope: __module.model.0/__module.model.0.conv/_module.model.0.conv.bn # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\functional.py:2016:0
%input.4 : Tensor = aten::leaky_relu
(%input.3, %88), scope: __module.model.0/__module.model.0.conv/__module.model.0.conv.act # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\functional.py:1293:0
%119 : float = prim::Constantvalue=0.10000000000000001, scope: __module.model.1/__module.model.1.act # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\functional.py:1293:0
%120 : float = prim::Constantvalue=0.029999999999999999, scope: __module.model.1/__module.model.1.bn # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\functional.py:2016:0
%121 : float = prim::Constantvalue=0.001, scope: __module.model.1/__module.model.1.bn # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\functional.py:2016:0
%122 : None = prim::Constant(), scope: __module.model.1/__module.model.1.conv
%123 : int = prim::Constantvalue=2, scope: __module.model.1/__module.model.1.conv # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py:416:0
%124 : int = prim::Constantvalue=1, scope: __module.model.1/__module.model.1.conv # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py:416:0
%125 : bool = prim::Constantvalue=0, scope: __module.model.1/__module.model.1.conv # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py:416:0
%126 : int = prim::Constantvalue=0, scope: __module.model.1/__module.model.1.conv # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py:416:0
%127 : bool = prim::Constantvalue=1, scope: __module.model.1/__module.model.1.conv # D:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\conv.py:416:0
%128 : torch.torch.nn.modules.batchnorm.BatchNorm2d = prim::GetAttrname="bn"
%129 : torch.torch.nn.modules.conv.Conv2d = prim::GetAttrname="conv"
%130 : Tensor = prim::GetAttr[name="weight

@wyuzyf wyuzyf added the question Further information is requested label Feb 24, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Feb 24, 2021

👋 Hello @wyuzyf, thank you for your interest in 🚀 YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

glenn-jocher commented Feb 24, 2021

👋 Hello, thank you for your interest in our work! This issue seems to lack the minimum requirements for a proper response, or is insufficiently detailed for us to help you. Please note that most technical problems are due to:

  • Your modified or out-of-date code. If your issue is not reproducible in a new git clone version of this repo we can not debug it. Before going further run this code and verify your issue persists:
$ git clone https://github.com/ultralytics/yolov5 yolov5_new  # clone latest
$ cd yolov5_new
$ python detect.py  # verify detection

# CODE TO REPRODUCE YOUR ISSUE HERE
  • Your custom data. If your issue is not reproducible in one of our 3 common datasets (COCO, COCO128, or VOC) we can not debug it. Visit our Custom Training Tutorial for guidelines on training your custom data. Examine train_batch0.jpg and test_batch0.jpg for a sanity check of your labels and images.

  • Your environment. If your issue is not reproducible in one of the verified environments below we can not debug it. If you are running YOLOv5 locally, verify your environment meets all of the requirements.txt dependencies specified below. If in doubt, download Python 3.8.0 from https://www.python.org/, create a new venv, and install requirements.

If none of these apply to you, we suggest you close this issue and raise a new one using the 🐛 Bug Report template, providing screenshots and a minimum reproducible example of your issue. Thank you!

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are passing. These tests evaluate proper operation of basic YOLOv5 functionality, including training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu.

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@SkalskiP
Copy link
Contributor

SkalskiP commented Apr 7, 2021

@glenn-jocher any progress on that issue? I have the same issue.

Interesting update: If I run ts = torch.jit.trace(model, img) once again the last error does not occur and the tracing is successful. But this traced model doesn't work.

@glenn-jocher
Copy link
Member

@SkalskiP @wyuzyf I looked into this. The detect() layer checks the input size to adapt the grid if required, and tracing does not seem to like this shape check (even if the shape is fine and no grid recomputation is required). The following will warn:

yolov5/train.py

Line 335 in 0cae757

# tb_writer.add_graph(model, imgs) # add model to tensorboard

I found a solution below. This is a YOLOv5s model displayed in TensorBoard. You can see the Detect() layer merging the 3 layers into a single output.

tb_writer.add_graph(torch.jit.trace(model, imgs, strict=False), imgs)

Screenshot 2021-04-11 at 01 10 09

@glenn-jocher
Copy link
Member

@SkalskiP @wyuzyf this bug should be resolved now in PR #2758. Please let us know if this resolves the problem on your side, and let us know if you run into any other issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants