Skip to content

Instantly share code, notes, and snippets.

@phillipi
Last active October 8, 2023 01:25
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phillipi/d2921f2d4726d7e3cdac7a4780c6050a to your computer and use it in GitHub Desktop.
Save phillipi/d2921f2d4726d7e3cdac7a4780c6050a to your computer and use it in GitHub Desktop.
Slerp through the BigGAN latent space
# to be used in conjunction with the functions defined here:
# https://colab.research.google.com/github/tensorflow/hub/blob/master/examples/colab/biggan_generation_with_tf_hub.ipynb
# party parrot transformation
noise_seed_A = 3 # right facing
noise_seed_B = 31 # left facing
num_interps = 14
truncation = 0.2
category = 14
def slerp(A, B, num_interps):
# see https://en.wikipedia.org/wiki/Slerp
alphas = np.linspace(-1.5, 2.5, num_interps) # each unit step tends to be a 90 degree rotation in high-D space, so this is ~360 degrees
omega = np.zeros((A.shape[0],1))
for i in range(A.shape[0]):
tmp = np.dot(A[i],B[i])/(np.linalg.norm(A[i])*np.linalg.norm(B[i]))
omega[i] = np.arccos(np.clip(tmp,0.0,1.0))+1e-9
return np.array([(np.sin((1-a)*omega)/np.sin(omega))*A + (np.sin(a*omega)/np.sin(omega))*B for a in alphas])
def slerp_and_shape(A, B, num_interps):
interps = slerp(A, B, num_interps)
return (interps.transpose(1, 0, *range(2, len(interps.shape)))
.reshape(num_interps, *interps.shape[2:]))
z_A, z_B = [truncated_z_sample(num_samples, truncation, noise_seed)
for noise_seed in [noise_seed_A, noise_seed_B]]
z_slerp = slerp_and_shape(z_A, z_B, num_interps)
ims_slerp = sample(sess, z_slerp, one_hot([category] * num_interps), truncation=truncation)
imshow(imgrid(ims_slerp, cols=num_interps))
# output video
import cv2
video_name = 'video.mov'
_, height, width, _ = ims_slerp.shape
video = cv2.VideoWriter(video_name, cv2.VideoWriter_fourcc('M','J','P','G'), fps=24, frameSize=(width,height))
for iter in range(0,ims_slerp.shape[0]):
video.write(ims_slerp[iter,:,:,::-1])
cv2.destroyAllWindows()
video.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment