that means, for the i-th image, we want to get a small tensor using slice (i, :, left_index[i]: right_index[i], bottom_index[i]: top_index[i])
Of course, if I write a for-loop code enumerating through all the images and stacking the results, I can obtain the wanted small tensor.
However, I think “for-loop+stacking” algorithm takes time, and I want a more efficient method. Please give me some advice. Thank you!
Thank you, @ptrblck. I read this post. In my view, the solution (inclusion-exclusion version) provided in this post works because the sum of the mask_region is wanted. However, in my question, I want to obtain the raw values in the mask region. It seems that the inclusion-exclusion version is not very suitable in this case. I am not very sure… Can you give me some tips in using inclusion-exclusion method?
Ah OK, sorry for the misunderstanding.
I’m not sure, if it would be possible to get the desired regions without a reduction operation, and I think you would have to use a loop.
Since the slicing might return different output shapes, you wouldn’t be able to create one output tensor (nested tensors would be needed, which is still a work in progress).