-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_ood.py
390 lines (334 loc) · 14.3 KB
/
main_ood.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
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
"""
The main file used for OOD detection.
"""
import copy
from PIL import Image
import io
import matplotlib.pyplot as plt
import torchvision
import numpy as np
import torch
import dypy as dy
from jsonargparse import ArgumentParser, ActionConfigFile
import wandb
from dataclasses import dataclass
from random_word import RandomWords
from model_zoo.datasets import get_loaders
import traceback
import typing as th
from model_zoo.utils import load_model_with_checkpoints
from dotenv import load_dotenv
import os
from tqdm import tqdm
from math import sqrt
import datetime
# Needed for log_prob
torch.backends.cuda.enable_flash_sdp(False)
torch.backends.cuda.enable_mem_efficient_sdp(False)
torch.backends.cuda.enable_math_sdp(True)
@dataclass
class OODConfig:
base_model: dict
data: dict
ood: dict
logger: dict
def plot_likelihood_ood_histogram(
model: torch.nn.Module,
data_loader_in: torch.utils.data.DataLoader,
data_loader_out: torch.utils.data.DataLoader,
limit: th.Optional[int] = None,
log_prob_kwargs: th.Optional[dict] = None,
):
"""
Run the model on the in-distribution and out-of-distribution data
and then plot the histogram of the log likelihoods of the models to show
the pathologies if it exists.
Args:
model (torch.nn.Module): The likelihood model that contains a log_prob method
data_loader_in (torch.utils.data.DataLoader): A dataloader for the in-distribution data
data_loader_out (torch.utils.data.DataLoader): A dataloader for the out-of-distribution data
limit (int, optional): The limit of number of datapoints to consider for the histogram.
Defaults to None => no limit.
"""
# create a function that returns a list of all the likelihoods when given
# a dataloader
model.eval()
def list_all_scores(dloader: torch.utils.data.DataLoader, description: str):
log_probs = []
for x in tqdm(dloader, desc=f"Calculating likelihoods for {description}"):
with torch.no_grad():
t = model.log_prob(x, **(log_prob_kwargs or {})).cpu()
# turn t into a list of floats
t = t.flatten()
t = t.tolist()
log_probs += t
if limit is not None and len(log_probs) > limit:
break
return log_probs
# List the likelihoods for both dataloaders
in_distr_scores = list_all_scores(data_loader_in, "in distribution")
out_distr_scores = list_all_scores(data_loader_out, "out of distribution")
# plot using matplotlib and then visualize the picture
# using W&B media.
try:
# return an image of the histogram
plt.hist(in_distr_scores, density=True, bins=100,
alpha=0.5, label="in distribution")
plt.hist(out_distr_scores, density=True, bins=100,
alpha=0.5, label="out distribution")
plt.title("Histogram of log likelihoods")
plt.legend(loc="upper right")
buf = io.BytesIO()
# Save your plot to the buffer
plt.savefig(buf, format="png")
# Use PIL to convert the BytesIO object to an image object
buf.seek(0)
img = Image.open(buf)
finally:
plt.close()
return np.array(img)
def standardize_sample_visualizing_format(sample):
new_sample = sample
if len(new_sample.shape) < 2 or new_sample.shape[-1] != new_sample.shape[-2]:
# If the shape is not of an image, create a square image and do zero padding
# to visualize and compare
sqr_root = int(sqrt(new_sample.shape[-1]))
if sqr_root * sqr_root < new_sample.shape[-1]:
sqr_root += 1
new_sample = torch.nn.functional.pad(
input=new_sample,
pad=(0, sqr_root ** 2 - new_sample.shape[-1]),
mode='constant',
value=0.0
)
new_shape = [sqr_root, sqr_root]
if len(new_sample.shape) > 1:
new_shape = list(new_sample.shape[:-1]) + new_shape
new_sample = new_sample.reshape(new_shape)
new_sample = new_sample.unsqueeze(-3)
mn = new_sample.min()
mx = new_sample.max()
return (new_sample - mn) / (mx - mn)
return sample
def run_ood(config: dict, gpu_index: int = 0, checkpoint_dir: th.Optional[str] = None):
"""
Check the docs to see how the config dictionary looks like.
This is the dictionary obtained after parsing the YAML file using jsonargparse.
"""
###################
# (1) Model setup #
###################
load_dotenv(override=True)
if 'MODEL_DIR' in os.environ:
model_root = os.environ['MODEL_DIR']
else:
model_root = './runs'
device = f"cuda:{gpu_index}" if torch.cuda.is_available() else "cpu"
model = load_model_with_checkpoints(config=config['base_model'], root=model_root, device=device)
model.to(device)
##################
# (1) Data setup #
##################
# Load the environment variables
# Set the data directory if it is specified in the environment
# variables, otherwise, set to './data'
if 'DATA_DIR' in os.environ:
data_root = os.environ['DATA_DIR']
else:
data_root = './data'
in_train_loader, _, in_test_loader = get_loaders(
**config["data"]["in_distribution"]["dataloader_args"],
device=device,
shuffle=False,
data_root=data_root,
unsupervised=True,
)
ood_train_loader, _, ood_test_loader = get_loaders(
**config["data"]["out_of_distribution"]["dataloader_args"],
device=device,
shuffle=False,
data_root=data_root,
unsupervised=True,
)
# in_loader is the loader that is used for the in-distribution data
if not 'pick_loader' in config['data']['in_distribution']:
print("pick_loader for in-distribution not in config, setting to test")
config['data']['in_distribution']['pick_loader'] = 'test'
if config['data']['in_distribution']['pick_loader'] == 'test':
in_loader = in_test_loader
elif config['data']['in_distribution']['pick_loader'] == 'train':
in_loader = in_train_loader
# out_loader is the loader that is used for the out-of-distribution data
if not 'pick_loader' in config['data']['out_of_distribution']:
print("pick_loader for ood not in config, setting to test")
config['data']['out_of_distribution']['pick_loader'] = 'test'
if config['data']['out_of_distribution']['pick_loader'] == 'test':
out_loader = ood_test_loader
elif config['data']['out_of_distribution']['pick_loader'] == 'train':
out_loader = ood_train_loader
############################################################
# (3) Log model samples and in/out of distribution samples #
############################################################
# print out a sample ood and in distribution image onto the wandb logger
if "seed" in config["data"]:
np.random.seed(config["data"]["seed"])
# you can set to visualize or bypass the visualization for speedup!
if 'bypass_visualization' not in config['ood'] or not config['ood']['bypass_visualization']:
if not config['ood'].get('bypass_dataset_visualization', False):
# get 9 random samples from the in distribution dataset
sample_set = np.random.randint(len(in_loader.dataset), size=9)
in_samples = []
for s in sample_set:
in_samples.append(standardize_sample_visualizing_format(in_loader.dataset[s]))
sample_set = np.random.randint(len(out_loader.dataset), size=9)
out_samples = []
for s in sample_set:
out_samples.append(standardize_sample_visualizing_format(out_loader.dataset[s]))
in_samples = torch.stack(in_samples)
out_samples = torch.stack(out_samples)
in_samples = torchvision.utils.make_grid(in_samples, nrow=3)
out_samples = torchvision.utils.make_grid(out_samples, nrow=3)
wandb.log({"data/in_distribution_samples": [wandb.Image(
in_samples, caption="in distribution_samples")]})
wandb.log({"data/out_of_distribution samples": [wandb.Image(
out_samples, caption="out of distribution samples")]})
# generate 9 samples from the model if bypass sampling is not set to True
if 'samples_visualization' in config['ood']:
if config['ood']['samples_visualization'] > 0:
# with torch.no_grad():
def log_samples():
samples = model.sample(9, **config['ood'].get('sampling_kwargs', {}))
# samples = standardize_sample_visualizing_format(samples)
samples = torchvision.utils.make_grid(samples, nrow=3)
wandb.log(
{"data/model_generated": [wandb.Image(samples, caption="model generated")]})
# set torch seed for reproducibility
if config["ood"]["seed"] is not None:
if device.startswith("cuda"):
torch.cuda.manual_seed(config["ood"]["seed"])
torch.manual_seed(config["ood"]["seed"])
log_samples()
else:
log_samples()
if config['ood']['samples_visualization'] > 1:
wandb.log({"data/most_probable":
[
wandb.Image(
standardize_sample_visualizing_format(model.sample(-1).squeeze()),
caption="max likelihood"
)
]
})
def log_histograms():
limit = config['ood'].get('histogram_limit', None)
img_array = plot_likelihood_ood_histogram(
model,
in_loader,
out_loader,
limit=limit,
log_prob_kwargs=config['ood'].get('log_prob_kwargs', {}),
)
wandb.log({"likelihood_ood_histogram": [wandb.Image(
img_array, caption="Histogram of log likelihoods")]})
if "bypass_visualize_histogram" not in config['ood'] or not config['ood']['bypass_visualize_histogram']:
if config["ood"]["seed"] is not None:
if device.startswith("cuda"):
torch.cuda.manual_seed(config["ood"]["seed"])
torch.manual_seed(config["ood"]["seed"])
log_histograms()
else:
log_histograms()
#########################################
# (4) Instantiate an OOD solver and run #
#########################################
# For dummy runs that you just use for visualization
if "method_args" not in config["ood"] or "method" not in config["ood"]:
print("No ood method available! Exiting...")
return
method_args = copy.deepcopy(config["ood"]["method_args"])
method_args["likelihood_model"] = model
# pick a random batch with seed for reproducibility
if config["ood"]["seed"] is not None:
np.random.seed(config["ood"]["seed"])
idx = np.random.randint(len(out_loader))
for _ in range(idx + 1):
x = next(iter(out_loader))
if config["ood"].get("pick_single", False):
# pick a single image the selected batch
method_args["x_loader"] = [x[np.random.randint(x.shape[0])].unsqueeze(0)]
elif config["ood"].get("use_dataloader", False):
method_args["x_loader"] = out_loader
if config["ood"].get("pick_count", 0) > 0:
t = min(config['ood']['pick_count'], len(out_loader))
method_args["x_loader"] = []
iterable_ = iter(out_loader)
for _ in range(t):
method_args["x_loader"].append(next(iterable_))
elif "pick_count" not in config["ood"]:
raise ValueError("pick_count not in config when pick_single=False")
else:
# pass in the entire batch
r = min(config["ood"]["pick_count"], x.shape[0])
method_args["x_loader"] = [x[:r]]
method_args["in_distr_loader"] = in_train_loader
method_args["checkpoint_dir"] = checkpoint_dir
if device.startswith("cuda"):
torch.cuda.manual_seed(config["ood"]["seed"])
torch.manual_seed(config["ood"]["seed"])
method = dy.eval(config["ood"]["method"])(**method_args)
# Call the run function of the given method
method.run()
def dysweep_compatible_run(config, checkpoint_dir, gpu_index: int = 0):
"""
Function compatible with dysweep
"""
try:
run_ood(config, gpu_index=gpu_index, checkpoint_dir=checkpoint_dir)
except Exception as e:
print("Exception:\n", e)
print(traceback.format_exc())
print("-----------")
raise e
if __name__ == "__main__":
# create a jsonargparse that gets a config file
parser = ArgumentParser()
parser.add_class_arguments(
OODConfig,
fail_untyped=False,
sub_configs=True,
)
# add an argument to the parser pertaining to the gpu index
parser.add_argument(
'--gpu-index',
type=int,
help="The index of GPU being used",
default=0,
)
parser.add_argument(
"--config",
action=ActionConfigFile,
help="Path to the config file",
)
args = parser.parse_args()
print("Running on gpu index", args.gpu_index)
conf = {
"base_model": args.base_model,
"data": args.data,
"ood": args.ood,
}
if "name" in args.logger:
# add a random word to the name
r = RandomWords()
args.logger["name"] += f"-{r.get_random_word()}"
wandb.init(config=conf, **args.logger)
# set the checkpoint_dir to the dotenv variable if it exists
load_dotenv(override=True)
if 'MODEL_DIR' in os.environ:
checkpoint_dir = os.environ['MODEL_DIR']
else:
checkpoint_dir = './runs'
timestamp = datetime.datetime.now().strftime('%b%d_%H-%M-%S')
checkpoint_dir = os.path.join(checkpoint_dir, timestamp)
# make the directories if they do not exist
os.makedirs(checkpoint_dir, exist_ok=True)
run_ood(conf, gpu_index=args.gpu_index, checkpoint_dir=checkpoint_dir)