-
Notifications
You must be signed in to change notification settings - Fork 5
/
model.py
504 lines (392 loc) · 15.9 KB
/
model.py
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
import torch
import torch.nn as nn
import torch.nn.functional as F
import timm
import torchvision.models as models
from efficientnet_pytorch import EfficientNet
# effiecientnet 'efficientnet-b4' test
class effiecientnet_test(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = EfficientNet.from_pretrained('efficientnet-b4', num_classes)
def forward(self, x):
x = self.model(x)
return x
class BaseModel(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=7, stride=1)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1)
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, stride=1)
self.dropout1 = nn.Dropout(0.25)
self.dropout2 = nn.Dropout(0.25)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(128, num_classes)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = self.conv3(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dropout2(x)
x = self.avgpool(x)
x = x.view(-1, 128)
return self.fc(x)
# Custom Model Template
class MyModel(nn.Module):
def __init__(self, num_classes):
super().__init__()
"""
1. 위와 같이 생성자의 parameter 에 num_claases 를 포함해주세요.
2. 나만의 모델 아키텍쳐를 디자인 해봅니다.
3. 모델의 output_dimension 은 num_classes 로 설정해주세요.
"""
def forward(self, x):
"""
1. 위에서 정의한 모델 아키텍쳐를 forward propagation 을 진행해주세요
2. 결과로 나온 output 을 return 해주세요
"""
return x
# timm library models
# Refactoring Needed
class EfficientNetB5Custom(nn.Module):
def __init__(self):
super().__init__()
self.model = timm.create_model('tf_efficientnet_b5', pretrained=True, num_classes = 1000)
self.dropout = nn.Dropout(0.5)
self.dropouts = nn.ModuleList([
nn.Dropout(0.5) for _ in range(5)])
self.age_layer = nn.Linear(in_features=1000, out_features=3, bias=True)
self.mask_layer = nn.Linear(in_features=1000, out_features=3, bias=True)
self.sex_layer = nn.Linear(in_features=1000, out_features=2, bias=True)
def forward(self, x):
x = self.model(x)
x_ = self.dropout(x)
for i, dropout in enumerate(self.dropouts):
if i==0:
x_age = self.age_layer(dropout(x_))
x_mask = self.mask_layer(dropout(x_))
x_sex = self.sex_layer(dropout(x_))
else:
x_age += self.age_layer(dropout(x_))
x_mask += self.mask_layer(dropout(x_))
x_sex += self.sex_layer(dropout(x_))
else:
x_age /= len(self.dropouts)
x_mask /= len(self.dropouts)
x_sex /= len(self.dropouts)
return x_age, x_mask, x_sex
class EfficientNetCustom(nn.Module):
def __init__(self):
super().__init__()
self.model = timm.create_model('tf_efficientnet_b4', pretrained=True, num_classes = 1000)
self.dropout = nn.Dropout(0.5)
self.dropouts = nn.ModuleList([
nn.Dropout(0.5) for _ in range(5)])
self.age_layer = nn.Linear(in_features=1000, out_features=3, bias=True)
self.mask_layer = nn.Linear(in_features=1000, out_features=3, bias=True)
self.sex_layer = nn.Linear(in_features=1000, out_features=2, bias=True)
def forward(self, x):
x = self.model(x)
x_ = self.dropout(x)
for i, dropout in enumerate(self.dropouts):
if i==0:
x_age = self.age_layer(dropout(x_))
x_mask = self.mask_layer(dropout(x_))
x_sex = self.sex_layer(dropout(x_))
else:
x_age += self.age_layer(dropout(x_))
x_mask += self.mask_layer(dropout(x_))
x_sex += self.sex_layer(dropout(x_))
else:
x_age /= len(self.dropouts)
x_mask /= len(self.dropouts)
x_sex /= len(self.dropouts)
return x_age, x_mask, x_sex
class EfficientNet(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = timm.create_model('tf_efficientnet_b4', pretrained=True, num_classes = num_classes)
def forward(self, x):
x = self.model(x)
return x
class EfficientNetDropout(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = timm.create_model('tf_efficientnet_b4', pretrained=True, num_classes = 1000)
self.layer = nn.Linear(in_features=1000, out_features= num_classes, bias=True)
self.dropouts = nn.ModuleList([
nn.Dropout(0.5) for _ in range(5)])
def forward(self, x):
x_ = self.model(x)
for i, dropout in enumerate(self.dropouts):
if i==0:
x = self.layer(dropout(x_))
else:
x += self.layer(dropout(x_))
else:
x /= len(self.dropouts)
return x
class ConvNextBIn22ft1kDropout(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = timm.create_model('convnext_base_in22ft1k', pretrained=True, num_classes = 1000)
self.layer = nn.Linear(in_features=1000, out_features= num_classes, bias=True)
self.dropouts = nn.ModuleList([
nn.Dropout(0.5) for _ in range(5)])
def forward(self, x):
x_ = self.model(x)
for i, dropout in enumerate(self.dropouts):
if i==0:
x = self.layer(dropout(x_))
else:
x += self.layer(dropout(x_))
else:
x /= len(self.dropouts)
return x
class ConvNextLIn22ft1kCustom(nn.Module):
def __init__(self):
super().__init__()
self.model = timm.create_model('convnext_large_in22ft1k', pretrained=True, num_classes = 1000)
self.dropout = nn.Dropout(0.5)
self.dropouts = nn.ModuleList([
nn.Dropout(0.5) for _ in range(5)])
self.age_layer = nn.Linear(in_features=1000, out_features=3, bias=True)
self.mask_layer = nn.Linear(in_features=1000, out_features=3, bias=True)
self.sex_layer = nn.Linear(in_features=1000, out_features=2, bias=True)
def forward(self, x):
x = self.model(x)
x_ = self.dropout(x)
for i, dropout in enumerate(self.dropouts):
if i==0:
x_age = self.age_layer(dropout(x_))
x_mask = self.mask_layer(dropout(x_))
x_sex = self.sex_layer(dropout(x_))
else:
x_age += self.age_layer(dropout(x_))
x_mask += self.mask_layer(dropout(x_))
x_sex += self.sex_layer(dropout(x_))
else:
x_age /= len(self.dropouts)
x_mask /= len(self.dropouts)
x_sex /= len(self.dropouts)
return x_age, x_mask, x_sex
class ConvNextLIn22Custom(nn.Module):
def __init__(self):
super().__init__()
self.model = timm.create_model('convnext_large_in22k', pretrained=True, num_classes = 1536)
self.dropout = nn.Dropout(0.5)
self.dropouts = nn.ModuleList([
nn.Dropout(0.5) for _ in range(5)])
self.age_layer = nn.Linear(in_features=1536, out_features=3, bias=True)
self.mask_layer = nn.Linear(in_features=1536, out_features=3, bias=True)
self.sex_layer = nn.Linear(in_features=1536, out_features=2, bias=True)
def forward(self, x):
x = self.model(x)
x_ = self.dropout(x)
for i, dropout in enumerate(self.dropouts):
if i==0:
x_age = self.age_layer(dropout(x_))
x_mask = self.mask_layer(dropout(x_))
x_sex = self.sex_layer(dropout(x_))
else:
x_age += self.age_layer(dropout(x_))
x_mask += self.mask_layer(dropout(x_))
x_sex += self.sex_layer(dropout(x_))
else:
x_age /= len(self.dropouts)
x_mask /= len(self.dropouts)
x_sex /= len(self.dropouts)
return x_age, x_mask, x_sex
class CoatLiteMini(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = timm.create_model('coat_lite_mini', pretrained=True, num_classes=num_classes)
def forward(self, x):
x = self.model(x)
return x
class ConvNextLIn22ft1k(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = timm.create_model('convnext_large_in22ft1k', pretrained=True, num_classes=num_classes)
def forward(self, x):
x = self.model(x)
return x
class ConvNextLIn22(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = timm.create_model('convnext_large_in22k', pretrained=True, num_classes=num_classes)
def forward(self, x):
x = self.model(x)
return x
class ConvNextBIn22ft1k(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = timm.create_model('convnext_base_in22ft1k', pretrained=True, num_classes=num_classes)
def forward(self, x):
x = self.model(x)
return x
class Efficientnet_B0(nn.Module):
def __init__(self, num_classes):
super(Efficientnet_B0, self).__init__()
self.model = timm.create_model('efficientnet_b3a', pretrained=True, num_classes=num_classes)
def forward(self, x):
x = self.model(x)
return x
class VitBase(nn.Module):
def __init__(self, num_classes):
super(Efficientnet_B0, self).__init__()
self.model = timm.create_model('vit_base_patch16_224', pretrained=True, num_classes=num_classes)
def forward(self, x):
x = self.model(x)
return x
class VitLarge(nn.Module):
def __init__(self, num_classes):
super(VitLarge, self).__init__()
self.model = timm.create_model('vit_large_patch16_224', pretrained=True, num_classes=num_classes)
def forward(self, x):
x = self.model(x)
return x
class SWSResnext(nn.Module):
def __init__(self, num_classes):
super(SWSRexnext, self).__init__()
self.model = timm.create_model('swsl_resnext50_32x4d', pretrained = True, num_classes = num_classes)
def forward(self, x):
x = self.model
return x
class SWSResnext(nn.Module):
def __init__(self, num_classes):
super(SWSRexnext, self).__init__()
self.model = timm.create_model('swsl_resnext50_32x4d', pretrained = True, num_classes = num_classes)
def forward(self, x):
x = self.model
return x
class Mobilenet(nn.Module):
def __init__(self, num_classes):
super(Mobilenet, self).__init__()
self.model = timm.create_model('mobilenetv2_100', pretrained = True, num_classes = num_classes)
def forward(self, x):
x = self.model(x)
return x
class SwinLarge(nn.Module):
def __init__(self, num_classes):
super(SwinLarge, self).__init__()
self.model = timm.create_model('swin_large_patch4_window7_224', pretrained = True, num_classes = num_classes)
def forward(self, x):
x = self.model(x)
return x
class CaiT(nn.Module):
def __init__(self, num_classes):
super(CaiT, self).__init__()
self.model = timm.create_model('cait_s24_224', pretrained=True, num_classes=num_classes)
def forward(self, x):
x = self.model(x)
return x
# torchvision.models
# Refactoring Needed
def initialize_weights(m):
if isinstance(m, nn.Linear):
nn.init.xavier_uniform_(m.weight)
nn.init.zeros_(m.bias)
if isinstance(m, nn.Conv2d):
nn.init.kaiming_uniform_(m.weight.data, nonlinearity='relu')
def change_last_layer(model, num_classes):
name_last_layer = list(model.named_modules())[-1][0]
if name_last_layer == 'classifier':
model.classifier = nn.Linear(in_features = model.classifier.in_features,
out_features = num_classes, bias = True)
initialize_weights(model.classifier)
return model
elif name_last_layer == 'fc':
model.fc = nn.Linear(in_features = model.fc.in_features,
out_features = num_classes, bias = True)
initialize_weights(model.fc)
return model
else:
raise Exceptionception('last layer should be nn.Linear Module named as either fc or classifier')
class DenseNet201(nn.Module):
def __init__(self, num_classes):
super(DenseNet201, self).__init__()
self.model = models.densenet201(pretrained = True)
self.model = change_last_layer(self.model, num_classes)
def forward(self, x):
x = self.model(x)
return x
class DenseNet161(nn.Module):
def __init__(self, num_classes):
super(DenseNet161, self).__init__()
self.model = models.densenet161(pretrained = True)
self.model = change_last_layer(self.model, num_classes)
def forward(self, x):
x = self.model(x)
return x
class DenseNet121(nn.Module):
def __init__(self, num_classes):
super(DenseNet121, self).__init__()
self.model = models.densenet121(pretrained = True)
self.model = change_last_layer(self.model, num_classes)
def forward(self, x):
x = self.model(x)
return x
class InceptionV3(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = models.inception_v3(pretrained = True)
self.model = change_last_layer(self.model, num_classes)
def forward(self, x):
x = self.model(x)
return x
class Resnet152(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = models.resnet152(pretrained = True)
self.model = change_last_layer(self.model, num_classes)
def forward(self, x):
x = self.model(x)
return x
class ResNext(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = models.resnext50_32x4d(pretrained = True)
self.model = change_last_layer(self.model, num_classes)
def forward(self, x):
x = self.model(x)
return x
class Resnet50(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = models.resnet50(pretrained = True)
self.model = change_last_layer(self.model, num_classes)
def forward(self, x):
x = self.model(x)
return x
class Resnet50dropout(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = models.resnet50(pretrained = True)
self.model = change_last_layer(self.model, num_classes)
def forward(self, x):
x = self.model(x)
return x
class Resnet50dropout(nn.Module):
def __init__(self, num_classes):
super().__init__()
self.model = models.resnet50(pretrained = True)
self.model = change_last_layer(self.model, 1000)
self.layer = nn.Linear(1000, num_classes)
self.dropouts = nn.ModuleList([
nn.Dropout(0.5) for _ in range(5)])
def forward(self, x):
x_ = self.model(x)
for i, dropout in enumerate(self.dropouts):
if i==0:
x = self.layer(dropout(x_))
else:
x += self.layer(dropout(x_))
else:
x /= len(self.dropouts)
return x