File size: 10,430 Bytes
aecb8e5
 
 
 
 
 
 
1db7857
 
 
 
 
 
aecb8e5
1db7857
 
 
 
 
 
 
 
aecb8e5
dd6626e
aecb8e5
 
 
 
1db7857
aecb8e5
 
dd6626e
aecb8e5
 
 
 
1db7857
f426032
aecb8e5
 
 
 
 
 
 
f329a69
aecb8e5
 
5b0f497
 
 
1db7857
5b0f497
d6b5da0
d924dcb
64f4e85
1db7857
ec25784
1db7857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232a99d
09e820f
1db7857
 
 
f426032
1db7857
 
 
f426032
1db7857
 
 
f426032
1db7857
 
 
f426032
1db7857
 
 
f426032
1db7857
 
 
f426032
1db7857
 
 
f426032
1db7857
 
 
f426032
1db7857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aecb8e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1db7857
 
 
 
 
 
 
 
 
 
aecb8e5
1db7857
aecb8e5
1db7857
aecb8e5
1db7857
 
 
 
 
f329a69
1db7857
 
 
aecb8e5
1db7857
 
aecb8e5
dd6626e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from PIL import Image
from tqdm import tqdm
from torchvision import transforms
import argparse
import os
from natsort import natsorted
from glob import glob
import torch
import torch.nn as nn
import torch.nn.functional as F
import timm
from tqdm import tqdm
import numpy as np

model = ['swin',
         'beit',
         'dmnfnet',
         'ecaresnet_50',
         'efficient',
         'regnet',
         'vit',
         'convnext']

def main(input_model=None):
    parser = argparse.ArgumentParser(description='Quick demo Image Classification')
    parser.add_argument('--input_dir', default='./test/', type=str, help='Input images root')
    parser.add_argument('--result_dir', default='./result/', type=str, help='Results images root')
    parser.add_argument('--weights_root', default='experiments/pretrained_models', type=str, help='Weights root')
    parser.add_argument('--model', default='convnext', type=str, help='Classifier')

    args = parser.parse_args()
    args.model = input_model
    inp_dir = args.input_dir
    out_dir = args.result_dir
    os.makedirs(out_dir, exist_ok=True)

    files = natsorted(glob(os.path.join(inp_dir, '*.jpg')) + glob(os.path.join(inp_dir, '*.png')))
    model,img_size = build_model(args.model, False, args.weights_root)

    print('Start predicting......')

    result = []
    for i, file_ in enumerate(tqdm(files)):
        image_name = os.path.split(file_)[-1]
        img = Image.open(file_).convert('RGB')
        input_ = transform_size(img_size )(img).unsqueeze(0)
        with torch.no_grad():
            predict_result = model(input_)
        # prob = torch.argmax(predict_result, dim=1).item()
        top5 = torch.topk(predict_result, 5).indices.tolist()

    result = {}
    predict_result = predict_result.tolist()
    for i in top5[0]:
        label = int(i) # class number
        prob = predict_result[0][i]
        result[label] = prob
    print('result:', result)
    return result


def transform_size(size: int):
    # mean & std for different sizes
    mean = {224: (0.5446, 0.4137, 0.3847),
            256: (0.5364, 0.4142, 0.3821),
            320: (0.5188, 0.4166, 0.3773),
            352: (0.5100, 0.4183, 0.3750),
            384: (0.5015, 0.4198, 0.3728),
            480: (0.4806, 0.4232, 0.3675)}

    std = {224: (0.2329, 0.2484, 0.2500),
           256: (0.2354, 0.2470, 0.2490),
           320: (0.2403, 0.2442, 0.2479),
           352: (0.2423, 0.2431, 0.2479),
           384: (0.2440, 0.2424, 0.2481),
           480: (0.2478, 0.2423, 0.2500)}

    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Resize(size=480, interpolation=3),
        transforms.CenterCrop(size),
        transforms.Normalize(mean=mean[size], std=std[size])
    ])
    return transform


