Skip to content

Instantly share code, notes, and snippets.

@tc87
Last active March 31, 2020 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tc87/451b765a7d23c7b210b9135a41dbf910 to your computer and use it in GitHub Desktop.
Save tc87/451b765a7d23c7b210b9135a41dbf910 to your computer and use it in GitHub Desktop.
@st.cache(show_spinner=False, hash_funcs={tf.Session: id})
def generate_image(session, pg_gan_model, tl_gan_model, features, feature_names):
# Create rescaled feature vector.
feature_values = np.array([features[name] for name in feature_names])
feature_values = (feature_values - 50) / 250
# Multiply by Shaobo's matrix to get the latent variables.
latents = np.dot(tl_gan_model, feature_values)
latents = latents.reshape(1, -1)
dummies = np.zeros([1] + pg_gan_model.input_shapes[1][1:])
# Feed the latent vector to the GAN in TensorFlow.
with session.as_default():
images = pg_gan_model.run(latents, dummies)
# Rescale and reorient the GAN's output to make an image.
images = np.clip(np.rint((images + 1.0) / 2.0 * 255.0),
0.0, 255.0).astype(np.uint8) # [-1,1] => [0,255]
if USE_GPU:
images = images.transpose(0, 2, 3, 1) # NCHW => NHWC
return images[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment