-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
327 lines (252 loc) · 9.34 KB
/
utils.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import h5py
import json
import numpy as np
import time
import torch
import torchvision
from pathlib import Path
from torch import nn
from typing import Optional, List, Tuple
from torch.utils.data import Dataset
from transformations import NSTransforms
# ========================================================
# Datasets and data-loader Navier-Stokes
# ========================================================
class RandomCrop3d(torch.nn.Module):
def __init__(self, crop_size):
super().__init__()
self.crop_size = crop_size
def forward(self, tensor):
C, T, H, W = tensor.size()
t, h, w = self.crop_size
if t > T or h > H or w > W:
raise ValueError("Crop size must be smaller than input size")
left = torch.randint(0, W - w + 1, size=(1,))
top = torch.randint(0, H - h + 1, size=(1,))
start = torch.randint(0, T - t + 1, size=(1,))
right = left + w
bottom = top + h
end = start + t
return tensor[..., start:end, top:bottom, left:right]
# This class is used as our torch augmentations where we sample transformations and apply them
class LPSNavierStokes(object):
def __init__(
self,
transforms_strength: Optional[List[float]] = [0] * 9,
steps: Optional[int] = 2,
order: Optional[int] = 2,
crop_size: Optional[Tuple[int]] = (56, 128, 128),
) -> None:
self.transforms_strength = transforms_strength
self.crop = RandomCrop3d(crop_size)
self.steps = steps
self.order = order
def __call__(
self,
sample: torch.Tensor,
) -> torch.Tensor:
x, y, t, vx, vy = sample
lie_transform = NSTransforms()
vals = []
vals.append(np.random.uniform(0, self.transforms_strength[0]))
for strength in self.transforms_strength[1:]:
vals.append(np.random.uniform(-strength, strength))
if self.steps == 0:
t_2, x_2, y_2, vx_2, vy_2 = t,x,y,vx,vy
else:
t_2, x_2, y_2, vx_2, vy_2 = lie_transform.apply(
torch.tensor(vals),
t,
x,
y,
vx,
vy,
order=self.order,
steps=self.steps,
)
image = torch.stack((x_2, y_2, t_2, vx_2, vy_2)).to(torch.float32)
image = self.crop(image)
return image
class NSDataset(Dataset):
def __init__(
self,
data_root: str = "/data/pde_arena/NavierStokes2D_cond_smoke_v1/",
transforms_strength: Optional[List[float]] = [0] * 9,
steps: Optional[int] = 2,
order: Optional[int] = 2,
mode: Optional[str] = "train",
crop_size: Optional[Tuple[int]] = (56, 64, 64),
size: Optional[int] = 100000,
):
self.data_root = Path(data_root)
self.mode = mode
self.size=size
self.transform = LPSNavierStokes(
transforms_strength=transforms_strength,
steps=steps,
order=order,
crop_size=crop_size,
)
self.h5_files = []
#Get all training files
files = []
for file in self.data_root.iterdir():
if str(file)[-2:] != "h5" or not self.mode in str(file):
continue
files.append(str(file))
#Sort files
def extract_seed(path):
path = str(path)
if "train" in path:
seed = int(path.split('_')[-3])
else:
seed = int(path.split('_')[-2])
return seed
files.sort(key=extract_seed)
#Create data
for file in files:
self.h5_files.append(h5py.File(str(file), 'r'))
self.size_per_file = 32
def __len__(self):
return len(self.h5_files * self.size_per_file)
def __getitem__(self, idx):
idx = idx % self.size
file = idx // self.size_per_file
index = idx % self.size_per_file
if self.mode == "val":
data = self.h5_files[file]['valid']
else:
data = self.h5_files[file][self.mode]
vx = torch.Tensor(data['vx'][index])
vy = torch.Tensor(data['vy'][index])
x_ = data['x'][index]
y_ = data['y'][index]
t_ = data['t'][index]
x = torch.Tensor(np.tile(np.tile(x_, (y_.shape[0], 1)), (t_.shape[0], 1, 1)))
y = torch.Tensor(np.tile(np.tile(y_, (x_.shape[0], 1)).T, (t_.shape[0], 1, 1)))
t = torch.Tensor(np.tile(t_, (x_.shape[0], y_.shape[0], 1)).T)
b = data['buo_y'][index]
sample = (x, y, t, vx, vy)
view_1 = self.transform(sample)
view_2 = self.transform(sample)
view_1 = view_1.flatten(0, 1)
view_2 = view_2.flatten(0, 1)
return view_1, view_2, np.float32(b)
def get_loader_ns(data_root, batch_size, steps, order, num_workers, strengths, mode, crop_size, dataset_size):
dataset = NSDataset(data_root, strengths, steps, order, mode=mode, crop_size=crop_size,size=dataset_size)
loader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers
)
return loader
class NSDatasetEval(Dataset):
def __init__(
self,
data_root: str = "/data/pde_arena/NavierStokes2D_cond_smoke_v1/",
mode: str = "test",
crop_size: Optional[Tuple[int]] = (56, 64, 64)
):
self.data_root = Path(data_root)
self.mode = mode
self.transform = LPSNavierStokes(
crop_size=crop_size,
order=0,
steps=0,
)
self.h5_files = []
#Get all training files
files = []
for file in self.data_root.iterdir():
if str(file)[-2:] != "h5" or not self.mode in str(file):
continue
files.append(str(file))
#Sort files
def extract_seed(path):
path = str(path)
if "train" in path:
seed = int(path.split('_')[-3])
else:
seed = int(path.split('_')[-2])
return seed
files.sort(key=extract_seed)
#Create data
for file in files:
self.h5_files.append(h5py.File(str(file),'r'))
self.size_per_file = 32
def __len__(self):
return len(self.h5_files*self.size_per_file)
def __getitem__(self, idx):
file = idx // self.size_per_file
index = idx % self.size_per_file
if self.mode == "val":
data = self.h5_files[file]['valid']
else:
data = self.h5_files[file][self.mode]
vx = torch.Tensor(data['vx'][index])
vy = torch.Tensor(data['vy'][index])
x_ = data['x'][index]
y_ = data['y'][index]
t_ = data['t'][index]
x = torch.Tensor(np.tile(np.tile(x_,(y_.shape[0],1)),(t_.shape[0],1,1)))
y = torch.Tensor(np.tile(np.tile(y_,(x_.shape[0],1)).T,(t_.shape[0],1,1)))
t = torch.Tensor(np.tile(t_,(x_.shape[0],y_.shape[0],1)).T)
b = data['buo_y'][index]
sample = (x, y, t, vx, vy)
view_1 = self.transform(sample)
view_1 = view_1.flatten(0, 1)
return view_1, np.float32(b)
def get_eval_loader_ns(data_root, batch_size, num_workers, mode,crop_size):
dataset = NSDatasetEval(data_root,mode=mode,crop_size=crop_size)
loader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
shuffle=True,
num_workers=num_workers
)
return loader
# ========================================================
# Random utils functions
# ========================================================
def log_stats(stats, writer, epoch):
for k, v in stats.items():
writer.add_scalar(k, v, epoch)
def log_imgs(imgs_to_log, writer, epoch):
legend, imgs = imgs_to_log
imgs = [torch.unsqueeze(img.detach(), dim=0) for img in imgs]
grid = torchvision.utils.make_grid(imgs, nrow=1, normalize=True, scale_each=True, padding=2, pad_value=1.0)
writer.add_image(legend, grid, epoch)
class RangeSigmoid(nn.Module):
def __init__(self, max, min):
super().__init__()
self.max = max
self.min = min
def forward(self, input):
return torch.sigmoid(input) * (self.max - self.min) + self.min
def log(folder, content, start_time):
print(f'=> Log: {content}')
# if self.rank != 0: return
cur_time = time.time()
with open(folder + '/log', 'a+') as fd:
fd.write(json.dumps({
'timestamp': cur_time,
'relative_time': cur_time - start_time,
**content
}) + '\n')
fd.flush()
def relative_error(y_pred, y):
err = torch.abs(y_pred - y) / torch.abs(y)
return torch.mean(err)
def exclude_bias_and_norm(p):
return p.ndim == 1
def off_diagonal(x):
n, m = x.shape
assert n == m
return x.flatten()[:-1].view(n - 1, n + 1)[:, 1:].flatten()