How to slice multiple blocks efficiently from a torch tensor?

username: anand
cooked: <p>I want to slice 3D slices from a tensor of size [N,C,H,W] on basis of some indices. The indices are of the format [B,3] where B is the number of slices and 3 corresponds to N,h0,w0 where N corresponds to the batch of the input and h0, w0 correspond to the corner of the slice needed to be cut.</p>
<pre><code class="lang-python">
gathered = input[indices[0][0]:indices[0][0]+1, :, indices[0][1]*kstride[0]:indices[0][1]*kstride[0]+ksize[0], indices[0][2]*kstride[1]: indices[0][2]*kstride[1]+ksize[1]]
for B, h0, w0 in indices[1:]:
	gathered = torch.cat((gathered, input[B:B+1, :, h0*kstride[0]:h0*kstride[0]+ksize[0], w0*kstride[1]: w0*kstride[1]+ksize[1]]), 0)
return gathered
</code></pre>
<p>input is the tensor of size [N,C,H,W] where N stands for batch size, C for channels and H,W for width and height. ksize is the size of the block that i want to slice, and kstride is used to upscale the indices to the image size.</p>
<p>This approach is not fast enough for my purposes is there a better way to do this ? I am new to pytorch.</p>

username: Jk749
cooked: <p>Can you give some dummy example for the problem?</p>
<p>If I understand it correctly, you have an input of shape (N,C,H,W); get an output of shape (B,N,h0,w0).</p>
<p>Let’s say, (N,h0,w0) is a tuple stored in variable <code>index</code></p>
<pre><code class="lang-auto">N,C,H,W = input.shape
N,h0,w0 = index

input = input.transpose(1,0) # input will now have shape (C,N,H,W)
output = input[B,N,h0,w0]
return output
</code></pre>
<p>I assume, slicing input of B,h0,w0 values start from zero. If not, you can add the stride value to it.</p>

username: anand
cooked: <p>A dummy example would be :</p>
<p>Lets us say I have an input tensor of size [64,3,30,30] and I want to cut out slices of shape [3, 5, 5] . where 5 = ksize[0].<br>
Indices are of the form :</p>
<pre><code class="lang-python">indices = [
[0, 1, 2],
[1, 0, 5],
[60, 4, 1]
]
</code></pre>
<p>where each element is the index corresponding to N, w0, h0. Now I want to cut a slice of size [C, 5,5] from input using the indices. So I do it in the following way :</p>
<pre><code class="lang-python">slice = input[N, :, w0:w0+5, h0:h0+5]
</code></pre>
<p>So, the indices correspond to the top left of the slice that I want to make</p>

username: Jk749
cooked: <p>Yeah, this would do!!</p>
<aside class="quote no-group" data-username="Jk749" data-post="2" data-topic="19680">
<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/dfb087/48.png" class="avatar"> Jk749:</div>
<blockquote>
<p>output = input[B,N,h0,w0]</p>
</blockquote>
</aside>
<p>change the above line to <code>output = input[B,N,h0:h0+5,w0:w0+5]</code> and again swap the 0,1 axes.</p>

username: Gaozhongpai
cooked: <p>Hi, have you solved the problem? I have a similar problem.</p>

username: Gaozhongpai
cooked: <p>I have found the answer. Please refer to <a href="https://stackoverflow.com/questions/46091111/python-slice-array-at-different-position-on-every-row" rel="nofollow noopener">https://stackoverflow.com/questions/46091111/python-slice-array-at-different-position-on-every-row</a></p>