-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
217 lines (183 loc) · 6.84 KB
/
utils.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
from imageio import imwrite
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tqdm import tqdm
import os
import requests
import zipfile
def get_settings(data_to_use):
if 'lsun' in data_to_use:
is_bedroom = 'bedroom' in data_to_use
data_to_use = 'lsun'
settings = {
"cifar10":
{
"adambeta1": 0.9,
"adambeta2": 0.98,
"adameps": 1e-8,
"average_decay": 0.995,
"lr": [ 2e-4, 5000],
"epochs": 25,
"shardsize": 102400,
"n_test_examples": 51200,
"batch_size": 512
},
"celeba":
{
"adambeta1": 0.9,
"adambeta2": 0.98,
"adameps": 1e-8,
"average_decay": 0.995,
"lr": [ 5e-5, 5000],
"epochs": 35,
"shardsize": 25600,
"n_test_examples": 10240,
"batch_size": 512
},
"lsun":
{
"adambeta1": 0.98,
"adambeta2": 0.999,
"adameps": 1e-8,
"average_decay": 0.9995,
"lr": [ 5e-6, 1000],
"shardsize": 2560,
"n_test_examples": 2560,
"batch_size": 32
}
}
if 'lsun' in data_to_use:
if is_bedroom:
settings['lsun']['epochs'] = 50
else:
settings['lsun']['epochs'] = 40
return settings[data_to_use]
def show_images(images, scale=5, savepath=None, dims = None):
if isinstance(images, tf.Tensor):
if len(images.shape) == 4:
images = tf.split(images, images.shape[0], axis=0)
for i in range(len(images)):
images[i] = tf.squeeze(images[i])
if not isinstance(images[0], np.ndarray):
for i in range(len(images)):
images[i] = float_to_image(images[i])
if dims is None:
m = len(images)//10 + 1
n = 10
else:
m, n = dims
plt.figure(figsize=(scale*n, scale*m))
for i in range(len(images)):
plt.subplot(m, n, i+1)
plt.imshow(images[i])
plt.axis('off')
plt.tight_layout()
if savepath is not None:
plt.savefig(savepath)
plt.show()
def float_to_image(x):
x = tf.clip_by_value(x, -1.0, 1.0)
x = x*127.5 + 127.5
return x.numpy().astype('uint8')
def slerp(z1, z2, t):
omega = tf.math.acos(tf.reduce_sum(z1 * z2)/(tf.norm(z1)*tf.norm(z2)))
a = tf.sin((1-t) * omega)/tf.sin(omega)
b = tf.sin(t*omega)/tf.sin(omega)
return a * z1 + b * z2
class WarmupSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):
def __init__(self, learnrate, warmup_steps):
super().__init__()
self.learnrate = learnrate
self.warmup_steps = warmup_steps
def __call__(self, step):
arg1 = self.learnrate
arg2 = step * self.learnrate / float(self.warmup_steps)
return tf.math.minimum(arg1, arg2)
class WarmupSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):
def __init__(self, learnrate, warmup_steps):
super().__init__()
self.learnrate = learnrate
self.warmup_steps = warmup_steps
def __call__(self, step):
arg1 = self.learnrate
arg2 = step * self.learnrate / float(self.warmup_steps)
return tf.math.minimum(arg1, arg2)
def get_strategy(devices_to_use=None):
if isinstance(devices_to_use, str):
if "CPU" in devices_to_use.upper():
return tf.distribute.get_strategy(), "CPU"
if tf.config.list_physical_devices('GPU'):
return tf.distribute.MirroredStrategy(devices=devices_to_use), "GPU"
else:
try:
resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu=devices_to_use)
tf.config.experimental_connect_to_cluster(resolver)
tf.tpu.experimental.initialize_tpu_system(resolver)
print("All devices: ", tf.config.list_logical_devices('TPU'))
return tf.distribute.TPUStrategy(resolver), "TPU"
except:
return tf.distribute.get_strategy(), "CPU"
def make_runfn(model, strategy, run_ddim_process=False):
if not run_ddim_process:
@tf.function
def runmodel(z):
def replica_fn(z):
return model(z)
result = strategy.run(replica_fn, args=(z,))
return result
else:
@tf.function
def runmodel(xt, index, alpha, alpha_next):
def replica_fn(xt, index, alpha, alpha_next):
return model.run_ddim_step(xt, index, alpha, alpha_next)
result = strategy.run(replica_fn, args=(xt, index, alpha, alpha_next))
return result
return runmodel
def make_batch_of_images(model, runfn, z=None, n_samples=None):
if n_samples is None and z is None:
raise RuntimeError("Specify input or number of samples.")
elif z is None:
z = tf.random.normal([n_samples, model.spatialres, model.spatialres, 3])
else:
n_samples = z.shape[0]
#must be a tensor of [N, H, W, 3]
assert list(z.shape[1:]) == [model.spatialres, model.spatialres, 3] and z.dtype == tf.float32
images = runfn(z)
images = float_to_image(images)
return images
def load_models_from_gdrive(targetdir, get_original_models):
if not os.path.exists(targetdir):
os.mkdir(targetdir)
if get_original_models:
zipped_loc = os.path.join(targetdir, "Original_Models.zip")
#loads the adapted H5 files of the trained models in "Denoising Diffusion Probabilistic Models" and "Denoising Diffusion Implicit Models"
drive_id = "1KlUuwAbWqHI0u9FXTb5xqSOIUnOMI7EV"
else:
zipped_loc = os.path.join(targetdir, "denoising_student_models.zip")
#loads our trained generative models.
drive_id = "1tW5t3W4wqE5f0NXaaiYuFK_2JOBrf9cY"
download_file_from_google_drive(drive_id, zipped_loc)
with zipfile.ZipFile(zipped_loc,"r") as zip_ref:
zip_ref.extractall(targetdir)
#download_file_from_google_drive and its helper functions taken from https://stackoverflow.com/a/39225039
def download_file_from_google_drive(id, destination):
URL = "https://docs.google.com/uc?export=download"
session = requests.Session()
response = session.get(URL, params = { 'id' : id }, stream = True)
token = get_confirm_token(response)
if token:
params = { 'id' : id, 'confirm' : token }
response = session.get(URL, params = params, stream = True)
save_response_content(response, destination)
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith('download_warning'):
return value
return None
def save_response_content(response, destination):
CHUNK_SIZE = 32768
with open(destination, "wb") as f:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)