# Gradient through image processor

**URL:** <https://discuss.huggingface.co/t/gradient-through-image-processor/53678>\
**Category:** 🤗Transformers\
**Created:** [September 5, 2023, 7:13pm UTC](https://discuss.huggingface.co/t/gradient-through-image-processor/53678 "2023-09-05T19:13:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![krama85](https://avatars.discourse-cdn.com/v4/letter/k/439d5e/32.png) [@krama85](https://discuss.huggingface.co/u/krama85)\
**Post date:** [September 5, 2023, 7:13pm UTC](https://discuss.huggingface.co/t/gradient-through-image-processor/53678/1 "2023-09-05T19:13:49Z")

</div>

So in my code i am computing the gradient of the image given as input to a resnet. I use AutoImageProcessor from transformers library. Is it that the gradient will not flow through the AutoImageProcessor ? For example in below;

from transformers import AutoImageProcessor

image\_processor = AutoImageProcessor.from\_pretrained(“microsoft/resnet-18”)

model\_input = image\_processor(image, return\_tensors=“pt”)

outputs = resnet(\*\*model\_input, output\_hidden\_states=True)

When i backpropogate the loss from the model, i obtain gradient at model\_input (using model\_input.grad), however image.grad gives me an error " The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. Its .grad attribute won’t be populated during autograd.backward(). If you indeed want the .grad field to be populated for a non-leaf Tensor, use .retain\_grad() on the non-leaf Tensor. "

Is there a way to get the gradient for image in the above snippet ?
