-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats_utils.py
More file actions
397 lines (325 loc) · 11.3 KB
/
stats_utils.py
File metadata and controls
397 lines (325 loc) · 11.3 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
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
import os
import torch
import numpy as np
from einops import rearrange
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
from src import Unet
from torch.nn import DataParallel
from train_utils import split_data
import torch.nn as nn
from pathlib import Path
from lpips import ModifiedLPIPS
from typing import List, Union, Callable, Tuple
"""
Note: Assume samples are between (-1, 1)
"""
def get_unet(args, data_parallel: bool = True) -> nn.Module:
"""Create a UNet model with the given arguments
Args:
args: Command line arguments
data_parallel: Whether to use DataParallel for the model
Returns:
model: A UNet model
"""
model = Unet(
image_size=args.model.image_size,
in_channels=args.model.in_channels,
dim=args.model.dim,
dim_mults=args.model.dim_mults,
attn_resolutions=args.model.attn_resolutions,
num_res_blocks=args.model.num_res_blocks,
dropout=args.model.dropout,
conditional=args.model.conditional,
resamp_with_conv=args.model.resamp_with_conv,
nonlinearity=args.model.nonlinearity,
scale_by_sigma=args.model.scale_by_sigma,
)
if data_parallel:
model = DataParallel(model)
return model
def create_dirs(paths: Union[str, List[str]]):
"""Create directories giving a list of paths
Args:
paths: a str or a list of strs indicating path(s) to create directories
"""
if type(paths) == str:
os.makedirs(paths, exist_ok=True)
elif type(paths) == list:
for path in paths:
os.makedirs(path, exist_ok=True)
else:
raise NotImplementedError("Must be a str or a sequence of strs.")
def remove_pth(path: str) -> str:
"""Remove pth or pt extension from a string"""
if path.endswith(".pth"):
return path[:-4]
elif path.endswith(".pt"):
return path[:-3]
else:
raise Exception("Path does not end with .pth or .pt")
def sort_files(files: List[str]) -> List[str]:
"""Sort a bunch of strings using their suffix where we assume the suffix is a number
E.g.,
./ckpts ./ckpts
/unet_5.pt => /unet_1.pt
/unet_1.pt /unet_5.pt
"""
files.sort(key=lambda file: int(Path(file).name.split(".")[0]))
return files
def to_zero_one(x: torch.Tensor) -> torch.Tensor:
"""Convert a (-1, 1) tensor to (0, 1) scale"""
if x is None:
return x
x.mul_(0.5).add_(0.5)
return x
def convert_dataset(train_loader: DataLoader) -> torch.Tensor:
"""Take a data iterator (with no shuffle enabled) and concat all of its samples"""
xs = []
for x, _ in train_loader:
if len(x.shape) == 3:
x = x[None]
xs.append(x)
return torch.cat(xs, dim=0)
def get_train_loader(
dataset, batch_size: int, args, shuffle: bool = False
) -> DataLoader:
"""Given the Dataset object, return a dataloader of the training set of some given split size and batch size
Args:
dataset: Dataset object (e.g., MNIST, CIFAR10, etc.)
batch_size: An integer determining the batch size
args: Command line argument which includes a manual seed value and split sizes
Notes: we set num_workers to 0, and pin_memory and drop_last to False
"""
train_data, _ = split_data(
dataset,
args.train.train_size,
args.train.valid_size,
args.train.global_seed,
)
train_loader = DataLoader(
train_data,
batch_size=batch_size,
shuffle=shuffle,
num_workers=0,
pin_memory=False,
drop_last=False,
)
return train_loader
@torch.no_grad()
def compute_norm(
x1: torch.Tensor, x2: torch.Tensor, unsqueeze: bool = True
) -> torch.Tensor:
"""Compute the l2 distance between two tensors
Args:
x1: Tensor of size b x ...
x2: Tensor of size b x ...
unsqueeze: Whether to add an extra dimension to the tensors
Returns:
Tensor of size b1 x b2
"""
# convert from b x ... to b x (...)
x1, x2 = map(lambda z: torch.flatten(z, start_dim=1), (x1, x2))
if unsqueeze:
x1 = x1[:, None]
x2 = x2[None]
# b1 x b2 x (...)
diff = x1 - x2
# b1 x b2
return 1.0 / (torch.norm(diff, dim=-1) + 1e-8)
def get_metric_fn(
use_lpips: bool,
network: str = "alex",
unsqueeze: bool = True,
rescale: bool = True,
device: str = "cuda",
) -> Callable:
"""Get the metric function for computing distances between two tensors
Args:
use_lpips: Whether to use LPIPS or not
network: The network to use for LPIPS
unsqueeze: Whether to add an extra dimension to the tensors
rescale: Whether to rescale the tensors
device: The device to use for computation
Returns:
metric_fn: A function which takes two tensors and computes their distance
"""
if not rescale:
rescale_fn = lambda z: z
else:
rescale_fn = lambda z: z * 0.5 + 0.5
if use_lpips:
metric = ModifiedLPIPS(network=network, reduction="none").to(device)
metric_fn = lambda x, y: metric(x, y, fn=rescale_fn, unsqueeze=unsqueeze)
else:
metric_fn = lambda x, y: compute_norm(x, y, unsqueeze=unsqueeze)
return metric_fn
@torch.no_grad()
def compute_metrics(
m_top1_dists: torch.Tensor,
s_top1_dists: torch.Tensor,
deltas: Tuple[float, float],
device: str = "cuda",
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""
Compute the memorized, spurious, and generalized binary scores
Args:
m_top1_dists: Tensor of size B
s_top1_dists: Tensor of size B
deltas: Tuple of two floats (delta_m, delta_s)
device: Device to use for computation
Returns:
m_bins: Tensor of size B
s_bins: Tensor of size B
g_bins: Tensor of size B
"""
delta_m, delta_s = deltas
if device is not None:
m_top1_dists, s_top1_dists = map(
lambda z: z.to(device), (m_top1_dists, s_top1_dists)
)
# compute memorized scores
m_bins = torch.where(m_top1_dists <= delta_m, 1, 0)
# compute spurious scores
s_bins = torch.where(s_top1_dists <= delta_s, 1, 0)
neg_m_bins = m_bins ^ 1
s_bins = torch.logical_and(neg_m_bins, s_bins)
# compute generalized_scores
g_bins = torch.logical_and(neg_m_bins, s_bins ^ 1)
m_bins, s_bins, g_bins = map(lambda z: z.float().cpu(), (m_bins, s_bins, g_bins))
return m_bins, s_bins, g_bins
def get_nonzero_entries(targets: torch.Tensor, binaries: torch.Tensor) -> torch.Tensor:
"""
Given a binary vector of scores, find indices non-zero entries and grab the appropriate entries
"""
indices = torch.where(binaries == 1)
return targets[indices]
def find_nns(target, indices):
"""
Given a list of indices, find the corresponding nearest neighbors in the target tensor
Args:
target: Tensor of size B x ...
indices: List of indices
Returns:
nns: Tensor of size B x k x ...
"""
if indices is None:
return None
elif len(indices) == 0:
return None
def not_tuple(item):
if type(item) == tuple:
return item[0]
return item
nns = []
for idx in indices:
try:
nns.append(not_tuple(target[idx]))
except:
nn_i = []
for i in idx:
nn_i.append(not_tuple(target[i]))
nn_i = np.stack(nn_i, axis=0)
nns.append(nn_i)
return np.stack(nns, axis=0)
@torch.no_grad()
def compute_rel_potential(
model: nn.Module,
target: torch.Tensor,
reference: torch.Tensor,
alpha_misc: Tuple[float, torch.Tensor, torch.Tensor],
diff_misc: Tuple[torch.Tensor, torch.Tensor],
mult_fn: nn.Module,
t: int = 0,
):
"""
Compute the relative potential between two tensors
Args:
model: Score function
target: Target tensor of size B x ...
reference: Reference tensor of size M x ...
alpha_misc: Tuple of (dA, cos_alpha, sin_alpha)
diff_misc: Tuple of (betas, std)
mult_fn: A function to perform matrix multiplication
t: Time step
Returns:
rel_potential: Tensor of size B x M
"""
dA, cos_alpha, sin_alpha = alpha_misc
betas, std = diff_misc
b = len(target)
p = len(cos_alpha)
x = cos_alpha * target[None] + sin_alpha * reference[None]
v = -sin_alpha * target[None] + cos_alpha * reference[None]
x = rearrange(x, "p b ... -> (p b) ...") # combine perturbations and batch
t = torch.zeros(len(x), device=x.device).long() + t
std_t = std[t]
beta_t = betas[t][:, None, None, None]
score = -model(x, t) / std_t[:, None, None, None]
grad_u = -beta_t * score - 0.5 * beta_t * x
grad_u = rearrange(grad_u, "(p b) ... -> p b ...", p=p, b=b)
cumprod_u = 0
v.mul_(dA)
for i in range(p):
val = mult_fn(
grad_u[i].view(b, -1), v[i].view(b, -1)
) # return a vector of size b
cumprod_u += val.sum(-1)
rel_potential = cumprod_u
return rel_potential.cpu().numpy()
def batch_potential(
model: nn.Module,
x1: torch.Tensor, # assume this is a set of target images of size B
x2: torch.Tensor, # assume this is a set of reference images of size M
betas: torch.Tensor, # diffusion variances of size T,
batch_size: int = 128,
device: str = "cuda",
):
"""
Given batches of images and a reference image,
calculate the relative potential between those images and x_ref (batch wise),
following (Spotaneous Symmetry ....) https://arxiv.org/pdf/2305.19693 paper,
return all of the relative potentials of those images w.r.t to the reference image
Args:
model: Score function
x1: Target images
x2: A reference image
betas: Diffusion betas
batch_size: Number of images to evaluate at each iteration
"""
class Model(nn.Module):
"""A simple torch Module object for DataParallel
to perform matrix multiplication across multiple gpus
"""
def __init__(self):
super().__init__()
def __call__(self, x1: torch.Tensor, x2: torch.Tensor):
return x1 * x2
assert batch_size > 0 and len(x2) == 1
# according to paper https://arxiv.org/abs/2305.19693
alpha = torch.linspace(0, 0.5 * torch.pi, 20)
dA = (alpha[1] - alpha[0]).item()
cos_alpha = torch.cos(alpha)
sin_alpha = torch.sin(alpha)
cos_alpha = cos_alpha[:, None, None, None, None].to(device)
sin_alpha = sin_alpha[:, None, None, None, None].to(device)
alpha_misc = (dA, cos_alpha, sin_alpha)
rel_potentials = []
model = model.eval()
betas = betas.to(device)
alpha_cps = torch.cumprod(betas, dim=0)
diff_misc = (betas, (1 - alpha_cps) ** 0.5)
mult_fn = nn.DataParallel(Model())
# assume that x2 is smaller than x1 batch wise
reference = x2.to(device)
batch_size = min(len(x1), batch_size)
for i in range(0, len(x1), batch_size):
j = min(i + batch_size, len(x1))
target = x1[i:j].to(device)
rel_potential = compute_rel_potential(
model, target, reference, alpha_misc, diff_misc, mult_fn
)
rel_potentials.append(rel_potential)
if len(rel_potentials) == 1:
return rel_potentials[0]
return np.concatenate(rel_potentials)