Hey there!
I’ve been playing around with plotting images generated from StyleGAN3, but there seems to be something funky going on with the color channels that i can’t really pin down.
Generating an image with:
device = torch.device('cuda')
# open pickle file and load generator
network_pkl = './models/stylegan3-r-afhqv2-512x512.pkl'
with dnnlib.util.open_url(network_pkl) as f:
G = legacy.load_network_pkl(f, force_fp16=True)['G_ema'].cuda()
noise_mode = 'const'
trunc = 1
seed = 10
z = torch.from_numpy(np.random.RandomState(seed).randn(1, G.z_dim)).to(device)
# returns [b, c, 512, 512] tensor
img = G(z, truncation_psi=trunc, noise_mode=noise_mode, c=None)
# permute to [b, 512, 512, c] and scale to 0-255
img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
# remove batch channel
img = img.squeeze(0)
# move tensors to cpu
img_cpu = img.cpu()
# plot with pyplot
plt.imshow(img_cpu.numpy())
# save with pillow
PIL.Image.fromarray(img_cpu.numpy(), 'RGB').save(f'seed{seed}.png')
From my understanding the generator of StyleGAN3 returns an image as a tensor of [batch_size, channels, height, width] with channels being RGB. The plotted image seems to be have completely wrong color information.
From plt.imshow(img_cpu.numpy()):
Saving with PIL.Image.fromarray(img_cpu.numpy(), 'RGB').save(f'seed{seed}.png'):
That’s quite strange, as I can load your second image via PIL.Image.open and visualize it in PIL as well as matplotlib to get the same output.
Did you change any colormaps in matplotlib or loaded any other template (e.g. via seaborn)?
Yeah, it’s unrelated to PyTorch, but still interesting as I haven’t seen the issue before.
It doesn’t look like a perfectly inverted image though.
Using your code snippet I get the same image back and inverting it naively via:
Good to hear you’ve found the issue.
I’m not familiar with this IDE, but do you have an idea why colors seems to be inverted or at least remapped? That seems to be unexpected (unless it’s of course a bug, then it’ll get fixed I guess).
EDIT: Also, just out of interest: could you still post the red, green, blue outputs?
Yeah, weird indeed. I was thinking if the color scheme might interact with matplotlib and override the colors or rather be globally applied to the entire IDE. In any case, you might forward the issue to the IDE devs.
Oh man, this feature which is active by default has cost me hours of my life. Why would I want the image outputs to be inverted? It’s great to have more features but they should leave this off by default.
Yes, I totally agree and don’t know the reason why image outputs should be remapped using the IDE color scheme by default (it also took some time and imagination to figure out what might be causing the issue).
Maybe @clbr forwarded the feedback to the IDE devs so that the default might be changed?