Hi,
I am looking for some kind of way to transform a NxWxH LongTensor into a (NxWxH)x9 LongTensor. The transform would be to extract a square patch at each position, it would return a 2D tensor (NxWxH) lines x (number of elements in square) columns. It has to take into account that if a full square cannot be extracted, it should be filled with a value (border limit case), the original tensor can be padded before of course.
I already know how to do this with a for loop over each dimensions, however I am looking for a smarter way to break complexity, currently I can’t think of a way to use a vectorized op to do this. It needs to support autograd, has I don’t want to break the graph.

IMG_20171120_164209_01_01_01.jpg3125×2695 1.56 MB

it would look like :
target = torch.LongTensor(4, 15, 15)

# magic transform here

# shape of expected tensor
trans_target = torch.LongTensor(4 * 15 * 15, 9)

This question might be a little ambitious, but I know some of you know how to be really creative when it comes to this kind a tensor manipulation 
I will post updates if I find anything interesting.
Thanks !!