-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
439 lines (385 loc) · 16.4 KB
/
models.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# -*- coding: utf-8 -*-
# Torch
import torch.nn as nn
import torch.nn.functional as F
import torch
import torch.optim as optim
from torch.nn import init
# utils
import math
import os
import datetime
import numpy as np
#from sklearn.externals
import joblib
from tqdm import tqdm
from utils import grouper, sliding_window, count_sliding_window,\
camel_to_snake
def get_model(name, **kwargs):
"""
Instantiate and obtain a model with adequate hyperparameters
Args:
name: string of the model name
kwargs: hyperparameters
Returns:
model: PyTorch network
optimizer: PyTorch optimizer
criterion: PyTorch loss Function
kwargs: hyperparameters with sane defaults
"""
device = kwargs.setdefault('device', torch.device('cpu'))
n_classes = kwargs['n_classes']
n_bands = kwargs['n_bands']
weights = torch.ones(n_classes)
#weights[torch.LongTensor(kwargs['ignored_labels'])] = 0.
weights[torch.LongTensor(kwargs['ignored_labels'])] = 0.
weights = weights.to(device)
weights = kwargs.setdefault('weights', weights)
if name == 'hamida':
patch_size = kwargs.setdefault('patch_size', 5)
center_pixel = True
model = HamidaEtAl(n_bands, n_classes, patch_size=patch_size)
lr = kwargs.setdefault('learning_rate', 0.01)
optimizer = optim.SGD(model.parameters(), lr=lr, weight_decay=0.0005)
kwargs.setdefault('batch_size', 100)
criterion = nn.CrossEntropyLoss(weight=kwargs['weights'])
elif name == 'hamida2d':
patch_size = kwargs.setdefault('patch_size', 5)
center_pixel = True
model = HamidaEtAl2DCNN(n_bands, n_classes, patch_size=patch_size)
lr = kwargs.setdefault('learning_rate', 0.01)
optimizer = optim.SGD(model.parameters(), lr=lr, weight_decay=0.0005)
kwargs.setdefault('batch_size', 100)
criterion = nn.CrossEntropyLoss(weight=kwargs['weights'])
else:
raise KeyError("{} model is unknown.".format(name))
model = model.to(device)
epoch = kwargs.setdefault('epoch', 100)
kwargs.setdefault('scheduler', optim.lr_scheduler.ReduceLROnPlateau(optimizer, factor=0.1, patience=epoch//4, verbose=True))
kwargs.setdefault('batch_size', 100)
kwargs.setdefault('supervision', 'full')
kwargs.setdefault('flip_augmentation', False)
kwargs.setdefault('radiation_augmentation', False)
kwargs.setdefault('mixture_augmentation', False)
kwargs['center_pixel'] = center_pixel
return model, optimizer, criterion, kwargs
class Baseline(nn.Module):
"""
Baseline network
"""
@staticmethod
def weight_init(m):
if isinstance(m, nn.Linear):
init.kaiming_normal_(m.weight)
init.zeros_(m.bias)
def __init__(self, input_channels, n_classes, dropout=False):
super(Baseline, self).__init__()
self.use_dropout = dropout
if dropout:
self.dropout = nn.Dropout(p=0.5)
self.fc1 = nn.Linear(input_channels, 2048)
self.fc2 = nn.Linear(2048, 4096)
self.fc3 = nn.Linear(4096, 2048)
self.fc4 = nn.Linear(2048, n_classes)
self.apply(self.weight_init)
def forward(self, x):
x = F.relu(self.fc1(x))
if self.use_dropout:
x = self.dropout(x)
x = F.relu(self.fc2(x))
if self.use_dropout:
x = self.dropout(x)
x = F.relu(self.fc3(x))
if self.use_dropout:
x = self.dropout(x)
x = self.fc4(x)
return x
class HamidaEtAl(nn.Module):
"""
3-D Deep Learning Approach for Remote Sensing Image Classification
Amina Ben Hamida, Alexandre Benoit, Patrick Lambert, Chokri Ben Amar
IEEE TGRS, 2018
https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=8344565
"""
@staticmethod
def weight_init(m):
if isinstance(m, nn.Linear) or isinstance(m, nn.Conv3d):
init.kaiming_normal_(m.weight)
init.zeros_(m.bias)
def __init__(self, input_channels, n_classes, patch_size=5, dilation=1):
super(HamidaEtAl, self).__init__()
# The first layer is a (3,3,3) kernel sized Conv characterized
# by a stride equal to 1 and number of neurons equal to 20
self.patch_size = patch_size
self.input_channels = input_channels
dilation = (dilation, 1, 1)
if patch_size == 3:
self.conv1 = nn.Conv3d(
1, 2, (3, 3, 3), stride=(1, 1, 1), dilation=dilation, padding=1)
else:
self.conv1 = nn.Conv3d(
1, 2, (3, 3, 3), stride=(1, 1, 1), dilation=dilation, padding=0)
# Next pooling is applied using a layer identical to the previous one
# with the difference of a 1D kernel size (1,1,3) and a larger stride
# equal to 2 in order to reduce the spectral dimension
self.pool1 = nn.Conv3d(
2, 2, (3, 1, 1), dilation=dilation, stride=(2, 1, 1), padding=(1, 0, 0))
# Then, a duplicate of the first and second layers is created with
# 35 hidden neurons per layer.
self.conv2 = nn.Conv3d(
2, 2, (3, 3, 3), dilation=dilation, stride=(1, 1, 1), padding=(1, 0, 0))
self.pool2 = nn.Conv3d(
2, 2, (3, 1, 1), dilation=dilation, stride=(2, 1, 1), padding=(1, 0, 0))
# Finally, the 1D spatial dimension is progressively reduced
# thanks to the use of two Conv layers, 35 neurons each,
# with respective kernel sizes of (1,1,3) and (1,1,2) and strides
# respectively equal to (1,1,1) and (1,1,2)
self.conv3 = nn.Conv3d(
2, 2, (3, 1, 1), dilation=dilation, stride=(1, 1, 1), padding=(1, 0, 0))
self.conv4 = nn.Conv3d(
2, 2, (2, 1, 1), dilation=dilation, stride=(2, 1, 1), padding=(1, 0, 0))
#self.dropout = nn.Dropout(p=0.5)
self.features_size = self._get_final_flattened_size()
# The architecture ends with a fully connected layer where the number
# of neurons is equal to the number of input classes.
self.fc = nn.Linear(self.features_size, n_classes)
self.apply(self.weight_init)
def _get_final_flattened_size(self):
with torch.no_grad():
x = torch.zeros((1, 1, self.input_channels,
self.patch_size, self.patch_size))
x = self.pool1(self.conv1(x))
x = self.pool2(self.conv2(x))
x = self.conv3(x)
x = self.conv4(x)
_, t, c, w, h = x.size()
return t * c * w * h
def forward(self, x):
x = F.relu(self.conv1(x))
x = self.pool1(x)
x = F.relu(self.conv2(x))
x = self.pool2(x)
x = F.relu(self.conv3(x))
x = F.relu(self.conv4(x))
x = x.view(-1, self.features_size)
#x = self.dropout(x)
x = self.fc(x)
return x
class HamidaEtAl2DCNN(nn.Module):
"""
3-D Deep Learning Approach for Remote Sensing Image Classification
Amina Ben Hamida, Alexandre Benoit, Patrick Lambert, Chokri Ben Amar
IEEE TGRS, 2018
https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=8344565
"""
@staticmethod
def weight_init(m):
if isinstance(m, nn.Linear) or isinstance(m, nn.Conv2d):
init.kaiming_normal_(m.weight)
init.zeros_(m.bias)
def __init__(self, input_channels, n_classes, patch_size=5, dilation=1):
super(HamidaEtAl2DCNN, self).__init__()
# The first layer is a (3,3,3) kernel sized Conv characterized
# by a stride equal to 1 and number of neurons equal to 20
self.patch_size = patch_size
self.input_channels = input_channels
# dilation = (dil)
if patch_size == 3:
self.conv1 = nn.Conv2d(
200, 20, (3, 3), stride=(1, 1), dilation=dilation, padding=1)
else:
self.conv1 = nn.Conv2d(
200, 20, (3, 3), stride=(1, 1), dilation=dilation, padding=0)
# Next pooling is applied using a layer identical to the previous one
# with the difference of a 1D kernel size (1,1,3) and a larger stride
# equal to 2 in order to reduce the spectral dimension
self.pool1 = nn.Conv2d(
20, 20, (3, 1), dilation=dilation, stride=(2, 1), padding=(1, 0))
# Then, a duplicate of the first and second layers is created with
# 35 hidden neurons per layer.
self.conv2 = nn.Conv2d(
20, 75, (3, 3), dilation=dilation, stride=(1, 1), padding=(1, 0))
self.pool2 = nn.Conv2d(
75, 75, (3, 1), dilation=dilation, stride=(2, 1), padding=(1, 0))
# Finally, the 1D spatial dimension is progressively reduced
# thanks to the use of two Conv layers, 35 neurons each,
# with respective kernel sizes of (1,1,3) and (1,1,2) and strides
# respectively equal texo (1,1,1) and (1,1,2)
self.conv3 = nn.Conv2d(
75, 75, (3, 1), dilation=dilation, stride=(1, 1), padding=(1, 0))
self.conv4 = nn.Conv2d(
75, 75, (2, 1), dilation=dilation, stride=(2, 1), padding=(1, 0))
#self.dropout = nn.Dropout(p=0.5)
self.features_size = self._get_final_flattened_size()
# The architecture ends with a fully connected layer where the number
# of neurons is equal to the number of input classes.
self.fc = nn.Linear(self.features_size, n_classes)
self.apply(self.weight_init)
def _get_final_flattened_size(self):
with torch.no_grad():
x = torch.zeros((1, self.input_channels,
self.patch_size, self.patch_size))
x = self.pool1(self.conv1(x))
x = self.pool2(self.conv2(x))
x = self.conv3(x)
x = self.conv4(x)
print(x.shape)
_, c, w, h = x.size()
return c * w * h
def forward(self, x):
x = x.squeeze()
x = F.relu(self.conv1(x))
x = self.pool1(x)
x = F.relu(self.conv2(x))
x = self.pool2(x)
x = F.relu(self.conv3(x))
x = F.relu(self.conv4(x))
x = x.view(-1, self.features_size)
#x = self.dropout(x)
x = self.fc(x)
return x
def train(net, optimizer, criterion, data_loader, epoch, scheduler=None,
display_iter=100, device=torch.device('cpu'), display=None,
val_loader=None, supervision='full'):
"""
Training loop to optimize a network for several epochs and a specified loss
Args:
net: a PyTorch model
optimizer: a PyTorch optimizer
data_loader: a PyTorch dataset loader
epoch: int specifying the number of training epochs
criterion: a PyTorch-compatible loss function, e.g. nn.CrossEntropyLoss
device (optional): torch device to use (defaults to CPU)
display_iter (optional): number of iterations before refreshing the
display (False/None to switch off).
scheduler (optional): PyTorch scheduler
val_loader (optional): validation dataset
supervision (optional): 'full' or 'semi'
"""
if criterion is None:
raise Exception("Missing criterion. You must specify a loss function.")
net.to(device)
save_epoch = epoch // 20 if epoch > 20 else 1
losses = np.zeros(1000000)
mean_losses = np.zeros(100000000)
iter_ = 1
loss_win, val_win = None, None
val_accuracies = []
for e in tqdm(range(1, epoch + 1), desc="Training the network"):
# Set the network to training mode
net.train()
avg_loss = 0.
# Run the training loop for one epoch
for batch_idx, (data, target) in tqdm(enumerate(data_loader), total=len(data_loader)):
# Load the data into the GPU if required
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
if supervision == 'full':
output = net(data)
#target = target - 1
loss = criterion(output, target)
elif supervision == 'semi':
outs = net(data)
output, rec = outs
#target = target - 1
loss = criterion[0](output, target) + net.aux_loss_weight * criterion[1](rec, data)
else:
raise ValueError("supervision mode \"{}\" is unknown.".format(supervision))
loss.backward()
optimizer.step()
avg_loss += loss.item()
losses[iter_] = loss.item()
mean_losses[iter_] = np.mean(losses[max(0, iter_ - 100):iter_ + 1])
# Update the scheduler
avg_loss /= len(data_loader)
if val_loader is not None:
val_acc = val(net, val_loader, device=device, supervision=supervision)
val_accuracies.append(val_acc)
metric = -val_acc
else:
metric = avg_loss
if isinstance(scheduler, optim.lr_scheduler.ReduceLROnPlateau):
scheduler.step(metric)
elif scheduler is not None:
scheduler.step()
# Save the weights
if e % save_epoch == 0:
save_model(net, camel_to_snake(str(net.__class__.__name__)), data_loader.dataset.name, epoch=e, metric=abs(metric))
def save_model(model, model_name, dataset_name, **kwargs):
model_dir = './checkpoints/' + model_name + "/" + dataset_name + "/"
if not os.path.isdir(model_dir):
os.makedirs(model_dir, exist_ok=True)
if isinstance(model, torch.nn.Module):
filename = str('run') + "_epoch{epoch}_{metric:.2f}".format(**kwargs)
tqdm.write("Saving neural network weights in {}".format(filename))
torch.save(model.state_dict(), model_dir + filename + '.pth')
else:
filename = str('run')
tqdm.write("Saving model params in {}".format(filename))
joblib.dump(model, model_dir + filename + '.pkl')
def test(net, img, hyperparams):
"""
Test a model on a specific image
"""
net.eval()
patch_size = hyperparams['patch_size']
center_pixel = hyperparams['center_pixel']
batch_size, device = hyperparams['batch_size'], hyperparams['device']
n_classes = hyperparams['n_classes']
kwargs = {'step': hyperparams['test_stride'], 'window_size': (patch_size, patch_size)}
probs = np.zeros(img.shape[:2] + (n_classes,))
iterations = count_sliding_window(img, **kwargs) // batch_size
for batch in tqdm(grouper(batch_size, sliding_window(img, **kwargs)),
total=(iterations),
desc="Inference on the image"
):
with torch.no_grad():
if patch_size == 1:
data = [b[0][0, 0] for b in batch]
data = np.copy(data)
data = torch.from_numpy(data)
else:
data = [b[0] for b in batch]
data = np.copy(data)
data = data.transpose(0, 3, 1, 2)
data = torch.from_numpy(data)
data = data.unsqueeze(1)
indices = [b[1:] for b in batch]
data = data.to(device)
output = net(data)
if isinstance(output, tuple):
output = output[0]
output = output.to('cpu')
if patch_size == 1 or center_pixel:
output = output.numpy()
else:
output = np.transpose(output.numpy(), (0, 2, 3, 1))
for (x, y, w, h), out in zip(indices, output):
if center_pixel:
probs[x + w // 2, y + h // 2] += out
else:
probs[x:x + w, y:y + h] += out
return probs
def val(net, data_loader, device='cpu', supervision='full'):
# TODO : fix me using metrics()
net.eval()
accuracy, total = 0., 0.
ignored_labels = data_loader.dataset.ignored_labels
for batch_idx, (data, target) in enumerate(data_loader):
with torch.no_grad():
# Load the data into the GPU if required
data, target = data.to(device), target.to(device)
if supervision == 'full':
output = net(data)
elif supervision == 'semi':
outs = net(data)
output, rec = outs
_, output = torch.max(output, dim=1)
#target = target - 1
for pred, out in zip(output.view(-1), target.view(-1)):
if out.item() in ignored_labels:
continue
else:
accuracy += out.item() == pred.item()
total += 1
return accuracy / total