TLME commited on
Commit
6d3c96e
·
1 Parent(s): dcb55fb
ConvNeXt_v2-v2_ep90.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6c824bc8322a1bc76cfd1e56c94fc76bf380c7d2890148b3a5d5e8a3d27d47d3
3
+ size 449531965
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ import os
4
+ import json
5
+ from mmpretrain import ImageClassificationInferencer
6
+ import torch
7
+ import gradio as gr
8
+
9
+ config = 'convnext-v2-tiny_32xb32_in1k-384px.py'
10
+ checkpoint = 'ConvNeXt_v2-v2_ep90.pth'
11
+
12
+
13
+ inferencer = ImageClassificationInferencer(model=config, pretrained=checkpoint, device= "cuda" if torch.cuda.is_available() else "cpu")
14
+
15
+ def single_image_classifier(image):
16
+ inf_result = inferencer(image)[0]
17
+ label = inf_result['pred_class']
18
+ score = inf_result['pred_score']
19
+ if label == "not_western":
20
+ another_label = "western"
21
+ another_score = 1 - score
22
+ else:
23
+ another_label = "not_western"
24
+ another_score = 1 - score
25
+ return {label : score,another_label:another_score}
26
+
27
+ def batch_process(path,is_pred_score):
28
+ result={}
29
+ try:
30
+ for root, dirs, files in os.walk(path):
31
+ for file in files:
32
+ if file.lower().endswith(('.png', '.jpg','jpeg')):
33
+ inf_result = inferencer(os.path.join(root, file))[0]
34
+ print(result,os.path.join(root, file))
35
+ if is_pred_score == True:
36
+ result[os.path.join(root, file)]= [{'pred_class' : inf_result['pred_class']},{'pred_score' : inf_result['pred_score']}]
37
+ else:
38
+ result[os.path.join(root, file)]= [{'pred_class' : inf_result['pred_class']}]
39
+ with open(path+ "/" + "predict_result.json", "w") as file:
40
+ json.dump(result, file, ensure_ascii=False,indent=2)
41
+ return "sucess"
42
+ except:
43
+ return "failed"
44
+
45
+ with gr.Blocks() as demo:
46
+ gr.Markdown("# Western anime images classification")
47
+ gr.Markdown("A classification using mmpretrain trained to classify western images based on ConvNeXtV2-tiny.Used for classifying anime images based on whether they are in the Western style.\n\n"
48
+ "The inference script: https://huggingface.co/TLME/western-classification"
49
+ )
50
+ with gr.Tab("Single image"):
51
+ input_img = gr.Image(source='upload')
52
+
53
+
54
+ output_label =gr.Label(label="Predict result")
55
+ examples_imgs = ["./testimg2/1.jpg","./testimg2/2.jpg","./testimg2/3.jpg","./testimg2/4.jpg","./testimg/1.jpg","./testimg/2.jpg","./testimg/3.jpg","./testimg/4.jpg"]
56
+
57
+ button = gr.Button("Submit",variant="primary")
58
+
59
+ button.click(single_image_classifier,inputs= input_img,outputs= output_label)
60
+ gr.Examples(examples= examples_imgs ,inputs = input_img , outputs= output_label,fn=single_image_classifier)
61
+
62
+ with gr.Tab("Batch process"):
63
+ with gr.Row(label ='Input path'):
64
+ with gr.Column():
65
+ input_path = gr.Textbox(label="input your images folder")
66
+ is_pred_score = gr.Checkbox(value = True, label="Output pred_score")
67
+
68
+ output_msg = gr.Textbox(label="Message")
69
+
70
+ buttom =gr.Button("Process",variant="primary")
71
+ buttom.click(batch_process, inputs= [input_path,is_pred_score] , outputs=output_msg)
72
+
73
+ if __name__ == "__main__":
74
+ demo.queue(concurrency_count=4)
75
+ demo.launch()
76
+
77
+
78
+
convnext-v2-tiny_32xb32_in1k-384px.py ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ auto_scale_lr = dict(base_batch_size=96)
2
+ custom_hooks = [
3
+ dict(momentum=0.0001, priority='ABOVE_NORMAL', type='EMAHook'),
4
+ ]
5
+ data_preprocessor = dict(
6
+ mean=[
7
+ 123.675,
8
+ 116.28,
9
+ 103.53,
10
+ ],
11
+ num_classes=2,
12
+ std=[
13
+ 58.395,
14
+ 57.12,
15
+ 57.375,
16
+ ],
17
+ to_rgb=True)
18
+ dataset_type = 'CustomDataset'
19
+ default_hooks = dict(
20
+ checkpoint=dict(interval=2, type='CheckpointHook'),
21
+ logger=dict(interval=100, type='LoggerHook'),
22
+ param_scheduler=dict(type='ParamSchedulerHook'),
23
+ sampler_seed=dict(type='DistSamplerSeedHook'),
24
+ timer=dict(type='IterTimerHook'),
25
+ visualization=dict(
26
+ enable=True,
27
+ interval=1,
28
+ out_dir=None,
29
+ type='VisualizationHook',
30
+ wait_time=2))
31
+ default_scope = 'mmpretrain'
32
+ env_cfg = dict(
33
+ cudnn_benchmark=False,
34
+ dist_cfg=dict(backend='nccl'),
35
+ mp_cfg=dict(mp_start_method='fork', opencv_num_threads=0))
36
+ launcher = 'none'
37
+ load_from = './ConvNeXt_v2-v2_ep90.pth'
38
+ log_level = 'INFO'
39
+ model = dict(
40
+ backbone=dict(
41
+ arch='tiny',
42
+ drop_path_rate=0.5,
43
+ layer_scale_init_value=0.0,
44
+ type='ConvNeXt',
45
+ use_grn=True),
46
+ head=dict(
47
+ in_channels=768,
48
+ init_cfg=None,
49
+ loss=dict(label_smooth_val=0.2, type='LabelSmoothLoss'),
50
+ num_classes=2,
51
+ type='LinearClsHead'),
52
+ init_cfg=dict(
53
+ bias=0.0, layer=[
54
+ 'Conv2d',
55
+ 'Linear',
56
+ ], std=0.02, type='TruncNormal'),
57
+ train_cfg=dict(augments=[
58
+ dict(alpha=0.8, type='Mixup'),
59
+ dict(alpha=1.0, type='CutMix'),
60
+ ]),
61
+ type='ImageClassifier')
62
+ optim_wrapper = dict(
63
+ accumulative_counts=3,
64
+ clip_grad=None,
65
+ loss_scale='dynamic',
66
+ optimizer=dict(
67
+ betas=(
68
+ 0.9,
69
+ 0.999,
70
+ ),
71
+ eps=1e-08,
72
+ lr=0.00032,
73
+ type='AdamW',
74
+ weight_decay=0.05),
75
+ paramwise_cfg=dict(
76
+ bias_decay_mult=0.0,
77
+ custom_keys=dict({
78
+ '.absolute_pos_embed': dict(decay_mult=0.0),
79
+ '.relative_position_bias_table': dict(decay_mult=0.0)
80
+ }),
81
+ flat_decay_mult=0.0,
82
+ norm_decay_mult=0.0),
83
+ type='AmpOptimWrapper')
84
+ param_scheduler = [
85
+ dict(
86
+ by_epoch=True,
87
+ convert_to_iter_based=True,
88
+ end=2,
89
+ start_factor=0.001,
90
+ type='LinearLR'),
91
+ dict(begin=2, by_epoch=True, eta_min=8e-05, type='CosineAnnealingLR'),
92
+ ]
93
+ randomness = dict(deterministic=False, seed=None)
94
+ resume = False
95
+ test_cfg = dict()
96
+ test_dataloader = dict(
97
+ batch_size=16,
98
+ collate_fn=dict(type='default_collate'),
99
+ dataset=dict(
100
+ data_root='./testimgs',
101
+ pipeline=[
102
+ dict(type='LoadImageFromFile'),
103
+ dict(
104
+ backend='pillow',
105
+ interpolation='bicubic',
106
+ scale=384,
107
+ type='Resize'),
108
+ dict(type='PackInputs'),
109
+ ],
110
+ type='CustomDataset'),
111
+ num_workers=5,
112
+ persistent_workers=True,
113
+ pin_memory=True,
114
+ sampler=dict(shuffle=False, type='DefaultSampler'))
115
+ test_evaluator = dict(topk=(1, ), type='Accuracy')
116
+ test_pipeline = [
117
+ dict(type='LoadImageFromFile'),
118
+ dict(backend='pillow', interpolation='bicubic', scale=384, type='Resize'),
119
+ dict(type='PackInputs'),
120
+ ]
121
+ train_cfg = dict(by_epoch=True, max_epochs=120, val_interval=1)
122
+ train_dataloader = dict(
123
+ batch_size=32,
124
+ collate_fn=dict(type='default_collate'),
125
+ dataset=dict(
126
+ data_root='./procset',
127
+ pipeline=[
128
+ dict(type='LoadImageFromFile'),
129
+ dict(
130
+ backend='pillow',
131
+ interpolation='bicubic',
132
+ scale=384,
133
+ type='RandomResizedCrop'),
134
+ dict(direction='horizontal', prob=0.5, type='RandomFlip'),
135
+ dict(type='PackInputs'),
136
+ ],
137
+ type='CustomDataset'),
138
+ num_workers=5,
139
+ persistent_workers=True,
140
+ pin_memory=True,
141
+ sampler=dict(shuffle=True, type='DefaultSampler'))
142
+ train_pipeline = [
143
+ dict(type='LoadImageFromFile'),
144
+ dict(
145
+ backend='pillow',
146
+ interpolation='bicubic',
147
+ scale=384,
148
+ type='RandomResizedCrop'),
149
+ dict(direction='horizontal', prob=0.5, type='RandomFlip'),
150
+ dict(type='PackInputs'),
151
+ ]
152
+ val_cfg = dict()
153
+ val_dataloader = dict(
154
+ batch_size=16,
155
+ collate_fn=dict(type='default_collate'),
156
+ dataset=dict(
157
+ data_root='./valset',
158
+ pipeline=[
159
+ dict(type='LoadImageFromFile'),
160
+ dict(
161
+ backend='pillow',
162
+ interpolation='bicubic',
163
+ scale=384,
164
+ type='Resize'),
165
+ dict(type='PackInputs'),
166
+ ],
167
+ type='CustomDataset'),
168
+ num_workers=5,
169
+ persistent_workers=True,
170
+ pin_memory=True,
171
+ sampler=dict(shuffle=False, type='DefaultSampler'))
172
+ val_evaluator = dict(topk=(1, ), type='Accuracy')
173
+ vis_backends = [
174
+ dict(type='LocalVisBackend'),
175
+ ]
176
+ visualizer = dict(
177
+ type='UniversalVisualizer', vis_backends=[
178
+ dict(type='LocalVisBackend'),
179
+ ])
180
+ work_dir = './work_dirs\\convnext-v2-tiny_32xb32_in1k-384px'
infer.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ import os
4
+ import json
5
+ from mmpretrain import ImageClassificationInferencer
6
+
7
+
8
+ path = './testimg/'
9
+ config = 'convnext-v2-tiny_32xb32_in1k-384px.py'
10
+ checkpoint = 'ConvNeXt_v2-v2_ep90.pth'
11
+
12
+
13
+ inferencer = ImageClassificationInferencer(model=config, pretrained=checkpoint, device='cuda')
14
+
15
+ result={}
16
+
17
+ for root, dirs, files in os.walk(path):
18
+ for file in files:
19
+ if file.lower().endswith(('.png', '.jpg','jpeg')):
20
+ # print(os.path.join(root, file))
21
+ inf_result = inferencer(os.path.join(root, file))[0]
22
+ # print(result['pred_class'])
23
+ print(result,os.path.join(root, file))
24
+ result[os.path.join(root, file)]= [{'pred_class' : inf_result['pred_class']},{'pred_score' : inf_result['pred_score']}]
25
+
26
+ with open(path + "predict_result.json", "w") as file:
27
+ json.dump(result, file, ensure_ascii=False,indent=2)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ openmim
2
+ mmcv>=2.0.0,<2.1.0
3
+ mmengine>=0.8.3,<1.0.0
4
+ mmpretrain
5
+ torch
testimg/1.jpg ADDED

