-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel.py
195 lines (142 loc) · 5.86 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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import models
import timm
class BaseModel(nn.Module):
def __init__(self, config, num_classes):
super().__init__()
self.num_classes = num_classes
self.drop_rate = config["drop_rate"]
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(self.drop_rate)
self.dropout2 = nn.Dropout(self.drop_rate)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(128, self.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, config):
super().__init__()
"""
1. 위와 같이 생성자의 parameter 에 num_claases 를 포함해주세요.
2. 나만의 모델 아키텍쳐를 디자인 해봅니다.
3. 모델의 output_dimension 은 num_classes 로 설정해주세요.
"""
def forward(self, x):
"""
1. 위에서 정의한 모델 아키텍쳐를 forward propagation 을 진행해주세요
2. 결과로 나온 output 을 return 해주세요
"""
return x
class Resnet50(nn.Module):
def __init__(self, config, num_classes):
super().__init__()
self.num_classes = num_classes
self.pretrained = config["pretrained"]
self.model = models.resnet50(pretrained=self.pretrained)
self.model.fc = nn.Linear(2048, self.num_classes, bias=True)
nn.init.kaiming_normal_(self.model.fc.weight)
def forward(self, x):
return self.model(x)
class Resnet152(nn.Module):
def __init__(self, config, num_classes):
super().__init__()
self.num_classes = num_classes
self.pretrained = config["pretrained"]
self.model = models.resnet152(pretrained=self.pretrained)
self.model.fc = nn.Linear(2048, self.num_classes, bias=True)
nn.init.kaiming_normal_(self.model.fc.weight)
def forward(self, x):
return self.model(x)
class Resnext101(nn.Module):
def __init__(self, config, num_classes):
super().__init__()
self.num_classes = num_classes
self.pretrained = config["pretrained"]
self.model = models.resnext101_32x8d(pretrained=self.pretrained)
self.model.fc = nn.Linear(2048, self.num_classes, bias=True)
nn.init.kaiming_normal_(self.model.fc.weight)
def forward(self, x):
return self.model(x)
class Inceptionv3(nn.Module):
def __init__(self, config, num_classes):
super().__init__()
self.num_classes = num_classes
self.pretrained = config["pretrained"]
self.model = models.inception_v3(pretrained=self.pretrained)
self.model.fc = nn.Linear(2048, self.num_classes, bias=True)
nn.init.kaiming_normal_(self.model.fc.weight)
def forward(self, x):
return self.model(x)
class Mobilenetv3(nn.Module):
def __init__(self, config, num_classes):
super().__init__()
self.num_classes = num_classes
self.pretrained = config["pretrained"]
self.model = models.mobilenet_v3_large(pretrained=self.pretrained)
self.model.classifier = nn.Linear(1280, self.num_classes, bias=True)
nn.init.kaiming_normal_(self.model.classifier.weight)
def forward(self, x):
return self.model(x)
class Densenet121(nn.Module):
def __init__(self, config, num_classes):
super().__init__()
self.num_classes = num_classes
self.pretrained = config["pretrained"]
self.model = models.densenet121(pretrained=self.pretrained)
self.model.classifier = nn.Linear(1024, self.num_classes, bias=True)
nn.init.kaiming_normal_(self.model.classifier.weight)
def forward(self, x):
return self.model(x)
class Densenet161(nn.Module):
def __init__(self, config, num_classes):
super().__init__()
self.num_classes = num_classes
self.pretrained = config["pretrained"]
self.model = models.densenet161(pretrained=self.pretrained)
self.model.classifier = nn.Linear(2208, self.num_classes, bias=True)
nn.init.kaiming_normal_(self.model.classifier.weight)
def forward(self, x):
return self.model(x)
class Densenet201(nn.Module):
def __init__(self, config, num_classes):
super().__init__()
self.num_classes = num_classes
self.pretrained = config["pretrained"]
self.model = models.densenet201(pretrained=self.pretrained)
self.model.classifier = nn.Linear(1920, self.num_classes, bias=True)
nn.init.kaiming_normal_(self.model.classifier.weight)
def forward(self, x):
return self.model(x)
class VIT(nn.Module):
def __init__(self, config, num_classes):
super().__init__()
self.num_classes = num_classes
self.pretrained = config["pretrained"]
self.vit = timm.create_model('vit_base_patch16_224', pretrained=self.pretrained, num_classes=self.num_classes)
def forward(self, x):
return self.vit(x)
class Efficientnet_B4(nn.Module):
def __init__(self, config, num_classes):
super().__init__()
self.num_classes = num_classes
self.pretrained = config["pretrained"]
self.effi_net = timm.create_model('efficientnet_b4', pretrained=self.pretrained, num_classes=self.num_classes)
def forward(self, x):
return self.effi_net(x)