Cropping a minibatch of images, each image is differently


(tjl) #1

have a tensor named input with dimensions 16x3x64x64. It is a minibatch of 16 RGB images, each 64x64 pixels. I’d like to crop each image down to a patch . Then I want to resize all the patches to the same size(32×32). So the output tensor I want would have dimensions 64x3×32x32.

I’d like to crop each image based on the point of lefttop and rightdown . They are given by a 4-dimensional numpy array named box [x1,y1,x2,y2] with dimensions 64x4.

I try crop the input with:
for i in range (batchsize):
output[i] = input[ i, : , box[i, 0]:box[i, 2], box[i, 1]:box[i, 3] ]
Then I translate the tensor output to numpy and resize it. Finally I translate it back to the tensor to forward.

However, when I do this,it tells:
TypeError: only integer tensors of a single element can be converted to an index.

I really do not know where is wrong. And Is there an efficient way to do the process I say (crop the tensor and resize based on a batch) in pytorch (on the gpu)? And if I translate the tensor to numpy, does it influence the gradient?
Thanks for any help!


(tjl) #2

Sorry, The output dimension is 16x3×32x32, not 64x3×32x32.


(Sleep Chell) #3

I think you could use torchvision like torchvision.transforms.FiveCrop to transform input image


(tjl) #4

thanks,but the input tensor is a intermediate variable ,not read in by PIL, I feel I can not do this.


(Sleep Chell) #5

Are you sure that your box[i, j] is a single element tensor? And I don’t understamd why you have to convert your data into numpy.array first when torch.Tensor could use indexing too


(tjl) #7

Because I want to resize the tensor from random size to (32×32). (.view() may not work)However, I do not know if there is a function in pytorch? I want to translate to numpy and call the cv2.resize to do it. And then I have to translate it back to the tensor to forward in GPU.
Maybe I am wrong.Or how can I resize the tensor directly?


(Sleep Chell) #8

Maybe How to resize a tensor or image will help?


(tjl) #9

I will try it , thanks!