yuval-alaluf / stylegan3-editing

Official Implementation of "Third Time's the Charm? Image and Video Editing with StyleGAN3" (AIM ECCVW 2022) https://arxiv.org/abs/2201.13433
https://yuval-alaluf.github.io/stylegan3-editing/
MIT License
654 stars 73 forks source link

pkl to pt conversion #16

Closed vedantdere closed 2 years ago

vedantdere commented 2 years ago

How to convert pkl file to pt ?

woctezuma commented 2 years ago

https://github.com/yuval-alaluf/stylegan3-editing/blob/e2a63fc7f072c4f8b8a7ba1dac8c7f87a8eb1647/models/stylegan3/legacy.py#L300-L301

woctezuma commented 2 years ago

The function does not support StyleGAN3 checkpoints, I believe.

https://github.com/yuval-alaluf/stylegan3-editing/blob/e2a63fc7f072c4f8b8a7ba1dac8c7f87a8eb1647/models/stylegan3/legacy.py#L303-L304

hqnicolas commented 2 years ago

How did you create the weights .PT Files For StyleGan3? https://github.com/yuval-alaluf/stylegan3-editing#stylegan3-encoder testing rosinality pkl to pt

yuval-alaluf commented 2 years ago

I believe this is what you guys want:

path = "/path/to/your/sg3.pkl"

with open(path, "rb") as f:
    decoder = pickle.load(f)['G_ema'].cuda()

state_dict = decoder.state_dict()
torch.save(state_dict, "/path/to/your/sg3.pt") 

Let me know if this works as I haven't tested it out yet

yuval-alaluf commented 2 years ago

@hotnikq , I'm not sure I follow here. Please open a new issue if you are having a problem running something. And please provide a clear, detailed summary of what you're trying to run.

yuval-alaluf commented 2 years ago

@hotnikq , you're saying the code I posted above did not work for you on the stylegan3-r-ffhqu-256x256.pkl file?

yuval-alaluf commented 2 years ago

How did you create the weights .PT Files For StyleGan3?

I have answered the question above and provided the code that we used to convert pkl files to pt files. I am really not sure why you are still trying to use the legacy.py file after it was noted above that this file is not relevant for SG3 models. And @hotnikq , it is quite difficult to follow these threads with so many comments. Please try to limit your comments when possible to make it easier for myself and others to follow these threads.

hqnicolas commented 2 years ago

I believe this is what you guys want:

path = "/path/to/your/sg3.pkl"

with open(path, "rb") as f:
    decoder = pickle.load(f)['G_ema'].cuda()

state_dict = decoder.state_dict()
torch.save(state_dict, "/path/to/your/sg3.pt") 

Let me know if this works as I haven't tested it out yet

Testing now !

hqnicolas commented 2 years ago
import pickle
from enum import Enum
from pathlib import Path
from typing import Optional

import torch

checkpoint_path = "pretrained_models/stylegan3-r-ffhqu-256x256.pkl"

print(f"Loading StyleGAN3 generator from path: {checkpoint_path}")
with open(checkpoint_path, "rb") as f:
    decoder = pickle.load(f)['G_ema'].cuda()
print('Done!')

state_dict = decoder.state_dict()
torch.save(state_dict, "pretrained_models/stylegan3-r-ffhqu-256x256.pt")

print('Done!')

This code works just fine the topic ends here!

now i need to adapt the generator model to math the checkpoint RuntimeError: Error(s) in loading state_dict for Generator: size mismatch for synthesis.L3_52_1024.up_filter: copying a param with shape torch.Size([24]) from checkpoint, the shape in current model is torch.Size([12]).

yuval-alaluf commented 2 years ago

I am happy to see that the code I gave you above works fine and that we can now close this issue. If you have an issue that is unrelated to this, please open a new issue with a concise yet clear explanation of the problem you are facing