Tensor to numpy on pytorch 1.8

shuuny-matrix: <p>Hi, I tried to train a yolov4 pytorch model with a reference to this repo:</p><aside class="onebox githubfolder">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="16" height="16">
      <a href="https://github.com/WongKinYiu/PyTorch_YOLOv4" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars.githubusercontent.com/u/12152972?s=400&amp;amp;v=4" class="thumbnail onebox-full-image" width="60" height="60">

<h3><a href="https://github.com/WongKinYiu/PyTorch_YOLOv4" target="_blank" rel="noopener nofollow ugc">WongKinYiu/PyTorch_YOLOv4</a></h3>


  <p><span class="label1">PyTorch implementation of YOLOv4. Contribute to WongKinYiu/PyTorch_YOLOv4 development by creating an account on GitHub.</span></p>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<p>
Then I run into a error like this</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "train.py", line 438, in &lt;module&gt;
    train(hyp, opt, device, tb_writer)
  File "train.py", line 301, in train
    results, maps, times = test.test(opt.data,
  File "/home/kamal/Documents/PyTorch_YOLOv4/test.py", line 206, in test
    plot_images(img, output_to_target(output, width, height), paths, str(f), names)  # predictions
  File "/home/kamal/Documents/PyTorch_YOLOv4/utils/general.py", line 907, in output_to_target
    return np.array(targets)
  File "/home/kamal/.virtualenvs/pytorch-yolov4/lib/python3.8/site-packages/torch/tensor.py", line 621, in __array__
    return self.numpy()
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
</code></pre>
<p>My pytorch version is 1.8.0 and cuda is 11.2<br>
Can somebody look into this?</p>

Dwight_Foster: <p>In test.py just change this line</p>
<pre><code class="lang-auto">    plot_images(img, output_to_target(output, width, height), paths, str(f), names)  # predictions
</code></pre>
<p>to this</p>
<pre><code class="lang-auto">    plot_images(img, output_to_target(output.cpu(), width, height), paths, str(f), names)  # predictions
</code></pre>

Alexey_Demyanchuk: <p>They actually have the conversion part in the code of <code>output_to_target</code> function if the <code>output</code> argument is a tensor. Cuda tensor is definitely a <code>torch.Tensor</code> as well, so this part of code should put it on CPU and convert to NumPy. Are you sure, you are using the latest version of their GitHub repo?</p>
<pre><code class="lang-auto">def output_to_target(output, width, height):
    # Convert model output to target format [batch_id, class_id, x, y, w, h, conf]
    if isinstance(output, torch.Tensor):
        output = output.cpu().numpy()
</code></pre>

shuuny-matrix: <p>Actually this was the first thing I tried but it results into error like this:</p>
<pre><code class="lang-auto">Starting training for 300 epochs...

     Epoch   gpu_mem      GIoU       obj       cls     total   targets  img_size
     0/299     8.92G   0.03446    0.1387         0    0.1732         2      1280
               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95:   0%|                                                                                 | 0/124 [00:00&lt;?, ?it/s]
Traceback (most recent call last):
  File "train.py", line 438, in &lt;module&gt;
    train(hyp, opt, device, tb_writer)
  File "train.py", line 301, in train
    results, maps, times = test.test(opt.data,
  File "/home/kamal/Documents/PyTorch_YOLOv4/test.py", line 206, in test
    plot_images(img, output_to_target(output.cpu(), width, height), paths, str(f), names)  # predictions
AttributeError: 'list' object has no attribute 'cpu'
</code></pre>
<p>It starts training but somehow when it calls the test function, it fails to load the tensor.</p>

shuuny-matrix: <p>Yes I am using the latest version of their repo and yes they have this output_to_target function but some how it fails. It starts training but somehow fails as soon as it calls the test function.</p>

Alexey_Demyanchuk: <p>Based on both of your provided errors this check <code>if isinstance(output, torch.Tensor):</code> doesn’t work because <code>output</code> has type list. I don’t know how this supposed to work, but <code>output</code> coming from <code>non_max_suppression</code> function in <code>test.py</code> is a list. And then you check <code>non_max_suppression</code> function its supposed return is also a list and they are never converting that to tensor somehow.</p>
<p>You should probably dig dipper in the code and rewrite it a bit saving the logic. You may want to move outputs of the model to <code>cpu</code> before <code>non_max_suppression</code> call in <code>test.py</code> or do the same in <code>output_to_target</code> on the <code>output</code> variable iteratively</p>