summaryrefslogtreecommitdiff
path: root/inversion/live.py
blob: 6a01eae43cad123dfc3331e64ad721e74fb72bab (plain)
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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import sys
import glob
import h5py
import numpy as np
import params
import json
import tensorflow as tf
import tensorflow_probability as tfp
import tensorflow_hub as hub
import time
import random
from scipy.stats import truncnorm
from PIL import Image
import visualize as vs
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), '../../live-cortex/rpc/'))
from rpc import CortexRPC

from params import Params

FPS = 25

params = Params(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'params_dense-512.json'))

# --------------------------
# Make directories.
# --------------------------
tag = "test"
OUTPUT_DIR = os.path.join('output', tag)
if not os.path.exists(OUTPUT_DIR):
  os.makedirs(OUTPUT_DIR)

# --------------------------
# Load Graph.
# --------------------------
print("Loading module...")
generator = hub.Module(str(params.generator_path))
print("Loaded!")

gen_signature = 'generator'
if 'generator' not in generator.get_signature_names():
  gen_signature = 'default'

input_info = generator.get_input_info_dict(gen_signature)

BATCH_SIZE = 1
Z_DIM = input_info['z'].get_shape().as_list()[1]
N_CLASS = input_info['y'].get_shape().as_list()[1]

# --------------------------
# Utils
# --------------------------

def clamp(n, a=0, b=1):
  if n < a:
    return a
  if n > b:
    return b
  return n

# --------------------------
# Initializers
# --------------------------

def label_sampler(num_classes=1, shape=(BATCH_SIZE, N_CLASS,)):
  label = np.zeros(shape)
  for i in range(shape[0]):
    for _ in range(random.randint(1, shape[1])):
      j = random.randint(0, shape[1]-1)
      label[i, j] = random.random()
    label[i] /= label[i].sum()
  return label

def truncated_z_sample(shape=(BATCH_SIZE, Z_DIM,), truncation=1.0):
  values = truncnorm.rvs(-2, 2, size=shape)
  return truncation * values

def normal_z_sample(shape=(BATCH_SIZE, Z_DIM,)):
  return np.random.normal(size=shape)

# --------------------------
# More complex ops
# --------------------------

class SinParam:
  def __init__(self, name, shape, type="noise"):
    noise = LerpParam(name + '_noise', shape, type=type)
    orbit_radius = InterpolatorParam(name=name + '_radius', value=0.1)
    orbit_speed = InterpolatorParam(name=name + '_speed', value=0.1)
    orbit_time = InterpolatorParam(name=name + '_time', value=0.0)
    output = tf.math.sin(orbit_time.variable + noise.output) * orbit_radius.variable
    interpolator.sin_params[name] = self
    self.name = name
    self.orbit_speed = orbit_speed
    self.orbit_time = orbit_time
    self.output = output

  def update(self, dt):
    self.orbit_time.value += self.orbit_speed.value * dt

class LerpParam:
  def __init__(self, name, shape, type="noise"):
    a = InterpolatorParam(name=name + '_a', shape=shape, type=type)
    b = InterpolatorParam(name=name + '_b', shape=shape, type=type)
    n = InterpolatorParam(name=name + '_n', value=0.0)
    speed = InterpolatorParam(name=name + '_speed', value=0.1)
    output = a.variable * (1 - n.variable) + b.variable * n.variable
    interpolator.lerp_params[name] = self
    self.name = name
    self.a = a
    self.b = b
    self.n = n
    self.speed = speed
    self.output = output
    self.direction = 0

  def switch(self):
    if self.n > 0.5:
      self.a.randomize()
      self.direction = -1
    else:
      self.b.randomize()
      self.direction = 1

  def update(self, dt):
    if self.direction != 0:
      self.n.value = clamp(self.n.value + self.direction * self.speed.value * dt)
      if self.n.value == 0 or self.n.value == 1:
        self.direction = 0

# --------------------------
# Placeholder params
# --------------------------

class InterpolatorParam:
  def __init__(self, name, dtype=tf.float32, shape=(), value=None, type="noise"):
    self.scalar = shape == ()
    self.shape = shape
    self.type = type
    self.value = value if value is not None else np.zeros(shape)
    self.variable = tf.placeholder(dtype=dtype, shape=shape)
    interpolator.opts[name] = self

  def assign(self, value):
    self.value = value

  def randomize(self):
    if self.type == 'noise':
      val = truncated_z_sample(shape=self.shape, truncation=interpolator.opt['truncation'].value)
    elif self.type == 'label':
      val = label_sampler(shape=self.shape, num_classes=interpolator.opt['num_classes'].value)
    self.assign(val)

# --------------------------
# Interpolator graph
# --------------------------

