Can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first

lavender99: <pre><code class="lang-auto">def display_prediction(img,true_label):
    seg_pred = model(img_test.unsqueeze(0).cuda())
    fig,axes = plt.subplots(nrows = 1,ncols=3,figsize = (10,10))
    axes[0].imshow(img.permute(1,2,0))
    axes[0].set_title('Image')
    axes[1].imshow(seg_pred[0].squeeze().detach(),cmap = 'gray')
    axes[1].set_title('Prediction')
    axes[2].imshow(true_label.squeeze(),cmap = 'gray')
    axes[2].set_title('Ground Truth')
display_prediction(img_test,lb_test)

</code></pre>
<p>i am ruuning my code on google colab and i get this error , how can i solve it?</p>

JuanFMontesinos: <p>You need to allocate the tensor in RAM by using</p>
<pre><code class="lang-auto">model(img_test.unsqueeze(0).cuda()).deatch().cpu().clone().numpy()
</code></pre>
<p>which means that you are going to:<br>
deatch --&gt; cut computational graph<br>
cpu --&gt; allocate tensor in RAM<br>
clone --&gt; clone the tensor not to modify the output in-place<br>
numpy --&gt; port tensor to numpy</p>
<p>Note: permute is a pytorch function, if you map it into a numpy tensor you should use transpose</p>

lavender99: <aside class="quote no-group" data-username="JuanFMontesinos" data-post="2" data-topic="38301">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/juanfmontesinos/48/76115_2.png" class="avatar"> JuanFMontesinos:</div>
<blockquote>
<p>h function, if you map it into a numpy tensor you should use transpose</p>
</blockquote>
</aside>
<p>Now it gives this error</p>
<pre><code class="lang-auto">'Tensor' object has no attribute 'deatch'
</code></pre>
<p>the same when i try the word detach instead of deatch</p>

albanD: <p>Hi,</p>
<p>Which version of pytorch are you using? <code>.detach()</code> was introduced in 0.4 if i’m not mistaken.</p>

lavender99: <p>i am using 1.0.1. version</p>

albanD: <p>Then <code>Tensor</code> does have  a <a href="https://pytorch.org/docs/stable/autograd.html#torch.Tensor.detach" rel="nofollow noopener"><code>.detach()</code></a> method. Make sure you call it on a Tensor.</p>
<p>Also you use both <code>img</code> and <code>seg_pred</code> in your code. Make sure to do the <code>.detach().cpu().numpy()</code> (the <code>.clone()</code> is not necessary in this case I think, if you get an error from numpy saying that you try to modify a read-only array, then add it back) to each of them if you need a numpy array from them.</p>

lavender99: <p>actually, the error now is related to numpy</p>
<pre><code class="lang-auto">'numpy.ndarray' object has no attribute 'detach'
</code></pre>

albanD: <p>You try and call <code>.detach()</code> on something that is already a numpy array as stated in the error.</p>

lavender99: <p>so what i should i do ? if i delete <code>model(img_test.unsqueeze(0).cuda()).deatch().cpu().clone().numpy()</code><br>
i go back to the first error</p>
<pre><code class="lang-auto">Can’t convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first 

</code></pre>

albanD: <p>I think the simplest is going for you to print the objects. And check whether you have a Tensor (if not specified, it’s on the cpu, otherwise it will tell your it’s a cuda Tensor) or a np.array.</p>
<p>You need to give a Tensor to your model, torch operations and np.array to everything else.</p>
<p>To go from np.array to cpu Tensor, use <code>torch.from_numpy()</code>.<br>
To go from cpu Tensor to gpu Tensor, use <code>.cuda()</code>.<br>
To go from a Tensor that requires_grad to one that does not, use <code>.detach()</code> (in your case, your net output will most likely requires gradients and so it’s output will need to be detached).<br>
To go from a gpu Tensor to cpu Tensor, use <code>.cpu()</code>.<br>
Tp gp from a cpu Tensor to np.array, use <code>.numpy()</code>.</p>

lavender99: <aside class="quote no-group" data-username="lavender99" data-post="9" data-topic="38301">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lavender99/48/10608_2.png" class="avatar"> lavender99:</div>
<blockquote>
<p>model(img_test.unsqueeze(0).cuda()).deatch().cpu()</p>
</blockquote>
</aside>
<p>Thank you <a class="mention" href="/u/alband">@albanD</a>  and <a class="mention" href="/u/juanfmontesinos">@JuanFMontesinos</a>  it works by using <code>model(img_test.unsqueeze(0).cuda()).cpu()</code><br>
thank you again <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=12" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>

saran: <p>for me its Giving this Error<br>
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!</p>

ptrblck: <p>Double post from <a href="https://discuss.pytorch.org/t/plotting-the-output-from-cuda/140433">here</a>.</p>

Emmanuel_Obu_Junior: <p>To solve this problem move to the plot.py file in the utils folder and make these modifications to the output_to_target function</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/9/59eab6d7a445c54fcb63cbf71a4942fe7e1e43d7.png" data-download-href="https://discuss.pytorch.org/uploads/default/59eab6d7a445c54fcb63cbf71a4942fe7e1e43d7" title="solution"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/9/59eab6d7a445c54fcb63cbf71a4942fe7e1e43d7.png" alt="solution" data-base62-sha1="cPrmNLfbwp1SyBaJgEWnpYR8yRF" width="690" height="402" data-dominant-color="232423"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">solution</span><span class="informations">723×422 11.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>