Coder Social home page Coder Social logo

Comments (5)

tonylins avatar tonylins commented on May 20, 2024

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.

from anycost-gan.

gongmm avatar gongmm commented on May 20, 2024

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
image
image
But when chang the output resolution such as self.generator.target_res = 256 the output image go wrong.
image

from anycost-gan.

tonylins avatar tonylins commented on May 20, 2024

Could you provide a code segment to reproduce the issue?

from anycost-gan.

gongmm avatar gongmm commented on May 20, 2024
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
image
when changing anycost_resolution to 1024 the image turn to
image

from anycost-gan.

tonylins avatar tonylins commented on May 20, 2024

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.

from anycost-gan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.