Git LFS Details

  • SHA256: 1ff13336911a9e6913b22c549d52936140f0e48d46d1d20dc6595e695cb5c00a
  • Pointer size: 131 Bytes
  • Size of remote file: 558 kB
testimg/2.jpg ADDED

Git LFS Details

  • SHA256: 6f7cd8a2f6b8d5debb39f90974fecc349a1d78c2a843715f628130fe2d27118a
  • Pointer size: 131 Bytes
  • Size of remote file: 150 kB
testimg/3.jpg ADDED

Git LFS Details

  • SHA256: 062cc59fa0bb950714821bd56e730499b4d96dbf30b7380d232f6a592f993e5d
  • Pointer size: 131 Bytes
  • Size of remote file: 772 kB
testimg/4.jpg ADDED

Git LFS Details

  • SHA256: c105225d9d636aaf54106f5fb32cd7c031e71552b721b59011b84656461457c2
  • Pointer size: 131 Bytes
  • Size of remote file: 478 kB
testimg2/1.jpg ADDED

Git LFS Details

  • SHA256: 8304dfa68daab7c7b5acc7d215b1820b14750d0fa519794381e25a659559f738
  • Pointer size: 131 Bytes
  • Size of remote file: 378 kB
testimg2/2.jpg ADDED

Git LFS Details

  • SHA256: 60e03c2c4d435a7e7d959ed1d76d0df06695a3aa9ede56aad4181b038d2dc970
  • Pointer size: 131 Bytes
  • Size of remote file: 145 kB
testimg2/3.jpg ADDED

Git LFS Details

  • SHA256: 8ceb8fc36326e5951340f89877a406dc63938b22415200c5a82d0e9efb6dfa17
  • Pointer size: 130 Bytes
  • Size of remote file: 92.1 kB
testimg2/4.jpg ADDED

Git LFS Details

  • SHA256: 981ef982c1a740a58e57d60bb30dda6cfc8c70c5e3e4661e285ade98661d952f
  • Pointer size: 131 Bytes
  • Size of remote file: 131 kB
testimg2/predict_result.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "F:\\aethe_checker\\western-classification\\testimg2\\1.jpg": [
3
+ {
4
+ "pred_class": "western"
5
+ }
6
+ ],
7
+ "F:\\aethe_checker\\western-classification\\testimg2\\2.jpg": [
8
+ {
9
+ "pred_class": "western"
10
+ }
11
+ ],
12
+ "F:\\aethe_checker\\western-classification\\testimg2\\3.jpg": [
13
+ {
14
+ "pred_class": "western"
15
+ }
16
+ ],
17
+ "F:\\aethe_checker\\western-classification\\testimg2\\4.jpg": [
18
+ {
19
+ "pred_class": "western"
20
+ }
21
+ ]
22
+ }