Fix max size deprecated warning
Author: HichTala
Pull request: yes
Labels: name: cleanup

name: Vision

name: Processing

This pull request focuses on removing the deprecated `max_size` argument from the `preprocess` method across multiple image processing modules in the `transformers` library. This change simplifies the code and aligns with the planned deprecation of the `max_size` argument.

Key changes include:

### Removal of `max_size` argument:

* [`src/transformers/models/conditional_detr/image_processing_conditional_detr.py`](diffhunk://#diff-c521b281a0d72092aec2dcad7ad516928e7462b4f4bcdcea5d007b7fe28bc43bL1367): Removed `max_size` argument from the `preprocess` method and its usage in the `get_size_dict` and `resize` calls. [[1]](diffhunk://#diff-c521b281a0d72092aec2dcad7ad516928e7462b4f4bcdcea5d007b7fe28bc43bL1367) [[2]](diffhunk://#diff-c521b281a0d72092aec2dcad7ad516928e7462b4f4bcdcea5d007b7fe28bc43bL1377-R1376) [[3]](diffhunk://#diff-c521b281a0d72092aec2dcad7ad516928e7462b4f4bcdcea5d007b7fe28bc43bL1473-R1472)
* [`src/transformers/models/deformable_detr/image_processing_deformable_detr.py`](diffhunk://#diff-9d778d1efecc45f9608e6df1b9160fa5b84d9f3e90ecf2f55c4ac0e9cdb4ab01L1365): Removed `max_size` argument from the `preprocess` method and its usage in the `get_size_dict` and `resize` calls. [[1]](diffhunk://#diff-9d778d1efecc45f9608e6df1b9160fa5b84d9f3e90ecf2f55c4ac0e9cdb4ab01L1365) [[2]](diffhunk://#diff-9d778d1efecc45f9608e6df1b9160fa5b84d9f3e90ecf2f55c4ac0e9cdb4ab01L1375-R1374) [[3]](diffhunk://#diff-9d778d1efecc45f9608e6df1b9160fa5b84d9f3e90ecf2f55c4ac0e9cdb4ab01L1471-R1470)
* [`src/transformers/models/detr/image_processing_detr.py`](diffhunk://#diff-74625eb393c7631e74bd81b176d57cfb08a80929942837a54c861741f3a5e934L1340): Removed `max_size` argument from the `preprocess` method and its usage in the `get_size_dict` and `resize` calls. [[1]](diffhunk://#diff-74625eb393c7631e74bd81b176d57cfb08a80929942837a54c861741f3a5e934L1340) [[2]](diffhunk://#diff-74625eb393c7631e74bd81b176d57cfb08a80929942837a54c861741f3a5e934L1350-R1349) [[3]](diffhunk://#diff-74625eb393c7631e74bd81b176d57cfb08a80929942837a54c861741f3a5e934L1446-R1445)
* [`src/transformers/models/grounding_dino/image_processing_grounding_dino.py`](diffhunk://#diff-6d900076812c7c1a5596a3dfa1143580dd0fe4f9b827458ef7d832a318a713f0L1373): Removed `max_size` argument from the `preprocess` method and its usage in the `get_size_dict` and `resize` calls. [[1]](diffhunk://#diff-6d900076812c7c1a5596a3dfa1143580dd0fe4f9b827458ef7d832a318a713f0L1373) [[2]](diffhunk://#diff-6d900076812c7c1a5596a3dfa1143580dd0fe4f9b827458ef7d832a318a713f0L1383-R1382) [[3]](diffhunk://#diff-6d900076812c7c1a5596a3dfa1143580dd0fe4f9b827458ef7d832a318a713f0L1479-R1478)
* [`src/transformers/models/yolos/image_processing_yolos.py`](diffhunk://#diff-1d1f26cc1e12a255bd8addb6425a7616648e746f9ba92a24318df5f3ea91aa5cL1277): Removed `max_size` argument from the `preprocess` method and its usage in the `get_size_dict` and `resize` calls. [[1]](diffhunk://#diff-1d1f26cc1e12a255bd8addb6425a7616648e746f9ba92a24318df5f3ea91aa5cL1277) [[2]](diffhunk://#diff-1d1f26cc1e12a255bd8addb6425a7616648e746f9ba92a24318df5f3ea91aa5cL1287-R1286) [[3]](diffhunk://#diff-1d1f26cc1e12a255bd8addb6425a7616648e746f9ba92a24318df5f3ea91aa5cL1382-R1381)# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes #34977 




## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [x] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@qubvel here is my pull request.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker
- vision models: @amyeroberts, @qubvel
- speech models: @ylacombe, @eustlb
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Rocketknight1
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @SunMarc
- chat templates: @Rocketknight1

Integrations:

- deepspeed: HF Trainer/Accelerate: @muellerzr
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc @MekkCyber

Documentation: @stevhliu

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Comments:
While working in this PR I've notices some other possible improvement, to advance with the expected depreciation of the `max_size` argument.

Here I'm not sure what type `size` we want to handle :
https://github.com/huggingface/transformers/blob/5e8c1d713d0e8177163dbbc2a30d72f099fb48a6/src/transformers/models/detr/image_processing_detr.py#L992-L1000

If it is a only `Dict` type, like the typing of the function specify, then `max_size` variable can be removed, we can imagine something like this ?
```python
size = size if size is not None else {"shortest_edge": 800, "longest_edge": 1333}
if "max_size" in kwargs:
    logger.warning_once(
        "The `max_size` parameter is deprecated and will be removed in v4.26. "
        "Please specify in `size['longest_edge'] instead`.",
    )
    size['longest_edge'] = kwargs.pop("max_size")

size = get_size_dict(size, default_to_square=False)
```

I've also noticed weird use of size in `SameImageProcessor`
https://github.com/huggingface/transformers/blob/5e8c1d713d0e8177163dbbc2a30d72f099fb48a6/src/transformers/models/sam/image_processing_sam.py#L136-L137

I think we should normalized to use of `Dict` across the code, for example here by passing something like `{'longest_edge': size}` as argument ?

And here also the typing of the function specify the `Dict` type for size, but we are also handling other types, is this ok ?

If I get more precision, I'd be happy to contribute and may be work on the suppression of the `max_size` parameter across the code ?

No worries, if you'd like me to clean up a bit more as I explained in my previous comment, don't hesitate to let me know!

I suppose max_size can be entirely removed here, because we are at version 4.47 right now

```
if "max_size" in kwargs: 
     logger.warning_once( 
         "The `max_size` parameter is deprecated and will be removed in v4.26. " 
         "Please specify in `size['longest_edge'] instead`.", 
     ) 
     max_size = kwargs.pop("max_size") 
 else: 
     max_size = None 
 size = get_size_dict(size, max_size=max_size, default_to_square=False)
 ```

> And here also the typing of the function specify the Dict type for size, but we are also handling other types, is this ok ?

There might be configs on the Hub that still have an integer as a size parameter. Thats why here we make it for backward compatability

Ok then, I'll try to clean up the detr variants a bit more and try not to break everything. 🫡

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_34998). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

The tests pipelines still use `max_size`

https://github.com/huggingface/transformers/blob/0b5b5e6a70249837293499e9363a64765a57111c/tests/models/detr/test_image_processing_detr.py#L167-L169

I don't know if I'm allowed to modify those ?

Thanks for iterating! it seems its a breaking change, you can check with https://huggingface.co/microsoft/table-transformer-detection/ model on your branch and main:

```python
from transformers import AutoProcessor

image_processor = AutoProcessor.from_pretrained(model.id)
print(image_processor.size)
```

The model you sent use `max_size`: 
https://huggingface.co/microsoft/table-transformer-detection/blob/main/preprocessor_config.json
(L16)

Do we raise an error saying it's deprecated, or do we put back the deprecation warnings?

Ideally, it would be great to make PRs with updated `size` for all detection models with over 100 downloads on the hub, merge them, and then eliminate the max size in the codebase. It might be a bit time-consuming, so it's up to you! If you choose this route, I can share a draft script on how to fetch the required configs and make PRs. Otherwise, let's revert to backward compatibility with a warning and clean up just where it's possible.

I'm happy with whichever decision you make!

I'm ok to work on this. I think I'll rollback this PR to point where backward compatibility is ensured with warning, I'll update the "version 4.26" to "future version" it's may be more meaning full. That way, it can be merged and my code will finally be able to launch without those flooding warning messages. I'll then open some new PRs to start eliminating the max size.

It would be great if you can share the draft script with me.

Here is a draft I used to fetch object detection models image processor's configs:

```python
from transformers import AutoProcessor
from huggingface_hub import list_models, hf_hub_download
import json
import os

# Filter models with the tag 'pipeline_tag=object-detection'
models = list_models(tags="object-detection", gated=False, sort="downloads", limit=1000)

count = 0
for model in models:
    try:
        # Download preprocessing_config.json
        config_path = hf_hub_download(repo_id=model.id, filename="preprocessor_config.json")
        with open(config_path, "r") as file:
            config = json.load(file)

        # Check if 'size' is a dict
        if not isinstance(config.get("size"), dict):
            print(f"#{count} Model: {model.id}")
            print(f"Created: {model.created_at}")
            print(f"Downloads: {model.downloads}\n")
            print()
            count += 1
            
            # resotor processor
            image_processor = AutoProcessor.from_pretrained(model.id)
            print(image_processor.size)

    except Exception as e:
        print(f"Error processing model {model.id}: {e}")

```

The idea is to run this on **main** to load configs and then push them

You first load it
```python
image_processor = AutoProcessor.from_pretrained(model.id)
```

And then push back + open a PR
```python
image_processor.save_preatrained(model.id, create_pr=True)   # or smth similar
```

I will also need links to all the PRs, to share them with someone who can merge them.

Please be careful in order **not to spam all models**, lets do it batch-wise, for the first iteration we can do it for the first five models.

Thanks for sharing! I’ll probably work on this at the start of next week. Could you let me know how can I provide links to the PRs once they’re created?

Probably, save_pretrained returns something, but I'm not sure! It's something that has to be explored. Alternatively, you can use the `huggingface_hub` library to upload a `json` file and similarly open a PR. This way, it will return a link/object containing a link.

> I’ll probably work on this at the start of next week.

No worries at all! Manage your time according to your preferences, and we will always be happy with your contributions. 🤗

@ArthurZucker, a minor cleanup for `max_size` variables and warnings in image processors. 

@ArthurZucker this one should not break anything, just a minor clean-up. The second one (linked) is breaking, we are working to update hub config first

Thanks again 🤗