-
Notifications
You must be signed in to change notification settings - Fork 32.8k
[Bugfix] Remove incorrect torchvision requirement from PIL backend image processors #45045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ArthurZucker
merged 30 commits into
huggingface:main
from
Lidang-Jiang:fix/pil-backend-torchvision-requirement
Mar 30, 2026
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
1abefc9
[Bugfix] Remove incorrect torchvision requirement from PIL backend im…
Lidang-Jiang 035abaa
[Bugfix] Remove redundant @requires(backends=("vision",)) from PIL ba…
Lidang-Jiang 1417bc1
update
ArthurZucker be82ad8
remove torch when its not necessary
ArthurZucker ed3111a
remove if typechecking
ArthurZucker c078455
fix import shinanigans
ArthurZucker 4099913
marvellous that's how we protect torch :)
ArthurZucker 6e011bf
beit is torchvisionbackend
ArthurZucker a1bda86
more import cleanup
ArthurZucker 6e906b5
fiixup
ArthurZucker d266fed
fix-repo
ArthurZucker 756aa7c
update
ArthurZucker 2f0267a
style
ArthurZucker f9d73c9
fixes
ArthurZucker 678dbc5
up
ArthurZucker 5a0e02d
more
ArthurZucker e8ea722
fix repo
ArthurZucker b15369e
up
ArthurZucker a5c7481
update
ArthurZucker b747308
fix imports
ArthurZucker 58ddf7f
style
ArthurZucker f5956fd
fix check copies
ArthurZucker 5ef4900
arf
ArthurZucker 5df7f82
converter up
ArthurZucker 8d445e5
fix?
ArthurZucker 0a2c4f9
fix copies
ArthurZucker e153efd
fix for func
ArthurZucker 2e76424
style
ArthurZucker c2510fe
ignore
ArthurZucker 93239c1
type
ArthurZucker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,9 +14,6 @@ | |
| """Image processor class for BEiT.""" | ||
|
|
||
| import numpy as np | ||
| import torch | ||
| import torch.nn.functional as F | ||
| from torchvision.transforms.v2 import functional as tvF | ||
|
|
||
| from ...image_processing_backends import PilBackend | ||
| from ...image_processing_utils import BatchFeature | ||
|
|
@@ -28,14 +25,24 @@ | |
| PILImageResampling, | ||
| SizeDict, | ||
| ) | ||
| from ...processing_utils import Unpack | ||
| from ...utils import TensorType, auto_docstring, is_torch_available | ||
| from ...processing_utils import ImagesKwargs, Unpack | ||
| from ...utils import TensorType, auto_docstring | ||
| from ...utils.import_utils import requires | ||
| from .image_processing_beit import BeitImageProcessorKwargs | ||
|
|
||
|
|
||
| # Adapted from transformers.models.beit.image_processing_beit.BeitImageProcessorKwargs | ||
| class BeitImageProcessorKwargs(ImagesKwargs, total=False): | ||
| r""" | ||
| do_reduce_labels (`bool`, *optional*, defaults to `self.do_reduce_labels`): | ||
| Whether or not to reduce all label values of segmentation maps by 1. Usually used for datasets where 0 | ||
| is used for background, and background itself is not included in all classes of a dataset (e.g. | ||
| ADE20k). The background label will be replaced by 255. | ||
| """ | ||
|
|
||
| do_reduce_labels: bool | ||
|
|
||
|
|
||
| @auto_docstring | ||
| @requires(backends=("vision", "torch", "torchvision")) | ||
| class BeitImageProcessorPil(PilBackend): | ||
| """PIL backend for BEiT with reduce_label support.""" | ||
|
|
||
|
|
@@ -124,7 +131,7 @@ def _preprocess( | |
| images: list[np.ndarray], | ||
| do_resize: bool, | ||
| size: SizeDict, | ||
| resample: "PILImageResampling | tvF.InterpolationMode | int | None", | ||
| resample: PILImageResampling | None, | ||
| do_center_crop: bool, | ||
| crop_size: SizeDict, | ||
| do_rescale: bool, | ||
|
|
@@ -152,6 +159,7 @@ def _preprocess( | |
|
|
||
| return processed_images | ||
|
|
||
| @requires(backends=("torch",)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As seen with yu, I don't think this was initially designed for object-methods themselves, only for module-root objects |
||
| def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] | None = None): | ||
| """ | ||
| Converts the output of [`BeitForSemanticSegmentation`] into semantic segmentation maps. | ||
|
|
@@ -168,8 +176,8 @@ def post_process_semantic_segmentation(self, outputs, target_sizes: list[tuple] | |
| segmentation map of shape (height, width) corresponding to the target_sizes entry (if `target_sizes` is | ||
| specified). Each entry of each `torch.Tensor` correspond to a semantic class id. | ||
| """ | ||
| if not is_torch_available(): | ||
| raise ImportError("PyTorch is required for post_process_semantic_segmentation") | ||
| import torch | ||
| import torch.nn.functional as F | ||
|
|
||
| logits = outputs.logits | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @LysandreJik updated to work as such