How to crop image tensor in model

You can crop in a fully differentiable way with indexing i.e.

import torch
from torch.autograd import Variable

image = Variable(torch.random(256, 256))

# Crop a 128 x 128 subimage in the top left corner
cropped_image = image[0:128, 0:128]

Since you have different landmarks for each image, you would have to first split your batch into single images, then crop, and then concatenate.

12 Likes