-
Notifications
You must be signed in to change notification settings - Fork 2
/
changestar_1x96.py
198 lines (164 loc) · 6.56 KB
/
changestar_1x96.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
from mit import SiameseMiTEncoder
from functools import partial
from typing import Dict
import ever as er
import ever.module as M
@er.registry.MODEL.register()
class SiameseFarSegEncoder(M.ResNetEncoder):
def __init__(self, config):
super().__init__(config)
max_channels = 512
self.fpn = M.FPN([max_channels // (2 ** (3 - i)) for i in range(4)], 256)
self.fsr = M.FSRelation(max_channels, [256 for _ in range(4)], 256, True)
self.dec = M.AssymetricDecoder(256, self.config.out_channels)
def forward(self, inputs):
x = rearrange(inputs, 'b (t c) h w -> (b t) c h w', t=2)
bi_features = super().forward(x)
coarsest_features = bi_features[-1]
scene_embedding = F.adaptive_avg_pool2d(coarsest_features, 1)
bi_features = self.fpn(bi_features)
bi_features = self.fsr(scene_embedding, bi_features)
bi_features = self.dec(bi_features)
t1_features, t2_features = rearrange(bi_features, '(b t) c h w -> t b c h w', t=2)
return t1_features, t2_features
def set_default_config(self):
super().set_default_config()
self.config.update(dict(
out_channels=96,
))
@er.registry.MODEL.register()
class SiameseMiTFarSegEncoder(SiameseMiTEncoder):
def __init__(self, config):
super().__init__(config)
encoder_channels = self.out_channels()
fpn_channels = self.config.fpn_channels
self.fpn = M.FPN(encoder_channels, fpn_channels)
self.fsr = M.FSRelation(encoder_channels[-1], [fpn_channels for _ in range(4)], fpn_channels, True)
self.dec = M.AssymetricDecoder(fpn_channels, self.config.out_channels)
def forward(self, x):
x = rearrange(x, 'b (t c) h w -> (b t) c h w', t=2)
bi_features = self.features(x)
coarsest_features = bi_features[-1]
scene_embedding = F.adaptive_avg_pool2d(coarsest_features, 1)
bi_features = self.fpn(bi_features)
bi_features = self.fsr(scene_embedding, bi_features)
bi_features = self.dec(bi_features)
t1_features, t2_features = rearrange(bi_features, '(b t) c h w -> t b c h w', t=2)
return t1_features, t2_features
def set_default_config(self):
super().set_default_config()
self.config.update(dict(
fpn_channels=256,
out_channels=96,
))
@er.registry.MODEL.register()
class ChangeMixinBiSupN1(er.ERModule):
def __init__(self, config):
super().__init__(config)
k = self.config.conv_k
d = self.config.conv_d
self.conv = M.ConvBlock(self.config.in_channels,
self.config.out_channels,
k,
stride=1,
padding=d * (k - 1) // 2,
dilation=d,
bias=False,
bn=self.config.bn,
relu=self.config.relu)
def forward(self, t1_feature, t2_feature):
pre_logit = self.conv(torch.cat([t1_feature, t2_feature], dim=1))
if self.config.temporal_symmetric:
pre_logit = pre_logit + self.conv(torch.cat([t2_feature, t1_feature], dim=1))
features = {'t1': t1_feature,
't2': t2_feature,
'change': pre_logit
}
if self.config.return_type == 'tuple':
return tuple(features[key] for key in self.config.returns)
elif self.config.return_type == 'dict':
return {key: features[key] for key in self.config.returns}
else:
raise NotImplementedError
def set_default_config(self):
self.config.update(dict(
in_channels=96 * 2,
out_channels=96,
conv_k=3,
conv_d=1,
bn=True,
relu=True,
returns=['change'],
return_type='tuple',
temporal_symmetric=False,
))
@er.registry.MODEL.register()
class ConvUpsampleHead(er.ERModule):
def __init__(self, config):
super().__init__(config)
self.change_conv = M.ConvUpsampling(**self.config.change_conv)
def forward(self, features: Dict[str, torch.Tensor], y=None):
if 'change' in features:
logitc = self.change_conv(features['change'])
return self.binary(logitc, y)
def binary(self, logitc, y=None):
return {
'change_prediction': self.activation_fn(self.config.activation.change)(logitc),
}
def set_default_config(self):
self.config.update(dict(
loss=dict(),
activation=dict(
change='sigmoid'
),
change_conv=dict(
in_channels=128,
out_channels=1,
scale_factor=4.,
kernel_size=1,
),
))
@staticmethod
def activation_fn(act_type):
if act_type == 'sigmoid':
return torch.sigmoid
elif act_type == 'softmax':
return partial(torch.softmax, dim=1)
raise NotImplementedError
@er.registry.MODEL.register()
class EncoderDecoder(er.ERModule):
def __init__(self, config):
super(EncoderDecoder, self).__init__(config)
self.encoder = er.registry.MODEL[self.config.encoder.type](self.config.encoder.params)
self.decoder = er.registry.MODEL[self.config.decoder.type](self.config.decoder.params)
self.head = er.registry.MODEL[self.config.head.type](self.config.head.params)
self.init_from_weight_file()
def forward(self, x, y=None):
bitemporal_features = self.encoder(x)
if isinstance(bitemporal_features, tuple):
decoder_out = self.decoder(*bitemporal_features)
else:
decoder_out = self.decoder(bitemporal_features)
if isinstance(decoder_out, tuple):
return self.head(*decoder_out, y=y)
else:
return self.head(decoder_out, y=y)
def set_default_config(self):
self.config.update(dict(
encoder=dict(type='', params=dict()),
decoder=dict(type='', params=dict()),
head=dict(type='', params=dict()),
))
def changestar_1x96_r18():
m = _make_model('cstar_r18_farseg_d96')
return m
def changestar_1x96_mitb1():
m = _make_model('cstar_mitb1_farseg_d96')
return m
def _make_model(config_name) -> nn.Module:
cfg = er.config.import_config(config_name)
return er.builder.make_model(cfg.model)