Skip to content

Instantly share code, notes, and snippets.

@Mason-McGough
Last active June 25, 2021 17:33
Show Gist options
  • Save Mason-McGough/0bce4949aeef05a93182cdd25bd66811 to your computer and use it in GitHub Desktop.
Save Mason-McGough/0bce4949aeef05a93182cdd25bd66811 to your computer and use it in GitHub Desktop.
GIRAFFE object rotation method
# adapted from https://github.com/autonomousvision/giraffe (MIT License)
class Renderer(object):
# ...
def render_object_rotation(self, img_out_path, batch_size=15, n_steps=32):
gen = self.generator
bbox_generator = gen.bounding_box_generator
n_boxes = bbox_generator.n_boxes
# Set rotation range
is_full_rotation = (bbox_generator.rotation_range[0] == 0
and bbox_generator.rotation_range[1] == 1)
n_steps = int(n_steps * 2) if is_full_rotation else n_steps
r_scale = [0., 1.] if is_full_rotation else [0.1, 0.9]
# Get Random codes and bg rotation
latent_codes = gen.get_latent_codes(batch_size, tmp=self.sample_tmp)
bg_rotation = gen.get_random_bg_rotation(batch_size)
# Set Camera
camera_matrices = gen.get_camera(batch_size=batch_size)
s_val = [[0, 0, 0] for i in range(n_boxes)]
t_val = [[0.5, 0.5, 0.5] for i in range(n_boxes)]
r_val = [0. for i in range(n_boxes)]
s, t, _ = gen.get_transformations(s_val, t_val, r_val, batch_size)
out = []
for step in range(n_steps):
# Get rotation for this step
r = [step * 1.0 / (n_steps - 1) for i in range(n_boxes)]
r = [r_scale[0] + ri * (r_scale[1] - r_scale[0]) for ri in r]
r = gen.get_rotation(r, batch_size)
# define full transformation and evaluate model
transformations = [s, t, r]
with torch.no_grad():
out_i = gen(batch_size, latent_codes, camera_matrices,
transformations, bg_rotation, mode='val')
out.append(out_i.cpu())
out = torch.stack(out)
out_folder = join(img_out_path, 'rotation_object')
makedirs(out_folder, exist_ok=True)
self.save_video_and_images(
out, out_folder, name='rotation_object',
is_full_rotation=is_full_rotation,
add_reverse=(not is_full_rotation))
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment