Crop a batch of features based on different centers

username: Zhihua_Liu
cooked: <p>Hi there.</p>
<p>My model goes to this way:</p>
<p>Read a batch of images and their annotations(represents different object I am interested in different images).</p>
<p>Batch of images (B, C, H, W) feed into a network, output a new feature (B, C, H, W).</p>
<p>Now, how can I center-crop a fixed patch (e.g. 64 pixels)(Ideally should be B, C, 64, 64) from the generated feature maps, but based on different centers.</p>
<p>Can this kind of batch center crop be included within the training step? (My loss take the cropped patch as y_pred compare to some y_true).</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Zhihua_Liu" data-post="1" data-topic="150160">
<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/zhihua_liu/48/49032_2.png" class="avatar"> Zhihua_Liu:</div>
<blockquote>
<p>Now, how can I center-crop a fixed patch (e.g. 64 pixels)(Ideally should be B, C, 64, 64) from the generated feature maps, but based on different centers.</p>
</blockquote>
</aside>
<p>You should be able to directly index the tensor and can thus specify the start and end indices (or derive them from the center and the size of the patch).</p>
<aside class="quote no-group" data-username="Zhihua_Liu" data-post="1" data-topic="150160">
<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/zhihua_liu/48/49032_2.png" class="avatar"> Zhihua_Liu:</div>
<blockquote>
<p>Can this kind of batch center crop be included within the training step?</p>
</blockquote>
</aside>
<p>Yes, slicing a tensor is differentiable, so should also work during training.</p>

username: Zhihua_Liu
cooked: <p>Thanks for your answer!</p>
<p>Based on your answer, my init idea is turn the batched annotations into batched indexes, and then batch indexed the feature map to get cropped features. Is that right understanding?</p>
<p>BTW I saw this:</p><aside class="quote quote-modified" data-post="1" data-topic="12247">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/e19b73/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/cropping-a-minibatch-of-images-each-image-a-bit-differently/12247">Cropping a minibatch of images, each image a bit differently</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    I have a tensor named input with dimensions 64x21x21. It is a minibatch of 64 images, each 21x21 pixels. I’d like to crop each image down to 11x11 pixels. So the output tensor I want would have dimensions 64x11x11. 
I’d like to crop each image around a different “center pixel.” The center pixels are given by a 2-dimensional long tensor named center with dimensions 64x2. For image i, center[i][0] gives the row index and center[i][1] gives the column index for the pixel that should be at the cente…
  </blockquote>
</aside>

<p>I think we have the same purpose. I try the code in that reply, however, seems like not a exactly cropping?</p>
<p>Thanks again!</p>