(args)
Generate a video of the latent space walk (interpolation)
    Args:
        args: configuration used for the lsid
    Returns: None (writes generated video to disk)
Generate a video of the latent space walk (interpolation)
    Args:
        args: configuration used for the lsid
    Returns: None (writes generated video to disk)
Generate a video of the latent space walk ( interpolation ) Args : args : configuration used for the lsid Returns : None ( writes generated video to disk )
def latent_space_interpolation(args):
    """
    Generate a video of the latent space walk (interpolation)
    Args:
        args: configuration used for the lsid
    Returns: None (writes generated video to disk)
    """
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    # load the data from the trained-model
    print(f"loading data from the trained model at: {args.model_path}")
    generator = create_generator_from_saved_model(args.model_path).to(device)

    # total_frames in the video:
    total_frames = int(args.time * args.fps)

    # create the video from the latent space interpolation (walk)
    # all latent vectors for each and every frame:
    all_latents = torch.randn(total_frames, generator.latent_size).to(device)
    all_latents = gaussian_filter(all_latents.cpu(), [args.smoothing * args.fps, 0])
    all_latents = torch.from_numpy(all_latents).to(device)

    # create output directory
    args.output_path.parent.mkdir(parents=True, exist_ok=True)

    # make the cv2 video object
    print("Generating the video frames ...")
    generation_depth = (
        generator.depth if args.generation_depth is None else args.generation_depth
    )
    img_dim = 2 ** generation_depth
    video_out = cv2.VideoWriter(
        str(args.output_path),
        cv2.VideoWriter_fourcc(*"mp4v"),
        args.fps,
        (img_dim, img_dim),
    )

    # Run the main loop for the interpolation:
    with torch.no_grad():  # no need to compute gradients here :)
        for latent in tqdm(all_latents):
            latent = torch.unsqueeze(latent, dim=0)

            # generate the image for this latent vector:
            frame = post_process_generated_images(
                generator(latent, depth=generation_depth)
            )
            frame = frame[0, ..., ::-1]  # need to reverse the channel order for cv2 :D

            # write the generated frame to the video
            video_out.write(frame)

    print(f"video has been generated and saved to {args.output_path}")

    # don't forget to close the video stream :)
    video_out.release()
def latent_space_interpolation ( args ) : device = torch . device ( "cuda" if torch . cuda . is_available ( ) else "cpu" ) # load the data from the trained-model print ( f"loading data from the trained model at: {args.model_path}" ) generator = create_generator_from_saved_model ( args . model_path ) . to ( device ) # total_frames in the video: total_frames = int ( args . time * args . fps ) # create the video from the latent space interpolation (walk) # all latent vectors for each and every frame: all_latents = torch . randn ( total_frames , generator . latent_size ) . to ( device ) all_latents = gaussian_filter ( all_latents . cpu ( ) , [ args . smoothing * args . fps , 0 ] ) all_latents = torch . from_numpy ( all_latents ) . to ( device ) # create output directory args . output_path . parent . mkdir ( parents = True , exist_ok = True ) # make the cv2 video object print ( "Generating the video frames ..." ) generation_depth = ( generator . depth if args . generation_depth is None else args . generation_depth ) img_dim = 2 ** generation_depth video_out = cv2 . VideoWriter ( str ( args . output_path ) , cv2 . VideoWriter_fourcc ( * "mp4v" ) , args . fps , ( img_dim , img_dim ) , ) # Run the main loop for the interpolation: with torch . no_grad ( ) : # no need to compute gradients here :) for latent in tqdm ( all_latents ) : latent = torch . unsqueeze ( latent , dim = 0 ) # generate the image for this latent vector: frame = post_process_generated_images ( generator ( latent , depth = generation_depth ) ) frame = frame [ 0 , ... , : : - 1 ] # need to reverse the channel order for cv2 :D # write the generated frame to the video video_out . write ( frame ) print ( f"video has been generated and saved to {args.output_path}" ) # don't forget to close the video stream :) video_out . release ( )