Last active
March 31, 2020 15:33
-
-
Save tc87/451b765a7d23c7b210b9135a41dbf910 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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