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

Use of tensorboard in yolo.py #2898

Closed
leotongxue opened this issue Apr 22, 2021 · 24 comments · Fixed by #2899 or #2949
Closed

Use of tensorboard in yolo.py #2898

leotongxue opened this issue Apr 22, 2021 · 24 comments · Fixed by #2899 or #2949
Labels
question Further information is requested

Comments

@leotongxue
Copy link

❔Question

When I uncomment Tensorboard,the following error occurred

Additional context

Run 'tensorboard --logdir=models/runs' to view tensorboard at http://localhost:6006/
Traceback (most recent call last):
File "/Users/leo/Desktop/yolov5/models/yolo.py", line 279, in
tb_writer.add_graph(model.model, img) # add model to tensorboard
File "/Users/leo/.virtualenvs/yolov5_3/lib/python3.7/site-packages/torch/utils/tensorboard/writer.py", line 724, in add_graph
self._get_file_writer().add_graph(graph(model, input_to_model, verbose))
File "/Users/leo/.virtualenvs/yolov5_3/lib/python3.7/site-packages/torch/utils/tensorboard/_pytorch_graph.py", line 286, in graph
trace = torch.jit.trace(model, args)
File "/Users/leo/.virtualenvs/yolov5_3/lib/python3.7/site-packages/torch/jit/_trace.py", line 742, in trace
_module_class,
File "/Users/leo/.virtualenvs/yolov5_3/lib/python3.7/site-packages/torch/jit/_trace.py", line 940, in trace_module
_force_outplace,
File "/Users/leo/.virtualenvs/yolov5_3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 725, in _call_impl
result = self._slow_forward(*input, **kwargs)
File "/Users/leo/.virtualenvs/yolov5_3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 709, in _slow_forward
result = self.forward(*input, **kwargs)
File "/Users/leo/.virtualenvs/yolov5_3/lib/python3.7/site-packages/torch/nn/modules/container.py", line 117, in forward
input = module(input)
File "/Users/leo/.virtualenvs/yolov5_3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 725, in _call_impl
result = self._slow_forward(*input, **kwargs)
File "/Users/leo/.virtualenvs/yolov5_3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 709, in _slow_forward
result = self.forward(*input, **kwargs)
File "/Users/leo/Desktop/yolov5/models/common.py", line 210, in forward
return torch.cat(x, self.d)
TypeError: cat() received an invalid combination of arguments - got (Tensor, int), but expected one of:

  • (tuple of Tensors tensors, name dim, *, Tensor out)
  • (tuple of Tensors tensors, int dim, *, Tensor out)
@leotongxue leotongxue added the question Further information is requested label Apr 22, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Apr 22, 2021

👋 Hello @leotongxue, 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

@leotongxue I'm sorry I don't know what you mean by 'uncomment tensorboard'. Tensorboard is enabled by default for all training runs, you don't need to uncomment anything or modify the code in any way. See the YOLOv5 Colab Notebook training section for an example of how to use TensorBoard:
https://colab.research.google.com/github/ultralytics/yolov5/blob/master/tutorial.ipynb

@glenn-jocher
Copy link
Member

Just tested again right now, everything works well!

Screenshot 2021-04-22 at 18 01 54

@leotongxue
Copy link
Author

leotongxue commented Apr 22, 2021

thank you for your reply,I run the code on the picture and it will go wrong
image

@glenn-jocher
Copy link
Member

@leotongxue you can produce a Tensorboard model graph visual by uncommenting L333 and L335 in train.py.

Screenshot 2021-04-22 at 18 20 36

@leotongxue
Copy link
Author

image

Thanks, but how to modify so that the code on the picture does not report errors

@glenn-jocher
Copy link
Member

@leotongxue sorry, we generally don't help debug custom code, but you can use train.py to get started, as it works correctly there!

@leotongxue
Copy link
Author

This code is not written by me, this code is in yolo.py😂

