Closed gongmm closed 2 years ago
Hi, could you please provide a minimum reproducible code to help with debug?
Notice that we may need to use different style codes for AnycostGAN and StyleGAN. If you use the style code from AnycostGAN with StyleGAN, it might reproduce weird results.
thank! When I used style codes for StyleGAN, the bug is solved. But I found another problem when changing the output resolution. For StyleGAN, when use the default resolution the output image is perfect But when chang the output resolution such as self.generator.target_res = 256 the output image go wrong.
Could you provide a code segment to reproduce the issue?
from glob import glob
import torch
import numpy as np
import os
from PIL import Image
import models
class FaceEditor:
def __init__(self, config, device, anycost_resolution=1024, n_style_to_change=12):
# load assets
self.device = device
self.anycost_channel = 1.0
self.anycost_resolution = anycost_resolution
self.n_style_to_change = n_style_to_change
# build the generator
self.generator = models.get_pretrained('generator', config).to(device)
self.generator.eval()
self.generator.target_res = anycost_resolution # set resolution
# self.generator.target_res = self.anycost_resolution
self.mean_latent = self.generator.mean_style(10000)
# select only a subset of the directions to use
direction_map = {
'smiling': '31_Smiling',
'young': '39_Young',
'wavy hair': '33_Wavy_Hair',
'gray hair': '17_Gray_Hair',
'blonde hair': '09_Blond_Hair',
'eyeglass': '15_Eyeglasses',
'mustache': '22_Mustache',
}
boundaries = models.get_pretrained('boundary', config)
self.direction_dict = dict()
for k, v in boundaries.items():
self.direction_dict[k] = v.view(1, 1, -1)
def generate_image(self, save_path, input_kwargs):
def image_to_np(x):
assert x.shape[0] == 1
x = x.squeeze(0).permute(1, 2, 0)
x = (x + 1) * 0.5 # 0-1
x = (x * 255).cpu().numpy().astype('uint8')
return x
with torch.no_grad():
out = self.generator(**input_kwargs)[0].clamp(-1, 1)
out = image_to_np(out)
out = np.ascontiguousarray(out)
img_pil = Image.fromarray(out)
img_pil.save(save_path)
def edit(self, latent_code_path, attr_sliders, force_full_g=False):
latent_code = torch.from_numpy(np.load(os.path.join(latent_code_path))).view(1, -1, 512).to(self.device)
# input kwargs for the generator
edited_code = latent_code.clone()
for direction_name in attr_sliders.keys():
edited_code[:, :self.n_style_to_change] = edited_code[:, :self.n_style_to_change] \
+ attr_sliders[direction_name] * self.direction_dict[
direction_name].to(self.device)
edited_code = edited_code.to(self.device)
return latent_code, edited_code
if __name__ == '__main__':
gan_config = 'stylegan2-ffhq-config-f'
fe = FaceEditor(config=gan_config, anycost_resolution=512, device='cuda:0')
# fe = FaceEditor(config=gan_config, device='cuda:0')
attr_sliders = {
'31_Smiling': 0.2
}
latent_path = "demo.npy"
ori, edit = fe.edit(latent_code_path=latent_path, attr_sliders=attr_sliders)
ori_kwargs = {'styles': ori, 'noise': None, 'randomize_noise': False, 'input_is_style': True}
edit_kwargs = {'styles': edit, 'noise': None, 'randomize_noise': False, 'input_is_style': True}
fe.generate_image(save_path="origin.png", input_kwargs=ori_kwargs)
fe.generate_image(save_path="edit.png", input_kwargs=edit_kwargs)
demo.npy is here : https://drive.google.com/file/d/1cx_6epZjIH9uoADo744ySQQan15dL4Fa/view?usp=sharing this is the output image when changing anycost_resolution to 1024 the image turn to
Hi, sorry for the late reply. You are using the original StyleGAN2 instead of AnycostGAN.
You need to replace gan_config = 'stylegan2-ffhq-config-f'
with gan_config = 'anycost-ffhq-config-f'
.
I tested it and it works normally, so I will close the issue for now. But feel free to reopen if you encounter other problems.
image generate by config anycost-ffhq-config-f is pretty fine, but there the image generate with config stylegan2-ffhq-config-f is wrong. How can I fix the bug? Thankyou