Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to save the pkl file from colab? #42

Closed
nicolai256 opened this issue Apr 8, 2022 · 7 comments
Closed

how to save the pkl file from colab? #42

nicolai256 opened this issue Apr 8, 2022 · 7 comments

Comments

@nicolai256
Copy link

nicolai256 commented Apr 8, 2022

i'm using this colab repo
the new pkl file should end up in the checkpoints folder right?
or do i have to add some particular lines of code?
https://github.com/rinongal/StyleGAN-nada/blob/StyleGAN3-NADA/stylegan3_nada.ipynb

edit:
found the code but it doesn't work in the normal stylegan, is that right? and if so, how do i use it after generating?
i also found out how to get the .pkl files with the save_interval but those don't work with the normal stylegan either
model_name = "network-snapshot-011120.pt"
torch.save(
{
"g_ema": net.generator_trainable.generator.state_dict(),
"g_optim": g_optim.state_dict(),
},
f"{ckpt_dir}/{model_name}",
)
!ls /content/output/checkpoint

is there any way to convert the file into a normal stylegan model again like this repo states it can convert a stylegan2-nada.pt to stylegan2.pkl so maybe this is possible for stylegan3?
https://github.com/eps696/stylegan2ada
all my pictures are slightly tilted to the left and i normally use the visualizer to fix that but it doesnt work with these files :(

@rinongal
Copy link
Owner

The .pt models are pytorch checkpoints, they'll work with rosinality's stylegan2 code but not with the official implementations.

If you're working with SG3, you should just save the model as we do in this if block. This produces a pkl checkpoint that you can then use with the official SG3 code.

@nicolai256
Copy link
Author

nicolai256 commented Apr 10, 2022

The .pt models are pytorch checkpoints, they'll work with rosinality's stylegan2 code but not with the official implementations.

If you're working with SG3, you should just save the model as we do in this if block. This produces a pkl checkpoint that you can then use with the official SG3 code.

when i try to use the generated pkl in stylegan3 it gives me an error (it only exports the save_intervals, not the actual trained model (training_iterations))
stylegan3 error =

Loading networks from "/content/drive/MyDrive/000150.pkl"...
Traceback (most recent call last):
  File "/content/drive/MyDrive/WIP/stylegan3/gen_images.py", line 143, in <module>
    generate_images() # pylint: disable=no-value-for-parameter
  File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.7/dist-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/content/drive/MyDrive/WIP/stylegan3/gen_images.py", line 108, in generate_images
    G = legacy.load_network_pkl(f)['G_ema'].to(device) # type: ignore
  File "/content/drive/MyDrive/WIP/stylegan3/legacy.py", line 40, in load_network_pkl
    assert isinstance(data['G'], torch.nn.Module)
KeyError: 'G'

@rinongal
Copy link
Owner

rinongal commented Apr 11, 2022

What StyleGAN3 script are you trying to run? If it's one of the generation scripts, could you try to change the loading code from:

with dnnlib.util.open_url(network_pkl) as f:
    G = legacy.load_network_pkl(f)['G_ema'].to(device)

to:

with dnnlib.util.open_url(network_pkl) as f:
    G = pickle.load(f)['G_ema'].to(device)

and let me know if that works?
You may also have to import pickle

@nicolai256
Copy link
Author

that worked, thanks!

@rinongal
Copy link
Owner

Glad to hear. Closing the issue. Feel free to re-open or open a new issue if you need more help.

@nicolai256
Copy link
Author

it seems like the colab only saves the previous save_interval, for example if i put the number of save_interval on for example 400 and training_iterations on 800 it only seems to save 000400.pkl and not 000800.pkl, how do i fix this? the model definitely exists somewhere because i can generate pictures with it but it doesn't appear anywhere

@rinongal
Copy link
Owner

Set the training_iterations to 801 and it should work get you that last checkpoint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants