-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmodel.py
More file actions
136 lines (111 loc) · 3.97 KB
/
model.py
File metadata and controls
136 lines (111 loc) · 3.97 KB
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
import torch
import torch.nn as nn
from torch.nn.functional import adaptive_avg_pool1d
class ConvBNACT(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size,
stride=1, padding=0, groups=1):
super().__init__()
self.conv = nn.Conv2d(in_channels, out_channels,
kernel_size, stride, padding, 1, groups, False)
self.bn = nn.BatchNorm2d(out_channels)
self.act = nn.GELU()
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.act(x)
return x
class MicroBlock(nn.Module):
def __init__(self, nh):
super().__init__()
self.conv1 = ConvBNACT(nh, nh, 1)
self.conv2 = ConvBNACT(nh, nh, 3, 1, 1, nh)
def forward(self, x):
x = self.conv1(x)
x = x + self.conv2(x)
return x
class MicroStage(nn.Sequential):
def __init__(self, depth, nh):
super().__init__(*[MicroBlock(nh) for _ in range(depth)])
class MLP(nn.Sequential):
def __init__(self, input_dim: int, hidden_dim: int):
super().__init__(
nn.Linear(input_dim, hidden_dim),
nn.GELU(),
nn.Linear(hidden_dim, input_dim),
nn.Dropout(0.5)
)
class Residual(nn.Module):
def __init__(self, fn: nn.Module):
super().__init__()
self.fn = fn
def forward(self, x: torch.Tensor) -> torch.Tensor:
return x + self.fn(x)
class MLPBlock(nn.Module):
def __init__(self, input_dim: int, hidden_dim: int):
super().__init__()
self.layer_norm1 = nn.LayerNorm(input_dim)
self.residual1 = Residual(MLP(input_dim, hidden_dim))
self.layer_norm2 = nn.LayerNorm(input_dim)
self.conv = nn.Conv2d(
input_dim, input_dim, kernel_size=3, padding=1, groups=input_dim, bias=False)
self.layer_norm3 = nn.LayerNorm(input_dim)
self.residual2 = Residual(MLP(input_dim, hidden_dim))
def forward(self, x):
x = x.permute(0, 3, 2, 1)
x = self.layer_norm1(x)
x = self.residual1(x)
x = self.layer_norm2(x)
x = x.permute(0, 3, 2, 1)
x = self.conv(x)
x = x.permute(0, 3, 2, 1)
x = self.layer_norm3(x)
x = self.residual2(x)
x = x.permute(0, 3, 2, 1)
return x
class MLPStage(nn.Sequential):
def __init__(self, depth, input_dim, hidden_dim):
super().__init__(*[MLPBlock(input_dim, hidden_dim)
for _ in range(depth)])
class Tokenizer(nn.Sequential):
def __init__(self, in_channels: int, hidden_dim: int, out_dim: int):
super().__init__(
ConvBNACT(in_channels, hidden_dim // 2, 3, 2, 1),
ConvBNACT(hidden_dim // 2, hidden_dim // 2, 3, 1, 1),
ConvBNACT(hidden_dim // 2, out_dim, 3, 1, 1),
nn.MaxPool2d(3, 2, 1)
)
class MicroMLPNet(nn.Module):
def __init__(self, in_channels=3, nh=64, depth=2, nclass=60, img_height=32):
super().__init__()
"""
nh512可以
"""
self.embed = Tokenizer(in_channels, nh, nh)
self.micro_stages = MicroStage(depth, nh)
self.mlp_stages = MLPStage(depth, nh, nh)
self.flatten = nn.Flatten(1, 2)
self.dropout = nn.Dropout(0.5)
linear_in = nh * int(img_height//4)
self.fc = nn.Linear(linear_in, nclass)
def forward(self, x):
x_shape = x.size()
x = self.embed(x)
x = self.micro_stages(x)
x = self.mlp_stages(x)
x = self.flatten(x)
x = adaptive_avg_pool1d(x, int(x_shape[3]/4))
x = x.permute(0, 2, 1)
x = self.dropout(x)
x = self.fc(x)
return x
if __name__ == '__main__':
import time
x = torch.randn(1, 3, 32, 128)
model = MicroMLPNet(nh=32, depth=2, nclass=62, img_height=32)
t0 = time.time()
out = model(x)
t1 = time.time()
print(out.shape, (t1-t0)*1000)
#torch.save(model, 'test.pth')
#from torchsummaryX import summary
#summary(model, x)