Batch indexing rgb images with batch pixel values

Kanishk_Navale: <p>Hi,</p>
<p>I have got a batch of rgb tensor of shape BxHxWxC and batch of pixel locations B x index x 2. (Batch of us and vs)</p>
<p>I can’t batch index the rgb tensor right away often faced with cuda_allocation_conf error.</p>
<p>Is there a way this can be done?</p>

ptrblck: <p>Could you post a minimal, executable code snippet reproducing the issue, please?</p>

Kanishk_Navale: <p>Hi,</p>
<p>Assuming a tensor of RGBD images,<br>
batch_image = torch.rand((5, 4, 200, 200)) of format (B, 4, H, W)<br>
and its pixel location uvs = torch.rand(5, 8, 2) of format (B, us, vs)</p>
<p>here,<br>
I have to extract the pixel vector of the image using uv indices from rgbd image. Each pixel location extracts a vector → [R, G, B, D]</p>
<p>If I can think of output shape, it should be of shape (5, 8, 4) as each batch has 8 pixel locations to be queried.</p>
<p>I have written a loop as follows, which is computationally slower,<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/7/577bda30e58f5fe88697a7eb00fd32c47d7d205f.png" data-download-href="https://discuss.pytorch.org/uploads/default/577bda30e58f5fe88697a7eb00fd32c47d7d205f" title="Screenshot 2022-08-07 005442"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/7/577bda30e58f5fe88697a7eb00fd32c47d7d205f.png" alt="Screenshot 2022-08-07 005442" data-base62-sha1="ctUTjUsNr2xY8llmfnMI5HzlumH" width="690" height="216" data-dominant-color="262A2A"><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">Screenshot 2022-08-07 005442</span><span class="informations">765×240 16 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>
<p>as per the code above, I iterate over the batch of images to extract the descriptors, which are nothing but the same RGBD vector yielding the tensor of shape → (5, 8, 4)</p>
<p>I tried to index the descriptors as follows,<br>
batch_descriptors_a = batch_image_a[batch_matches_a[:, 0].long(), :, batch_matches_a[:, 1].long(), batch_matches_a[:, 2].long()]</p>
<p>which brings up this error,<br>
PYTORCH_CUDA_ALLOC_CONF error.<br>
I use a 24GB VRAM Linux machine I have an ample amount of memory still available.</p>
<p>and in windows 11 WSL2, I get BSD.</p>

Kanishk_Navale: <p><a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<p>I have shared the code.</p>

ptrblck: <p>Unfortunately, you didn’t share the code but have posted an image, which I would need to use to rewrite the code and which also doesn’t seem to be executable.<br>
You can post code snippets by wrapping them into three backticks ```.</p>

Kanishk_Navale: <p>Hi,</p>
<p>Please excuse me.</p>
<pre><code class="lang-auto">from typing import List, Tuple

import torch


@torch.jit.script
def _pick_descriptors(batch_image_a: torch.Tensor,
                      batch_image_b: torch.Tensor,
                      batch_matches_a: torch.Tensor,
                      batch_matches_b: torch.Tensor) -&gt; Tuple[torch.Tensor, torch.Tensor]:
    descriptors_a: List[torch.Tensor] = []
    descriptors_b: List[torch.Tensor] = []
    for image_a, image_b, matches_a, matches_b in zip(batch_image_a, batch_image_b, batch_matches_a, batch_matches_b):
        descriptors_a.append(image_a[:, matches_a[:, 0].long(), matches_a[:, 1].long()])
        descriptors_b.append(image_b[:, matches_b[:, 0].long(), matches_b[:, 1].long()])

    batch_descriptors_a = torch.stack(descriptors_a).permute(0, 2, 1)
    batch_descriptors_b = torch.stack(descriptors_b).permute(0, 2, 1)

    return batch_descriptors_a, batch_descriptors_b


if __name__ == "__main__":
    image_a = torch.rand((5, 4, 255, 255), device=torch.device("cuda"))
    image_b = torch.rand((5, 4, 255, 255), device=torch.device("cuda"))

    matches_a = torch.rand((5, 8, 2), device=torch.device("cuda")).clamp(0, 254)  # clipped for valid pixel locations
    matches_b = torch.rand((5, 8, 2), device=torch.device("cuda")).clamp(0, 254)

    # descriptors_a = _pick_descriptors(image_a, image_b, matches_a, matches_b)
    descriptors = image_a[matches_a[:, 0].long(), :, matches_a[:, 1].long(), matches_a[:, 2].long()]  # Error is generated here and does not generate desired shape
</code></pre>

Kanishk_Navale: <p>Hello <a class="mention" href="/u/ptrblck">@ptrblck</a>,</p>
<p>I am waiting for your assistance.</p>

ptrblck: <p>Thanks for sharing the code. Unfortunately, I cannot reproduce any error using the code yet. Could you post more information about your setup (i.e. which PyTorch version etc.) and update to the latest PyTorch release to check if you would still see the same issue?</p>