-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
73 lines (59 loc) · 2.48 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
import torch
import torch.nn as nn
import timm
from pooling import GeM, ViTGeM
'''
SkinModel : EfficientNet에서만 사용하세요.
'''
class SkinModel(nn.Module):
def __init__(self, model_name, num_classes=1, pretrained=True, checkpoint_path=None):
super(SkinModel, self).__init__()
self.model = timm.create_model(model_name, pretrained=pretrained,
checkpoint_path=checkpoint_path)
in_features = self.model.classifier.in_features
self.num_classes = num_classes
self.model.classifier = nn.Identity()
self.model.global_pool = nn.Identity()
self.pooling = GeM()
self.linear = nn.Linear(in_features, num_classes)
def forward(self, images):
output = self.model(images)
output=self.pooling(output).flatten(1)
output = self.linear(output)
return output
'''
ViTSkinModel : Vision Transformer를 사용하는 모델입니다.
ViT인 경우 이 모델을 사용하셔야 합니다.
'''
class ViTSkinModel(nn.Module):
def __init__(self, model_name,pretrained=True, checkpoint_path=None):
super(ViTSkinModel, self).__init__()
self.model = timm.create_model(model_name, pretrained=pretrained,
checkpoint_path=checkpoint_path,
global_pool='', num_classes=0)
self.pooling = ViTGeM()
self.linear = nn.Linear(self.model.head.in_features, 1)
def forward(self, images):
# cls 토큰 뽑아봄
output = self.model(images)
output = self.pooling(output.permute(0, 2, 1)).flatten(1)
output = self.linear(output)
return output
'''
SkinCoat
'''
class SkinCoat(nn.Module):
def __init__(self, model_name, num_classes=1, pretrained=True, checkpoint_path=None):
super(SkinCoat, self).__init__()
self.model = timm.create_model(model_name, pretrained=pretrained,
checkpoint_path=checkpoint_path, global_pool="", num_classes=0)
self.num_classes = num_classes
self.model.classifier = nn.Identity()
self.model.global_pool = nn.Identity()
self.pooling = GeM()
self.linear = nn.Linear(self.model.head.in_features, num_classes)
def forward(self, images):
output = self.model(images)
output=self.pooling(output).flatten(1)
output = self.linear(output)
return output