class Interpolator:
  def __init__(self):
    self.opts = {}
    self.sin_params = {}
    self.lerp_params = {}

  def build(self):
    InterpolatorParam(name='truncation', value=1.0),
    InterpolatorParam(name='num_classes', value=1.0),
    lerp_z = LerpParam('latent', [BATCH_SIZE, Z_DIM])
    sin_z = SinParam('orbit', [BATCH_SIZE, Z_DIM])
    lerp_label = LerpParam('label', [BATCH_SIZE, N_CLASS], type="label")

    # self.opts['z'] = InterpolatorParam(name='z', shape=[BATCH_SIZE, Z_DIM], type='noise')
    # self.opts['y'] = InterpolatorParam(name='y', shape=[BATCH_SIZE, N_CLASS], type='label')

    gen_in = {}
    gen_in['truncation'] = 1.0 # self.opts['truncation'].variable
    gen_in['z'] = lerp_z.output + sin_z.output
    gen_in['y'] = lerp_label.output
    self.gen_img = generator(gen_in, signature=gen_signature)

    sys.stderr.write("Sin params: {}".format(", ".join(self.sin_params.keys())))
    sys.stderr.write("Lerp params: {}".format(", ".join(self.lerp_params.keys())))
    sys.stderr.write("Opts: {}".format(", ".join(self.opts.keys())))

    # Convert generated image to channels_first.
    # self.gen_img = tf.transpose(gen_img, [0, 3, 1, 2])

  def get_feed_dict(self):
    opt = {}
    for param in self.opts.values():
      opt[param.variable] = param.value
    return opt

  def get_state(self):
    opt = {}
    for key, param in self.opts.items():
      if param.scalar:
        if type(param.value) is np.ndarray:
          sys.stderr.write('{} is ndarray\n'.format(key))
          opt[key] = param.value.tolist()
        else:
          opt[key] = param.value
    return opt

  def set_value(self, key, value):
    if key in self.opts:
      self.opts[key].assign(value)
    else:
      sys.stderr.write('{} not a valid option\n'.format(key))

  def on_step(self, i, dt, sess):
    for param in self.sin_params.values():
      param.update(dt)
    for param in self.lerp_params.values():
      param.update(dt)
    gen_images = sess.run(self.gen_img, feed_dict=self.get_feed_dict())
    return gen_images

  def run(self, cmd, payload):
    if cmd == 'switch' and payload in self.lerp_params:
      self.lerp_params[payload].switch()
    pass

# --------------------------
# RPC Listener
# --------------------------

interpolator = Interpolator()

class Listener:
  def connect(self):
    self.rpc_client = CortexRPC(self.on_get, self.on_set, self.on_ready, self.on_cmd)

  def on_set(self, key, value):
    interpolator.set_value(key, value)

  def on_get(self):
    state = interpolator.get_state()
    sys.stderr.write(json.dumps(state) + "\n")
    sys.stderr.flush()
    return state

  def on_cmd(self, cmd, payload):
    print("got command {}".format(cmd))
    interpolator.run(cmd, payload)

  def on_ready(self, rpc_client):
    self.sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    self.sess.run(tf.global_variables_initializer())
    self.sess.run(tf.tables_initializer())
    print("Ready!")
    interpolator.build()
    self.rpc_client = rpc_client
    self.rpc_client.send_status('processing', True)
    dt = 1 / FPS
    for i in range(99999):
      if (i % 100) == 0:
        print("Step {}".format(i))
      gen_time = time.time()
      gen_images = interpolator.on_step(i, dt, self.sess)
      if gen_images is None:
        print("Exiting...")
        break
      if (i % 100) == 0:
        print(gen_images.shape)
        print("Generation time: {:.1f}s".format(time.time() - gen_time))
      out_img = vs.data2pil(gen_images[0])
      if out_img is not None:
        #if out_img.resize_before_sending:
        img_to_send = out_img.resize((256, 256), Image.BICUBIC)
        meta = {
          'i': i,
          'sequence_i': i,
          'skip_i': 0,
          'sequence_len': 99999,
        }
        self.rpc_client.send_pil_image("frame_{:05d}.png".format(i+1), meta, img_to_send, 'jpg')
    self.rpc_client.send_status('processing', False)
    self.sess.close()

if __name__ == '__main__':
  listener = Listener()
  listener.connect()

# layer_name = 'module_apply_' + gen_signature + '/' + params.inv_layer
# gen_encoding = tf.get_default_graph().get_tensor_by_name(layer_name)
# ENC_SHAPE = gen_encoding.get_shape().as_list()[1:]
# encoding = tf.get_variable(name='encoding', dtype=tf.float32,
#                            shape=[BATCH_SIZE,] + ENC_SHAPE)
# tf.contrib.graph_editor.swap_ts(gen_encoding, tf.convert_to_tensor(encoding))