justinpinkney/awesome-pretrained-stylegan2

Cannot process pkl files using legacy.load_network_pkl

Opened this issue · 8 comments

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!

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

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)`

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.

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

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

Did you solved this problem please?

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

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.

I just forked the repo. good idea.