@glenn-jocher glenn-jocher linked a pull request Apr 22, 2021 that will close this issue
@glenn-jocher
Copy link
Member

@leotongxue oh! Ok then I will take a look. Let's see

@glenn-jocher glenn-jocher reopened this Apr 22, 2021
@glenn-jocher
Copy link
Member

@leotongxue I took a look but could not get Tensorboard to cooperate. I'm not sure why it works in train.py and not in yolo.py :(

@jaskiratsingh2000
Copy link

Hey, if I run this will that give me the latency of each layer? Just by running the yolo.py?

@glenn-jocher
Copy link
Member

glenn-jocher commented Apr 26, 2021

@jaskiratsingh2000 in yolo.py first uncomment this line:

# x = x.copy() # for profiling

then you can uncomment these lines and run the model with profile=True for a layer by layer profile result

yolov5/models/yolo.py

Lines 268 to 271 in 1849916

# Profile
# img = torch.rand(8 if torch.cuda.is_available() else 1, 3, 320, 320).to(device)
# y = model(img, profile=True)

@glenn-jocher
Copy link
Member

@jaskiratsingh2000 I just updated yolo.py for improved profiling output in PR #2940. Output when running yolo.py in profiling mode (above 2 changes) should look like this:

YOLOv5 🚀 v5.0-39-g1849916 torch 1.8.1+cu101 CUDA:0 (Tesla P100-PCIE-16GB, 16280.875MB)


                 from  n    params  module                                  arguments                     
  0                -1  1      3520  models.common.Focus                     [3, 32, 3]                    
  1                -1  1     18560  models.common.Conv                      [32, 64, 3, 2]                
  2                -1  1     18816  models.common.C3                        [64, 64, 1]                   
  3                -1  1     73984  models.common.Conv                      [64, 128, 3, 2]               
  4                -1  1    156928  models.common.C3                        [128, 128, 3]                 
  5                -1  1    295424  models.common.Conv                      [128, 256, 3, 2]              
  6                -1  1    625152  models.common.C3                        [256, 256, 3]                 
  7                -1  1   1180672  models.common.Conv                      [256, 512, 3, 2]              
  8                -1  1    656896  models.common.SPP                       [512, 512, [5, 9, 13]]        
  9                -1  1   1182720  models.common.C3                        [512, 512, 1, False]          
 10                -1  1    131584  models.common.Conv                      [512, 256, 1, 1]              
 11                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          
 12           [-1, 6]  1         0  models.common.Concat                    [1]                           
 13                -1  1    361984  models.common.C3                        [512, 256, 1, False]          
 14                -1  1     33024  models.common.Conv                      [256, 128, 1, 1]              
 15                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          
 16           [-1, 4]  1         0  models.common.Concat                    [1]                           
 17                -1  1     90880  models.common.C3                        [256, 128, 1, False]          
 18                -1  1    147712  models.common.Conv                      [128, 128, 3, 2]              
 19          [-1, 14]  1         0  models.common.Concat                    [1]                           
 20                -1  1    296448  models.common.C3                        [256, 256, 1, False]          
 21                -1  1    590336  models.common.Conv                      [256, 256, 3, 2]              
 22          [-1, 10]  1         0  models.common.Concat                    [1]                           
 23                -1  1   1182720  models.common.C3                        [512, 512, 1, False]          
 24      [17, 20, 23]  1    229245  Detect                                  [80, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]]
