How to resize coordinates of bounding box in torchvision.datasets.CoCoDetection()

username: ELA-Bang
cooked: <p>I load the CoCo dataset with torchvision.datasets.CoCoDetection()<br>
the example code on official doc:<br>
cap = dset.CocoDetections(root = ‘dir where images are’,<br>
annFile = ‘json annotation file’, transform=None,target_transform=None)</p>
<p>The images in this dataset has various size. So how can I resize its images to (416,416) and rescale coordinates of bounding boxes?<br>
e.g. original size=(640,480), bounding box=[ x, y, w, h ]<br>
processed size=(416,416), bounding box=[ x* 416/640, y* 416/480, w* 416/640, h* 416/480]</p>
<p>I know use the argument: transform = transforms.Resize([416,416]) can resize the images, but how can I modify those bounding box coordinates efficiently?</p>

username: JuanFMontesinos
cooked: <p>Hmmm i don’t really know which files are u using but if u have a tensor or a np vector ur scaling is just new=scalar*old. That operation is optimized in both, np and pytorch.</p>
<p>If u have a python list you can conver it to a tensor using torch.stack (i guess) or np.asarray()</p>

username: ELA-Bang
cooked: <p>I mean, I loaded a dataset, this dataset has length 40000+, and dataset [ i ] [ 0 ] is a PIL image, their sizes vary, like (480, 640), (537, 450). dataset [ i ] [ 1 ] is a tuple containing tuple containing some dictionaries. Every dictionary represent an annotation for an object in this picture, denote this dictionary as dict. dict [ ‘bbox’ ] is a list [ x, y, width, height ] which represents the bounding box, and dict [ ‘category_id’ ] is an int representing the object’s  category.</p>
<p>What I want is to get some images with size (416, 416), this size fits my network. And the [ ‘bbox’ ] value should be processed simultaneously as I changed the size of picture.</p>
<p>The solution I take now is to process these images and annotations during training time. Well, I find it slow to load data ( a tensor of batch size 32 : (32, 3, 416, 416). Loading this tensor with its annotation takes about 3 seconds )</p>
<p>Previously I tried to changes the value of bbox coordinates before training, It would takes about 4 hours to process 40000+ images’ annotations. I didn’t stand to wait for so long time…</p>

username: marina_neseem
cooked: <p>Did you find an efficient way to do this?</p>

username: paschalis_m
cooked: <p>A very convenient solution to this would be probably to normalize the boxes in a similar way as to YOLO format. In such case, (x,y,w,h) should be processed so as to be relative to width and height of image. Speaking in code  :</p>
<pre><code class="lang-auto"># image is not resized yet
normalized_x = absolute_x / image.shape[0] 
normalized_y = absolute_y / image.shape[1]
normalized_width = absolute_width / image.shape[0]
normalized_height = absolute_height / image.shape[1]
</code></pre>
<p>Now, as long as you resize the image according to the initial aspect ratio, you will always have access to the proportional coordinates.</p>
<p>Not to mention that you should take care of what x and y represents in you annotation formats, as sometimes they tend to infer to the center whether the top_left corner of the bounding box.</p>
<p>You can also check:</p><aside class="onebox stackexchange">
  <header class="source">
      <a href="https://stackoverflow.com/questions/49466033/resizing-image-and-its-bounding-box" target="_blank" rel="noopener nofollow ugc">stackoverflow.com</a>
  </header>
  <article class="onebox-body">
      <a href="https://stackoverflow.com/users/8184563/jejjejd" target="_blank" rel="noopener nofollow ugc">
    <img alt="jejjejd" src="https://www.gravatar.com/avatar/997a198366f66365dd532a4004c83bb7?s=128&amp;d=identicon&amp;r=PG&amp;f=1" class="thumbnail onebox-avatar" width="60" height="60">
  </a>
<h4>
  <a href="https://stackoverflow.com/questions/49466033/resizing-image-and-its-bounding-box" target="_blank" rel="noopener nofollow ugc">Resizing image and its bounding box</a>
</h4>

<div class="tags">
  <strong>python-3.x, numpy, opencv, math</strong>
</div>

<div class="date">
  asked by
  
  <a href="https://stackoverflow.com/users/8184563/jejjejd" target="_blank" rel="noopener nofollow ugc">
    jejjejd
  </a>
  on <a href="https://stackoverflow.com/questions/49466033/resizing-image-and-its-bounding-box" target="_blank" rel="noopener nofollow ugc">02:46PM - 24 Mar 18 UTC</a>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<aside xmlns:svg="http://www.w3.org/2000/svg" class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/AlexeyAB/Yolo_mark/issues/60" target="_blank" rel="noopener nofollow ugc">github.com/AlexeyAB/Yolo_mark</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg:svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><svg:path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></svg:path></svg:svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/AlexeyAB/Yolo_mark/issues/60" target="_blank" rel="noopener nofollow ugc">Specific format of annotation</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2018-07-02" data-time="14:51:19" data-timezone="UTC">02:51PM - 02 Jul 18 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2018-07-11" data-time="16:52:09" data-timezone="UTC">04:52PM - 11 Jul 18 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/ycui123" target="_blank" rel="noopener nofollow ugc">
          <img alt="ycui123" src="https://avatars.githubusercontent.com/u/30300229?v=4" class="onebox-avatar-inline" width="20" height="20">
          ycui123
        </a>
      </div>
    </div>
  </div>
</div>

<div class="github-row">
  <p class="github-content">Could you please tell me the format of annotation? I generated my own dataset and I want to train it. I...</p>
</div>

<div class="labels">
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">Solved</span>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>