def load_checkpoint(model, weights):
    checkpoint = torch.load(weights, map_location=torch.device('cpu'))
    model.load_state_dict(checkpoint)


def build_model(model: str, pretrained: bool, pretrained_path: str):
    models = ['vit', 'beit', 'swin', 'convnext', 'ecaresnet50', 'dmnfnet', 'regnet', 'efficient']
    if model == 'vit':
        classifier = vit_model(pretrained=pretrained)
        load_checkpoint(classifier, os.path.join(pretrained_path, model+'.pth'))
        return classifier.eval(), 384
    elif model == 'beit':
        classifier = beit_model(pretrained=pretrained)
        load_checkpoint(classifier, os.path.join(pretrained_path, model+'_1.pth'))
        return classifier.eval(), 384
    elif model == 'swin':
        classifier = swin_model(pretrained=pretrained)
        load_checkpoint(classifier, os.path.join(pretrained_path, model+'.pth'))
        return classifier.eval(), 384
    elif model == 'convnext':
        classifier = convnext_model(pretrained=pretrained)
        load_checkpoint(classifier, os.path.join(pretrained_path, model+'.pth'))
        return classifier.eval(), 384
    elif model == 'ecaresnet_50':
        classifier = ecaresnet50_model(pretrained=pretrained)
        load_checkpoint(classifier, os.path.join(pretrained_path, model+'.pth'))
        return classifier.eval(), 320
    elif model == 'dmnfnet':
        classifier = dmnfnet_model(pretrained=pretrained)
        load_checkpoint(classifier, os.path.join(pretrained_path, model+'.pth'))
        return classifier.eval(), 256
    elif model == 'regnet':
        classifier = regnetz_model(pretrained=pretrained)
        load_checkpoint(classifier, os.path.join(pretrained_path, model+'.pth'))
        return classifier.eval(), 320
    elif model == 'efficient':
        classifier = efficientnet_model(pretrained=pretrained)
        load_checkpoint(classifier, os.path.join(pretrained_path, model+'.pth'))
        return classifier.eval(), 480
    else:
        raise Exception(
            "\nNo corresponding model! \nPlease enter the supported model: \n\n{}".format('\n'.join(models)))

def save_img(filepath, img):
    cv2.imwrite(filepath, cv2.cvtColor(img, cv2.COLOR_RGB2BGR))


        
def clean_folder(folder):
    for filename in os.listdir(folder):
        file_path = os.path.join(folder, filename)
        try:
            if os.path.isfile(file_path) or os.path.islink(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path)
        except Exception as e:
            print('Failed to delete %s. Reason: %s' % (file_path, e))

class beit_model(nn.Module):
    def __init__(self, classes=219, pretrained=True):
        super(beit_model, self).__init__()
        self.model = timm.create_model('beit_base_patch16_384', pretrained=pretrained, num_classes=classes)

    def forward(self, x):
        return self.model(x)


class convnext_model(nn.Module):
    def __init__(self, classes=219, pretrained=True):
        super(convnext_model, self).__init__()
        self.model = timm.create_model('convnext_base_384_in22ft1k', pretrained=pretrained, num_classes=classes)

    def forward(self, x):
        return self.model(x)


class swin_model(nn.Module):
    def __init__(self, classes=219, pretrained=True):
        super(swin_model, self).__init__()
        self.model = timm.create_model('swin_base_patch4_window12_384', pretrained=pretrained, num_classes=classes)

    def forward(self, x):
        return self.model(x)


class vit_model(nn.Module):
    def __init__(self, classes=219, pretrained=True):
        super(vit_model, self).__init__()
        self.model = timm.create_model('vit_base_patch16_384', pretrained=pretrained, num_classes=classes)

    def forward(self, x):
        return self.model(x)

