TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first. even though .cpu() is used

Mona_Jalal: <p>I used .cpu() but still get error. Could you please guide how to fix?</p>
<pre><code class="lang-auto"># Test the model
model.eval()  # eval mode (batchnorm uses moving mean/variance instead of mini-batch mean/variance)
with torch.no_grad():
    correct = 0
    total = 0
    for i, sample_batched in enumerate(train_loader):
        #print(i, sample_batched['image'].size(),
        # sample_batched['landmarks'].size())
        
        images_batch, landmarks_batch = \
            sample_batched['image'], sample_batched['landmarks']
        
        images = images_batch
        labels = landmarks_batch.reshape(-1, 68 * 2)
        
        images = Variable(images.float())
        labels = Variable(labels)
        
        images = images.to(device)
        labels = labels.to(device)
        
        outputs = model(images)
        #_, predicted = torch.max(outputs.data, 1)
        #_, predicted = outputs.data
        print("Predicted", outputs.data.shape)
        
        outputs = outputs.cpu()
        images = images.cpu()
        
        if i_batch == 3:
          plt.figure()
          show_landmarks_batch({'image': images, 'landmarks': outputs.data.reshape(-1, 68, 2) })
          plt.axis('off')
          plt.ioff()
          plt.show()
          show_landmarks_batch({'image': images, 'landmarks': labels.reshape(-1, 68, 2) })
          plt.axis('off')
          plt.ioff()
          plt.show()
          break
</code></pre>
<p>It is using this method:</p>
<pre><code class="lang-auto"># Helper function to show a batch
def show_landmarks_batch(sample_batched):
    """Show image with landmarks for a batch of samples."""
    images_batch, landmarks_batch = \
            sample_batched['image'], sample_batched['landmarks']
    batch_size = len(images_batch)
    im_size = images_batch.size(2)

    grid = utils.make_grid(images_batch)
    grid = grid.cpu()
    plt.imshow(grid.numpy().transpose((1, 2, 0)))
    #sample_batched = sample_batched.to('cpu')
    cpu_sample_batched = {}
    for k, v in sample_batched.items():
        cpu_sample_batched[k] = v.cpu()

    for i in range(batch_size):
        plt.scatter(landmarks_batch[i, :, 0].numpy() + i * im_size,
                    landmarks_batch[i, :, 1].numpy(),
                    s=10, marker='.', c='r')

        plt.title('Batch from dataloader')
</code></pre>
<p>The error is:</p>
<pre><code class="lang-auto">Predicted torch.Size([3, 136])

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-67-43119a6c95ec&gt; in &lt;module&gt;
     34           plt.ioff()
     35           plt.show()
---&gt; 36           show_landmarks_batch({'image': images, 'landmarks': labels.reshape(-1, 68, 2) })
     37           plt.axis('off')
     38           plt.ioff()

&lt;ipython-input-65-a1ebb35aa3f1&gt; in show_landmarks_batch(sample_batched)
     20 
     21     for i in range(batch_size):
---&gt; 22         plt.scatter(landmarks_batch[i, :, 0].numpy() + i * im_size,
     23                     landmarks_batch[i, :, 1].numpy(),
     24                     s=10, marker='.', c='r')

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>Please let me know if there might be a need for more details.</p>
<pre><code class="lang-auto">(base) mona@mona:~/research/Pytorch-Tutorials$ python
Python 3.7.6 (default, Jan  8 2020, 19:59:22) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.6.0'
</code></pre>

Mona_Jalal: <p>never mind. I missed to convert one of the variables. I added <code>    landmarks_batch = landmarks_batch.cpu()</code> and now it works.</p>
<pre><code class="lang-auto"># Helper function to show a batch
def show_landmarks_batch(sample_batched):
    """Show image with landmarks for a batch of samples."""
    images_batch, landmarks_batch = \
            sample_batched['image'], sample_batched['landmarks']
    batch_size = len(images_batch)
    im_size = images_batch.size(2)

    grid = utils.make_grid(images_batch)
    grid = grid.cpu()
    plt.imshow(grid.numpy().transpose((1, 2, 0)))
    #sample_batched = sample_batched.to('cpu')
    cpu_sample_batched = {}
    ##for k, v in sample_batched.items():
    ##    cpu_sample_batched[k] = v.cpu()
    
    landmarks_batch = landmarks_batch.cpu()

    for i in range(batch_size):
        plt.scatter(landmarks_batch[i, :, 0].numpy() + i * im_size,
                    landmarks_batch[i, :, 1].numpy(),
                    s=10, marker='.', c='r')

        plt.title('Batch from dataloader')
</code></pre>