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

Cannot process pkl files using legacy.load_network_pkl #33

Open
metaphorz opened this issue Oct 6, 2021 · 8 comments
Open

Cannot process pkl files using legacy.load_network_pkl #33

metaphorz opened this issue Oct 6, 2021 · 8 comments

Comments

@metaphorz
Copy link

I am running a notebook called "structured dreaming". The notebook loads the pkl file using this:

print('Loading networks from "%s"...' % network_pkl) device = torch.device('cuda') with dnnlib.util.open_url(network_pkl) as f: G = legacy.load_network_pkl(f)['G_ema'].to(device) # type: ignore

For network_pkl, this works:

https://nvlabs-fi-cdn.nvidia.com/stylegan2-ada-pytorch/pretrained/ffhq.pkl'

but when I try one of your pkl files, for instance:

https://github.com/justinpinkney/awesome-pretrained-stylegan2#faces-FFHQ-config-f-512x512 (after downloading it first and then putting it into Gdrive), I get:

`Loading networks from "/content/drive/MyDrive/pretrained/ffhq-512-avg-tpurun1.pkl"...


ValueError Traceback (most recent call last)

in ()
5 device = torch.device('cuda')
6 with dnnlib.util.open_url(network_pkl) as f:
----> 7 G = legacy.load_network_pkl(f)['G_ema'].to(device) # type: ignore
8 #
9 # Read the data from the file

1 frames

/content/stylegan2-ada-pytorch/legacy.py in convert_tf_generator(tf_G)
153 unknown_kwargs = list(set(tf_kwargs.keys()) - known_kwargs)
154 if len(unknown_kwargs) > 0:
--> 155 raise ValueError('Unknown TensorFlow kwarg', unknown_kwargs[0])
156
157 # Collect params.

ValueError: ('Unknown TensorFlow kwarg', 'resolution_h')

`

Do you have any suggestions? Your list of pretrained networks is great!

@justinpinkney
Copy link
Owner

This network might have been trained with a slightly different repo than the original nvidia one. If you're lucky you might just be able to get away with ignoring that argument and it might load ok, otherwise it might take more effort to convert. To skip that check you could try just commenting out Lines 154 and 155 of legacy.py

@metaphorz
Copy link
Author

I wasn't lucky after legacy.py edits. I received another error on the legacy.load_network_pkl call. I also tried
a few other of the pkls, such as beetles, abstract art and received an error that the tf
"pickle version was too low".

I wonder if the problem is in how the structured dreaming notebook loads pkl files.
The notebook code is here. I wonder if there is a better loading method.

`device = torch.device('cuda')
print('Loading networks from "%s"...' % network_pkl)

for legacy tf models

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

@justinpinkney
Copy link
Owner

That code is calling into the nvidia stylegan-ada-pytorch functions to load the networks. Not quite quite sure what the exact issues are as I haven't tried loading thing in the pytorch version of stylegan. These were all loaded using the original StyleGAN2 (which should still work, but doesn't exactly help you).

Unfortunately this is really a consequence of NVidia's bad choice of pickle as a save format for their models.

One possible solution is to load the models in the tf stylegan2 repo, then save the actual values of weights e.g. to a npz file. Then create the equivalent size models in stylegan-ada-pytorch, load in the saved weights manually, then you should be able to use that (or save as a new pickle). Unfortunately this seems like quite a bit of not very fun work.

@metaphorz
Copy link
Author

Thanks for looking into this. I'll continue to investigate options.

@Chauban
Copy link

Chauban commented Feb 18, 2022

Thanks for looking into this. I'll continue to investigate options.

Did you solved this problem please?

@metaphorz
Copy link
Author

It has been ages since I looked into this, so feel free to close this ticket. Thanks.

@justinpinkney
Copy link
Owner

That error looks like the model wasn't trained using the original nvidia repo. As the keyword option resolution_h isn't in the original.

The original author of that model was aydao and their github is repo here: https://github.com/aydao/stylegan2-surgery That repo appears to have the resolution_h keyword so I'd try using that repo instead of the one you're using right now.

@metaphorz
Copy link
Author

I just forked the repo. good idea.

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

3 participants