-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
110 lines (98 loc) · 3.23 KB
/
models.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
import torch
import torch.nn as nn
import numpy as np
from .blocks import *
__all__ = ["MobileNetV2", "MobileNetV2_10", "MobileNetV2_075", "MobileNetV2_05"]
class MobileNetV2(nn.Module):
def __init__(
self,
image_channels: int,
num_classes: int,
alpha: float = 1.0,
dropout_rate: float = 0.5,
) -> None:
super().__init__()
self.alpha = alpha
self.feature_extractor = nn.Sequential(
ConvBlock(
in_channels=image_channels,
out_channels=self._multiply_width(32),
kernel_size=3,
stride=2,
padding=1,
act="ReLU6",
),
BottleNeck(
dim=[self._multiply_width(32), self._multiply_width(16)],
factor=1,
iterate=1,
stride=1,
),
BottleNeck(
dim=[self._multiply_width(16), self._multiply_width(24)],
factor=6,
iterate=2,
stride=2,
),
BottleNeck(
dim=[self._multiply_width(24), self._multiply_width(32)],
factor=6,
iterate=3,
stride=2,
),
BottleNeck(
dim=[self._multiply_width(32), self._multiply_width(64)],
factor=6,
iterate=4,
stride=2,
),
BottleNeck(
dim=[self._multiply_width(64), self._multiply_width(96)],
factor=6,
iterate=3,
stride=1,
),
BottleNeck(
dim=[self._multiply_width(96), self._multiply_width(160)],
factor=6,
iterate=3,
stride=2,
),
BottleNeck(
dim=[self._multiply_width(160), self._multiply_width(320)],
factor=6,
iterate=1,
stride=1,
),
ConvBlock(
in_channels=self._multiply_width(320),
out_channels=self._multiply_width(1280),
kernel_size=1,
act="ReLU6",
),
nn.AdaptiveAvgPool2d(1),
)
self.classifier = Classifier(
in_features=self._multiply_width(1280),
out_features=num_classes,
dropout_rate=dropout_rate,
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.feature_extractor(x)
x = torch.flatten(x, 1)
logits = self.classifier(x)
return logits
def _multiply_width(self, dim: int) -> int:
return int(np.ceil(self.alpha * dim))
def MobileNetV2_10(
image_channels: int, num_classes: int, dropout_rate: float = 0.5
) -> MobileNetV2:
return MobileNetV2(image_channels, num_classes, 1.0, dropout_rate)
def MobileNetV2_075(
image_channels: int, num_classes: int, dropout_rate: float = 0.5
) -> MobileNetV2:
return MobileNetV2(image_channels, num_classes, 0.75, dropout_rate)
def MobileNetV2_05(
image_channels: int, num_classes: int, dropout_rate: float = 0.5
) -> MobileNetV2:
return MobileNetV2(image_channels, num_classes, 0.5, dropout_rate)