Instructions to use animetimm/resnet152.dbv4-full with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- timm
How to use animetimm/resnet152.dbv4-full with timm:
import timm model = timm.create_model("hf_hub:animetimm/resnet152.dbv4-full", pretrained=True) - Transformers
How to use animetimm/resnet152.dbv4-full with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-classification", model="animetimm/resnet152.dbv4-full") pipe("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("animetimm/resnet152.dbv4-full", device_map="auto") - Notebooks
- Google Colab
- Kaggle
| tags: | |
| - image-classification | |
| - timm | |
| - transformers | |
| - animetimm | |
| - dghs-imgutils | |
| library_name: timm | |
| license: gpl-3.0 | |
| datasets: | |
| - animetimm/danbooru-wdtagger-v4-w640-ws-full | |
| base_model: | |
| - timm/resnet152.a1h_in1k | |
| # Anime Tagger resnet152.dbv4-full | |
| ## Model Details | |
| - **Model Type:** Multilabel Image classification / feature backbone | |
| - **Model Stats:** | |
| - Params: 83.7M | |
| - FLOPs / MACs: 67.9G / 33.9G | |
| - Image size: train = 384 x 384, test = 384 x 384 | |
| - **Dataset:** [animetimm/danbooru-wdtagger-v4-w640-ws-full](https://huggingface.co/datasets/animetimm/danbooru-wdtagger-v4-w640-ws-full) | |
| - Tags Count: 12476 | |
| - General (#0) Tags Count: 9225 | |
| - Character (#4) Tags Count: 3247 | |
| - Rating (#9) Tags Count: 4 | |
| ## Results | |
| | # | Macro@0.40 (F1/MCC/P/R) | Micro@0.40 (F1/MCC/P/R) | Macro@Best (F1/P/R) | | |
| |:----------:|:-----------------------------:|:-----------------------------:|:---------------------:| | |
| | Validation | 0.446 / 0.455 / 0.526 / 0.412 | 0.624 / 0.623 / 0.657 / 0.593 | --- | | |
| | Test | 0.448 / 0.456 / 0.526 / 0.413 | 0.624 / 0.624 / 0.657 / 0.594 | 0.486 / 0.513 / 0.488 | | |
| * `Macro/Micro@0.40` means the metrics on the threshold 0.40. | |
| * `Macro@Best` means the mean metrics on the tag-level thresholds on each tags, which should have the best F1 scores. | |
| ## Thresholds | |
| | Category | Name | Alpha | Threshold | Micro@Thr (F1/P/R) | Macro@0.40 (F1/P/R) | Macro@Best (F1/P/R) | | |
| |:----------:|:---------:|:-------:|:-----------:|:---------------------:|:---------------------:|:---------------------:| | |
| | 0 | general | 1 | 0.35 | 0.612 / 0.617 / 0.608 | 0.319 / 0.410 / 0.283 | 0.364 / 0.379 / 0.381 | | |
| | 4 | character | 1 | 0.48 | 0.846 / 0.903 / 0.795 | 0.813 / 0.855 / 0.781 | 0.835 / 0.895 / 0.791 | | |
| | 9 | rating | 1 | 0.38 | 0.796 / 0.744 / 0.856 | 0.803 / 0.778 / 0.836 | 0.806 / 0.778 / 0.839 | | |
| * `Micro@Thr` means the metrics on the category-level suggested thresholds, which are listed in the table above. | |
| * `Macro@0.40` means the metrics on the threshold 0.40. | |
| * `Macro@Best` means the metrics on the tag-level thresholds on each tags, which should have the best F1 scores. | |
| For tag-level thresholds, you can find them in [selected_tags.csv](https://huggingface.co/animetimm/resnet152.dbv4-full/resolve/main/selected_tags.csv). | |
| ## How to Use | |
| We provided a sample image for our code samples, you can find it [here](https://huggingface.co/animetimm/resnet152.dbv4-full/blob/main/sample.webp). | |
| ### Use TIMM And Torch | |
| Install [dghs-imgutils](https://github.com/deepghs/imgutils), [timm](https://github.com/huggingface/pytorch-image-models) and other necessary requirements with the following command | |
| ```shell | |
| pip install 'dghs-imgutils>=0.17.0' torch huggingface_hub timm pillow pandas | |
| ``` | |
| After that you can load this model with timm library, and use it for train, validation and test, with the following code | |
| ```python | |
| import json | |
| import pandas as pd | |
| import torch | |
| from huggingface_hub import hf_hub_download | |
| from imgutils.data import load_image | |
| from imgutils.preprocess import create_torchvision_transforms | |
| from timm import create_model | |
| repo_id = 'animetimm/resnet152.dbv4-full' | |
| model = create_model(f'hf-hub:{repo_id}', pretrained=True) | |
| model.eval() | |
| with open(hf_hub_download(repo_id=repo_id, repo_type='model', filename='preprocess.json'), 'r') as f: | |
| preprocessor = create_torchvision_transforms(json.load(f)['test']) | |
| # Compose( | |
| # PadToSize(size=(512, 512), interpolation=bilinear, background_color=white) | |
| # Resize(size=384, interpolation=bicubic, max_size=None, antialias=True) | |
| # CenterCrop(size=[384, 384]) | |
| # MaybeToTensor() | |
| # Normalize(mean=tensor([0.4850, 0.4560, 0.4060]), std=tensor([0.2290, 0.2240, 0.2250])) | |
| # ) | |
| image = load_image('https://huggingface.co/animetimm/resnet152.dbv4-full/resolve/main/sample.webp') | |
| input_ = preprocessor(image).unsqueeze(0) | |
| # input_, shape: torch.Size([1, 3, 384, 384]), dtype: torch.float32 | |
| with torch.no_grad(): | |
| output = model(input_) | |
| prediction = torch.sigmoid(output)[0] | |
| # output, shape: torch.Size([1, 12476]), dtype: torch.float32 | |
| # prediction, shape: torch.Size([12476]), dtype: torch.float32 | |
| df_tags = pd.read_csv( | |
| hf_hub_download(repo_id=repo_id, repo_type='model', filename='selected_tags.csv'), | |
| keep_default_na=False | |
| ) | |
| tags = df_tags['name'] | |
| mask = prediction.numpy() >= df_tags['best_threshold'] | |
| print(dict(zip(tags[mask].tolist(), prediction[mask].tolist()))) | |
| # {'general': 0.5816617608070374, | |
| # 'sensitive': 0.4577067196369171, | |
| # '1girl': 0.9958261251449585, | |
| # 'solo': 0.9633037447929382, | |
| # 'looking_at_viewer': 0.8500308990478516, | |
| # 'blush': 0.8500121831893921, | |
| # 'smile': 0.9115327596664429, | |
| # 'short_hair': 0.7201621532440186, | |
| # 'shirt': 0.6373851299285889, | |
| # 'long_sleeves': 0.8233276605606079, | |
| # 'holding': 0.718224823474884, | |
| # 'dress': 0.5167701840400696, | |
| # 'closed_mouth': 0.5078815221786499, | |
| # 'purple_eyes': 0.6588500142097473, | |
| # 'upper_body': 0.30264830589294434, | |
| # 'flower': 0.9456981420516968, | |
| # 'braid': 0.9781820774078369, | |
| # 'outdoors': 0.3688752353191376, | |
| # 'red_hair': 0.7415601015090942, | |
| # 'blunt_bangs': 0.4880999028682709, | |
| # 'apron': 0.6254206299781799, | |
| # 'plant': 0.33948495984077454, | |
| # 'blue_flower': 0.9264647364616394, | |
| # 'backlighting': 0.14451347291469574, | |
| # 'crown_braid': 0.8123992681503296, | |
| # 'potted_plant': 0.2292894870042801, | |
| # 'flower_pot': 0.29513847827911377, | |
| # 'wiping_tears': 0.46064630150794983} | |
| ``` | |
| ### Use ONNX Model For Inference | |
| Install [dghs-imgutils](https://github.com/deepghs/imgutils) with the following command | |
| ```shell | |
| pip install 'dghs-imgutils>=0.17.0' | |
| ``` | |
| Use `multilabel_timm_predict` function with the following code | |
| ```python | |
| from imgutils.generic import multilabel_timm_predict | |
| general, character, rating = multilabel_timm_predict( | |
| 'https://huggingface.co/animetimm/resnet152.dbv4-full/resolve/main/sample.webp', | |
| repo_id='animetimm/resnet152.dbv4-full', | |
| fmt=('general', 'character', 'rating'), | |
| ) | |
| print(general) | |
| # {'1girl': 0.9958261251449585, | |
| # 'braid': 0.9781820774078369, | |
| # 'solo': 0.9633036851882935, | |
| # 'flower': 0.9456979632377625, | |
| # 'blue_flower': 0.926464855670929, | |
| # 'smile': 0.9115328788757324, | |
| # 'looking_at_viewer': 0.8500310778617859, | |
| # 'blush': 0.8500126600265503, | |
| # 'long_sleeves': 0.8233274221420288, | |
| # 'crown_braid': 0.8123989105224609, | |
| # 'red_hair': 0.7415597438812256, | |
| # 'short_hair': 0.7201613783836365, | |
| # 'holding': 0.7182247638702393, | |
| # 'purple_eyes': 0.6588503122329712, | |
| # 'shirt': 0.6373854279518127, | |
| # 'apron': 0.6254194974899292, | |
| # 'dress': 0.516771137714386, | |
| # 'closed_mouth': 0.5078825354576111, | |
| # 'blunt_bangs': 0.4881010055541992, | |
| # 'wiping_tears': 0.46064335107803345, | |
| # 'outdoors': 0.36887407302856445, | |
| # 'plant': 0.3394850194454193, | |
| # 'upper_body': 0.3026488721370697, | |
| # 'flower_pot': 0.29513871669769287, | |
| # 'potted_plant': 0.22929036617279053, | |
| # 'backlighting': 0.14451327919960022} | |
| print(character) | |
| # {} | |
| print(rating) | |
| # {'general': 0.5816621780395508, 'sensitive': 0.45770588517189026} | |
| ``` | |
| For further information, see [documentation of function multilabel_timm_predict](https://dghs-imgutils.deepghs.org/main/api_doc/generic/multilabel_timm.html#multilabel-timm-predict). | |