venkatesh-thiru commited on
Commit
40ed350
1 Parent(s): 3d4a80a

Upload model

Browse files
Files changed (5) hide show
  1. AUNet.py +212 -0
  2. AUNetConfig.py +28 -0
  3. config.json +20 -0
  4. pytorch_model.bin +3 -0
  5. s2l8hModel.py +20 -0
AUNet.py ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+
7
+
8
+ class SqeezeExcite(nn.Module):
9
+ def __init__(self, channel, reduction_ratio = 16):
10
+ super(SqeezeExcite,self).__init__()
11
+ self.GAP = nn.AdaptiveAvgPool2d(1)
12
+
13
+ self.mlp = nn.Sequential(
14
+ nn.Linear(channel, channel//reduction_ratio, bias = False),
15
+ nn.ReLU(inplace=True),
16
+ nn.Linear(channel//reduction_ratio,channel,bias = False),
17
+ nn.Sigmoid()
18
+ )
19
+
20
+ def forward(self,x):
21
+ b,c,_,_ = x.size()
22
+ out = self.GAP(x).view(b,c)
23
+ out = self.mlp(out).view(b,c,1,1)
24
+ return x * out.expand_as(x)
25
+
26
+
27
+ class ECA(nn.Module):
28
+ # https://wandb.ai/diganta/ECANet-sweep/reports/Efficient-Channel-Attention--VmlldzozNzgwOTE
29
+ def __init__(self,channels, b = 1, gamma = 2):
30
+ super(ECA, self).__init__()
31
+ self.GAP = nn.AdaptiveAvgPool2d(1)
32
+ self.channels = channels
33
+ self.b = b
34
+ self.gamma = gamma
35
+ self.conv = nn.Conv1d(1, 1, kernel_size=self.adaptive_kernel(),padding = (self.adaptive_kernel()-1)//2, bias = False)
36
+ self.sigmoid = nn.Sigmoid()
37
+
38
+ def forward(self,x):
39
+ attn = self.GAP(x)
40
+ attn = self.conv(attn.squeeze(-1).transpose(-1,-2)).transpose(-1,-2).unsqueeze(-1)
41
+ attn = self.sigmoid(attn)
42
+ return x * attn.expand_as(x)
43
+
44
+
45
+ def adaptive_kernel(self):
46
+ k = int(abs(math.log2(self.channels)/self.gamma) + self.b)
47
+ ksize = k if k%2 else k+1
48
+ return ksize
49
+
50
+
51
+
52
+ class UNetConvBlock(nn.Module):
53
+ def __init__(self, in_channel, out_channel, ca_layer):
54
+ super(UNetConvBlock, self).__init__()
55
+ block = []
56
+
57
+ block.append(nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=1, padding=1))
58
+ block.append(nn.PReLU())
59
+
60
+ block.append(nn.Conv2d(out_channel, out_channel, kernel_size=3, padding=1, stride=1))
61
+ block.append(nn.PReLU())
62
+
63
+ if ca_layer:
64
+ block.append(ECA(out_channel))
65
+ self.block = nn.Sequential(*block)
66
+
67
+ def forward(self, x):
68
+ out = self.block(x)
69
+ return out
70
+
71
+ class AttentionGate(nn.Module):
72
+ def __init__(self, F_g, F_l, dimensions):
73
+ super(AttentionGate, self).__init__()
74
+ self.W_gate = nn.Sequential(
75
+ nn.Conv2d(F_g, dimensions, kernel_size=1, stride=1, padding=0, bias=True),
76
+ nn.BatchNorm2d(dimensions)
77
+ )
78
+
79
+ self.W_x = nn.Sequential(
80
+ nn.Conv2d(F_l, dimensions, kernel_size=1, stride=1, padding=0, bias=True),
81
+ nn.BatchNorm2d(dimensions)
82
+ )
83
+
84
+ self.psi = nn.Sequential(
85
+ nn.Conv2d(dimensions, 1, kernel_size=1, stride=1, padding=0, bias=True),
86
+ nn.BatchNorm2d(1),
87
+ nn.Sigmoid()
88
+ )
89
+
90
+ self.relu = nn.PReLU()
91
+
92
+ def forward(self, g, x):
93
+ g1 = self.W_gate(g)
94
+ x1 = self.W_x(x)
95
+ psi = self.relu(g1 + x1)
96
+ psi = self.psi(psi)
97
+ out = x * psi
98
+ return out
99
+
100
+ class UNetUpConvBlock(nn.Module):
101
+ def __init__(self, in_channel, out_channel, upmode, ca_layer, up_factor = 2, att_mode = "standard"):
102
+ super(UNetUpConvBlock, self).__init__()
103
+ self.att_mode = att_mode
104
+ self.ca_layer = ca_layer
105
+ if upmode == 'upsample':
106
+ self.Upsize = nn.Sequential(
107
+ nn.Upsample(scale_factor=up_factor, mode='bilinear', align_corners=False),
108
+ nn.Conv2d(in_channel, out_channel, kernel_size=1, stride=1, padding=0),
109
+ )
110
+ elif upmode == 'upconv':
111
+ self.Upsize = nn.ConvTranspose2d(in_channel,out_channel,kernel_size=2,stride = 2)
112
+ elif upmode == 'shuffle':
113
+ self.Upsize = nn.Sequential(
114
+ nn.Conv2d(in_channel,out_channel*4,kernel_size=3,stride=1,padding=1),
115
+ nn.PReLU(),
116
+ nn.PixelShuffle(2),
117
+ nn.Conv2d(out_channel,out_channel,kernel_size=3,stride = 1,padding=1)
118
+ )
119
+
120
+
121
+ # self.conv = UNetConvBlock(in_channel, out_channel)
122
+ if self.att_mode == 'standard':
123
+ self.attention_gate = AttentionGate(out_channel, out_channel, out_channel)
124
+ self.conv = UNetConvBlock(in_channel, out_channel, ca_layer=self.ca_layer)
125
+ elif self.att_mode == 'modified':
126
+ self.attention_gate = AttentionGate(out_channel, out_channel, out_channel )
127
+ self.conv = UNetConvBlock(3*out_channel, out_channel, ca_layer = self.ca_layer)
128
+ elif self.att_mode == 'None':
129
+ self.conv = UNetConvBlock(in_channel, out_channel, ca_layer=self.ca_layer)
130
+
131
+
132
+
133
+ def forward(self, x, residue):
134
+ x = self.Upsize(x)
135
+ x = F.interpolate(x, size=residue.shape[2:], mode='bilinear')
136
+ if self.att_mode == "standard":
137
+ attn = self.attention_gate(g = x, x=residue)
138
+ out = torch.cat([x, attn],dim = 1)
139
+ out = self.conv(out)
140
+ elif self.att_mode == 'modified':
141
+ attn = self.attention_gate(g = x, x = residue)
142
+ out = torch.cat([x,residue,attn],dim = 1)
143
+ out = self.conv(out)
144
+ elif self.att_mode == 'None':
145
+ out = torch.cat([x,residue], dim = 1)
146
+ out = self.conv(out)
147
+ return out
148
+
149
+
150
+
151
+
152
+
153
+ class AUNet(nn.Module):
154
+ def __init__(self,in_channels = 6,out_channels = 6,depth = 3,growth_factor = 6,
155
+ interp_mode = 'bicubic', up_mode = 'upconv',spatial_attention = "standard", ca_layer = True):
156
+ super(AUNet,self).__init__()
157
+
158
+ if not spatial_attention in ['None', 'modified', 'standard']:
159
+ raise AssertionError("spatial_attention options : \'None\'- no spatial attention, \'standard\'-spatial attention as in attention unet paper, \'modified\'-modified attention unet")
160
+
161
+ self.in_channels = in_channels
162
+ self.out_channels = out_channels
163
+ self.depth = depth
164
+ self.growth_factor = growth_factor
165
+ self.interp_mode = interp_mode
166
+ prev_channels = self.in_channels
167
+ self.up_mode = up_mode
168
+ self.att_mode = spatial_attention
169
+ self.ca_layer = ca_layer
170
+
171
+ self.encoding_module = nn.ModuleList()
172
+ for i in range(self.depth):
173
+ self.encoding_module.append(UNetConvBlock(in_channel=prev_channels,out_channel=2**(self.growth_factor + i), ca_layer=self.ca_layer))
174
+ prev_channels = 2**(self.growth_factor+i)
175
+
176
+ self.decoding_module = nn.ModuleList()
177
+ for i in reversed(range(self.depth-1)):
178
+ self.decoding_module.append(UNetUpConvBlock(prev_channels,2**(self.growth_factor+i),upmode = self.up_mode, att_mode = self.att_mode, ca_layer = self.ca_layer))
179
+ prev_channels = 2**(self.growth_factor+i)
180
+
181
+ self.final = nn.Conv2d(prev_channels,out_channels,1,1,0)
182
+
183
+ def forward(self,MS,PAN = None):
184
+ if PAN == None:
185
+ x = MS
186
+ else:
187
+ x = torch.cat([MS,PAN],dim = 1)
188
+ blocks = []
189
+ for i,down in enumerate(self.encoding_module):
190
+ x = down(x)
191
+ if i != len(self.encoding_module)-1:
192
+ blocks.append(x)
193
+ x = F.avg_pool2d(x,2)
194
+
195
+ for i,up in enumerate(self.decoding_module):
196
+ x = up(x,blocks[-i-1])
197
+
198
+ x = self.final(x)
199
+ return x
200
+
201
+
202
+ if __name__ == '__main__':
203
+ x = torch.rand([9,7,256,256]).cuda()
204
+ model = AUNet(in_channels=7, out_channels=6, depth=5, spatial_attention="modified", growth_factor=6,
205
+ interp_mode='bilinear', up_mode='upconv', ca_layer=True).cuda()
206
+ x = model(x)
207
+ # print(model)
208
+
209
+
210
+ activation = {}
211
+ for layer in model:
212
+ print(layer)
AUNetConfig.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PretrainedConfig
2
+ from typing import List
3
+
4
+
5
+ class AUNetConfig(PretrainedConfig):
6
+ model_type = "s2l8hModel"
7
+ def __init__(
8
+ self,
9
+ in_channels:int = 7,
10
+ out_channels:int = 6,
11
+ depth:int = 5,
12
+ spatial_attention:str = 'None',
13
+ growth_factor:int = 6,
14
+ interp_mode:str = 'bicubic',
15
+ up_mode:str = 'upsample',
16
+ ca_layer:bool = False,
17
+ **kwargs,
18
+ ):
19
+ self.in_channels = in_channels
20
+ self.out_channels = out_channels
21
+ self.depth = depth
22
+ self.spatial_attention = spatial_attention
23
+ self.growth_factor = growth_factor
24
+ self.interp_mode = interp_mode
25
+ self.up_mode = up_mode
26
+ self.ca_layer = ca_layer
27
+
28
+ super().__init__(**kwargs)
config.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "s2l8hModel"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "AUNetConfig.AUNetConfig",
7
+ "AutoModel": "s2l8hModel.s2l8hModel"
8
+ },
9
+ "ca_layer": false,
10
+ "depth": 6,
11
+ "growth_factor": 6,
12
+ "in_channels": 7,
13
+ "interp_mode": "bicubic",
14
+ "model_type": "s2l8hModel",
15
+ "out_channels": 6,
16
+ "spatial_attention": "None",
17
+ "torch_dtype": "float32",
18
+ "transformers_version": "4.33.1",
19
+ "up_mode": "upsample"
20
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:33e653a774bf138b91e453ecd09a52cea593a036ae3c7e967f51bb707a59b2a8
3
+ size 463962395
s2l8hModel.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import PreTrainedModel
2
+ from .AUNet import AUNet
3
+ from .AUNetConfig import AUNetConfig
4
+ import torch
5
+
6
+ class s2l8hModel(PreTrainedModel):
7
+ config_class=AUNetConfig
8
+
9
+ def __init__(self, config):
10
+ super().__init__(config)
11
+ self.model = AUNet(
12
+ in_channels = config.in_channels, out_channels = config.out_channels,
13
+ depth = config.depth, spatial_attention = config.spatial_attention,
14
+ growth_factor = config.growth_factor, interp_mode = config.interp_mode,
15
+ up_mode = config.up_mode, ca_layer = config.ca_layer
16
+ )
17
+
18
+
19
+ def forward(self, MS, PAN):
20
+ return self.model.forward(MS, PAN)