ViTPose tutorial fails
Author: fzoric8
Pull request: no
Labels: name: bug

name: Vision

### System Info

- `transformers` version: 4.49.0
- Platform: Linux-6.8.0-52-generic-x86_64-with-glibc2.39
- Python version: 3.13.2
- Huggingface_hub version: 0.29.1
- Safetensors version: 0.5.3
- Accelerate version: 1.4.0
- Accelerate config: 	not found
- DeepSpeed version: not installed
- PyTorch version (GPU?): 2.6.0 (True)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using distributed or parallel set-up in script?: False
- Using GPU in script?: True
- GPU type: NVIDIA GeForce RTX 4070 Laptop GPU


### Who can help?

Hi, 

I'm trying to run VitPose following this [tutorial](https://huggingface.co/docs/transformers/en/model_doc/vitpose). 

However, I'm getting following error output when running following code part: 
``` 
image_processor = AutoProcessor.from_pretrained("usyd-community/vitpose-base-simple")
model = VitPoseForPoseEstimation.from_pretrained("usyd-community/vitpose-base-simple", device_map=device)

inputs = image_processor(image, boxes=[person_boxes], return_tensors="pt").to(device)
```

Which fails with following message: 
```
NameError                                 Traceback (most recent call last)
Cell In[114], line 7
      4 image_processor = AutoProcessor.from_pretrained("usyd-community/vitpose-base-simple")
      5 model = VitPoseForPoseEstimation.from_pretrained("usyd-community/vitpose-base-simple", device_map=device)
----> 7 inputs = image_processor(image, boxes=[person_boxes], return_tensors="pt").to(device)

File ~/miniconda3/envs/huggingface/lib/python3.13/site-packages/transformers/image_processing_utils.py:42, in BaseImageProcessor.__call__(self, images, **kwargs)
     40 def __call__(self, images, **kwargs) -> BatchFeature:
     41     """Preprocess an image or a batch of images."""
---> 42     return self.preprocess(images, **kwargs)

File ~/miniconda3/envs/huggingface/lib/python3.13/site-packages/transformers/models/vitpose/image_processing_vitpose.py:524, in VitPoseImageProcessor.preprocess(self, images, boxes, do_affine_transform, size, do_rescale, rescale_factor, do_normalize, image_mean, image_std, return_tensors, data_format, input_data_format)
    517     for box in image_boxes:
    518         center, scale = box_to_center_and_scale(
    519             box,
    520             image_width=size["width"],
    521             image_height=size["height"],
    522             normalize_factor=self.normalize_factor,
    523         )
--> 524         transformed_image = self.affine_transform(
    525             image, center, scale, rotation=0, size=size, input_data_format=input_data_format
    526         )
    527         new_images.append(transformed_image)
    528 images = new_images

File ~/miniconda3/envs/huggingface/lib/python3.13/site-packages/transformers/models/vitpose/image_processing_vitpose.py:417, in VitPoseImageProcessor.affine_transform(self, image, center, scale, rotation, size, data_format, input_data_format)
    411 # input image requires channels last format
    412 image = (
    413     image
    414     if input_data_format == ChannelDimension.LAST
    415     else to_channel_dimension_format(image, ChannelDimension.LAST, input_data_format)
    416 )
--> 417 image = scipy_warp_affine(src=image, M=transformation, size=(size[1], size[0]))
    419 image = to_channel_dimension_format(image, data_format, ChannelDimension.LAST)
    421 return image

File ~/miniconda3/envs/huggingface/lib/python3.13/site-packages/transformers/models/vitpose/image_processing_vitpose.py:313, in scipy_warp_affine(src, M, size)
    311 M_scipy = np.vstack([M, [0, 0, 1]])
    312 # If you have a matrix for the ‘push’ transformation, use its inverse (numpy.linalg.inv) in this function.
--> 313 M_inv = inv(M_scipy)
    314 M_inv[0, 0], M_inv[0, 1], M_inv[1, 0], M_inv[1, 1], M_inv[0, 2], M_inv[1, 2] = (
    315     M_inv[1, 1],
    316     M_inv[1, 0],
   (...)
    320     M_inv[0, 2],
    321 )
    323 new_src = [affine_transform(channel, M_inv, output_shape=size, order=1) for channel in channels]

NameError: name 'inv' is not defined
```

### Information

- [x] The official example scripts
- [ ] My own modified scripts

### Reproduction

1. Create conda env
2. Install `transformers`, `torch`, `accelerate`, `pillow` and `numpy`
3. Run [VitPose](https://huggingface.co/docs/transformers/en/model_doc/vitpose) tutorial script

### Expected behavior

Instead of the error, output of the `image_processor` should be tensors that can be input to the model. 

### Solution: 

I've managed to solve the issue by replacing line `inv` on the line 313 with the `np.linalg.inv` in the 
models/vitpose/image_processing_vitpose.py. 

However, It works with this dirty solution, but without it, ViTPose tutorial fails.  

Comments:
cc @nielsrogge who recently worked on ViTPose!

Hey @fzoric8, do you have `scipy` installed?  I would recommend `pip install -U scipy`

Hi @qubvel, I've invoked `pip install scipy`, I'll try with `pip install -U scipy` and let you know. Tnx

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.