-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheval.py
154 lines (118 loc) · 3.79 KB
/
eval.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
#!/usr/bin/env python
import inox
import io
import zipfile
from dawgz import job, schedule
from functools import partial
from torch import Tensor
from torch.utils import data
from torch_fidelity.fidelity import calculate_metrics
from torchvision.transforms.functional import pil_to_tensor
from tqdm import tqdm
from typing import *
# isort: split
from utils import *
class ZipDataset(data.Dataset):
r"""Zip image dataset."""
def __init__(self, archive: Path):
self.images = []
with zipfile.ZipFile(archive, mode='r') as file:
for name in file.namelist():
with file.open(name) as data:
img = Image.open(data)
img = img.convert('RGB')
self.images.append(img)
def __len__(self) -> int:
return len(self.images)
def __getitem__(self, i: int) -> Tensor:
return pil_to_tensor(self.images[i])
@staticmethod
def zip(archive: Path, images: List):
with zipfile.ZipFile(archive, mode='w') as file:
for i, img in enumerate(images):
buffer = io.BytesIO()
img.save(buffer, 'png')
file.writestr(f'IMG_{i}.png', buffer.getvalue())
def generate(checkpoint: Path, archive: Path, seed: int = None):
# Sharding
jax.config.update('jax_threefry_partitionable', True)
mesh = jax.sharding.Mesh(jax.devices(), 'i')
replicated = jax.sharding.NamedSharding(mesh, jax.sharding.PartitionSpec())
# RNG
seed = hash((checkpoint, seed)) % 2**16
rng = inox.random.PRNG(seed)
# Model
model = load_module(checkpoint)
static, arrays = model.partition()
arrays = jax.device_put(arrays, replicated)
model = static(arrays)
# Generate
images = []
for _ in tqdm(range(0, 50000, 256), ncols=88):
x = sample_any(
model=model,
shape=(256, 32 * 32 * 3),
shard=True,
sampler='ddim',
steps=256,
key=rng.split(),
)
x = unflatten(x, 32, 32)
x = np.asarray(x)
for img in map(to_pil, x):
images.append(img)
# Archive
ZipDataset.zip(archive, images)
def fid(archive: Path, run: str, lap: int, seed: int):
stats = calculate_metrics(
input1=ZipDataset(archive),
input2='cifar10-train',
fid=True,
isc=True,
)
fid = stats['frechet_inception_distance']
isc = stats['inception_score_mean']
with open(PATH / 'statistics.csv', mode='a') as f:
f.write(f'{run},{lap},{seed},{fid},{isc}\n')
if __name__ == '__main__':
# run = 'brisk-violet-19_j2mcd2x5' # Tweedie (25%)
# run = 'dandy-cherry-17_sspxwzxa' # Tweedie (50%)
run = 'proud-glitter-18_ju7gfg9d' # Tweedie (75%)
# run = 'iconic-disco-20_tw5lit5o' # (I + Σ_t^{-1})^{-1}
# run = 'eager-bush-21_bddhh5lh' # Σ_t
runpath = PATH / f'runs/{run}'
seed = 0
jobs = []
for lap in range(32):
checkpoint = runpath / f'checkpoint_{lap}.pkl'
archive = runpath / f'archive_{lap}_{seed}.zip'
if not checkpoint.exists():
break
a = job(
partial(generate, checkpoint, archive, seed),
name=f'generate_{lap}',
cpus=4,
gpus=4,
ram='64GB',
time='06:00:00',
partition='gpu',
)
b = job(
partial(fid, archive, run, lap, seed),
name=f'fid_{lap}',
cpus=4,
gpus=1,
ram='64GB',
time='01:00:00',
partition='gpu',
)
if not archive.exists():
b.after(a)
jobs.append(b)
schedule(
*jobs,
name=f'Eval {run} ({seed})',
backend='slurm',
export='ALL',
account='ariacpg',
)