How to slice using two tensors?

I have a tensor representign an image, and I want to cut a subimage out. I have a tensor representingn the topleft annd bottomright corner of the cut. I try:

  view = image[subimage_min: subimage_max - subimage_min]

ie, in reproducible copy/paste code:

import torch

image = torch.rand(10, 10, 3)
subimage_min = torch.LongTensor([2, 3])
subimage_max = torch.LongTensor([5, 7])
view = image[subimage_min: subimage_max - subimage_min]

… but this gives ‘slice indices must be integers or None or have an index method’


Would this work?

view = image[subimage_min[0]: subimage_max[0], subimage_min[1] : subimage_max[1]]

Assuming the min and max tensors are sored as [x, y], you could slice it in both dimensions.
Let me know if I misunderstood the question.

Sure, thats the workaround. But im hoping for something less verbose :slight_smile:

Ok, I see.
Would you like to apply your indices as a sliding window or is it just a single call, i.e. you just want this exact piece of subimage sliced?