Applying transformation to bounding boxes is not producing proper result

Amit_Sur: <p>I am trying to applying following transformations to training image and bounding boxes</p>
<pre><code class="lang-auto">t = v2.Compose(
    [
        v2.ToImage(),
        v2.RandomHorizontalFlip(),
        v2.RandomVerticalFlip(),
        v2. Resize((448, 448)),
        v2.ToDtype(torch.float32, scale=True),
        v2.SanitizeBoundingBoxes()
    ]
)
</code></pre>
<pre><code class="lang-auto"># Apply transformations
        if self.transforms:
            # The coordinates are relative to 1,
            # scale them according to the image height and width
            # As this is required by the transforms
            print(box_coords)
            box_coords[:, [0, 2]] *= img_w
            box_coords[:, [1, 3]] *= img_h
            print(box_coords)
            img, box_coords = self.transforms(img, box_coords)
            print(box_coords)
            # Change the coordinates back to relative to 1 value
            box_coords[:, [0, 2]] /= img_w
            box_coords[:, [1, 3]] /= img_h
            print(box_coords)

</code></pre>
<p>But I am not getting correct bounding boxes after the transformation.<br>
Here is the output of those <code>print</code> statements that I have used for debugging.</p>
<pre><code class="lang-auto">BoundingBoxes([[0.4040, 0.4230, 0.3707, 0.1140],
               [0.3907, 0.4840, 0.2320, 0.1760],
               [0.2293, 0.5170, 0.3573, 0.2660],
               [0.6773, 0.6340, 0.3147, 0.4520]], format=BoundingBoxFormat.CXCYWH, canvas_size=(500, 375))
BoundingBoxes([[151.5000, 211.5000, 139.0000,  57.0000],
               [146.5000, 242.0000,  87.0000,  88.0000],
               [ 86.0000, 258.5000, 134.0000, 133.0000],
               [254.0000, 317.0000, 118.0000, 226.0000]], format=BoundingBoxFormat.CXCYWH, canvas_size=(500, 375))
BoundingBoxes([[180.9920, 189.5040, 166.0587,  51.0720],
               [175.0187, 216.8320, 103.9360,  78.8480],
               [102.7413, 231.6160, 160.0853, 119.1680],
               [303.4453, 284.0320, 140.9707, 202.4960]], format=BoundingBoxFormat.CXCYWH, canvas_size=(448, 448))
BoundingBoxes([[0.4826, 0.3790, 0.4428, 0.1021],
               [0.4667, 0.4337, 0.2772, 0.1577],
               [0.2740, 0.4632, 0.4269, 0.2383],
               [0.8092, 0.5681, 0.3759, 0.4050]], format=BoundingBoxFormat.CXCYWH, canvas_size=(448, 448))
</code></pre>
<p>Original image with the bounding boxes:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/1/a1c0ea31ca20f4f023bbcbd40d2f5a8e33975582.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/a1c0ea31ca20f4f023bbcbd40d2f5a8e33975582" title="Original image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/a/1/a1c0ea31ca20f4f023bbcbd40d2f5a8e33975582.jpeg" alt="Original image" data-base62-sha1="n4WbPBa8Zgc7feFBJTrvCG9feyS" width="446" height="499" data-dominant-color="847768"><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">Original image</span><span class="informations">664×744 47.8 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>Output after transformation:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/1/911c812339c3909ceef19fe42043bedefb49d9b8.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/911c812339c3909ceef19fe42043bedefb49d9b8" title="Output after transformation"><img src="https://discuss.pytorch.org/uploads/default/original/3X/9/1/911c812339c3909ceef19fe42043bedefb49d9b8.jpeg" alt="Output after transformation" data-base62-sha1="kHIiPrddBfIT7lVEEsuQTnQ9QNO" width="574" height="499" data-dominant-color="7D7162"><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">Output after transformation</span><span class="informations">844×734 54.9 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>

ptrblck: <p>If I understand your use case correctly, the bounding boxes are normalized in <code>[0, 1]</code> for the original canvas size of <code>[500, 375]</code>.<br>
You are then scaling them with <code>img_w/h</code>, which I assume is set to 500 and 375, respectively.<br>
The transformation resizes these bounding boxes to a desired canvas size of <code>[448, 448]</code>. Afterwards you are normalizing these transformed bounding boxes with the same <code>img_w/h</code> instead of <code>448</code>?<br>
If so, the last step would be wrong, but I also don’t know what <code>img_w.h</code> refers to. If it’s set to <code>448</code> then the first scaling step would be wrong.</p>

Amit_Sur: <p>Yes, the bounding boxes are normalized in <code>[0, 1]</code>. And, <code>img_w/h</code> refers to the original width/height of the image. So, after applying transformations, I was normalizing using the original width and height, as you pointed out. I changed it to normalize according to the new height and width and it worked. Thank you.</p>