Model Summary: 283 layers, 7276605 parameters, 7276605 gradients, 17.1 GFLOPS

 time (ms)     GFLOPS     params  module
      0.93       1.44       3520  models.common.Focus
      0.52       1.90      18560  models.common.Conv
      1.31       1.93      18816  models.common.C3
      0.38       1.89      73984  models.common.Conv
      1.58       4.02     156928  models.common.C3
      0.37       1.89     295424  models.common.Conv
      1.48       4.00     625152  models.common.C3
      0.51       1.89    1180672  models.common.Conv
      0.79       1.05     656896  models.common.SPP
      0.98       1.89    1182720  models.common.C3
      0.18       0.21     131584  models.common.Conv
      0.03       0.00          0  torch.nn.modules.upsampling.Upsample
      0.05       0.00          0  models.common.Concat
      0.87       2.32     361984  models.common.C3
      0.16       0.21      33024  models.common.Conv
      0.05       0.00          0  torch.nn.modules.upsampling.Upsample
      0.09       0.00          0  models.common.Concat
      0.81       2.33      90880  models.common.C3
      0.35       0.95     147712  models.common.Conv
      0.03       0.00          0  models.common.Concat
      0.80       1.90     296448  models.common.C3
      0.35       0.94     590336  models.common.Conv
      0.03       0.00          0  models.common.Concat
      1.04       1.89    1182720  models.common.C3
      0.69       1.47     229245  Detect
14.4 ms total

@jaskiratsingh2000
Copy link

Hey, @glenn-jocher I am still getting the following error when I run what you stated above.

Traceback (most recent call last):
  File "yolo.py", line 11, in <module>
    from models.common import *
ModuleNotFoundError: No module named 'models'

Here is the Google Collab on which I ran this https://colab.research.google.com/drive/1-h9snr0Tu5BJj2lNpbbRRl7HHpxrcf1z?usp=sharing

I ran these following commands in the Google Colab step by step to view layers @glenn-jocher

Step-1: Cloning the forked repository with that thing uncomment
!git clone https://github.com/jaskiratsingh2000/yolov5.git # clone repo

Step-2: Installing dependencies:
!pip install -r yolov5/requirements.txt # install dependencies

Step-3: Changing the project file in Google Colab
%cd yolov5

Step-4: Changing the directory in which "yolo.py" file is kept because we want to run this.
cd /content/yolov5/models/

Step-5: Finally running the "yolo.py" for layer by layer checking. (I have uncommented the "# x = x.copy() # for profiling which you mentioned above"
!python yolo.py

Please let me know @glenn-jocher what step I missed or where I need to make changes. I would really be grateful for your response. Thanks

@jaskiratsingh2000
Copy link

@glenn-jocher Also another thing do I need to run this "yolo.py" directly or I need to run the "detect.py" first before all these. Please let me know. Thanks

@glenn-jocher
Copy link
Member

@jaskiratsingh2000 the standard yolo.py use case is:

cd yolov5
python models/yolo.py

I was not able to run it from within the models/ directory as you mentioned, so to increase robustness to these additional use-cases I've updated yolo.py and export.py in PR #2949. Once this PR is merged you should be able to additionally run yolo.py directly from anywhere in the repo (including within yolov5/models/).

@jaskiratsingh2000
Copy link

@glenn-jocher good to hear that it can be run directly but in the meanwhile can you just let me know exactly how can I run it?
The steps you followed directly. Your help would really be appreciable.

Do let me know. Thanks

@glenn-jocher
Copy link
Member

glenn-jocher commented Apr 27, 2021

@jaskiratsingh2000 the standard yolo.py use-case is:

cd yolov5
python models/yolo.py

@jaskiratsingh2000
Copy link

@glenn-jocher I just checked that as you mentioned above that once this PR will get merged then I would be able to run it directly from anywhere.
So I see it is now merged and that should work but it still throws the same error for me. I have followed the same steps now which were written here #2898 (comment)

So, @glenn-jocher Can you let me know exactly that whether it is a bug or I am missing something? You can also refer to my Google Colab above.

@glenn-jocher
Copy link
Member

glenn-jocher commented Apr 27, 2021

@jaskiratsingh2000 good news 😃! Your original issue may now been fixed ✅ in PR #2949. To receive this update you can:

  • git pull from within your yolov5/ directory
  • git clone https://github.com/ultralytics/yolov5 again
  • Force-reload PyTorch Hub: model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)

Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀!

