-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
188 lines (147 loc) · 4.66 KB
/
utils.py
File metadata and controls
188 lines (147 loc) · 4.66 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
import os
import random
from types import SimpleNamespace
import numpy as np
import scipy
import scipy.interpolate
import torch
from torch import nn
from torch.profiler import ProfilerActivity, profile, record_function
class AttrDict(dict):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__dict__ = self
for k, v in self.items():
if isinstance(v, dict):
self[k] = AttrDict(v)
def seed_worker(worker_id):
worker_seed = torch.initial_seed() % 2**32
np.random.seed(worker_seed)
random.seed(worker_seed)
def seed(seed):
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.use_deterministic_algorithms(True)
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
def get_activation_class(activation):
if activation is None or activation == "identity":
return nn.Identity
if activation == "relu":
return nn.ReLU
elif activation == "tanh":
return nn.Tanh
elif activation == "sigmoid":
return nn.Sigmoid
elif activation == "softplus":
return nn.Softplus
elif activation == "softsign":
return nn.Softsign
elif activation == "elu":
return nn.ELU
elif activation == "selu":
return nn.SELU
elif activation == "gelu":
return nn.GELU
elif activation == "leaky_relu":
return nn.LeakyReLU
else:
raise ValueError(f"Activation function {activation} not supported.")
def idx_1D_to_2D(x, m, n):
"""
Convert a 1D index to a 2D index.
Args:
x (torch.Tensor): 1D index.
Returns:
torch.Tensor: 2D index.
"""
return torch.stack((x // m, x % n))
def idx_2D_to_1D(x, m, n):
"""
Convert a 2D index to a 1D index.
Args:
x (torch.Tensor): 2D index.
Returns:
torch.Tensor: 1D index.
"""
return x[0] * n + x[1]
def print_mem_stats():
f, t = torch.cuda.mem_get_info()
print(f"Free/Total: {f/(1024**3):.2f}GB/{t/(1024**3):.2f}GB")
def count_parameters(model):
total_params = 0
for param in model.parameters():
num_params = (
param._nnz()
if param.layout in (torch.sparse_coo, torch.sparse_csr, torch.sparse_csc)
else param.numel()
)
total_params += num_params
return total_params
def profile_fn(fn, kwargs, sort_by="cuda_time_total", row_limit=50):
with profile(
activities=[ProfilerActivity.CPU, ProfilerActivity.CUDA],
record_shapes=True,
) as prof:
fn(kwargs)
return prof.key_averages.table(sort_by=sort_by, row_limit=row_limit)
def r_theta_mp(data):
tmp = np.exp(data[0] + 1j * data[1]) - 0.5
return np.abs(tmp), np.angle(tmp)
def normalize_for_mp(indices, N_x=150, N_y=300, retina_radius=80):
x, y = indices
normalized_x = (1 - (x) / retina_radius) * 2.4 - 0.6
normalized_y = ((y - N_y // 2) / np.sqrt(retina_radius**2.0)) * 3.5
return normalized_x, normalized_y
def flatten_indices(indices, N_y=300):
return indices[0] * N_y + indices[1]
def image2v1(
image,
retina_indices,
image_top_corner=(4, 4),
N_x=150,
N_y=300,
retina_radius=80,
):
image_x, image_y = image.shape[1:] # (C, H, W)
img_ind = np.zeros((2, image_x, image_y))
img_ind[0, :, :] = (
np.tile(0 + np.arange(image_x), (image_y, 1)).T / image_x * image_top_corner[0]
)
img_ind[1, :, :] = (
np.tile(np.arange(image_y) - image_y // 2, (image_x, 1))
/ image_y
* image_top_corner[1]
* 2
)
flat_img_ind = img_ind.reshape((2, image_x * image_y))
normed_indices_retina = normalize_for_mp(retina_indices, N_x, N_y, retina_radius)
r_indices, theta_indices = r_theta_mp(normed_indices_retina)
v_field_x = r_indices * np.cos(theta_indices)
v_field_y = r_indices * np.sin(theta_indices)
device = image.device
image = image.cpu().numpy()
if len(image.shape) == 3:
img_on_vfield = [
scipy.interpolate.griddata(
flat_img_ind.T,
im.flatten(),
np.array((v_field_x, v_field_y)).T,
)
for im in image
]
img_on_vfield = np.stack(img_on_vfield)
else:
img_on_vfield = scipy.interpolate.griddata(
flat_img_ind.T,
image[0].flatten(),
np.array((v_field_x, v_field_y)).T,
)
img_on_vfield = torch.from_numpy(img_on_vfield).to(device).float()
img_on_vfield = torch.nan_to_num(img_on_vfield)
return img_on_vfield
def compact(l):
return list(filter(None, l))
def rescale(x):
return x * 2 - 1