stylegan-xl icon indicating copy to clipboard operation
stylegan-xl copied to clipboard

AttributeError: 'VisionTransformer' object has no attribute 'forward_flex'

Open swing148 opened this issue 1 year ago • 3 comments

Hello,I get an error when running gen_images.py. Traceback (most recent call last): File "gen_images.py", line 124, in generate_images() # pylint: disable=no-value-for-parameter File "/root/.local/lib/python3.8/site-packages/click/core.py", line 1130, in call return self.main(*args, **kwargs) File "/root/.local/lib/python3.8/site-packages/click/core.py", line 1055, in main rv = self.invoke(ctx) File "/root/.local/lib/python3.8/site-packages/click/core.py", line 1404, in invoke return ctx.invoke(self.callback, **ctx.params) File "/root/.local/lib/python3.8/site-packages/click/core.py", line 760, in invoke return __callback(*args, **kwargs) File "gen_images.py", line 98, in generate_images G = legacy.load_network_pkl(f)['G_ema'] File "/root/GANs/stylegan_xl/legacy.py", line 25, in load_network_pkl data = _LegacyUnpickler(f).load() File "/root/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1207, in getattr raise AttributeError("'{}' object has no attribute '{}'".format( AttributeError: 'VisionTransformer' object has no attribute 'forward_flex'

swing148 avatar Jul 08 '22 05:07 swing148

torch 1.9.1 torchvision 0.10.1

swing148 avatar Jul 08 '22 05:07 swing148

  • #55
  • #66

woctezuma avatar Jul 08 '22 06:07 woctezuma

I had the same error when using gen_images.py and was able to solve it. with Windows 10 OS, I generated images with my trained model.

I installed the environment according to the version in the requirements to solve this error. I also checked #55 and #66 but couldn't solve it.

I have a training file(.pkl), and the sample images generated during training was well generated as I wanted.

I thought there was no problem with training the model, but there was a problem with the way the saved file was loaded. My problem was not solved with #66, but I was inspired.

So, after loading the save file through the #66 approach, I slowly explored the contents of the op variable.

Finally I solved the problem. In #66, the solution is to search for "BINUNICODE", but my save file doesn't have that, so an assertion occurred.

I found that there was "SHORT_BINUNICODE" instead.

I don't know why my save file is saved as "SHORT_BINUNICODE" instead of "BINUNICODE" unlike #66, If you have not been able to solve the problem with the method #66, this method is recommended. (If there is a problem with either "SHORT_BINUNICODE" or "BINUNICODE", read the op[0] contents yourself, find "G_ema" or "D" in op[1])


The solution is well explained in #66, but let's rearrange it for readability. original code, in gen_images.py, line 97 - 99

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

replace original code with

G = load_g_ema(network_pkl)
G = G.eval().requires_grad_(False).to(device)
...

def load_g_ema(network_pkl):
    with open(network_pkl, 'rb') as handle:
        d_starts = -1
        g_ema_starts = -1
        for i, op in enumerate(pickletools.genops(handle)):
            if op[0].name == "SHORT_BINUNICODE":
                if op[1] == 'G_ema':
                    g_ema_starts = op[2]
                elif op[1] == 'D':
                    d_starts = op[2]
        assert d_starts >= 0 and g_ema_starts >= 0
    
    with open(network_pkl, 'rb') as handle:
        bs = handle.read()
        bs = bs[:d_starts] + bs[g_ema_starts:]
        obj = pickle.Unpickler(io.BytesIO(bs)).load()

    return obj['G_ema']

If assertion occurs, check op[0].name directly, and find the index of 'G_ema' and 'D' in op[1].

withdongyeong avatar Aug 30 '22 12:08 withdongyeong