Get location of RandomCrop

username: sneer
cooked: <p>Hi,<br>
Using <code>torchvision.transforms.RandomCrop</code> is there a way to get the location from where the crop was taken.<br>
For example the coordinates of the top left corner of the crop in the original image.</p>
<p>Thanks</p>

username: Andrei_Cristea
cooked: <p>Hello Tristan,</p>
<p>I’m not sure if there’s a way to do this directly, but you can write your own custom transform that does retain the coordinates, while piggybacking on the RandomCrop sampler, per ptrblck’s suggestion <a href="https://discuss.pytorch.org/t/cropping-batches-at-the-same-position/24550/5">here</a>.</p>
<p>Specifically, this bit:</p>
<pre><code class="lang-auto">self.crop_indices = transforms.RandomCrop.get_params(
                image, output_size=(512, 512))
i, j, h, w = self.crop_indices  # btw i think h, w are just always going to be 512, 512 here
image = TF.crop(image, i, j, h, w)
</code></pre>
<p>If you post your solution in a public repo perhaps others would use it as well. I can imagine the usefulness extends to other Random* transforms.</p>
<p>Best,<br>
Andrei</p>

username: sneer
cooked: <p>Hello Andrei,<br>
Thanks, the solution you provided works to some extent.<br>
However the <code>transforms.RandomCrop</code> can do crops with padding.<br>
I tried to do :<br>
<code>transforms.RandomCrop(size=(224,224), padding=128).get_params(                 image, output_size=(224, 224))</code><br>
But never get parameters that extend over the boarder of the image.<br>
Also the <code>TF.crop</code> would not be able to handle any paddings.</p>

username: Andrei_Cristea
cooked: <p>Hi -</p>
<p>For the padded case, what if you just pad the image in a previous step, and then repeat the suggested approach? Something like:</p>
<pre><code class="lang-auto">padded_image = transforms.Pad(128)(image)
i, j, _, _ = transforms.RandomCrop(size=(224,224)).get_params(padded_image, output_size=(224, 224))
cropped_image = padded_image.crop((j, i, j + 224, i + 224))
print(i, j)  # below notice i &lt; 128 which means we're extending into the padded area

Output:
96 1185
</code></pre>
<p>You can convert back to the old coords by just subtracting 128. Of course, you’ll get a negative coordinate if you were in the padded area to begin with.</p>
<p>Would this work?</p>

username: sneer
cooked: <p>Hi,<br>
thanks, Yes this is also the solution I came up with.</p>

username: InnovArul
cooked: <p>In addition to <a class="mention" href="/u/andrei_cristea">@Andrei_Cristea</a>’s answer, here is an implementation of a random crop that you could adapt for your purpose:</p><aside class="onebox githubblob" data-onebox-src="https://github.com/KaiyangZhou/deep-person-reid/blob/e34e3ae85fe02314e3a9a9a93c5828f4fed1b225/torchreid/data/transforms.py#L12-L48">
  <header class="source">

      <a href="https://github.com/KaiyangZhou/deep-person-reid/blob/e34e3ae85fe02314e3a9a9a93c5828f4fed1b225/torchreid/data/transforms.py#L12-L48" target="_blank" rel="noopener">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/KaiyangZhou/deep-person-reid/blob/e34e3ae85fe02314e3a9a9a93c5828f4fed1b225/torchreid/data/transforms.py#L12-L48" target="_blank" rel="noopener">KaiyangZhou/deep-person-reid/blob/e34e3ae85fe02314e3a9a9a93c5828f4fed1b225/torchreid/data/transforms.py#L12-L48</a></h4>



    <pre class="onebox">      <code class="lang-py">
        <ol class="start lines" start="12" style="counter-reset: li-counter 11 ;">
            <li>class Random2DTranslation(object):</li>
            <li>    """Randomly translates the input image with a probability.</li>
            <li>
            </li>
<li>    Specifically, given a predefined shape (height, width), the input is first</li>
            <li>    resized with a factor of 1.125, leading to (height*1.125, width*1.125), then</li>
            <li>    a random crop is performed. Such operation is done with a probability.</li>
            <li>
            </li>
<li>    Args:</li>
            <li>        height (int): target image height.</li>
            <li>        width (int): target image width.</li>
            <li>        p (float, optional): probability that this operation takes place.</li>
            <li>            Default is 0.5.</li>
            <li>        interpolation (int, optional): desired interpolation. Default is</li>
            <li>            ``PIL.Image.BILINEAR``</li>
            <li>    """</li>
            <li>
            </li>
<li>    def __init__(self, height, width, p=0.5, interpolation=Image.BILINEAR):</li>
            <li>        self.height = height</li>
            <li>        self.width = width</li>
            <li>        self.p = p</li>
        </ol>
      </code>
    </pre>


  This file has been truncated. <a href="https://github.com/KaiyangZhou/deep-person-reid/blob/e34e3ae85fe02314e3a9a9a93c5828f4fed1b225/torchreid/data/transforms.py#L12-L48" target="_blank" rel="noopener">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>