-
Notifications
You must be signed in to change notification settings - Fork 27.7k
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
Add TimmWrapper #34564
Add TimmWrapper #34564
Changes from 46 commits
39c42db
4b98375
aa494f1
44d123e
4b35ae2
2b5db8f
b07a5c9
e3a88b6
baffbe2
6fc50cf
ed00b41
50b507b
de87e54
a907061
1f55841
23b38af
2f0aee1
bff2e98
0c80253
a32de6a
59f55d1
666419f
5540d32
88f737c
496d38d
4cfa51f
3891975
f878ab4
b1f145f
7d87a95
ae0425d
ec9eade
9767048
d5478f6
b61048b
72236b5
e290f22
0a08a1f
1faf9ec
addbac8
8ba4592
2b993a7
0b00cef
485fe7a
dede4e4
1ec132c
d96b257
6b1b621
bb7d465
4390213
4233850
ef96b6d
c3bb39a
5444a6c
06039d0
0b78735
5bb2950
38b9423
c1cc1fa
3acafad
8e8f41b
c5c288f
a4ae76f
464874f
02fbd58
eb9a66f
e08fe70
7734804
051acee
185fab8
bca3279
672cc6d
983b9b2
9c128ce
f2dba79
9bc887b
f92216f
42278a7
6b3ba3b
ff6efde
a476610
327095a
7e0d2c6
a10bc0d
a87fbf6
90c1c88
fd7b646
cdd3811
3d1a76e
cc0a330
4edfe90
8b82e2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<!--Copyright 2022 The HuggingFace Team. All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
specific language governing permissions and limitations under the License. | ||
|
||
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be | ||
rendered properly in your Markdown viewer. | ||
|
||
--> | ||
|
||
# TimmWrapper | ||
|
||
## Overview | ||
|
||
Helper class to enable loading timm models to be used with the transformers library and its autoclasses. | ||
|
||
```python | ||
import torch | ||
from urllib.request import urlopen | ||
from PIL import Image | ||
from transformers import AutoConfig, AutoModelForImageClassification, AutoImageProcessor | ||
|
||
checkpoint = "timm/resnet50.a1_in1k" | ||
img = Image.open(urlopen( | ||
'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png' | ||
)) | ||
|
||
image_processor = AutoImageProcessor.from_pretrained(checkpoint) | ||
inputs = image_processor(img, return_tensors="pt") | ||
model = AutoModelForImageClassification.from_pretrained(checkpoint) | ||
|
||
with torch.no_grad(): | ||
logits = model(**inputs).logits | ||
|
||
top5_probabilities, top5_class_indices = torch.topk(logits.softmax(dim=1) * 100, k=5) | ||
``` | ||
|
||
## TimmWrapperConfig | ||
|
||
[[autodoc]] TimmWrapperConfig | ||
|
||
## TimmWrapperImageProcessor | ||
|
||
[[autodoc]] TimmWrapperImageProcessor | ||
- preprocess | ||
|
||
## TimmWrapperModel | ||
|
||
[[autodoc]] TimmWrapperModel | ||
- forward | ||
|
||
## TimmWrapperForImageClassification | ||
|
||
[[autodoc]] TimmWrapperForImageClassification | ||
- forward |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -295,6 +295,7 @@ def get_image_processor_dict( | |
local_files_only = kwargs.pop("local_files_only", False) | ||
revision = kwargs.pop("revision", None) | ||
subfolder = kwargs.pop("subfolder", "") | ||
image_processor_filename = kwargs.pop("image_processor_filename", IMAGE_PROCESSOR_NAME) | ||
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. The 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. I understand - since it's a new kwarg that is does not seem to have an equivalent in hub methods (like |
||
|
||
from_pipeline = kwargs.pop("_from_pipeline", None) | ||
from_auto_class = kwargs.pop("_from_auto", False) | ||
|
@@ -321,15 +322,15 @@ def get_image_processor_dict( | |
pretrained_model_name_or_path = str(pretrained_model_name_or_path) | ||
is_local = os.path.isdir(pretrained_model_name_or_path) | ||
if os.path.isdir(pretrained_model_name_or_path): | ||
image_processor_file = os.path.join(pretrained_model_name_or_path, IMAGE_PROCESSOR_NAME) | ||
image_processor_file = os.path.join(pretrained_model_name_or_path, image_processor_filename) | ||
if os.path.isfile(pretrained_model_name_or_path): | ||
resolved_image_processor_file = pretrained_model_name_or_path | ||
is_local = True | ||
elif is_remote_url(pretrained_model_name_or_path): | ||
image_processor_file = pretrained_model_name_or_path | ||
resolved_image_processor_file = download_url(pretrained_model_name_or_path) | ||
else: | ||
image_processor_file = IMAGE_PROCESSOR_NAME | ||
image_processor_file = image_processor_filename | ||
try: | ||
# Load from local folder or from cache or download from model Hub and cache | ||
resolved_image_processor_file = cached_file( | ||
|
@@ -355,7 +356,7 @@ def get_image_processor_dict( | |
f"Can't load image processor for '{pretrained_model_name_or_path}'. If you were trying to load" | ||
" it from 'https://huggingface.co/models', make sure you don't have a local directory with the" | ||
f" same name. Otherwise, make sure '{pretrained_model_name_or_path}' is the correct path to a" | ||
f" directory containing a {IMAGE_PROCESSOR_NAME} file" | ||
f" directory containing a {image_processor_filename} file" | ||
) | ||
|
||
try: | ||
|
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.
let's do something explicit for this!
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.
I'm not sure what you meant here, but I made it a bit more readable in 4edfe90 IMO. It's actually not related to the timm wrapper, it's the same in the original code.