-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
321 lines (256 loc) · 10 KB
/
model.py
File metadata and controls
321 lines (256 loc) · 10 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.models import resnet50, ResNet50_Weights, AlexNet, AlexNet_Weights, vgg19, VGG19_Weights
class VGG19WithFeatures(nn.Module):
"""
VGG19 with scale-variant features
Architecture similar to ResNet50WithFeatures but using VGG19 backbone
"""
def __init__(self, num_features=14, num_classes=4, pretrained=True):
super(VGG19WithFeatures, self).__init__()
# 1x1 Conv to transform 2 channels (VV, VH) to 3 channels (RGB)
self.channel_adapter = nn.Conv2d(2, 3, kernel_size=1, stride=1, padding=0)
# Load pretrained VGG19
if pretrained:
weights = VGG19_Weights.IMAGENET1K_V1
self.vgg19 = vgg19(weights=weights)
else:
self.vgg19 = vgg19(weights=None)
# Remove final classification layer
self.vgg19 = nn.Sequential(*list(self.vgg19.children())[:-1])
# Image feature dimension from VGG19
vgg_out_features = 512 * 7 * 7 # After flattening
# 3-layer dense network for scale-variant features
self.feature_net = nn.Sequential(
nn.Linear(num_features, 64),
nn.ReLU(),
nn.Linear(64, 32),
nn.ReLU(),
nn.Linear(32, 16),
nn.ReLU()
)
# Combined feature dimension
combined_features = vgg_out_features + 16
# Final classification layers with L1 regularization
self.classifier = nn.Sequential(
nn.Linear(combined_features, 128),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(128, 64),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(64, num_classes)
)
def forward(self, image, features):
"""
Forward pass
Args:
image: (B, 2, 64, 64) - VV and VH stacked
features: (B, 14) - scale-variant features
Returns:
logits: (B, num_classes)
"""
# Convert 2 channels to 3 channels
x = self.channel_adapter(image)
# Extract image features with VGG19
x = self.vgg19(x)
x = torch.flatten(x, 1) # (B, 512*7*7)
# Process scale-variant features
f = self.feature_net(features) # (B, 16)
# Concatenate features
combined = torch.cat([x, f], dim=1) # (B, 512*7*7 + 16)
# Classification
logits = self.classifier(combined)
return logits
class AlexNetWithFeatures(nn.Module):
"""
AlexNet with scale-variant features
Architecture similar to ResNet50WithFeatures but using AlexNet backbone
"""
def __init__(self, num_features=14, num_classes=4, pretrained=True):
super(AlexNetWithFeatures, self).__init__()
# 1x1 Conv to transform 2 channels (VV, VH) to 3 channels (RGB)
self.channel_adapter = nn.Conv2d(2, 3, kernel_size=1, stride=1, padding=0)
# Load pretrained AlexNet
alexnet = AlexNet(num_classes=num_classes)
# Extract only the feature extraction part (convolutional layers)
self.features = alexnet.features
# Extract the avgpool
self.avgpool = alexnet.avgpool
# Extract classifier layers except the last one
self.alexnet_classifier = nn.Sequential(*list(alexnet.classifier.children())[:-1])
# Image feature dimension from AlexNet (after the second-to-last FC layer)
alexnet_out_features = 4096
# 3-layer dense network for scale-variant features
self.feature_net = nn.Sequential(
nn.Linear(num_features, 64),
nn.ReLU(),
nn.Linear(64, 32),
nn.ReLU(),
nn.Linear(32, 16),
nn.ReLU()
)
# Combined feature dimension
combined_features = alexnet_out_features + 16
# Final classification layers
self.classifier = nn.Sequential(
nn.Linear(combined_features, 128),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(128, 64),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(64, num_classes)
)
def forward(self, image, features):
"""
Forward pass
Args:
image: (B, 2, 64, 64) - VV and VH stacked
features: (B, 14) - scale-variant features
Returns:
logits: (B, num_classes)
"""
# Convert 2 channels to 3 channels
x = self.channel_adapter(image)
# Extract image features with AlexNet
x = self.features(x) # Convolutional features
x = self.avgpool(x) # Adaptive average pooling
x = torch.flatten(x, 1) # Flatten
x = self.alexnet_classifier(x) # Through FC layers (except last)
# Process scale-variant features
f = self.feature_net(features) # (B, 16)
# Concatenate features
combined = torch.cat([x, f], dim=1) # (B, 4096 + 16)
# Classification
logits = self.classifier(combined)
return logits
class ResNet50WithFeatures(nn.Module):
"""
ResNet50 with scale-variant features
Architecture as shown in Fig. 1 of the paper
"""
def __init__(self, num_features=14, num_classes=4, pretrained=True):
super(ResNet50WithFeatures, self).__init__()
# 1x1 Conv to transform 2 channels (VV, VH) to 3 channels (RGB)
self.channel_adapter = nn.Conv2d(2, 3, kernel_size=1, stride=1, padding=0)
# Load pretrained ResNet50
if pretrained:
weights = ResNet50_Weights.IMAGENET1K_V2
self.resnet = resnet50(weights=weights)
else:
self.resnet = resnet50(weights=None)
# Remove final classification layer
self.resnet = nn.Sequential(*list(self.resnet.children())[:-1])
# Freeze early layers (fine-tune last 20 layers only)
# for i, param in enumerate(self.resnet.parameters()):
# if i < len(list(self.resnet.parameters())) - 20:
# param.requires_grad = False
# Image feature dimension from ResNet50
resnet_out_features = 2048
# 3-layer dense network for scale-variant features
self.feature_net = nn.Sequential(
nn.Linear(num_features, 64),
nn.ReLU(),
nn.Linear(64, 32),
nn.ReLU(),
nn.Linear(32, 16),
nn.ReLU()
)
# Combined feature dimension
combined_features = resnet_out_features + 16
# Final classification layers with L1 regularization
self.classifier = nn.Sequential(
nn.Linear(combined_features, 128),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(128, 64),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(64, num_classes)
)
def forward(self, image, features):
"""
Forward pass
Args:
image: (B, 2, 64, 64) - VV and VH stacked
features: (B, 14) - scale-variant features
Returns:
logits: (B, num_classes)
"""
# Convert 2 channels to 3 channels
x = self.channel_adapter(image)
# Extract image features with ResNet50
x = self.resnet(x)
x = torch.flatten(x, 1) # (B, 2048)
# Process scale-variant features
f = self.feature_net(features) # (B, 16)
# Concatenate features
combined = torch.cat([x, f], dim=1) # (B, 2048 + 16)
# Classification
logits = self.classifier(combined)
return logits
class BaselineModel(nn.Module):
"""
Baseline model from the paper (8 conv layers)
"""
def __init__(self, num_features=14, num_classes=4):
super(BaselineModel, self).__init__()
# Convolutional block (8 layers with 3 max pooling)
self.conv_block = nn.Sequential(
# Conv block 1
nn.Conv2d(2, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
# Conv block 2
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
# Conv block 3
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
# Conv block 4
nn.Conv2d(128, 256, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU()
)
# Calculate flattened size: 64x64 -> 8x8 after 3 pooling layers
conv_out_size = 256 * 8 * 8
# Feature network
self.feature_net = nn.Sequential(
nn.Linear(num_features, 64),
nn.ReLU(),
nn.Linear(64, 32),
nn.ReLU(),
nn.Linear(32, 16),
nn.ReLU()
)
# Final classifier
self.classifier = nn.Sequential(
nn.Linear(conv_out_size + 16, 128),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(128, 64),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(64, num_classes)
)
def forward(self, image, features):
# Process image
x = self.conv_block(image)
x = torch.flatten(x, 1)
# Process features
f = self.feature_net(features)
# Combine and classify
combined = torch.cat([x, f], dim=1)
logits = self.classifier(combined)
return logits