genforce / interfacegan

[CVPR 2020] Interpreting the Latent Space of GANs for Semantic Face Editing
https://genforce.github.io/interfacegan/
MIT License
1.51k stars 281 forks source link

Fix saving/exporting of models #14

Closed kklemon closed 5 years ago

kklemon commented 5 years ago

Saving/exporting models with torch.save relies on pickling the given network, which results in errors for the generator models in the code base as they use unpickable lambda functions when an identity operation needs to be used.

These changes replace the usage of lambda functions by their corresponding torch operations, enabling the saving of models.

The error can be simply reproduced for a given model using the following code:

import torch
from models.stylegan_generator import StyleGANGenerator

model = StyleGANGenerator('stylegan_ffhq')
torch.save(model.model, 'model.pt')

for the StyleGAN model and

import torch
from models.pggan_generator_model import PGGANGeneratorModel

model = PGGANGenerator('pggan_celebahq')
torch.save(model.model, 'model.pt')

for the PGGAN one.

Edit: the identity operation is a relatively new feature and has been merged in this PR.

ShenYujun commented 5 years ago

Good point! Thanks a lot!