CPU /CUDA:can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first

Saifeddine_Barkia: <p>I’m trying to run an attention model here from this repo “<a href="https://github.com/wvinzh/WS_DAN_PyTorch" rel="nofollow noopener">https://github.com/wvinzh/WS_DAN_PyTorch</a>”<br>
When I wanted to run the train model , I have got this error</p>
<pre><code class="lang-auto"> File "train_bap.py", line 210, in &lt;module&gt;
    train()
  File "train_bap.py", line 145, in train
    train_prec, train_loss = engine.train(state, e)
  File "/content/WS_DAN_PyTorch-master/utils/engine.py", line 56, in train
    img_crop, img_drop = attention_crop_drop(attention_maps, input)
  File "/content/WS_DAN_PyTorch-master/utils/attention.py", line 118, in attention_crop_drop
    itemindex = np.where(mask &gt;= mask.max()*threshold).cpu()
  File "&lt;__array_function__ internals&gt;", line 6, in where
  File "/usr/local/lib/python3.6/site-packages/torch/tensor.py", line 486, in __array__
    return self.numpy()
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
</code></pre>
<p>here is the code for the attention code</p>
<pre><code class="lang-auto">def attention_crop_drop(attention_maps,input_image):
    # start = time.time()
    B,N,W,H = input_image.shape
    input_tensor = input_image
    batch_size, num_parts, height, width = attention_maps.shape
    attention_maps = torch.nn.functional.interpolate(attention_maps.detach(),size=(W,H),mode='bilinear')
    part_weights = F.avg_pool2d(attention_maps.detach(),(W,H)).reshape(batch_size,-1)
    part_weights = torch.add(torch.sqrt(part_weights),1e-12)
    part_weights = torch.div(part_weights,torch.sum(part_weights,dim=1).unsqueeze(1)).cpu()
    part_weights = part_weights.numpy()
    # print(part_weights.shape)
    ret_imgs = []
    masks = []
    # print(part_weights[3])
    for i in range(batch_size):
        attention_map = attention_maps[i]
        part_weight = part_weights[i]
        selected_index = np.random.choice(np.arange(0, num_parts), 1, p=part_weight)[0]
        selected_index2 = np.random.choice(np.arange(0, num_parts), 1, p=part_weight)[0]
        ## create crop imgs
        mask = attention_map[selected_index, :, :]
        # mask = (mask-mask.min())/(mask.max()-mask.min())
        threshold = random.uniform(0.4, 0.6)
        # threshold = 0.5
        itemindex = np.where(mask &gt;= mask.max()*threshold)
        # print(itemindex.shape)
        # itemindex = torch.nonzero(mask &gt;= threshold*mask.max())
        padding_h = int(0.1*H)
        padding_w = int(0.1*W)
        height_min = itemindex[0].min()
        height_min = max(0,height_min-padding_h)
        height_max = itemindex[0].max() + padding_h
        width_min = itemindex[1].min()
        width_min = max(0,width_min-padding_w)
        width_max = itemindex[1].max() + padding_w
        # print('numpy',height_min,height_max,width_min,width_max)
        out_img = input_tensor[i][:,height_min:height_max,width_min:width_max].unsqueeze(0)
        out_img = torch.nn.functional.interpolate(out_img,size=(W,H),mode='bilinear',align_corners=True)
        out_img = out_img.squeeze(0)
        ret_imgs.append(out_img)

        ## create drop imgs
        mask2 = attention_map[selected_index2:selected_index2 + 1, :, :]
        threshold = random.uniform(0.2, 0.5)
        mask2 = (mask2 &lt; threshold * mask2.max()).float()
        masks.append(mask2)
    # bboxes = np.asarray(bboxes, np.float32)
    crop_imgs = torch.stack(ret_imgs)
    masks = torch.stack(masks)
    drop_imgs = input_tensor*masks
    return (crop_imgs,drop_imgs)
</code></pre>
<p>This is the code for the train model</p>
<pre><code class="lang-auto">def train(self,state,epoch):
        batch_time = AverageMeter()
        data_time = AverageMeter()
        losses = AverageMeter()
        top1 = AverageMeter()
        top5 = AverageMeter()
        config = state['config']
        print_freq = config.print_freq
        model = state['model']
        criterion = state['criterion']
        optimizer = state['optimizer']
        train_loader = state['train_loader']
        model.train()
        end = time.time()
        for i, (img, label) in enumerate(train_loader):
            # measure data loading time
            data_time.update(time.time() - end)

            target = label.cuda()
            input = img.cuda().
            # compute output
            attention_maps, raw_features, output1 = model(input)
            features = raw_features.reshape(raw_features.shape[0], -1)

            feature_center_loss, center_diff = calculate_pooling_center_loss(
                features, state['center'], target, alfa=config.alpha)

            # update model.centers
            state['center'][target] += center_diff

            # compute refined loss
            # img_drop = attention_drop(attention_maps,input)
            # img_crop = attention_crop(attention_maps, input)
            img_crop, img_drop = attention_crop_drop(attention_maps, input)
            _, _, output2 = model(img_drop)
            _, _, output3 = model(img_crop)

            loss1 = criterion(output1, target)
            loss2 = criterion(output2, target)
            loss3 = criterion(output3, target)

            loss = (loss1+loss2+loss3)/3 + feature_center_loss
            # measure accuracy and record loss
            prec1, prec5 = accuracy(output1, target, topk=(1, 5))
            losses.update(loss.item(), input.size(0))
            top1.update(prec1[0], input.size(0))
            top5.update(prec5[0], input.size(0))
</code></pre>
<p>I tried to add .cpu() at the end of the input and target. But then another error occurred ! expected Cuda but got cpu(). Can anyone help please?</p>

Nikronic: <aside class="quote no-group" data-username="Saifeddine_Barkia" data-post="1" data-topic="90171">
<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/saifeddine_barkia/48/27005_2.png" class="avatar"> Saifeddine_Barkia:</div>
<blockquote>
<p><code>itemindex = np.where(mask &gt;= mask.max()*threshold)</code></p>
</blockquote>
</aside>
<p>Hi,</p>
<p>It seems <code>mask</code> is on GPU but you are trying to convert it to numpy before transferring it to cpu. Try to use <code>torch.where</code> instead of <code>np.where</code> in the line of error if you want to use GPU version or convert to numpy using <code>mask.cpu().numpy()</code></p>
<p>Bests</p>