--- 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/caformer_m36.sail_in22k_ft_in1k_384 --- # Anime Tagger caformer_m36.dbv4-full ## Model Details - **Model Type:** Multilabel Image classification / feature backbone - **Model Stats:** - Params: 82.7M - FLOPs / MACs: 74.9G / 37.3G - 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.514 / 0.524 / 0.601 / 0.477 | 0.676 / 0.675 / 0.689 / 0.662 | --- | | Test | 0.515 / 0.525 / 0.602 / 0.478 | 0.676 / 0.675 / 0.690 / 0.663 | 0.559 / 0.569 / 0.573 | * `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.37 | 0.664 / 0.661 / 0.667 | 0.383 / 0.488 / 0.343 | 0.436 / 0.439 / 0.463 | | 4 | character | 1 | 0.45 | 0.916 / 0.944 / 0.889 | 0.889 / 0.925 / 0.861 | 0.907 / 0.936 / 0.884 | | 9 | rating | 1 | 0.4 | 0.821 / 0.782 / 0.863 | 0.829 / 0.800 / 0.861 | 0.830 / 0.808 / 0.856 | * `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/caformer_m36.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/caformer_m36.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/caformer_m36.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/caformer_m36.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()))) # {'sensitive': 0.6114680171012878, # '1girl': 0.9992470741271973, # 'solo': 0.9791421890258789, # 'looking_at_viewer': 0.735735297203064, # 'blush': 0.7675875425338745, # 'smile': 0.9113360643386841, # 'short_hair': 0.7131544947624207, # 'long_sleeves': 0.605222225189209, # 'brown_hair': 0.5074329376220703, # 'holding': 0.5099453926086426, # 'dress': 0.7616127729415894, # 'sitting': 0.5869372487068176, # 'purple_eyes': 0.6435719728469849, # 'flower': 0.8259618282318115, # 'braid': 0.8305136561393738, # 'sidelocks': 0.25697049498558044, # 'red_hair': 0.5593942403793335, # 'tears': 0.7834861874580383, # 'crying': 0.32964760065078735, # 'plant': 0.5743373036384583, # 'blue_flower': 0.414720743894577, # 'crown_braid': 0.5683920979499817, # 'potted_plant': 0.525945246219635, # 'yellow_dress': 0.3494560718536377, # 'flower_pot': 0.32117223739624023, # 'happy_tears': 0.1877446323633194, # 'wiping_tears': 0.8536925911903381} ``` ### 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/caformer_m36.dbv4-full/resolve/main/sample.webp', repo_id='animetimm/caformer_m36.dbv4-full', fmt=('general', 'character', 'rating'), ) print(general) # {'1girl': 0.9992470145225525, # 'solo': 0.9791421890258789, # 'smile': 0.9113359451293945, # 'wiping_tears': 0.8536921739578247, # 'braid': 0.8305132985115051, # 'flower': 0.8259615302085876, # 'tears': 0.7834857702255249, # 'blush': 0.7675874829292297, # 'dress': 0.7616124153137207, # 'looking_at_viewer': 0.7357356548309326, # 'short_hair': 0.713154673576355, # 'purple_eyes': 0.6435709595680237, # 'long_sleeves': 0.6052224040031433, # 'sitting': 0.5869368314743042, # 'plant': 0.5743359923362732, # 'crown_braid': 0.568390965461731, # 'red_hair': 0.5593934655189514, # 'potted_plant': 0.5259431600570679, # 'holding': 0.5099461078643799, # 'brown_hair': 0.5074328780174255, # 'blue_flower': 0.4147205948829651, # 'yellow_dress': 0.3494541049003601, # 'crying': 0.32964715361595154, # 'flower_pot': 0.32117021083831787, # 'sidelocks': 0.2569706439971924, # 'happy_tears': 0.1877431571483612} print(character) # {} print(rating) # {'sensitive': 0.6114680767059326} ``` 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).