-
Notifications
You must be signed in to change notification settings - Fork 9
/
annotation-cli.py
executable file
·316 lines (232 loc) · 11.9 KB
/
annotation-cli.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import numpy as np
import torch
from torch import nn, Tensor
from imle.wrapper import imle
from imle.target import TargetDistribution
from imle.noise import SumOfGammaNoiseDistribution
from solvers.dijkstra import get_solver
import multiprocessing
import ray
import random
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import animation
def set_seed(seed: int, is_deterministic: bool = True):
# set the seeds
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
if is_deterministic is True:
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
return
def maybe_parallelize(function, arg_list):
if ray.is_initialized():
ray_fn = ray.remote(function)
return ray.get([ray_fn.remote(arg) for arg in arg_list])
else:
return [function(arg) for arg in arg_list]
class HammingLoss(torch.nn.Module):
def forward(self, suggested, target):
errors = suggested * (1.0 - target) + (1.0 - suggested) * target
return errors.mean(dim=0).sum()
def main(argv):
neighbourhood_fn = "8-grid"
solver = get_solver(neighbourhood_fn)
grid_size = [16, 16]
def torch_solver(weights_batch: Tensor) -> Tensor:
r"""
Wrapper around the `solver` function, which implements Dijkstra's Shortest Path Algorithm.
Note that I-MLE assumes that the solver solves a maximisation problem, but here the `solver` function solves
a minimisation problem, by finding the path that minimises the total travel cost. As a consequence, here
we flip the sign of the weights: `solver` will find the path with minimal total cost, while `torch_solver`
will find the path that maximises the total gain (all weights are now negative).
Args:
weights_batch (Tensor): PyTorch tensor with shape [BATCH_SIZE, MAP_WIDTH, MAP_HEIGHT]
"""
weights_batch = - 1.0 * weights_batch.detach().cpu().numpy()
# y_batch = np.asarray([solver(w) for w in list(weights_batch)])
y_batch = np.asarray(maybe_parallelize(solver, arg_list=list(weights_batch)))
return torch.tensor(y_batch, requires_grad=False)
with torch.inference_mode():
weights = np.empty(shape=[1] + grid_size, dtype=float)
weights.fill(-1)
weights_batch = torch.tensor(weights)
y_batch = torch_solver(weights_batch)
loss_fn = HammingLoss()
def generate_distribution(input_noise_temperature: float = 5.0):
weights = np.empty(shape=[1] + grid_size, dtype=float)
weights.fill(-1)
weights[0, 1:6, 0:12] = -100
weights[0, 8:12, 1:] = -100
weights[0, 14:, 6:10] = -100
weights_tensor = torch.tensor(weights)
weights_params = nn.Parameter(weights_tensor, requires_grad=True)
y_tensor = torch.tensor(y_batch.detach().cpu().numpy())
target_distribution = TargetDistribution(alpha=1.0, beta=10.0)
noise_distribution = SumOfGammaNoiseDistribution(k=grid_size[0] * 1.3, nb_iterations=100)
# imle_solver = imle(torch_solver,
# target_distribution=target_distribution,
# noise_distribution=noise_distribution,
# input_noise_temperature=input_noise_temperature,
# target_noise_temperature=5.0)
@imle(target_distribution=target_distribution, noise_distribution=noise_distribution,
input_noise_temperature=input_noise_temperature, target_noise_temperature=5.0)
def imle_solver(weights_batch: Tensor) -> Tensor:
return torch_solver(weights_batch)
imle_y_tensor = imle_solver(weights_params)
loss = loss_fn(imle_y_tensor, y_tensor)
loss.backward()
return weights, imle_y_tensor, y_tensor, weights_params
# Inference
weights, imle_y_tensor, y_tensor, weights_params = generate_distribution(0.0)
sns.set_theme()
ax = sns.heatmap(weights[0])
ax.set_title(f'Map')
fig = ax.get_figure()
fig.savefig("figures/map.png")
plt.clf()
sampled_paths_lst = []
for i in range(110):
weights, imle_y_tensor, y_tensor, weights_params = generate_distribution(1.0)
sampled_paths_lst += [imle_y_tensor[0].detach().cpu().numpy()]
def init_fwd():
nonlocal sampled_paths_lst
plt.clf()
sns.set_theme()
ax = sns.heatmap(sampled_paths_lst[0]) #, vmin=0.0, vmax=1.0)
ax.set_title(f'Sampled path -- temperature 1.0, iteration: {0}')
def animate_fwd(i):
nonlocal sampled_paths_lst
plt.clf()
sns.set_theme()
ax = sns.heatmap(sampled_paths_lst[i]) #, vmin=0.0, vmax=1.0)
ax.set_title(f'Sampled path -- temperature 1.0, iteration: {i}')
fig = plt.figure()
anim = animation.FuncAnimation(fig, animate_fwd, init_func=init_fwd, frames=100, repeat=False)
anim.save('figures/paths.gif', writer='imagemagick', fps=8)
plt.clf()
def init_fwd():
nonlocal sampled_paths_lst
plt.clf()
sns.set_theme()
ax = sns.heatmap(sampled_paths_lst[0]) #, vmin=0.0, vmax=1.0)
ax.set_title(f'Distribution over paths -- temperature 1.0, iteration: {0}')
def animate_fwd(i):
nonlocal sampled_paths_lst
plt.clf()
sns.set_theme()
ax = sns.heatmap(np.mean(sampled_paths_lst[:i + 1], axis=0)) #, vmin=0.0, vmax=1.0)
ax.set_title(f'Distribution over paths -- temperature 1.0, iteration: {i}')
fig = plt.figure()
anim = animation.FuncAnimation(fig, animate_fwd, init_func=init_fwd, frames=100, repeat=False)
anim.save('figures/distribution.gif', writer='imagemagick', fps=8)
plt.clf()
def learning(argv):
set_seed(0)
neighbourhood_fn = "8-grid"
solver = get_solver(neighbourhood_fn)
grid_size = [16, 16]
def torch_solver(weights_batch: Tensor) -> Tensor:
r"""
Wrapper around the `solver` function, which implements Dijkstra's Shortest Path Algorithm.
Note that I-MLE assumes that the solver solves a maximisation problem, but here the `solver` function solves
a minimisation problem, by finding the path that minimises the total travel cost. As a consequence, here
we flip the sign of the weights: `solver` will find the path with minimal total cost, while `torch_solver`
will find the path that maximises the total gain (all weights are now negative).
Args:
weights_batch (Tensor): PyTorch tensor with shape [BATCH_SIZE, MAP_WIDTH, MAP_HEIGHT]
"""
weights_batch = - 1.0 * weights_batch.detach().cpu().numpy()
# y_batch = np.asarray([solver(w) for w in list(weights_batch)])
y_batch = np.asarray(maybe_parallelize(solver, arg_list=list(weights_batch)))
return torch.tensor(y_batch, requires_grad=False)
for input_noise_temperature in [0.0, 1.0, 2.0, 5.0]:
for target_noise_temperature in [0.0, 1.0, 2.0, 5.0]:
for nb_samples in [1, 10, 100]:
# Gradients
true_weights = np.empty(shape=[1] + grid_size, dtype=float)
true_weights.fill(-1)
true_weights[0, 1:6, 0:12] = -100
true_weights[0, 8:12, 1:] = -100
true_weights[0, 14:, 6:10] = -100
true_y = torch_solver(torch.tensor(true_weights)).detach()
plt.clf()
sns.set_theme()
ax = sns.heatmap(np.copy(true_y[0].cpu().numpy()))
ax.set_title(f'Gold path')
fig = ax.get_figure()
fig.savefig(f"figures/gold_int={input_noise_temperature}_tnt={target_noise_temperature}_ns={nb_samples}.png")
weights = np.random.uniform(low=-0.1, high=0.1, size=[1] + grid_size)
weights_tensor = torch.tensor(weights, dtype=torch.float)
weights_params = nn.Parameter(weights_tensor, requires_grad=True)
optimizer = torch.optim.Adam([weights_params], lr=0.005)
evolving_weights_lst = []
evolving_paths_lst = []
loss_fn = HammingLoss()
for t in range(1100):
target_distribution = TargetDistribution(alpha=1.0, beta=10.0)
noise_distribution = SumOfGammaNoiseDistribution(k=grid_size[0] * 1.3, nb_iterations=100)
@imle(target_distribution=target_distribution,
noise_distribution=noise_distribution,
input_noise_temperature=input_noise_temperature,
target_noise_temperature=target_noise_temperature,
nb_samples=nb_samples)
def imle_solver(weights_batch: Tensor) -> Tensor:
return torch_solver(weights_batch)
imle_y_tensor = imle_solver(weights_params)
evolving_weights_lst += [np.copy(weights_params[0].detach().cpu().numpy())]
evolving_paths_lst += [np.copy(imle_y_tensor[0].detach().cpu().numpy())]
loss = loss_fn(imle_y_tensor, true_y)
if t % 10:
print(f"Iteration: {t}\tLoss: {loss.item():.2f}")
optimizer.zero_grad()
loss.backward()
optimizer.step()
def init_paths():
nonlocal evolving_paths_lst
plt.clf()
sns.set_theme()
ax = sns.heatmap(evolving_paths_lst[0]) #, vmin=0.0, vmax=1.0)
ax.set_title(f'Inferred path -- Input noise {input_noise_temperature}, '
f'target noise {target_noise_temperature}, iteration: {0}')
def animate_paths(i):
nonlocal evolving_paths_lst
plt.clf()
sns.set_theme()
ax = sns.heatmap(evolving_paths_lst[i * 10]) #, vmin=0.0, vmax=1.0)
ax.set_title(f'Inferred path -- Input noise {input_noise_temperature}, '
f'target noise {target_noise_temperature}, iteration: {i}')
fig = plt.figure()
anim = animation.FuncAnimation(fig, animate_paths, init_func=init_paths, frames=100, repeat=False)
anim.save(f'figures/learning_paths_int={input_noise_temperature}_tnt={target_noise_temperature}_ns={nb_samples}.gif',
writer='imagemagick', fps=8)
def init_weights():
nonlocal evolving_paths_lst
plt.clf()
sns.set_theme()
ax = sns.heatmap(evolving_weights_lst[0]) #, vmin=0.0, vmax=1.0)
ax.set_title(f'Inferred weights -- Input noise {input_noise_temperature}, '
f'target noise {target_noise_temperature}, iteration: {0}')
def animate_weights(i):
nonlocal evolving_paths_lst
plt.clf()
sns.set_theme()
ax = sns.heatmap(evolving_weights_lst[i * 10]) #, vmin=0.0, vmax=1.0)
ax.set_title(f'Inferred weights -- Input noise {input_noise_temperature}, '
f'target noise {target_noise_temperature}, iteration: {i}')
fig = plt.figure()
anim = animation.FuncAnimation(fig, animate_weights, init_func=init_weights, frames=100, repeat=False)
anim.save(f'figures/learning_weights_int={input_noise_temperature}_tnt={target_noise_temperature}_ns={nb_samples}.gif',
writer='imagemagick', fps=8)
plt.clf()
if __name__ == '__main__':
ray.init(num_cpus=multiprocessing.cpu_count())
main(sys.argv[1:])
learning(sys.argv[1:])