rosinality / stylegan2-pytorch

Implementation of Analyzing and Improving the Image Quality of StyleGAN (StyleGAN 2) in PyTorch
MIT License
2.74k stars 623 forks source link

how to convert the "args" "optim_G" and "optim_D" in convert_weight.py? #105

Open GreenLimeSia opened 4 years ago

GreenLimeSia commented 4 years ago

I want to convert ckpt = {"g_ema": state_dict, "latent_avg": latent_avg, "args": tf_args, "optim_G": optm_G.state_dict, "optim_D": optm_D.state_dict}? But I don't know how to do this? Would you mind help me ?

rosinality commented 4 years ago

You should make code for it. But I think it could be not very easy as optimizer state_dict is somewhat complicated.

GreenLimeSia commented 4 years ago

Thanks for your reply! I will do it soon!

abelghazinyan commented 4 years ago

@GreenLimeSia Have you managed to export "optim_G" and "optim_D"?

GreenLimeSia commented 4 years ago

@abelghazinyan I do not export "optim_G" and "optim_D", but I have another scheme for using "optim_G" and "optim_D" in PyTorch. We can set it manually like this:

`g_ckpt = torch.load(args.g_ckpt, map_location=lambda storage, loc: storage)
# g_args = g_ckpt['args']

args.size = 1024  # g_args.size
args.latent = 512  # g_args.latent
args.n_mlp = 8  # g_args.n_mlp
args.channel_multiplier = 2  # g_args.channel_multiplier`

For optimizer parameters, we can choose not to load it for the first time when we use it, then train the model and save the new parameters, then load it. From the results of my experiment, It also works well by using this scheme.

240600

abelghazinyan commented 4 years ago

I can generate 1024x1024 images using the instructions provided in https://github.com/bryandlee/FreezeG, but the problem is that I want to finetune that model and in train.py "optim_G" and "optim_D" are required

rosinality commented 4 years ago

Hmm hard to know the use case of yours. Official tensorflow implementations does not includes optimizer states in the checkpoints, so by default you cannot get optimizer states from it. And I think you don't need to use optimizer states for most use cases. You can just skip loading state dict of optimizers.

abelghazinyan commented 4 years ago

I am trying to do transfer learning on the 1024x1024 model with https://github.com/bryandlee/FreezeG, and there optimizer states are required to continue finetuning on the pretrained network.

rosinality commented 4 years ago

Is reusing optimizer states crucial to FreezeG? I think it will be not very problematic to run without optimizer states.