class resmlp_model(nn.Module):
    def __init__(self, classes=219, pretrained=True):
        super(resmlp_model, self).__init__()
        self.model = timm.create_model('resmlp_big_24_224_in22ft1k', pretrained=pretrained, num_classes=classes)


class xcittiny_model(nn.Module):
    def __init__(self, classes=219, pretrained=True):
        super(xcittiny_model, self).__init__()
        self.model = timm.create_model('xcit_tiny_12_p8_384_dist', pretrained=pretrained, num_classes=classes)

    def forward(self, x):
        return self.model(x)


class ecaresnet269_model(nn.Module):
    def __init__(self, classes=219, pretrained=True):
        super(ecaresnet269_model, self).__init__()
        self.model = timm.create_model('ecaresnet269d', pretrained=pretrained, num_classes=classes)

    def forward(self, x):
        return self.model(x)


class dmnfnet_model(nn.Module):
    def __init__(self, classes=219, pretrained=True):
        super(dmnfnet_model, self).__init__()
        self.model = timm.create_model('dm_nfnet_f0', pretrained=pretrained, num_classes=classes)

    def forward(self, x):
        return self.model(x)


class ecaresnet50_model(nn.Module):
    def __init__(self, classes=219, pretrained=True):
        super(ecaresnet50_model, self).__init__()
        self.model = timm.create_model('ecaresnet50t', pretrained=pretrained, num_classes=classes)

    def forward(self, x):
        return self.model(x)


class regnetz_model(nn.Module):
    def __init__(self, classes=219, pretrained=True):
        super(regnetz_model, self).__init__()
        self.model = timm.create_model('regnetz_e8', pretrained=pretrained, num_classes=classes)

    def forward(self, x):
        return self.model(x)


class efficientnet_model(nn.Module):
    def __init__(self, classes=219, pretrained=True):
        super(efficientnet_model, self).__init__()
        self.model = timm.create_model('tf_efficientnetv2_m_in21ft1k', pretrained=pretrained, num_classes=classes)

    def forward(self, x):
        return self.model(x)

def transform_size(size: int):
    # mean & std for different sizes
    mean = {224: (0.5446, 0.4137, 0.3847),
            256: (0.5364, 0.4142, 0.3821),
            320: (0.5188, 0.4166, 0.3773),
            352: (0.5100, 0.4183, 0.3750),
            384: (0.5015, 0.4198, 0.3728),
            480: (0.4806, 0.4232, 0.3675)}

    std = {224: (0.2329, 0.2484, 0.2500),
           256: (0.2354, 0.2470, 0.2490),
           320: (0.2403, 0.2442, 0.2479),
           352: (0.2423, 0.2431, 0.2479),
           384: (0.2440, 0.2424, 0.2481),
           480: (0.2478, 0.2423, 0.2500)}

    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Resize(size=480, interpolation=3),
        transforms.CenterCrop(size),
        transforms.Normalize(mean=mean[size], std=std[size])
    ])
    return transform


def build_ensemble_model(model: dict, pretrained: bool):
    """
    Args:
        model:
        {
        CLASSIFIER1: ['vit', 384, 'pretrained/vit_testmodel.pth'],
        CLASSIFIER2: ['beit', 384, 'pretrained/beit_testmodel.pth'],
        CLASSIFIER3: ['swin', 384, 'pretrained/swin_testmodel.pth'],
        CLASSIFIER4: ['convnext', 384, 'pretrained/convnext_fold1_best_acc.pth']
        }

    Returns: [[finish loading pretrained model, corresponding transform], ...]

    """

    print('==> Build and load the ensemble models')
    ensemble_model = []
    for i, key in enumerate(tqdm(model)):
        #print(model[key][0])
        value = model[key]
        each_model = build_model(model=value[0], pretrained=pretrained)
        load_checkpoint(each_model, value[2])
        each_model.eval()
        ensemble_model.append([each_model, transform_size(value[1]), value[1]])

    return ensemble_model
    
if __name__ == '__main__':
    main(model=None)