(
    network_pkl: str,
    seed: int,
    num_rows: int,
    num_cols: int,
    resolution: int,
    num_phases: int,
    transition_frames: int,
    static_frames: int,
    truncation_psi: float,
    noise_mode: str,
    output: str
)
Generate gif using pretrained network pickle.

    Examples:

    \b
    python generate_gif.py --output=obama.gif --seed=0 --num-rows=1 --num-cols=8 \\
        --network=https://data-efficient-gans.mit.edu/models/DiffAugment-stylegan2-100-shot-obama.pkl
Generate gif using pretrained network pickle.
Generate gif using pretrained network pickle .
def generate_gif(
    network_pkl: str,
    seed: int,
    num_rows: int,
    num_cols: int,
    resolution: int,
    num_phases: int,
    transition_frames: int,
    static_frames: int,
    truncation_psi: float,
    noise_mode: str,
    output: str
):
    """Generate gif using pretrained network pickle.

    Examples:

    \b
    python generate_gif.py --output=obama.gif --seed=0 --num-rows=1 --num-cols=8 \\
        --network=https://data-efficient-gans.mit.edu/models/DiffAugment-stylegan2-100-shot-obama.pkl
    """
    print('Loading networks from "%s"...' % network_pkl)
    device = torch.device('cuda')
    with dnnlib.util.open_url(network_pkl) as f:
        G = legacy.load_network_pkl(f)['G_ema'].to(device) # type: ignore

    outdir = os.path.dirname(output)
    if outdir:
        os.makedirs(outdir, exist_ok=True)

    np.random.seed(seed)

    output_seq = []
    batch_size = num_rows * num_cols
    latent_size = G.z_dim
    latents = [np.random.randn(batch_size, latent_size) for _ in range(num_phases)]

    def to_image_grid(outputs):
        outputs = np.reshape(outputs, [num_rows, num_cols, *outputs.shape[1:]])
        outputs = np.concatenate(outputs, axis=1)
        outputs = np.concatenate(outputs, axis=1)
        return Image.fromarray(outputs).resize((resolution * num_cols, resolution * num_rows), Image.ANTIALIAS)
    
    def generate(dlatents):
        images = G.synthesis(dlatents, noise_mode=noise_mode)
        images = (images.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8).cpu().numpy()
        return to_image_grid(images)
    
    for i in range(num_phases):
        dlatents0 = G.mapping(torch.from_numpy(latents[i - 1]).to(device), None)
        dlatents1 = G.mapping(torch.from_numpy(latents[i]).to(device), None)
        for j in range(transition_frames):
            dlatents = (dlatents0 * (transition_frames - j) + dlatents1 * j) / transition_frames
            output_seq.append(generate(dlatents))
        output_seq.extend([generate(dlatents1)] * static_frames)
    
    if not output.endswith('.gif'):
        output += '.gif'
    output_seq[0].save(output, save_all=True, append_images=output_seq[1:], optimize=False, duration=50, loop=0)
def generate_gif ( network_pkl : str , seed : int , num_rows : int , num_cols : int , resolution : int , num_phases : int , transition_frames : int , static_frames : int , truncation_psi : float , noise_mode : str , output : str ) : print ( 'Loading networks from "%s"...' % network_pkl ) device = torch . device ( 'cuda' ) with dnnlib . util . open_url ( network_pkl ) as f : G = legacy . load_network_pkl ( f ) [ 'G_ema' ] . to ( device ) # type: ignore outdir = os . path . dirname ( output ) if outdir : os . makedirs ( outdir , exist_ok = True ) np . random . seed ( seed ) output_seq = [ ] batch_size = num_rows * num_cols latent_size = G . z_dim latents = [ np . random . randn ( batch_size , latent_size ) for _ in range ( num_phases ) ] def to_image_grid ( outputs ) : outputs = np . reshape ( outputs , [ num_rows , num_cols , * outputs . shape [ 1 : ] ] ) outputs = np . concatenate ( outputs , axis = 1 ) outputs = np . concatenate ( outputs , axis = 1 ) return Image . fromarray ( outputs ) . resize ( ( resolution * num_cols , resolution * num_rows ) , Image . ANTIALIAS ) def generate ( dlatents ) : images = G . synthesis ( dlatents , noise_mode = noise_mode ) images = ( images . permute ( 0 , 2 , 3 , 1 ) * 127.5 + 128 ) . clamp ( 0 , 255 ) . to ( torch . uint8 ) . cpu ( ) . numpy ( ) return to_image_grid ( images ) for i in range ( num_phases ) : dlatents0 = G . mapping ( torch . from_numpy ( latents [ i - 1 ] ) . to ( device ) , None ) dlatents1 = G . mapping ( torch . from_numpy ( latents [ i ] ) . to ( device ) , None ) for j in range ( transition_frames ) : dlatents = ( dlatents0 * ( transition_frames - j ) + dlatents1 * j ) / transition_frames output_seq . append ( generate ( dlatents ) ) output_seq . extend ( [ generate ( dlatents1 ) ] * static_frames ) if not output . endswith ( '.gif' ) : output += '.gif' output_seq [ 0 ] . save ( output , save_all = True , append_images = output_seq [ 1 : ] , optimize = False , duration = 50 , loop = 0 )