-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMT.py
230 lines (198 loc) · 7.31 KB
/
CMT.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import torch
import torch.nn as nn
from cmt_parts import CMTStem, Patch_Aggregate, CMTBlock
from Param_Our import *
class CMT(nn.Module):
def __init__(self,
in_channels = 3,
stem_channel = 32,
cmt_channel = [46, 92, 184, 368],
patch_channel = [46, 92, 184, 368],
block_layer = [2, 2, 10, 2],
R = 3.6,
img_size = 224,
):
super(CMT, self).__init__()
# Image size for each stage
size = [img_size // 4, img_size // 8, img_size // 16, img_size // 32]
# Stem layer
self.stem = CMTStem(in_channels, stem_channel)
# Patch Aggregation Layer
self.patch1 = Patch_Aggregate(stem_channel, patch_channel[0], pad_flag = False)
self.patch2 = Patch_Aggregate(patch_channel[0], patch_channel[1], pad_flag = False)
self.patch3 = Patch_Aggregate(patch_channel[1], patch_channel[2], pad_flag = False)
self.patch4 = Patch_Aggregate(patch_channel[2], patch_channel[3], pad_flag = True)
# CMT Block Layer
stage1 = []
for _ in range(block_layer[0]):
cmt_layer = CMTBlock(
img_size = size[0],
stride = 8,
d_k = cmt_channel[0],
d_v = cmt_channel[0],
num_heads = 1,
R = R,
in_channels = patch_channel[0]
)
stage1.append(cmt_layer)
self.stage1 = nn.Sequential(*stage1)
stage2 = []
for _ in range(block_layer[1]):
cmt_layer = CMTBlock(
img_size = size[1],
stride = 4,
d_k = cmt_channel[1] // 2,
d_v = cmt_channel[1] // 2,
num_heads = 2,
R = R,
in_channels = patch_channel[1]
)
stage2.append(cmt_layer)
self.stage2 = nn.Sequential(*stage2)
stage3 = []
for _ in range(block_layer[2]):
cmt_layer = CMTBlock(
img_size = size[2],
stride = 2,
d_k = cmt_channel[2] // 4,
d_v = cmt_channel[2] // 4,
num_heads = 4,
R = R,
in_channels = patch_channel[2]
)
stage3.append(cmt_layer)
self.stage3 = nn.Sequential(*stage3)
stage4 = []
for _ in range(block_layer[3]):
cmt_layer = CMTBlock(
img_size = size[3],
stride = 1,
d_k = cmt_channel[3] // 8,
d_v = cmt_channel[3] // 8,
num_heads = 8,
R = R,
in_channels = patch_channel[3]
)
stage4.append(cmt_layer)
self.stage4 = nn.Sequential(*stage4)
# Global Average Pooling
self.avg_pool = nn.AdaptiveAvgPool2d(1)
# Fully-Connected Layer
self.fc = nn.Sequential(
nn.Linear(cmt_channel[-1], 1280),
)
# Final Regression Layer
self.regression = nn.Linear(
1280, PREDICTION_TIMESTEP * local_image_size_x * local_image_size_y
)
def forward(self, x):
# print(f"\nShape before STEM: {x.shape}")
x = self.stem(x)
# print(f"Shape after STEM: {x.shape}")
# print()
# print(f"Shape before Patch-Aggregation 1: {x.shape}")
x = self.patch1(x)
# print(f"Shape after Patch-Aggregation 1: {x.shape}")
# print()
# print(f"Shape before Block 1: {x.shape}")
x = self.stage1(x)
# print(f"Shape after Block 1: {x.shape}")
# print(f"Shape before Patch-Aggregation 2: {x.shape}")
x = self.patch2(x)
# print(f"Shape after Patch-Aggregation 2: {x.shape}")
# print()
# print(f"Shape before Block 2: {x.shape}")
x = self.stage2(x)
# print(f"Shape after Block 2: {x.shape}")
# print(f"Shape before Patch-Aggregation 3: {x.shape}")
x = self.patch3(x)
# print(f"Shape after Patch-Aggregation 3: {x.shape}")
# print()
# print(f"Shape before Block 3: {x.shape}")
x = self.stage3(x)
# print(f"Shape after Block 3: {x.shape}")
# print(f"Shape before Patch-Aggregation 4: {x.shape}")
x = self.patch4(x)
# print(f"Shape after Patch-Aggregation 4: {x.shape}")
# print()
# print(f"Shape before Block 4: {x.shape}")
x = self.stage4(x)
# print(f"Shape after Block 4: {x.shape}")
# print(f"Shape before avg pool: {x.shape}")
x = self.avg_pool(x)
# print(f"Shape after avg pool: {x.shape}")
x = torch.flatten(x, 1)
# print(f"Shape after flatten: {x.shape}")
x = self.fc(x)
# print(f"Shape after fc: {x.shape}")
# print()
logit = self.regression(x)
# print("Shape after final regression:", logit.shape)
return logit
# Define He initialization function
# def init_weights(m):
# if isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d):
# nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
# class MyLSTM(nn.Module):
# def __init__(self, input_size, hidden_size, num_layers, output_size):
# super(MyLSTM, self).__init__()
# self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
# self.fc = nn.Linear(hidden_size, output_size)
# def forward(self, x):
# out, _ = self.lstm(x)
# out = self.fc(out[:, -1, :]) # Take the output from the last time step
# return out
def CMT_Ti(img_size = 224):
model = CMT(
in_channels = 24,
stem_channel = 16,
cmt_channel = [46, 92, 184, 368],
patch_channel = [46, 92, 184, 368],
block_layer = [2, 2, 10, 2],
R = 3.6,
img_size = img_size,
)
return model
def CMT_S(img_size = 224):
model = CMT(
in_channels = 24,
stem_channel = 32,
cmt_channel = [64, 128, 256, 512],
patch_channel = [64, 128, 256, 512],
block_layer = [3, 3, 16, 3],
R = 4,
img_size = img_size,
)
return model
def CMT_B(img_size = 224):
model = CMT(
in_channels = 24,
stem_channel = 38,
cmt_channel = [76, 152, 304, 608],
patch_channel = [76, 152, 304, 608],
block_layer = [4, 4, 20, 4],
R = 4,
img_size = img_size,
)
return model
def test():
calc_param = lambda net: sum(p.numel() for p in net.parameters() if p.requires_grad)
# img = torch.randn(903, 156, 30, 30) # (Hours(total), Timestep(24), Region(x), Region(y)), (B,C,H,W)
img = torch.randn(1416, 24, 30, 30) # (Hours(total), Timestep(24), Region(x), Region(y)), (B,C,H,W)
cmt_ti = CMT_Ti()
cmt_s = CMT_S()
cmt_b = CMT_B()
out = cmt_ti(img)
# lstm_model = MyLSTM(input_size=900, hidden_size=1024, num_layers=3, output_size=900)
# lstm_output = lstm_model(out.unsqueeze(1))
print("-"*100)
print(f"Shape of input: {img.shape}")
print(f"Shape of output: {out.shape}")
# print(f"Shape of rnn output: {lstm_output.shape}")
print("-"*100)
print(f"CMT_Ti Parameters: {calc_param(cmt_ti) / 1e6 : .2f} M")
print(f"CMT_S Parameters: {calc_param(cmt_s) / 1e6 : .2f} M")
print(f"CMT_B Parameters: {calc_param(cmt_b) / 1e6 : .2f} M")
print("-"*100)
if __name__ == "__main__":
test()