https://colab.research.google.com/github/ultralytics/yolov5/blob/master/tutorial.ipynb?hl=en
Screen Shot 2021-04-27 at 5 15 01 PM

@jaskiratsingh2000
Copy link

Thank you so much @glenn-jocher for doing this I am able to run with the use case. Now my another question for which I am encountering an error is that I only see the results of the parameters like this not the time frame of the layers:

YOLOv5 🚀 v5.0-39-g1849916 torch 1.8.1+cu101 CUDA:0 (Tesla P100-PCIE-16GB, 16280.875MB)


                 from  n    params  module                                  arguments                     
  0                -1  1      3520  models.common.Focus                     [3, 32, 3]                    
  1                -1  1     18560  models.common.Conv                      [32, 64, 3, 2]                
  2                -1  1     18816  models.common.C3                        [64, 64, 1]                   
  3                -1  1     73984  models.common.Conv                      [64, 128, 3, 2]               
  4                -1  1    156928  models.common.C3                        [128, 128, 3]                 
  5                -1  1    295424  models.common.Conv                      [128, 256, 3, 2]              
  6                -1  1    625152  models.common.C3                        [256, 256, 3]                 
  7                -1  1   1180672  models.common.Conv                      [256, 512, 3, 2]              
  8                -1  1    656896  models.common.SPP                       [512, 512, [5, 9, 13]]        
  9                -1  1   1182720  models.common.C3                        [512, 512, 1, False]          
 10                -1  1    131584  models.common.Conv                      [512, 256, 1, 1]              
 11                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          
 12           [-1, 6]  1         0  models.common.Concat                    [1]                           
 13                -1  1    361984  models.common.C3                        [512, 256, 1, False]          
 14                -1  1     33024  models.common.Conv                      [256, 128, 1, 1]              
 15                -1  1         0  torch.nn.modules.upsampling.Upsample    [None, 2, 'nearest']          
 16           [-1, 4]  1         0  models.common.Concat                    [1]                           
 17                -1  1     90880  models.common.C3                        [256, 128, 1, False]          
 18                -1  1    147712  models.common.Conv                      [128, 128, 3, 2]              
 19          [-1, 14]  1         0  models.common.Concat                    [1]                           
 20                -1  1    296448  models.common.C3                        [256, 256, 1, False]          
 21                -1  1    590336  models.common.Conv                      [256, 256, 3, 2]              
 22          [-1, 10]  1         0  models.common.Concat                    [1]                           
 23                -1  1   1182720  models.common.C3                        [512, 512, 1, False]          
 24      [17, 20, 23]  1    229245  Detect                                  [80, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [128, 256, 512]]
Model Summary: 283 layers, 7276605 parameters, 7276605 gradients, 17.1 GFLOPS

Now as you mentioned above that in order to know the profiler I need to run the command here #2898 (comment)

I am unable to do run the model.

I just wrote the following command in order to produce the time(latency) for every layer:

import torch
img = torch.rand(8 if torch.cuda.is_available() else 1, 3, 320, 320).to(device) 
y = model(img, profile=True)

The error I get after running the above is:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-f21874f58c27> in <module>()
----> 1 img = torch.rand(8 if torch.cuda.is_available() else 1, 3, 320, 320).to(device)
      2 y = model(img, profile=True)

NameError: name 'device' is not defined

I am not able to produce this. Can you exactly let me know that what steps and commands I have to follow in order to produce the output that you produced above for the latency?
Please @glenn-jocher let me know because this is where I am stuck now in order to reach there.

Here is my Collab - https://colab.research.google.com/drive/1sVlave0MwcmDJIhBcKnwn9CZEkEa8ZZR?usp=sharing

@jaskiratsingh2000
Copy link

@glenn-jocher Can you please let me know on above?

@glenn-jocher
Copy link
Member

@jaskiratsingh2000 do not post duplicate questions. This question has already been answered in #2898 (comment)

@pravastacaraka
Copy link

@jaskiratsingh2000 please see #2951 (comment)

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