-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from deepghs/dev/transformers
dev(narugo): prepare for the clip preprocessor
- Loading branch information
Showing
24 changed files
with
1,343 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -12,4 +12,5 @@ imgutils.preprocess | |
base | ||
pillow | ||
torchvision | ||
transformers | ||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
imgutils.preprocess.transformers | ||
=========================================== | ||
|
||
.. currentmodule:: imgutils.preprocess.transformers | ||
|
||
.. automodule:: imgutils.preprocess.transformers | ||
|
||
|
||
register_creators_for_transformers | ||
-------------------------------------------------------------------- | ||
|
||
.. autofunction:: register_creators_for_transformers | ||
|
||
|
||
|
||
NotProcessorTypeError | ||
-------------------------------------------------------------------- | ||
|
||
.. autoclass:: NotProcessorTypeError | ||
|
||
|
||
|
||
create_transforms_from_transformers | ||
-------------------------------------------------------------------- | ||
|
||
.. autofunction:: create_transforms_from_transformers | ||
|
||
|
||
|
||
|
||
create_clip_transforms | ||
-------------------------------------------------------------------- | ||
|
||
.. autofunction:: create_clip_transforms | ||
|
||
|
||
|
||
create_transforms_from_clip_processor | ||
-------------------------------------------------------------------- | ||
|
||
.. autofunction:: create_transforms_from_clip_processor | ||
|
||
|
||
|
||
|
||
create_convnext_transforms | ||
-------------------------------------------------------------------- | ||
|
||
.. autofunction:: create_convnext_transforms | ||
|
||
|
||
|
||
create_transforms_from_convnext_processor | ||
-------------------------------------------------------------------- | ||
|
||
.. autofunction:: create_transforms_from_convnext_processor | ||
|
||
|
||
|
||
create_vit_transforms | ||
-------------------------------------------------------------------- | ||
|
||
.. autofunction:: create_vit_transforms | ||
|
||
|
||
|
||
create_transforms_from_vit_processor | ||
-------------------------------------------------------------------- | ||
|
||
.. autofunction:: create_transforms_from_vit_processor | ||
|
||
|
||
|
||
create_siglip_transforms | ||
-------------------------------------------------------------------- | ||
|
||
.. autofunction:: create_siglip_transforms | ||
|
||
|
||
|
||
create_transforms_from_siglip_processor | ||
-------------------------------------------------------------------- | ||
|
||
.. autofunction:: create_transforms_from_siglip_processor | ||
|
||
|
||
|
||
|
56 changes: 56 additions & 0 deletions
56
docs/source/api_doc/preprocess/transformers_supported.demo.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import re | ||
import warnings | ||
|
||
import pandas as pd | ||
import transformers | ||
from hfutils.operate import get_hf_client | ||
|
||
from imgutils.preprocess.transformers.base import _FN_CREATORS | ||
|
||
hf_client = get_hf_client() | ||
df = pd.read_parquet(hf_client.hf_hub_download( | ||
repo_id='deepghs/hf_models_preprocessors', | ||
repo_type='dataset', | ||
filename='repos.parquet' | ||
)) | ||
df = df[~df['image_processor_type'].isnull()] | ||
df = df.sort_values(by=['likes'], ascending=[False]) | ||
|
||
d_repo_count = { | ||
item['image_processor_type']: item['count'] | ||
for item in df['image_processor_type'].value_counts().reset_index().to_dict('records') | ||
} | ||
|
||
d_create_functions = {} | ||
for xfn in _FN_CREATORS: | ||
xname = xfn.__name__ | ||
matching = re.fullmatch('^create_transforms_from_(?P<name>[\s\S]+)_processor$', xname) | ||
if not matching: | ||
warnings.warn(f'Cannot determine transformer type of {xfn!r}.') | ||
continue | ||
raw_name = matching.group('name').replace('_', '').lower() | ||
d_create_functions[raw_name] = xname | ||
|
||
suffix = 'ImageProcessor' | ||
|
||
rows = [] | ||
for name in dir(transformers): | ||
if name.endswith(suffix) and isinstance(getattr(transformers, name), type) \ | ||
and issubclass(getattr(transformers, name), transformers.BaseImageProcessor) \ | ||
and getattr(transformers, name) is not transformers.BaseImageProcessor: | ||
cls = getattr(transformers, name) | ||
pname = name[:-len(suffix)].lower() | ||
|
||
rows.append({ | ||
'Name': name, | ||
'Supported': '✅' if pname in d_create_functions else '❌', | ||
'Repos': d_repo_count.get(name, 0), | ||
'Function': f':func:`{d_create_functions[pname]}`' if pname in d_create_functions else 'N/A' | ||
}) | ||
|
||
df = pd.DataFrame(rows) | ||
total = df['Repos'].sum() | ||
df = df[df['Repos'] >= 5] | ||
df = df.sort_values(by=['Repos', 'Supported', 'Name'], ascending=[False, True, True]) | ||
df['Repos'] = df['Repos'].map(lambda x: f'{x} ({x / total * 100.0:.2f}%)') | ||
print(df.to_markdown(headers='keys', tablefmt='rst', index=False)) |
This file contains 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 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.