-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
213 lines (154 loc) · 6.74 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
import torch
import os
from PIL import Image
import logging
import numpy as np
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# taken and modified from https://dsp.stackexchange.com/questions/93607/pixel-shift-when-using-fourier-downsampling
def downsampling_fourier(input_tensor, scaling_factor = 2):
_ , _, height , width = input_tensor.shape #B x C x H x W
# select center pixels
center_x = height//2
center_y = width//2
crop_dim_x = int(center_x//scaling_factor)
crop_dim_y = int(center_y//scaling_factor)
fimage = torch.fft.fftshift(torch.fft.fft2(input_tensor, norm = "ortho"))
fft_crop = fimage[:,:,(center_x-crop_dim_x):(center_x+crop_dim_x),(center_y-crop_dim_y):(center_y+crop_dim_y)]
tensor_downsampled = torch.real(torch.fft.ifft2(torch.fft.ifftshift(fft_crop), norm = "ortho"))
return tensor_downsampled
def sample_test(sde, input_channels, input_height, num_steps, num_samples):
"""
generates samples from the reverse SDE
:param sde: instance of SDE class
:param input_channels:
:param input_height: resolution of input images
:param num_steps: number time steps for sampling
:param num_samples: number of samples
:return:
"""
delta = sde.T / num_steps
y0 = sde.prior.sample([num_samples, input_channels, input_height, input_height])
y1 = downsampling_fourier(y0)
ts = torch.linspace(0, 1, num_steps + 1).to(y0) * sde.T
ones = torch.ones(num_samples, 1, 1, 1).to(y0)
with torch.no_grad():
for i in range(num_steps):
mu = sde.mu(ones * ts[i], y0, lmbd = 1.)
sigma = sde.sigma(ones * ts[i], y0, lmbd = 1.)
epsilon = sde.prior.sample(y0.shape)
y0 = y0 + delta * mu + (delta ** 0.5) * sigma * epsilon
with torch.no_grad():
for i in range(num_steps):
mu = sde.mu(ones * ts[i], y1, lmbd = 1.)
sigma = sde.sigma(ones * ts[i], y1, lmbd = 1.)
epsilon = sde.prior.sample(y1.shape)
y1 = y1 + delta * mu + (delta ** 0.5) * sigma * epsilon
return y0, y1
def closed_form_score2(sde,matrix, prior, t, x):
res = x.shape[2]
matrix = matrix.to(device)#+1*torch.eye(matrix.shape[0], device = device)#.unsqueeze(0).repeat(x.shape[0],1,1)
matrix_inv = torch.linalg.inv(matrix)
x = x.requires_grad_()
var_weight = sde.base_sde.var_weight(t)
mean_weight = sde.base_sde.mean_weight(t)
func1 = torch.ones(x.shape[0],1, res,res, device = device)
func2 = torch.ones(x.shape[0],1, res,res, device = device)
for k in range(res):
func1[:,:,:,k] *= k/(res)
for k in range(res):
func2[:,:,k,:] *= (res-k)/(res)
mean1 = prior.Qmv(func1)
mean2 = prior.Qmv(func2)
prod1 = ((x-mean_weight*mean1).view(x.shape[0], res**2))@matrix_inv
prod2 = ((x-mean_weight*mean2).view(x.shape[0], res**2))@matrix_inv
prod1 = prod1
prod2 = prod2
vector1 = -0.5*((prod1**2).sum(dim = 1))
vector2 = -0.5*((prod2**2).sum(dim = 1))
score = torch.logsumexp(torch.cat((vector1.unsqueeze(0), vector2.unsqueeze(0)),0), dim = 0).view(x.shape[0])
grad = torch.autograd.grad(score.sum(), x)[0].view(x.shape[0],res**2)
return (grad@Q).view(x.shape[0],1,res,res)*sde.base_sde.g(t, x)**2-sde.base_sde.f(t,x)
def get_samples_true(sde, input_channels, input_height, num_steps, num_samples, matrix, prior):
"""
generates samples from the reverse SDE
:param sde: instance of SDE class
:param input_channels:
:param input_height: resolution of input images
:param num_steps: number time steps for sampling
:param num_samples: number of samples
:return:
"""
delta = sde.T / num_steps
y0 = sde.prior.sample([num_samples, input_channels, input_height, input_height])
ts = torch.linspace(0, 1, num_steps + 1).to(y0) * sde.T
ones = torch.ones(num_samples, 1, 1, 1).to(y0)
for i in range(num_steps):
mu = closed_form_score2(sde, matrix, prior, sde.T-ts[i],y0).detach()
sigma = sde.sigma(ones * ts[i], y0, lmbd = 0.)
epsilon = sde.prior.sample(y0.shape)
y0 = y0 + delta * mu + (delta ** 0.5) * sigma * epsilon
return y0
def get_samples(sde, input_channels, input_height, num_steps, num_samples, store_itermediates=True):
"""
generates samples from the reverse SDE
:param sde: instance of SDE class
:param input_channels:
:param input_height: resolution of input images
:param num_steps: number time steps for sampling
:param num_samples: number of samples
:return:
"""
delta = sde.T / num_steps
y0 = sde.prior.sample([num_samples, input_channels, input_height, input_height])
ts = torch.linspace(0, 1, num_steps + 1).to(y0) * sde.T
ones = torch.ones(num_samples, 1, 1, 1).to(y0)
Y = []
with torch.no_grad():
for i in range(num_steps):
mu = sde.mu(ones * ts[i], y0, lmbd = 0.)
sigma = sde.sigma(ones * ts[i], y0, lmbd = 0.)
epsilon = sde.prior.sample(y0.shape)
y0 = y0 + delta * mu + (delta ** 0.5) * sigma * epsilon
if store_itermediates:
Y.append(y0)
return y0, Y
def get_samples_batched(sde, input_channels, input_height, num_steps, num_samples):
"""
generates samples from the reverse SDE
:param sde: instance of SDE class
:param input_channels:
:param input_height: resolution of input images
:param num_steps: number time steps for sampling
:param num_samples: number of samples
:return:
"""
delta = sde.T / num_steps
samples = torch.empty(0, device = device)
for l in range(100):
y0 = sde.prior.sample([num_samples//100, input_channels, input_height, input_height])
ts = torch.linspace(0, 1, num_steps + 1).to(y0) * sde.T
ones = torch.ones(num_samples//100, 1, 1, 1).to(y0)
with torch.no_grad():
for i in range(num_steps):
mu = sde.mu(ones*ts[i], y0, lmbd = 0.)
sigma = sde.sigma(ones*ts[i], y0, lmbd = 0.)
epsilon = sde.prior.sample(y0.shape)
y0 = y0 + delta * mu + (delta ** 0.5) * sigma * epsilon
samples = torch.cat((samples, y0),0)
return samples
def save_samples(y0, i,folder):
"""
save samples as individual jpg images
:param y0: generated images
:param file_name: base file name (without the exension)
:return:
"""
makedirs(str(folder))
for j in range(y0.shape[0]):
y0j = torch.clamp(y0[j], 0., 1.)
arr = y0j.cpu().data.numpy() * 255
arr = arr.astype(np.uint8).squeeze(0)
im = Image.fromarray(arr)
print(str(folder+str(i*100+j)+'.jpeg'))
im.save(str(folder+str(i*100+j)+'.jpeg'))