Setting requires_grad for a specific region of the input image

I’m trying to optimise an image based on a trained model Is there a way to set requires_grad = False for a portion of the image? This way allowing only a specific region of an image to optimise and update through training.

I’m trying this but get the following error:

xin = Variable(x, requires_grad = False)
xin[:,:,r_start:r_end,c_start:c_end].requires_grad = True

RuntimeError: you can only change requires_grad flags of leaf variables.

Hi,

Unfortunately this is not possible.
You can either create a Variable that contains just this part that has requires_grad=True, then insert it into the full image with copy_ or add_ after zeroing this part of the image.
Another way to do it is to compute the gradients for the whole image and then zero out the gradients outside the part that is of interest for you.
Let me know if you have issues implementing that.

Thanks, will keep this topic posted on my results.

Hi Alban - how do you suggest going about doing this particular step?

‘Another way to do it is to compute the gradients for the whole image and then zero out the gradients outside the part that is of interest for you.’

I think something like this:
full_grad = torch.zeros_like(xin)
full_grad[:,:,r_start:r_end,c_start:c_end] = xin.grad.data[:,:,r_start:r_end,c_start:c_end]
xin.grad.data.copy_(full_grad)
Or you can do the same thing zeroing necessary elements in xin.grad.data directly without additional full_grad variable.

Thanks, will try it out.