anvoynov / GANLatentDiscovery

The authors official implementation of Unsupervised Discovery of Interpretable Directions in the GAN Latent Space
416 stars 52 forks source link

Why using wrapper function for generating "shifted" images? #20

Closed chi0tzp closed 3 years ago

chi0tzp commented 3 years ago

May I ask you why you chose to use the following code snippet in order to generate images from shifted latent codes, instead of, for instance, using the standard GAN's generator forward function?

import types
from functools import wraps

def add_forward_with_shift(generator):
    def gen_shifted(self, z, shift, *args, **kwargs):
        return self.forward(z + shift, *args, **kwargs)

    generator.gen_shifted = types.MethodType(gen_shifted, generator)
    generator.dim_shift = generator.dim_z

def gan_with_shift(gan_factory):
    @wraps(gan_factory)
    def wrapper(*args, **kwargs):
        gan = gan_factory(*args, **kwargs)
        add_forward_with_shift(gan)
        return gan

    return wrapper

More specifically, for getting images from latent codes z you apply:

imgs = G(z)

while for getting images from shifted latent codes z+shift, you apply:

imgs_shifted = G.gen_shifted(z, shift)

Is there any particular benefits of doing so, instead of getting the "shifted" images as follows?

imgs_shifted = G(z, shift)

Thank you.

anvoynov commented 3 years ago

Hi @chi0tzp , this lets you run G.gen_shifted(z, h) for stylegan with z in the original latent space and h in the W-sapce

chi0tzp commented 3 years ago

Hi @anvoynov, thanks for your prompt response. So there's no need/benefit of using it if the shifts are restricted only to the latent space; is this correct? I'm asking that because you use it by default, for any GAN type. Thanks again.

anvoynov commented 3 years ago

Yep, that's right!

chi0tzp commented 3 years ago

Thank you! Closing the issue now.