diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2020-01-18 16:22:05 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2020-01-18 16:22:05 +0100 |
| commit | bc7aee94be5597cd69a3bdda2d7109675d02b809 (patch) | |
| tree | cf2eb6b628b79892ba1333f5c96ec81c982c50f9 | |
| parent | 5ce6f983350e903a69784ce91a9304da4c26068c (diff) | |
parameter smoothing
| -rw-r--r-- | cli/app/search/live.py | 50 |
1 files changed, 35 insertions, 15 deletions
diff --git a/cli/app/search/live.py b/cli/app/search/live.py index 644fd05..cfde1a3 100644 --- a/cli/app/search/live.py +++ b/cli/app/search/live.py @@ -25,8 +25,12 @@ from app.utils.cortex_utils import results_folder, upload_file_to_cortex from app.utils.tf_utils import read_checkpoint from subprocess import Popen, PIPE +# frames per second FPS = 25 +# amount to smooth manual parameter changes. set to 1 to disable smoothing +SMOOTH_AMOUNT = 4 + params = params_dense_dict('live') # -------------------------- @@ -91,8 +95,8 @@ def normal_z_sample(shape=(BATCH_SIZE, Z_DIM,)): class SinParam: def __init__(self, name, shape, datatype="noise", lerp=True, radius=0.25): - orbit_radius = InterpolatorParam(name=name + '_radius', value=radius) - orbit_speed = InterpolatorParam(name=name + '_speed', value=FPS) + orbit_radius = InterpolatorParam(name=name + '_radius', value=radius, smooth=True) + orbit_speed = InterpolatorParam(name=name + '_speed', value=FPS, smooth=True) orbit_time = InterpolatorParam(name=name + '_time', value=0.0) if lerp: noise = LerpParam(name + '_noise', shape=shape, datatype=datatype) @@ -110,7 +114,7 @@ class SinParam: self.t = 0 def update(self): - self.orbit_time.value += (np.pi * 2) / self.orbit_speed.value + self.orbit_time.assign((np.pi * 2) / self.orbit_speed.value, immediate=True) self.t += 1 # randomize the orbit when possible - # - check if we've done one full orbital period @@ -127,8 +131,8 @@ class LerpParam: else: a = InterpolatorParam(name=name + '_a', shape=shape, datatype=datatype) b = InterpolatorParam(name=name + '_b', shape=shape, datatype=datatype) - n = InterpolatorParam(name=name + '_n', value=0.0) - speed = InterpolatorParam(name=name + '_speed', value=FPS) + n = InterpolatorParam(name=name + '_n', value=0.0, smooth=True) + speed = InterpolatorParam(name=name + '_speed', value=FPS, smooth=True) output = a.variable * (1 - n.variable) + b.variable * n.variable interpolator.lerp_params[name] = self self.name = name @@ -153,7 +157,8 @@ class LerpParam: def update(self): if self.direction != 0: - self.n.value = clamp(self.n.value + self.direction / self.speed.value) + n = clamp(self.n.value + self.direction / self.speed.value) + self.n.assign(n, immediate=True) print("set_opt: {}_n {}".format(self.name, self.n.value)) if self.n.value == 0 or self.n.value == 1: self.direction = 0 @@ -163,23 +168,32 @@ class LerpParam: # -------------------------- class InterpolatorParam: - def __init__(self, name, dtype=tf.float32, shape=(), value=None, datatype="float"): + def __init__(self, name, dtype=tf.float32, shape=(), value=None, datatype="float", smooth=False): self.scalar = shape == () self.shape = shape self.datatype = datatype + self.smooth = smooth if datatype == "float": - self.assign(value or 0.0) + self.assign(value or 0.0, immediate=True) + if self.smooth: + interpolator.smooth_params[name] = self else: self.randomize() self.variable = tf.placeholder(dtype=dtype, shape=shape) interpolator.opts[name] = self - def assign(self, value): + def assign(self, value, immediate=False): if self.datatype == 'float': - self.value = float(value) + value float(value) + self.next_value = float(value) + if immediate or not self.smooth: + self.value = self.next_value else: self.value = value + def update(self): + self.value = (self.value * (SMOOTH_AMOUNT - 1) + self.next_value) / (SMOOTH_AMOUNT) + def randomize(self): if self.datatype == 'noise': val = truncated_z_sample(shape=self.shape, truncation=interpolator.opts['truncation'].value) @@ -192,9 +206,10 @@ class InterpolatorParam: self.assign(val) class InterpolatorVariable: - def __init__(self, variable): + def __init__(self, variable, smooth=False): self.scalar = False self.variable = variable + self.smooth = smooth def assign(self): pass @@ -212,6 +227,7 @@ class Interpolator: self.opts = {} self.sin_params = {} self.lerp_params = {} + self.smooth_params = {} self.load_disentangled_latents() def build(self): @@ -267,7 +283,7 @@ class Interpolator: encoding_stored = LerpParam('encoding_stored', shape=encoding_shape_np, datatype="noise") encoding_stored_sin = SinParam('encoding_orbit', shape=encoding_shape_np, datatype="noise", radius=0.05) encoding_stored_sum = encoding_stored.output + encoding_stored_sin.output - encoding_stored_mix = LerpParam('encoding_stored_mix', a_in=encoding_latent_placeholder, b_in=encoding_stored_sum, shape=encoding_shape_np, datatype="encoding") + encoding_stored_mix = LerpParam('encoding_stored_mix', a_in=encoding_latent_placeholder, b_in=encoding_stored_sum, shape=encoding_shape_np, datatype="encoding", smooth=True) # Use the placeholder to redirect parts of the graph. # - computed encoding goes into the encoding_mix @@ -284,6 +300,7 @@ class Interpolator: sys.stderr.write("Sin params: {}\n".format(", ".join(self.sin_params.keys()))) sys.stderr.write("Lerp params: {}\n".format(", ".join(self.lerp_params.keys()))) + sys.stderr.write("Smooth params: {}\n".format(", ".join(self.smooth_params.keys()))) sys.stderr.write("Opts: {}\n".format(", ".join(self.opts.keys()))) def load_disentangled_latents(self): @@ -372,6 +389,8 @@ class Interpolator: param.update() for param in self.lerp_params.values(): param.update() + for param in self.smooth_params.values(): + param.update() gen_images = sess.run(self.gen_img, feed_dict=self.get_feed_dict()) return gen_images @@ -426,18 +445,19 @@ class Listener: fp_out = os.path.join(app_cfg.DIR_RENDERS, '{}.mp4'.format(tag)) pipe = Popen([ 'ffmpeg', + '-hide_banner', '-y', '-f', 'image2pipe', '-vcodec', 'png', - '-r', '25', + '-r', FPS, '-i', '-', '-c:v', 'libx264', '-preset', 'slow', '-crf', '19', - '-vf', 'fps=25', + '-vf', 'fps=' + FPS, '-pix_fmt', 'yuv420p', '-s', '512x512', - '-r', '25', + '-r', FPS, fp_out ], stdin=PIPE, stdout=PIPE) |
