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

Error during learning process #38

Closed
hw1204 opened this issue Aug 3, 2022 · 5 comments
Closed

Error during learning process #38

hw1204 opened this issue Aug 3, 2022 · 5 comments

Comments

@hw1204
Copy link

hw1204 commented Aug 3, 2022

I found this error while running finetune_dualstylegan.py in the last step of learning.
I changed it to jpg because I thought the format of the picture might be a problem, but there must be a problem.
I would appreciate it if you could tell me how to solve it.

load model: /content/drive/MyDrive/generator-pretrain.pt
Loading pSp from checkpoint: /content/drive/MyDrive/encoder.pt
Loading ResNet ArcFace
Encoder model successfully loaded!
Traceback (most recent call last):
File "finetune_dualstylegan.py", line 529, in
Simg = transform2(Simg).unsqueeze(dim=3)
File "/usr/local/lib/python3.7/dist-packages/torchvision/transforms/transforms.py", line 67, in call
img = t(img)
File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 727, in call_impl
result = self.forward(*input, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/torchvision/transforms/transforms.py", line 226, in forward
return F.normalize(tensor, self.mean, self.std, self.inplace)
File "/usr/local/lib/python3.7/dist-packages/torchvision/transforms/functional.py", line 284, in normalize
tensor.sub
(mean).div_(std)
RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0

@williamyang1991
Copy link
Owner

I changed it to jpg because I thought the format of the picture might be a problem, but there must be a problem.

So the error occurs when you use jpg or png?
The code uses mean and std to adjust 3 channels of your data.

RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0

It seems that your data has 4 channels.

    transform2 = transforms.Compose(
    [
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True),
    ])     

File "finetune_dualstylegan.py", line 529, in
Simg = transform2(Simg).unsqueeze(dim=3)

My original code is Simg = transform2(Simg).unsqueeze(dim=0)
Did you make modifications to the code, which leads to the error?

@hw1204
Copy link
Author

hw1204 commented Aug 3, 2022

@williamyang1991 It was originally a png format, but I changed it to jpg, but there is still an error. I tried your original code, but it's the same! Do you think the png format is a problem?

@williamyang1991
Copy link
Owner

you can add
print(np.array(Simg).shape) before
Simg = transform2(Simg).unsqueeze(dim=0)
to see the shape of your Simg.
It should be (1024, 1024, 3)
otherwise, there is something wrong with your style image.

@hw1204
Copy link
Author

hw1204 commented Aug 7, 2022

Alpha Channel was the problem! Thanks for letting me know. One question is that input image is definitely channel 3, but Simg is still channel 4. Can I assume there was a malfunction in the middle?

@williamyang1991
Copy link
Owner

My code is just read the image and transform it.

Simg = Image.open(os.path.join(path, filename))
Simg = transform2(Simg).unsqueeze(dim=0)

It was originally a png format, but I changed it to jpg

It should be that you incorrectly changed png to jpg without discarding its alpah channel.

This issue was closed.
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