forked from PyLops/pylops_transform2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatadriven.py
343 lines (287 loc) · 10.6 KB
/
datadriven.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
import random
import numpy as np
import torch
import torch.nn as nn
import pylops
import matplotlib.pyplot as plt
from torch.utils.data import TensorDataset, DataLoader
from tqdm.notebook import tqdm
def set_seed(seed):
"""Set all random seeds to a fixed value and take out any
randomness from cuda kernels
Parameters
----------
seed : :obj:`int`
Seed number
"""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.enabled = False
return True
def init_weights(model):
if type(model) == nn.Linear:
torch.nn.init.xavier_uniform(model.weight)
if model.bias is not None:
model.bias.data.fill_(0.01)
elif type(model) == nn.Conv1d or type(model) == nn.ConvTranspose1d:
torch.nn.init.xavier_uniform(model.weight)
if model.bias is not None:
model.bias.data.fill_(0.01)
class ContractingBlock(nn.Module):
"""Contracting block
Single block in contracting path composed of two convolutions followed by a max pool operation.
We allow also to optionally include a batch normalization and dropout step.
Parameters
----------
input_channels : :obj:`int`
Number of input channels
use_dropout : :obj:`bool`, optional
Add dropout
use_bn : :obj:`bool`, optional
Add batch normalization
"""
def __init__(self, input_channels, use_dropout=False, use_bn=True):
super(ContractingBlock, self).__init__()
self.conv1 = nn.Conv1d(input_channels, input_channels * 2, kernel_size=3, padding=1)
self.conv2 = nn.Conv1d(input_channels * 2, input_channels * 2, kernel_size=3, padding=1)
self.activation = nn.LeakyReLU(0.2)
self.maxpool = nn.MaxPool1d(kernel_size=2, stride=2)
if use_bn:
self.batchnorm = nn.BatchNorm1d(input_channels * 2, momentum=0.8)
self.use_bn = use_bn
if use_dropout:
self.dropout = nn.Dropout()
self.use_dropout = use_dropout
def forward(self, x):
x = self.conv1(x)
if self.use_bn:
x = self.batchnorm(x)
if self.use_dropout:
x = self.dropout(x)
x = self.activation(x)
x = self.conv2(x)
if self.use_bn:
x = self.batchnorm(x)
if self.use_dropout:
x = self.dropout(x)
x = self.activation(x)
x = self.maxpool(x)
return x
class ExpandingBlock(nn.Module):
"""Expanding block
Single block in expanding path composed of an upsampling layer, a convolution, a concatenation of
its output with the features at the same level in the contracting path, two additional convolutions.
We allow also to optionally include a batch normalization and dropout step.
Parameters
----------
input_channels : :obj:`int`
Number of input channels
use_dropout : :obj:`bool`, optional
Add dropout
use_bn : :obj:`bool`, optional
Add batch normalization
"""
def __init__(self, input_channels, use_dropout=False, use_bn=True):
super(ExpandingBlock, self).__init__()
self.upsample = nn.Upsample(scale_factor=2, mode='nearest')#, align_corners=False)
self.conv1 = nn.Conv1d(input_channels, input_channels // 2, kernel_size=3, padding=1)
self.conv2 = nn.Conv1d(input_channels, input_channels // 2, kernel_size=3, padding=1)
self.conv3 = nn.Conv1d(input_channels // 2, input_channels // 2, kernel_size=3, padding=1)
if use_bn:
self.batchnorm = nn.BatchNorm1d(input_channels // 2, momentum=0.8)
self.use_bn = use_bn
self.activation = nn.ReLU()
if use_dropout:
self.dropout = nn.Dropout()
self.use_dropout = use_dropout
def forward(self, x, skip_con_x):
x = self.upsample(x)
x = self.conv1(x)
x = torch.cat([x, skip_con_x], axis=1)
x = self.conv2(x)
if self.use_bn:
x = self.batchnorm(x)
if self.use_dropout:
x = self.dropout(x)
x = self.activation(x)
x = self.conv3(x)
if self.use_bn:
x = self.batchnorm(x)
if self.use_dropout:
x = self.dropout(x)
x = self.activation(x)
return x
class FeatureMapBlock(nn.Module):
"""Feature Map block
Final layer of U-Net which restores for the output channel dimensions to those of the input (or any other size)
using a 1x1 convolution.
Parameters
----------
input_channels : :obj:`int`
Number of input channels
output_channels : :obj:`int`
Number of output channels
"""
def __init__(self, input_channels, output_channels):
super(FeatureMapBlock, self).__init__()
self.conv = nn.Conv1d(input_channels, output_channels, kernel_size=1)
def forward(self, x):
x = self.conv(x)
return x
class UNet(nn.Module):
"""UNet architecture
UNet architecture composed of a series of contracting blocks followed by expanding blocks.
Most UNet implementations available online hard-code a certain number of levels. Here,
the number of levels for the contracting and expanding paths can be defined by the user and the
UNet is built in such a way that the same code can be used for any number of levels without modification.
Parameters
----------
input_channels : :obj:`int`
Number of input channels
output_channels : :obj:`int`, optional
Number of output channels
hidden_channels : :obj:`int`, optional
Number of hidden channels of first layer
levels : :obj:`int`, optional
Number of levels in encoding and deconding paths
"""
def __init__(self, input_channels=1, output_channels=1, hidden_channels=64, levels=2):
super(UNet, self).__init__()
self.levels = levels
self.upfeature = FeatureMapBlock(input_channels, hidden_channels)
self.contract = []
self.expand = []
for level in range(levels):
self.contract.append(ContractingBlock(hidden_channels * (2 ** level),
use_dropout=False, use_bn=False))
for level in range(levels):
self.expand.append(ExpandingBlock(hidden_channels * (2 ** (levels - level)),
use_dropout=False, use_bn=False))
self.contracts = nn.Sequential(*self.contract)
self.expands = nn.Sequential(*self.expand)
self.downfeature = FeatureMapBlock(hidden_channels, output_channels)
def forward(self, x):
xenc = []
x = self.upfeature(x)
xenc.append(x)
for level in range(self.levels):
x = self.contract[level](x)
xenc.append(x)
for level in range(self.levels):
x = self.expand[level](x, xenc[self.levels - level - 1])
xn = self.downfeature(x)
return xn
def create_reflectivity_and_data(nspikes, ampmax, nt, twav, f0=20):
nsp = np.random.randint(nspikes[0], nspikes[1])
spikes = np.random.uniform(10, nt-10, size=nsp).astype(int)
amps = np.random.uniform(-ampmax, ampmax, size=nsp)
x = np.zeros(nt)
x[spikes] = amps
h, th, hcenter = pylops.utils.wavelets.ricker(twav, f0=f0)
Cop = pylops.signalprocessing.Convolve1D(nt, h=h, offset=hcenter, dtype="float32")
y = Cop * x
#h1, th, hcenter1 = pylops.utils.wavelets.ricker(twav, f0=f0+10)
#C1op = pylops.signalprocessing.Convolve1D(nt, h=h1, offset=hcenter1, dtype="float32")
#x = C1op * x
return x, y
def train(model, criterion, optimizer, data_loader, device='cpu', plotflag=False):
"""Training step
Perform a training step over the entire training data (1 epoch of training)
Parameters
----------
model : :obj:`torch.nn.Module`
Model
criterion : :obj:`torch.nn.modules.loss`
Loss function
optimizer : :obj:`torch.optim`
Optimizer
data_loader : :obj:`torch.utils.data.dataloader.DataLoader`
Training dataloader
device : :obj:`str`, optional
Device
plotflag : :obj:`bool`, optional
Display intermediate results
Returns
-------
loss : :obj:`float`
Loss over entire dataset
accuracy : :obj:`float`
Accuracy over entire dataset
"""
model.train()
loss = 0
for X, y in data_loader:#tqdm(data_loader):
optimizer.zero_grad()
X, y = X.unsqueeze(1), y.unsqueeze(1)
ypred = model(X)
ls = criterion(ypred.view(-1), y.view(-1))
ls.backward()
optimizer.step()
loss += ls.item()
loss /= len(data_loader)
if plotflag:
fig, ax = plt.subplots(1, 1, figsize=(14, 4))
ax.plot(y.detach().squeeze()[:5].T, "k")
ax.plot(ypred.detach().squeeze()[:5].T, "r")
ax.set_xlabel("t")
plt.show()
return loss
def evaluate(model, criterion, data_loader, device='cpu', plotflag=False):
"""Evaluation step
Perform an evaluation step over the entire training data
Parameters
----------
model : :obj:`torch.nn.Module`
Model
criterion : :obj:`torch.nn.modules.loss`
Loss function
data_loader : :obj:`torch.utils.data.dataloader.DataLoader`
Evaluation dataloader
device : :obj:`str`, optional
Device
plotflag : :obj:`bool`, optional
Display intermediate results
Returns
-------
loss : :obj:`float`
Loss over entire dataset
accuracy : :obj:`float`
Accuracy over entire dataset
"""
model.train() # not eval because https://github.com/facebookresearch/SparseConvNet/issues/166
loss = 0
for X, y in data_loader:#tqdm(data_loader):
X, y = X.unsqueeze(1), y.unsqueeze(1)
with torch.no_grad():
ypred = model(X)
ls = criterion(ypred.view(-1), y.view(-1))
loss += ls.item()
loss /= len(data_loader)
if plotflag:
fig, ax = plt.subplots(1, 1, figsize=(14, 4))
ax.plot(y.detach().squeeze()[:5].T, "k")
ax.plot(ypred.detach().squeeze()[:5].T, "r")
ax.set_xlabel("t")
plt.show()
return loss
def predict(model, X, device='cpu'):
"""Prediction step
Perform a prediction over a batch of input samples
Parameters
----------
model : :obj:`torch.nn.Module`
Model
X : :obj:`torch.tensor`
Inputs
X : :obj:`torch.tensor`
Masks
device : :obj:`str`, optional
Device
"""
model.train() # not eval because https://github.com/facebookresearch/SparseConvNet/issues/166
with torch.no_grad():
ypred = model(X)
return ypred