-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
102 lines (78 loc) · 3.15 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
import torch
import timm
import torch.nn as nn
import torch.nn.functional as F
from efficientnet_pytorch import EfficientNet
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
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)
class Efficientnet_b6(nn.Module):
def __init__(self, num_classes, pretrained=True):
super().__init__()
if pretrained == True:
self.model = EfficientNet.from_pretrained('efficientnet-b6', num_classes=num_classes)
else:
self.model = EfficientNet.from_name('efficientnet-b6', num_classes=num_classes)
def forward(self, x):
return self.model(x)
class Efficientnet_b4(nn.Module):
def __init__(self, num_classes, pretrained=True):
super().__init__()
if pretrained == True:
self.model = EfficientNet.from_pretrained('efficientnet-b4', num_classes=num_classes)
else:
self.model = EfficientNet.from_name('efficientnet-b4', num_classes=num_classes)
def forward(self, x):
return self.model(x)
class nfnet_f0(nn.Module):
def __init__(self, num_classes, pretrained=True):
super().__init__()
if pretrained == True:
self.model = timm.create_model('dm_nfnet_f0', pretrained=True)
else:
self.model = timm.create_model('dm_nfnet_f0', pretrained=False)
self.model.head.fc = nn.Linear(in_features=3072, out_features=num_classes, bias=True)
def forward(self, x):
return self.model(x)
class ecaresnet50t(nn.Module):
def __init__(self, num_classes, pretrained=True):
super().__init__()
if pretrained == True:
self.model = timm.create_model('ecaresnet50t', pretrained=True)
else:
self.model = timm.create_model('ecaresnet50t', pretrained=False)
self.model.fc = nn.Linear(in_features=2048, out_features=num_classes, bias=True)
def forward(self, x):
return self.model(x)
class seresnet152d(nn.Module):
def __init__(self, num_classes, pretrained=True):
super().__init__()
if pretrained == True:
self.model = timm.create_model('seresnet152d', pretrained=True)
else:
self.model = timm.create_model('seresnet152d', pretrained=False)
self.model.fc = nn.Linear(in_features=2048, out_features=num_classes, bias=True)
def forward(self, x):
return self.model(x)