-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_gan.py
executable file
·282 lines (206 loc) · 9.53 KB
/
demo_gan.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
import os
import sys
import time
import pickle
import numpy as np
import tensorflow as tf
from PyQt5 import QtCore, QtWidgets, QtGui
from matplotlib.backends.qt_compat import is_pyqt5
if is_pyqt5():
from matplotlib.backends.backend_qt5agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
else:
from matplotlib.backends.backend_qt4agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
from helpers import plotting, audio
from models import gan, ae
import sounddevice as sd
import soundfile as sf
# ROOT_DIR = 'data/f/loss_ablation_selected'
# FLOWS = ['db0R 000', 'db0R 001', 'db0r 000', 'db0r 001', 'db0r 002', 'db0r 003', 'db0r 004', 'naive 001', 'naive 002', 'naive 003']
# ROOT_DIR = 'data/f_hpc/loss_ablation_selected'
# FLOWS = ['db0r 002']
# ROOT_DIR = 'data/f/extractor_cc'
# FLOWS = ['naive 000']
#
# ROOT_DIR = 'data/f/extractor_cc_simple/'
# FLOWS = '.*'
class SensorInteractWindow(QtWidgets.QMainWindow):
def keyPressEvent(self, QKeyEvent):
if QKeyEvent.key() == QtCore.Qt.Key_Space:
self.index = (self.index + 1) % 128
if QKeyEvent.key() == QtCore.Qt.Key_Backspace:
self.index = (self.index - 1) % 128
if QKeyEvent.key() == QtCore.Qt.Key_0:
self.index = 0
# if QKeyEvent.key() == QtCore.Qt.Key_Up:
# self._last_position = (self._last_position[0], self._last_position[1] - 1)
# if QKeyEvent.key() == QtCore.Qt.Key_Down:
# self._last_position = (self._last_position[0], self._last_position[1] + 1)
# if QKeyEvent.key() == QtCore.Qt.Key_Left:
# self._last_position = (self._last_position[0] - 1, self._last_position[1])
# if QKeyEvent.key() == QtCore.Qt.Key_Right:
# self._last_position = (self._last_position[0] + 1, self._last_position[1])
# if QKeyEvent.key() == QtCore.Qt.Key_K:
# self.k0 = self.flow.sample_spn(self.data.valid_patch_size_rgb // 2)
# if QKeyEvent.key() == QtCore.Qt.Key_F:
# self.load_flow(1)
if QKeyEvent.key() == QtCore.Qt.Key_P:
s0 = self.get_current_spectrum()
# s0 = self.gan.generator(self.z)[-1]
sp = s0.numpy().squeeze()
sp = np.vstack((sp, np.zeros((1, sp.shape[1])), sp[:0:-1]))
sp = np.clip(sp, 0, None)
wave_rec = audio.spectrum_to_signal(sp.T, int(16000 * 2.57), 100)
sd.play(wave_rec, 16000)
if QKeyEvent.key() == QtCore.Qt.Key_S:
self.z = self.gan.sample_z(1)
# if QKeyEvent.key() == QtCore.Qt.Key_D:
# self._diff_rgb = not self._diff_rgb
self.setWindowTitle(self.window_title)
self.update_viz(*self._last_position)
@property
def label(self):
pixel_label = 'RGGB'[self._pixel_selection] if self._pixel_selection < 4 else 'RGGB'
return f'image: {self._image_id}, pixel_sel: {pixel_label}'
@property
def window_title(self):
pos = 2 * (self._last_position[0] / 256 - 0.5)
return f'Window {pos}'
def get_current_spectrum(self):
pixel_x, pixel_y = self._last_position
if hasattr(self.gan, 'latent_dist') and self.gan.latent_dist == 'uniform':
value = (self._last_position[0] / 256)
else:
value = 6 * (self._last_position[0] / 256 - 0.5)
z0 = np.copy(self.z)
z0[0, self.index] = value
return self.gan.decode(z0)[-1]
def update_viz(self, pixel_x, pixel_y):
self._last_position = (pixel_x, pixel_y)
for ax in self.axes:
ax.cla()
# batch_x = self.data.next_validation_batch(self._image_id, 1)[0]
# batch_x = tf.convert_to_tensor(batch_x)
# k0 = tf.convert_to_tensor(self.k0)
# with tf.GradientTape() as tape:
# tape.watch(batch_x)
# tape.watch(k0)
# batch_x0 = self.flow.sensor.process(batch_x, k0)
# if self._pixel_selection < 4:
# pixel = batch_x0[0, pixel_y, pixel_x, self._pixel_selection]
# else:
# pixel = batch_x0[0, pixel_y, pixel_x, :]
# grad_x, grad_k = tape.gradient(pixel, (batch_x, k0))
# batch_x = batch_x.numpy()
# batch_x0 = batch_x0.numpy()
# batch_y = self.flow.isp.process(batch_x).numpy()
# batch_y0 = self.flow.isp.process(batch_x0).numpy()
# if self._fft:
# if self._diff_rgb:
# diff_label = 'FFT($y$ - $y_0$)'
# batch_d = batch_y - batch_y0
# batch_d = image.fft_log_norm(batch_d)
# # batch_d = np.abs(image.fft_log_norm(batch_y)) - np.abs(image.fft_log_norm(batch_y0))
# batch_d = image.normalize(batch_d, 0.1)
# else:
# diff_label = 'FFT($x$ - $x_0$)'
# batch_d = batch_x - batch_x0
# batch_d = np.sum(batch_d, axis=-1, keepdims=True)
# batch_d = image.fft_log_norm(batch_d)
# # batch_d = np.abs(image.fft_log_norm(batch_y)) - np.abs(image.fft_log_norm(batch_y0))
# batch_d = image.normalize(batch_d, 0.1)
# else:
# if self._diff_rgb:
# diff_label = '$y$ - $y_0$'
# batch_d = image.normalize_residual(batch_y - batch_y0)
# else:
# diff_label = '$x$ - $x_0$'
# batch_d = batch_x - batch_x0
# batch_d = np.sum(batch_d, axis=-1, keepdims=True)
# batch_d = image.normalize_residual(batch_d)
# # grad_x = grad_x * batch_x
# # grad_x = image.normalize_residual(grad_x.numpy())
# grad_x = grad_x.numpy()
# grad_x = np.moveaxis(grad_x.squeeze(), -1, 0)
# grad_xt = plots.thumbnails(grad_x, 2)
# # grad_k = grad_k * k0
# # grad_k = image.normalize_residual(grad_k.numpy())
# grad_k = grad_k.numpy()
# grad_k = np.moveaxis(grad_k.squeeze(), -1, 0)
# grad_kt = plots.thumbnails(grad_k, 2)
# index = 0
if hasattr(self.gan, 'latent_dist') and self.gan.latent_dist == 'uniform':
value = (self._last_position[0] / 256)
else:
value = 6 * (self._last_position[0] / 256 - 0.5)
# np.random.normal(size=(1,128))
# z = tf.convert_to_tensor(z)
sp = self.gan.decode(self.z)[-1].numpy()
z0 = np.copy(self.z)
z0[0, self.index] = value
# self.z = tf.convert_to_tensor(z0)
sp0 = self.gan.decode(z0)[-1].numpy()
plotting.quickshow(sp, 'original sample', cmap='jet', axes=self.axes[0])
plotting.quickshow(sp0, f'set idx={self.index} to {value:.2f}', cmap='jet', axes=self.axes[1])
self.axes[2].plot(z0.ravel())
self.axes[2].plot([self.index], z0[0, self.index], 'ro')
if hasattr(self.gan, 'latent_dist') and self.gan.latent_dist == 'uniform':
self.axes[2].set_ylim([0.05, 1.05])
else:
self.axes[2].set_ylim([-3.2, 3.2])
# plots.image(batch_x[..., [0, 1, 3]], f'RAW {self.label}', axes=self.axes[0])
# plots.image(batch_y0, 'RGB image ($y_0$)', axes=self.axes[1])
# plots.image(batch_d, diff_label, axes=self.axes[2])
# plots.image(grad_xt, '$\\nabla_x$', axes=self.axes[3], cmap='seismic', vrange=False)
# plots.image(grad_kt, '$\\nabla_k$', axes=self.axes[4], cmap='RdGy', vrange=False)
# self.fig.axes[0].annotate('o', (pixel_x, pixel_y), ha='center', va='center', color='red')
self.setWindowTitle(self.window_title)
self.fig.canvas.draw_idle()
def __init__(self):
super().__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
layout = QtWidgets.QHBoxLayout(self._main)
model = 'vae'
dataset = 'voxceleb'
version = 5
dist = 'normal'
patch = 256
if model == 'vae':
self.gan = ae.VariationalAutoencoder(dataset, version=version, z_dim=2048, patch_size=256)
if model == 'ae':
self.gan = ae.Autoencoder(dataset, version=version, z_dim=256, patch_size=256)
elif model == 'ms-gan':
self.gan = gan.MultiscaleGAN(dataset, version=version, patch=patch, width_ratio=1, min_output=8, latent_dist=dist)
self.gan.load()
self.index = 0
self.z = self.gan.sample_z(1)
self.fig, self.axes = plotting.sub(3, ncols=3)
self._onmove_disabled = False
def onclick(event):
self._onmove_disabled = not self._onmove_disabled
pixel_x = int(event.xdata)
pixel_y = int(event.ydata)
self.update_viz(pixel_x, pixel_y)
def onmove(event):
if self._onmove_disabled:
return
try:
pixel_x = int(event.xdata)
pixel_y = int(event.ydata)
self.update_viz(pixel_x, pixel_y)
except:
pass
static_canvas = FigureCanvas(self.fig)
layout.addWidget(static_canvas)
self.fig.canvas.mpl_connect('button_press_event', onclick)
self.fig.canvas.mpl_connect('motion_notify_event', onmove)
self.update_viz(0, 0)
self.setWindowTitle(self.window_title)
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = SensorInteractWindow()
app.show()
qapp.exec_()