Mooshie commited on
Commit
0254ab9
·
verified ·
1 Parent(s): e5915c0

Mirror of animetimm/swinv2_base_window8_256.dbv4-full (GPL-3.0)

Browse files
Files changed (11) hide show
  1. README.md +204 -0
  2. categories.json +14 -0
  3. config.json +0 -0
  4. meta.json +0 -0
  5. metrics.json +25 -0
  6. model.onnx +3 -0
  7. model.safetensors +3 -0
  8. preprocess.json +95 -0
  9. sample.webp +0 -0
  10. selected_tags.csv +0 -0
  11. thresholds.csv +4 -0
README.md ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - image-classification
4
+ - timm
5
+ - transformers
6
+ - animetimm
7
+ - dghs-imgutils
8
+ library_name: timm
9
+ license: gpl-3.0
10
+ datasets:
11
+ - animetimm/danbooru-wdtagger-v4-w640-ws-full
12
+ base_model:
13
+ - timm/swinv2_base_window8_256.ms_in1k
14
+ ---
15
+
16
+ # Anime Tagger swinv2_base_window8_256.dbv4-full
17
+
18
+ ## Model Details
19
+
20
+ - **Model Type:** Multilabel Image classification / feature backbone
21
+ - **Model Stats:**
22
+ - Params: 99.7M
23
+ - FLOPs / MACs: 121.6G / 60.7G
24
+ - Image size: train = 448 x 448, test = 448 x 448
25
+ - **Dataset:** [animetimm/danbooru-wdtagger-v4-w640-ws-full](https://huggingface.co/datasets/animetimm/danbooru-wdtagger-v4-w640-ws-full)
26
+ - Tags Count: 12476
27
+ - General (#0) Tags Count: 9225
28
+ - Character (#4) Tags Count: 3247
29
+ - Rating (#9) Tags Count: 4
30
+
31
+ ## Results
32
+
33
+ | # | Macro@0.40 (F1/MCC/P/R) | Micro@0.40 (F1/MCC/P/R) | Macro@Best (F1/P/R) |
34
+ |:----------:|:-----------------------------:|:-----------------------------:|:---------------------:|
35
+ | Validation | 0.540 / 0.546 / 0.583 / 0.528 | 0.683 / 0.682 / 0.672 / 0.693 | --- |
36
+ | Test | 0.541 / 0.547 / 0.584 / 0.528 | 0.683 / 0.682 / 0.673 / 0.694 | 0.575 / 0.581 / 0.591 |
37
+
38
+ * `Macro/Micro@0.40` means the metrics on the threshold 0.40.
39
+ * `Macro@Best` means the mean metrics on the tag-level thresholds on each tags, which should have the best F1 scores.
40
+
41
+ ## Thresholds
42
+
43
+ | Category | Name | Alpha | Threshold | Micro@Thr (F1/P/R) | Macro@0.40 (F1/P/R) | Macro@Best (F1/P/R) |
44
+ |:----------:|:---------:|:-------:|:-----------:|:---------------------:|:---------------------:|:---------------------:|
45
+ | 0 | general | 1 | 0.41 | 0.671 / 0.667 / 0.675 | 0.415 / 0.471 / 0.397 | 0.453 / 0.454 / 0.482 |
46
+ | 4 | character | 1 | 0.59 | 0.927 / 0.951 / 0.904 | 0.901 / 0.906 / 0.900 | 0.920 / 0.945 / 0.901 |
47
+ | 9 | rating | 1 | 0.41 | 0.827 / 0.791 / 0.867 | 0.833 / 0.803 / 0.866 | 0.834 / 0.812 / 0.859 |
48
+
49
+ * `Micro@Thr` means the metrics on the category-level suggested thresholds, which are listed in the table above.
50
+ * `Macro@0.40` means the metrics on the threshold 0.40.
51
+ * `Macro@Best` means the metrics on the tag-level thresholds on each tags, which should have the best F1 scores.
52
+
53
+ For tag-level thresholds, you can find them in [selected_tags.csv](https://huggingface.co/animetimm/swinv2_base_window8_256.dbv4-full/resolve/main/selected_tags.csv).
54
+
55
+ ## How to Use
56
+
57
+ We provided a sample image for our code samples, you can find it [here](https://huggingface.co/animetimm/swinv2_base_window8_256.dbv4-full/blob/main/sample.webp).
58
+
59
+ ### Use TIMM And Torch
60
+
61
+ Install [dghs-imgutils](https://github.com/deepghs/imgutils), [timm](https://github.com/huggingface/pytorch-image-models) and other necessary requirements with the following command
62
+
63
+ ```shell
64
+ pip install 'dghs-imgutils>=0.17.0' torch huggingface_hub timm pillow pandas
65
+ ```
66
+
67
+ After that you can load this model with timm library, and use it for train, validation and test, with the following code
68
+
69
+ ```python
70
+ import json
71
+
72
+ import pandas as pd
73
+ import torch
74
+ from huggingface_hub import hf_hub_download
75
+ from imgutils.data import load_image
76
+ from imgutils.preprocess import create_torchvision_transforms
77
+ from timm import create_model
78
+
79
+ repo_id = 'animetimm/swinv2_base_window8_256.dbv4-full'
80
+ model = create_model(f'hf-hub:{repo_id}', pretrained=True)
81
+ model.eval()
82
+
83
+ with open(hf_hub_download(repo_id=repo_id, repo_type='model', filename='preprocess.json'), 'r') as f:
84
+ preprocessor = create_torchvision_transforms(json.load(f)['test'])
85
+ # Compose(
86
+ # PadToSize(size=(512, 512), interpolation=bilinear, background_color=white)
87
+ # Resize(size=448, interpolation=bicubic, max_size=None, antialias=True)
88
+ # CenterCrop(size=[448, 448])
89
+ # MaybeToTensor()
90
+ # Normalize(mean=tensor([0.4850, 0.4560, 0.4060]), std=tensor([0.2290, 0.2240, 0.2250]))
91
+ # )
92
+
93
+ image = load_image('https://huggingface.co/animetimm/swinv2_base_window8_256.dbv4-full/resolve/main/sample.webp')
94
+ input_ = preprocessor(image).unsqueeze(0)
95
+ # input_, shape: torch.Size([1, 3, 448, 448]), dtype: torch.float32
96
+ with torch.no_grad():
97
+ output = model(input_)
98
+ prediction = torch.sigmoid(output)[0]
99
+ # output, shape: torch.Size([1, 12476]), dtype: torch.float32
100
+ # prediction, shape: torch.Size([12476]), dtype: torch.float32
101
+
102
+ df_tags = pd.read_csv(
103
+ hf_hub_download(repo_id=repo_id, repo_type='model', filename='selected_tags.csv'),
104
+ keep_default_na=False
105
+ )
106
+ tags = df_tags['name']
107
+ mask = prediction.numpy() >= df_tags['best_threshold']
108
+ print(dict(zip(tags[mask].tolist(), prediction[mask].tolist())))
109
+ # {'sensitive': 0.7605047821998596,
110
+ # '1girl': 0.9980626702308655,
111
+ # 'solo': 0.985005795955658,
112
+ # 'looking_at_viewer': 0.8788912892341614,
113
+ # 'blush': 0.8115326762199402,
114
+ # 'smile': 0.9378465414047241,
115
+ # 'short_hair': 0.8466857671737671,
116
+ # 'shirt': 0.49170181155204773,
117
+ # 'long_sleeves': 0.7332525849342346,
118
+ # 'brown_hair': 0.6334490180015564,
119
+ # 'holding': 0.5199263691902161,
120
+ # 'dress': 0.6529194116592407,
121
+ # 'closed_mouth': 0.43448883295059204,
122
+ # 'sitting': 0.6391631364822388,
123
+ # 'purple_eyes': 0.7848204970359802,
124
+ # 'flower': 0.9325912594795227,
125
+ # 'braid': 0.8920556902885437,
126
+ # 'outdoors': 0.41246461868286133,
127
+ # 'red_hair': 0.6809423565864563,
128
+ # 'blunt_bangs': 0.4314112067222595,
129
+ # 'tears': 0.8375990986824036,
130
+ # 'floral_print': 0.4037105143070221,
131
+ # 'crying': 0.3995090425014496,
132
+ # 'plant': 0.6664840579032898,
133
+ # 'blue_flower': 0.7186758518218994,
134
+ # 'backlighting': 0.27747398614883423,
135
+ # 'crown_braid': 0.7316360473632812,
136
+ # 'potted_plant': 0.5671563148498535,
137
+ # 'yellow_dress': 0.44971445202827454,
138
+ # 'flower_pot': 0.539954423904419,
139
+ # 'happy_tears': 0.37840017676353455,
140
+ # 'pavement': 0.22281722724437714,
141
+ # 'wiping_tears': 0.8595536351203918,
142
+ # 'brick_floor': 0.10392400622367859}
143
+ ```
144
+ ### Use ONNX Model For Inference
145
+
146
+ Install [dghs-imgutils](https://github.com/deepghs/imgutils) with the following command
147
+
148
+ ```shell
149
+ pip install 'dghs-imgutils>=0.17.0'
150
+ ```
151
+
152
+ Use `multilabel_timm_predict` function with the following code
153
+
154
+ ```python
155
+ from imgutils.generic import multilabel_timm_predict
156
+
157
+ general, character, rating = multilabel_timm_predict(
158
+ 'https://huggingface.co/animetimm/swinv2_base_window8_256.dbv4-full/resolve/main/sample.webp',
159
+ repo_id='animetimm/swinv2_base_window8_256.dbv4-full',
160
+ fmt=('general', 'character', 'rating'),
161
+ )
162
+
163
+ print(general)
164
+ # {'1girl': 0.9980627298355103,
165
+ # 'solo': 0.985005795955658,
166
+ # 'smile': 0.9378466010093689,
167
+ # 'flower': 0.932591438293457,
168
+ # 'braid': 0.8920557498931885,
169
+ # 'looking_at_viewer': 0.8788915872573853,
170
+ # 'wiping_tears': 0.8595534563064575,
171
+ # 'short_hair': 0.8466861248016357,
172
+ # 'tears': 0.8375992178916931,
173
+ # 'blush': 0.8115329742431641,
174
+ # 'purple_eyes': 0.784820556640625,
175
+ # 'long_sleeves': 0.7332528829574585,
176
+ # 'crown_braid': 0.7316359281539917,
177
+ # 'blue_flower': 0.7186765074729919,
178
+ # 'red_hair': 0.6809430122375488,
179
+ # 'plant': 0.6664847731590271,
180
+ # 'dress': 0.6529207229614258,
181
+ # 'sitting': 0.6391631364822388,
182
+ # 'brown_hair': 0.6334487199783325,
183
+ # 'potted_plant': 0.567157506942749,
184
+ # 'flower_pot': 0.5399554371833801,
185
+ # 'holding': 0.5199264287948608,
186
+ # 'shirt': 0.4917019009590149,
187
+ # 'yellow_dress': 0.44971588253974915,
188
+ # 'closed_mouth': 0.4344888925552368,
189
+ # 'blunt_bangs': 0.4314114451408386,
190
+ # 'outdoors': 0.4124644994735718,
191
+ # 'floral_print': 0.40371057391166687,
192
+ # 'crying': 0.399509072303772,
193
+ # 'happy_tears': 0.37840035557746887,
194
+ # 'backlighting': 0.2774738669395447,
195
+ # 'pavement': 0.22281798720359802,
196
+ # 'brick_floor': 0.10392436385154724}
197
+ print(character)
198
+ # {}
199
+ print(rating)
200
+ # {'sensitive': 0.7605049014091492}
201
+ ```
202
+
203
+ 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).
204
+
categories.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "category": 0,
4
+ "name": "general"
5
+ },
6
+ {
7
+ "category": 4,
8
+ "name": "character"
9
+ },
10
+ {
11
+ "category": 9,
12
+ "name": "rating"
13
+ }
14
+ ]
config.json ADDED
The diff for this file is too large to render. See raw diff
 
meta.json ADDED
The diff for this file is too large to render. See raw diff
 
metrics.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "test": {
3
+ "macro_f1": 0.5412678718566895,
4
+ "macro_mcc": 0.5470925569534302,
5
+ "macro_precision": 0.5844053626060486,
6
+ "macro_recall": 0.528182864189148,
7
+ "micro_f1": 0.6831042170524597,
8
+ "micro_mcc": 0.6823480725288391,
9
+ "micro_precision": 0.672918975353241,
10
+ "micro_recall": 0.6936023831367493
11
+ },
12
+ "val": {
13
+ "learning_rate": 4.730465232955667e-06,
14
+ "loss": 0.08092688824612221,
15
+ "macro_f1": 0.5402738451957703,
16
+ "macro_mcc": 0.5462148785591125,
17
+ "macro_precision": 0.5834081768989563,
18
+ "macro_recall": 0.5277011394500732,
19
+ "micro_f1": 0.6825976371765137,
20
+ "micro_mcc": 0.6818419098854065,
21
+ "micro_precision": 0.672331690788269,
22
+ "micro_recall": 0.6931818127632141,
23
+ "step": 93
24
+ }
25
+ }
model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e0cd170a003010a7b4f1dde12f9d3729f360cfc74bf326280362c065f6283bac
3
+ size 469933915
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:33d9e8b6493a405b1778bae1d39b71b99d3b6796a6df45a81c9bad13ba702bf8
3
+ size 398770728
preprocess.json ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "pre": [
3
+ {
4
+ "background_color": "white",
5
+ "interpolation": "bilinear",
6
+ "size": [
7
+ 512,
8
+ 512
9
+ ],
10
+ "type": "pad_to_size"
11
+ }
12
+ ],
13
+ "test": [
14
+ {
15
+ "background_color": "white",
16
+ "interpolation": "bilinear",
17
+ "size": [
18
+ 512,
19
+ 512
20
+ ],
21
+ "type": "pad_to_size"
22
+ },
23
+ {
24
+ "antialias": true,
25
+ "interpolation": "bicubic",
26
+ "max_size": null,
27
+ "size": 448,
28
+ "type": "resize"
29
+ },
30
+ {
31
+ "size": [
32
+ 448,
33
+ 448
34
+ ],
35
+ "type": "center_crop"
36
+ },
37
+ {
38
+ "type": "maybe_to_tensor"
39
+ },
40
+ {
41
+ "mean": [
42
+ 0.48500001430511475,
43
+ 0.4560000002384186,
44
+ 0.4059999883174896
45
+ ],
46
+ "std": [
47
+ 0.2290000021457672,
48
+ 0.2240000069141388,
49
+ 0.22499999403953552
50
+ ],
51
+ "type": "normalize"
52
+ }
53
+ ],
54
+ "val": [
55
+ {
56
+ "background_color": "white",
57
+ "interpolation": "bilinear",
58
+ "size": [
59
+ 512,
60
+ 512
61
+ ],
62
+ "type": "pad_to_size"
63
+ },
64
+ {
65
+ "antialias": true,
66
+ "interpolation": "bicubic",
67
+ "max_size": null,
68
+ "size": 448,
69
+ "type": "resize"
70
+ },
71
+ {
72
+ "size": [
73
+ 448,
74
+ 448
75
+ ],
76
+ "type": "center_crop"
77
+ },
78
+ {
79
+ "type": "maybe_to_tensor"
80
+ },
81
+ {
82
+ "mean": [
83
+ 0.48500001430511475,
84
+ 0.4560000002384186,
85
+ 0.4059999883174896
86
+ ],
87
+ "std": [
88
+ 0.2290000021457672,
89
+ 0.2240000069141388,
90
+ 0.22499999403953552
91
+ ],
92
+ "type": "normalize"
93
+ }
94
+ ]
95
+ }
sample.webp ADDED
selected_tags.csv ADDED
The diff for this file is too large to render. See raw diff
 
thresholds.csv ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ category,name,alpha,threshold,f1,precision,recall
2
+ 0,general,1.0,0.41000000000000003,0.6707966863427522,0.6665694337268755,0.6750778979711257
3
+ 4,character,1.0,0.59,0.9267068868528188,0.9506159183839009,0.9039710254684169
4
+ 9,rating,1.0,0.41000000000000003,0.8268949609018027,0.7905548894566996,0.8667369545088975