コード例 #1
0
    def setup_latents(self, dim_noise):
        self.origin = np.random.randn(1, dim_noise).astype('float32')
        self.noise_vars = [
            var for name, var in self.Gs.components.synthesis.vars.items()
            if name.startswith('noise')
        ]
        self.noise_values = [
            np.random.randn(*var.shape.as_list()).astype('float32')
            for var in self.noise_vars
        ]
        self.noise_values2 = [
            np.random.randn(*var.shape.as_list()).astype('float32')
            for var in self.noise_vars
        ]
        tflib.set_vars({
            var: self.noise_values[idx]
            for idx, var in enumerate(self.noise_vars)
        })

        self.latents = np.random.randn(1, dim_noise).astype('float32')
        self.dlatents = self.Gs.components.mapping.run(self.latents, None)
        self.chroma = random_orthonormal(12, dim_noise)
        self.rotation = random_rotation()
        self.rotation = fractional_rotation(self.rotation, 1 / 4)

        self.mfcc_buffer = np.zeros((64, 64), dtype='float32')
コード例 #2
0
def generate_images(network_pkl, seeds, truncation_psi):
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]

    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False
    if truncation_psi is not None:
        Gs_kwargs.truncation_psi = truncation_psi

    L = []
    for seed_idx, seed in enumerate(seeds):
        rnd = np.random.RandomState(seed)
        z = rnd.randn(1, *Gs.input_shape[1:]) # [minibatch, component]        
        vars = {var: rnd.randn(*var.shape.as_list()) for var in noise_vars}
        L.append((seed, z, vars))
    L0 = L[0]
    L = L[1:]
        
    for i in range (200):
        random.shuffle(L)
        #L0, L1 = L[0], L[1]
        L1 = L[0]
        r = random.random()
        z = r*L0[1] + (1-r)*L1[1]
        vars = { var : r*L0[2][var] + (1-r)*L1[2][var] for var in noise_vars }
        tflib.set_vars(vars) # [height, width]           
        images = Gs.run(z, None, **Gs_kwargs) # [minibatch, height, width, channel]
        path = 'seed{:010d}-{:010d}-{:03d}.png'.format(L0[0], L1[0], int(r*1000))
        print(path)
        PIL.Image.fromarray(images[0], 'RGB').save(dnnlib.make_run_dir_path(path))
コード例 #3
0
ファイル: main.py プロジェクト: bobycv06fpm/Docker_FaceMaker
def generate_images(network_pkl, num, truncation_psi=0.5):
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    noise_vars = [
        var for name, var in Gs.components.synthesis.vars.items()
        if name.startswith('noise')
    ]

    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8,
                                      nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False
    if truncation_psi is not None:
        Gs_kwargs.truncation_psi = truncation_psi
    for i in range(num):
        print('Generating image %d/%d ...' % (i, num))

        # Generate random latent
        z = np.random.randn(1, *Gs.input_shape[1:])  # [minibatch, component]

        # Save latent
        txt_filename = 'results/generate_codes/' + str(i).zfill(4) + '.txt'
        with open(txt_filename, 'w') as f:
            text_save(f, z)

        # Generate image
        tflib.set_vars(
            {var: np.random.randn(*var.shape.as_list())
             for var in noise_vars})  # [height, width]
        images = Gs.run(z, None,
                        **Gs_kwargs)  # [minibatch, height, width, channel]

        # Save image
        PIL.Image.fromarray(images[0], 'RGB').save(
            dnnlib.make_run_dir_path('results/' + str(i) + '.png'))
コード例 #4
0
def draw_noise_components_figure(png, Gs, w, h, seeds, noise_ranges, flips):
    print(png)
    Gsc = Gs.clone()
    noise_vars = [
        var for name, var in Gsc.components.synthesis.vars.items()
        if name.startswith('noise')
    ]
    noise_pairs = list(zip(noise_vars,
                           tflib.run(noise_vars)))  # [(var, val), ...]
    latents = np.stack(
        np.random.RandomState(seed).randn(Gs.input_shape[1]) for seed in seeds)
    all_images = []
    for noise_range in noise_ranges:
        tflib.set_vars({
            var: val * (1 if i in noise_range else 0)
            for i, (var, val) in enumerate(noise_pairs)
        })
        range_images = Gsc.run(latents,
                               None,
                               truncation_psi=1,
                               randomize_noise=False,
                               **synthesis_kwargs)
        range_images[flips, :, :] = range_images[flips, :, ::-1]
        all_images.append(list(range_images))

    canvas = PIL.Image.new('RGB', (w * 2, h * len(seeds)), 'white')
    for col, col_images in enumerate(zip(*all_images)):
        canvas.paste(PIL.Image.fromarray(col_images[0], 'RGB'), (0, col * h))
        canvas.paste(PIL.Image.fromarray(col_images[1], 'RGB'), (w, col * h))
        # canvas.paste(PIL.Image.fromarray(col_images[1], 'RGB'), (col * w + w//2, 0))
        #canvas.paste(PIL.Image.fromarray(col_images[2], 'RGB').crop((0, 0, w//2, h)), (col * w, h))
        #canvas.paste(PIL.Image.fromarray(col_images[3], 'RGB').crop((w//2, 0, w, h)), (col * w + w//2, h))
    canvas.save(png)
コード例 #5
0
def vary_seeds(network_pkl, seeds, psi0, psi1, save_noise, q, count):
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    #noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]
    noise_vars_dict = {name:var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')}

    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False
    #if truncation_psi is not None:
    #    Gs_kwargs.truncation_psi = truncation_psi

    for seed_idx, seed in enumerate(seeds):
        print('Generating image for seed %d (%d/%d) ...' % (seed, seed_idx, len(seeds)))
        rnd = np.random.RandomState(seed)
        z = rnd.randn(1, *Gs.input_shape[1:]) # [minibatch, component]
        z2 = z
        noise_seed = rnd.randint(0,1000000)
        rnd_noise = np.random.RandomState(noise_seed)
        vars = {var: rnd_noise.randn(*var.shape.as_list()) for name, var in noise_vars_dict.items()}
        truncation_psi = psi0 + (psi1-psi0)*np.random.random()
        Gs_kwargs.truncation_psi = truncation_psi        
        for i in range (count):
            tflib.set_vars(vars) # [height, width]           
            images = Gs.run(z2, None, **Gs_kwargs) # [minibatch, height, width, channel]            
            im = PIL.Image.fromarray(images[0], 'RGB')  #.save(dnnlib.make_run_dir_path('varysingle_%04d.png' % i))
            fn = 'vary_seed_%08d.jpg'%seed
            metadata = {'z':z2, 'truncation_psi':truncation_psi}            
            if save_noise:
                metadata_noise_vars = { name : vars[var] for name, var in noise_vars_dict.items()}
                metadata['noise_vars'] = metadata_noise_vars
            else:
                metadata['noise_seed'] = noise_seed
            save_with_metadata(im, fn, metadata, True)            
            z2 = (z + np.random.randn(*z.shape)/q) / (1 + 1/(q*q))            
コード例 #6
0
ファイル: dataset.py プロジェクト: itsayushthada/CBIVR
 def configure(self, minibatch_size, lod=0):
     lod = int(np.floor(lod))
     assert minibatch_size >= 1 and 0 <= lod <= self.resolution_log2
     tflib.set_vars({
         self._tf_minibatch_var: minibatch_size,
         self._tf_lod_var: lod
     })
コード例 #7
0
ファイル: create_photo.py プロジェクト: AlnBnd/facegan
def read_face_latent():
    tflib.init_tf()
    with open('networks/stylegan2-ffhq-config-f.pkl', "rb") as f:
        generator_network, discriminator_network, Gs_network = pickle.load(f)

    w_avg = Gs_network.get_var('dlatent_avg')
    noise_vars = [
        var for name, var in Gs_network.components.synthesis.vars.items()
        if name.startswith('noise')
    ]
    Gs_syn_kwargs = dnnlib.EasyDict()
    Gs_syn_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8,
                                          nchw_to_nhwc=True)
    Gs_syn_kwargs.randomize_noise = False
    Gs_syn_kwargs.minibatch_size = 1
    truncation_psi = 0.5

    face_latent = read_feature('results/generate_codes/0000.txt')
    z = np.stack(face_latent for _ in range(1))
    tflib.set_vars(
        {var: np.random.randn(*var.shape.as_list())
         for var in noise_vars})  # [height, width]
    w = Gs_network.components.mapping.run(z, None)
    w = w_avg + (w - w_avg) * truncation_psi
    return w, Gs_network, Gs_syn_kwargs
コード例 #8
0
def project_generated_images(network_pkl, seeds, num_snapshots, num_steps,
                             truncation_psi, save_target_dlatent,
                             save_every_dlatent, save_final_dlatent):
    assert num_snapshots <= num_steps, "Can't have more snapshots than number of steps taken!"
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    proj = projector.Projector(num_steps=num_steps)
    proj.set_network(Gs)
    noise_vars = [
        var for name, var in Gs.components.synthesis.vars.items()
        if name.startswith('noise')
    ]

    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.randomize_noise = False
    Gs_kwargs.truncation_psi = truncation_psi

    for seed_idx, seed in enumerate(seeds):
        print('Projecting seed %d (%d/%d) ...' % (seed, seed_idx, len(seeds)))
        rnd = np.random.RandomState(seed)
        z = rnd.randn(1, *Gs.input_shape[1:])
        tflib.set_vars(
            {var: rnd.randn(*var.shape.as_list())
             for var in noise_vars})
        w = Gs.components.mapping.run(z, None)
        if save_target_dlatent:
            np.save(dnnlib.make_run_dir_path('seed%04d.npy' % seed), w)
        images = Gs.components.synthesis.run(w, **Gs_kwargs)
        project_image(proj,
                      targets=images,
                      png_prefix=dnnlib.make_run_dir_path('seed%04d-' % seed),
                      num_snapshots=num_snapshots,
                      save_every_dlatent=save_every_dlatent,
                      save_final_dlatent=save_final_dlatent)
コード例 #9
0
    def get_images_from_seeds(self, truncation_psi, seeds):

        Gs_kwargs = dnnlib.EasyDict()
        Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8,
                                          nchw_to_nhwc=True)
        Gs_kwargs.randomize_noise = False
        if truncation_psi is not None:
            Gs_kwargs.truncation_psi = truncation_psi

        images_list = []
        zs = []
        for seed_idx, seed in enumerate(seeds):
            print('Generating image for seed %d (%d/%d) ...' %
                  (seed, seed_idx, len(range(len(seeds)))))
            rnd = np.random.RandomState(seed)
            z = rnd.randn(1,
                          *self.Gs.input_shape[1:])  # [minibatch, component]
            tflib.set_vars({
                var: rnd.randn(*var.shape.as_list())
                for var in self.noise_vars
            })  # [height, width]
            images = self.Gs.run(
                z, None, **Gs_kwargs)  # [minibatch, height, width, channel]
            images_list.append(Image.fromarray(images[0], 'RGB'))
            zs.append(z)

        return images_list, zs
コード例 #10
0
def gen_image_mix(Gs, latents_high, latents_band, latents_low, noise_vectors, noise_vars, fmt, lpf, hpf, bpf,
                  save_path="example.png"):
    for i in range(len(noise_vars)):
        tflib.set_vars({noise_vars[i]: noise_vectors[i]})

    high_latents = np.stack(latents_high)
    band_latents = np.stack(latents_band)
    low_latents = np.stack(latents_low)
    high_dlatents = Gs.components.mapping.run(high_latents, None)
    band_dlatents = Gs.components.mapping.run(band_latents, None)
    low_dlatents = Gs.components.mapping.run(low_latents, None)

    combined_dlatents = low_dlatents

    for style in lpf:
        combined_dlatents[:, style] = low_dlatents[:, style]

    for style in hpf:
        combined_dlatents[:, style] = high_dlatents[:, style]

    for style in bpf:
        combined_dlatents[:, style] = band_dlatents[:, style]

    # combined_dlatents = low_dlatents
    # combined_dlatents[:, 3:6] = high_dlatents[:, 3:6]
    # combined_dlatents[:, 6:] = band_dlatents[:, 6:]

    synthesis_kwargs = dict(output_transform=dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True),
                            minibatch_size=8)
    images = Gs.components.synthesis.run(combined_dlatents, randomize_noise=False, **synthesis_kwargs)
    PIL.Image.fromarray(images[0], 'RGB').save(save_path)
コード例 #11
0
def generate_images_in_w_space(ws, truncation_psi, outdir, save_npy, prefix,
                               vidname, framerate):

    Gs_kwargs = {
        'output_transform':
        dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True),
        'randomize_noise':
        False,
        'truncation_psi':
        truncation_psi
    }

    for w_idx, w in enumerate(ws):
        print('Generating image for step %d/%d ...' % (w_idx, len(ws)))
        noise_rnd = np.random.RandomState(1)  # fix noise
        tflib.set_vars(
            {var: noise_rnd.randn(*var.shape.as_list())
             for var in noise_vars})  # [height, width]
        images = Gs.components.synthesis.run(
            w, **Gs_kwargs)  # [minibatch, height, width, channel]
        PIL.Image.fromarray(images[0],
                            'RGB').save(f'{outdir}/{prefix}{w_idx:05d}.png')
        if save_npy:
            np.save(dnnlib.make_run_dir_path('%s%05d.npy' % (prefix, w_idx)),
                    w)

    cmd = "ffmpeg -y -r {} -i {}/{}%05d.png -vcodec libx264 -pix_fmt yuv420p {}/walk-{}-{}fps.mp4".format(
        framerate, outdir, prefix, outdir, vidname, framerate)
    subprocess.call(cmd, shell=True)
コード例 #12
0
def random_generate_demo():

    tflib.init_tf()
    with open(model_dir, "rb") as f:
        generator_network, discriminator_network, Gs_network = pickle.load(f)

    w_avg = Gs_network.get_var('dlatent_avg')
    noise_vars = [
        var for name, var in Gs_network.components.synthesis.vars.items()
        if name.startswith('noise')
    ]
    Gs_syn_kwargs = dnnlib.EasyDict()
    Gs_syn_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8,
                                          nchw_to_nhwc=True)
    Gs_syn_kwargs.randomize_noise = False
    Gs_syn_kwargs.minibatch_size = 1
    truncation_psi = 0.5

    z = np.random.randn(1, *Gs_network.input_shape[1:])
    tflib.set_vars(
        {var: np.random.randn(*var.shape.as_list())
         for var in noise_vars})  # [height, width]
    w = Gs_network.components.mapping.run(z, None)
    w = w_avg + (w - w_avg) * truncation_psi

    image = move_latent(w, Gs_network, Gs_syn_kwargs)
    if save:
        np.save('./latent_representations/{}'.format(time.ctime()), w[0])
    return image
コード例 #13
0
    def start(self, target_images):
        assert self._Gs is not None

        # Prepare target images.
        # 准备目标图像(组),即:优化迭代的目标对象
        self._info('Preparing target images...')
        target_images = np.asarray(target_images, dtype='float32')
        target_images = (target_images + 1) * (255 / 2)
        sh = target_images.shape
        assert sh[0] == self._minibatch_size
        # 如果目标图像尺寸太大,就按照_target_images_var的尺寸缩小到256x256
        if sh[2] > self._target_images_var.shape[2]:
            factor = sh[2] // self._target_images_var.shape[2]
            target_images = np.reshape(
                target_images,
                [-1, sh[1], sh[2] // factor, factor, sh[3] // factor, factor
                 ]).mean((3, 5))

        # Initialize optimization state.
        self._info('Initializing optimization state...')
        # 设置_target_images_var变量、_dlatents_var变量
        # 把_dlatent_avg作为_dlatents_var优化迭代的起点,_target_images_var为优化迭代的目标图像(组)
        tflib.set_vars({
            self._target_images_var:
            target_images,
            self._dlatents_var:
            np.tile(self._dlatent_avg, [self._minibatch_size, 1, 1])
        })
        # 初始化噪声
        tflib.run(self._noise_init_op)
        # 复位优化器状态
        self._opt.reset_optimizer_state()
        # 迭代计数从0开始
        self._cur_step = 0
コード例 #14
0
def get_sample(color=None):
    # import os
    # os.environ['CUDA_VISIBLE_DEVICES'] = "3"
    Gs, Gs_kwargs, noise_vars = load_model()
    # Gs = current_app.config['GS']
    # Gs_kwargs = current_app.config['GS_KWARGS']
    # noise_vars = current_app.config['NOISE_VARS']
    img_data_list = []
    seed_list = [random.randint(1, 100000) for i in range(500)]
    for i in seed_list:
        if len(img_data_list) >= 6:
            break
        rnd = np.random.RandomState(i)
        z = rnd.randn(1, *Gs.input_shape[1:])  # [minibatch, component]
        tflib.set_vars(
            {var: rnd.randn(*var.shape.as_list())
             for var in noise_vars})  # [height, width]
        images = Gs.run(z, None,
                        **Gs_kwargs)  # [minibatch, height, width, channel]
        img = Image.fromarray(images[0], 'RGB')
        base64_str_data = img_to_base64(img)
        if color:
            np_array = np.asarray(img)
            image_cv2 = cv2.cvtColor(np_array, cv2.COLOR_RGB2BGR)
            color_tags = majoColor_inrange(image_cv2)
            if color_tags == color:
                img_data_list.append(base64_str_data)
            del np_array, image_cv2
        else:
            img_data_list.append(base64_str_data)
        del images, img,
    return img_data_list
コード例 #15
0
def generate_images(network_pkl, seeds, truncation_psi, outdir, class_idx,
                    dlatents_npz, prefix):
    tflib.init_tf()
    print('Loading networks from "%s"...' % network_pkl)
    with dnnlib.util.open_url(network_pkl) as fp:
        _G, _D, Gs = pickle.load(fp)

    os.makedirs(outdir, exist_ok=True)

    # Render images for a given dlatent vector.
    if dlatents_npz is not None:
        print(f'Generating images from dlatents file "{dlatents_npz}"')
        dlatents = np.load(dlatents_npz)['dlatents']
        assert dlatents.shape[1:] == (18, 512)  # [N, 18, 512]
        imgs = Gs.components.synthesis.run(
            dlatents,
            output_transform=dict(func=tflib.convert_images_to_uint8,
                                  nchw_to_nhwc=True))
        for i, img in enumerate(imgs):
            fname = f'{outdir}/dlatent{i:02d}.png'
            print(f'Saved {fname}')
            PIL.Image.fromarray(img, 'RGB').save(fname)
        return

    # Render images for dlatents initialized from random seeds.
    Gs_kwargs = {
        'output_transform':
        dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True),
        'randomize_noise':
        False
    }
    if truncation_psi is not None:
        Gs_kwargs['truncation_psi'] = truncation_psi
        print("using tpsi: ", truncation_psi)

    noise_vars = [
        var for name, var in Gs.components.synthesis.vars.items()
        if name.startswith('noise')
    ]
    label = np.zeros([1] + Gs.input_shapes[1][1:])
    if class_idx is not None:
        label[:, class_idx] = 1

    for seed_idx, seed in enumerate(seeds):
        print('Generating image for seed %d (%d/%d) ...' %
              (seed, seed_idx, len(seeds)))
        rnd = np.random.RandomState(seed)
        z = rnd.randn(1, *Gs.input_shape[1:])  # [minibatch, component]
        tflib.set_vars(
            {var: rnd.randn(*var.shape.as_list())
             for var in noise_vars})  # [height, width]
        images = Gs.run(z, label,
                        **Gs_kwargs)  # [minibatch, height, width, channel]

        if prefix is None:
            fname = f'{outdir}/seed{seed:04d}.png'
        else:
            fname = f'{outdir}/{prefix}seed{seed:04d}.png'

        PIL.Image.fromarray(images[0], 'RGB').save(fname)
コード例 #16
0
def generate_ws(network_pkl, seeds, truncation_psi):
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]
    w_avg = Gs.get_var('dlatent_avg')  # [component]

    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False
    if truncation_psi is not None:
        Gs_kwargs.truncation_psi = truncation_psi

    Ws = np.zeros((len(seeds), 18, 512))

    for seed_idx, seed in enumerate(seeds):
        print('Generating W for seed %d (%d/%d) ...' % (seed, seed_idx+1, len(seeds)))
        rnd = np.random.RandomState(seed)
        z = rnd.randn(1, *Gs.input_shape[1:])  # [1, component]
        tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars})  # [height, width]
        start = time.time()
        w = Gs.components.mapping.run(z, None)  # [1, layer, component]
        w = w_avg + (w - w_avg) * truncation_psi  # [1, layer, component]
        print('Time: {}s'.format(time.time() - start))
        Ws[seed_idx, :, :] = w

    np.save('W_{}.npy'.format(len(seeds)), Ws)
コード例 #17
0
    def generate_images(self, zs, truncation_psi):
        # Get tf noise variables, for the stochastic variation
        noise_vars = [
            var for name,
            var in self.__Gs.components.synthesis.vars.items() if name.startswith('noise')]

        Gs_kwargs = dnnlib.EasyDict()
        Gs_kwargs.output_transform = dict(
            func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
        Gs_kwargs.randomize_noise = False
        if not isinstance(truncation_psi, list):
            truncation_psi = [truncation_psi] * len(zs)

        imgs = []
        for z_idx, z in tqdm(enumerate(zs)):
            Gs_kwargs.truncation_psi = truncation_psi[z_idx]
            noise_rnd = np.random.RandomState(1)  # fix noise
            tflib.set_vars({var: noise_rnd.randn(*var.shape.as_list())
                            for var in noise_vars})  # [height, width]
            # [minibatch, height, width, channel]
            images = self.__Gs.run(z, None, **Gs_kwargs)
            imgs.append(PIL.Image.fromarray(images[0], 'RGB'))

        # Return array of PIL.Image
        return imgs
コード例 #18
0
def generate_latent_images(zs, truncation_psi, outdir, save_npy,prefix,vidname,framerate):
    Gs_kwargs = {
        'output_transform': dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True),
        'randomize_noise': False
    }

    if not isinstance(truncation_psi, list):
        truncation_psi = [truncation_psi] * len(zs)

    os.makedirs(f'{outdir}/frames', exist_ok=True)

    for z_idx, z in enumerate(zs):
        if isinstance(z,list):
          z = np.array(z).reshape(1,512)
        elif isinstance(z,np.ndarray):
          z.reshape(1,512)
        print('Generating image for step %d/%d ...' % (z_idx, len(zs)))
        Gs_kwargs['truncation_psi'] = truncation_psi[z_idx]
        noise_rnd = np.random.RandomState(1) # fix noise
        tflib.set_vars({var: noise_rnd.randn(*var.shape.as_list()) for var in noise_vars}) # [height, width]
        images = Gs.run(z, None, **Gs_kwargs) # [minibatch, height, width, channel]
        PIL.Image.fromarray(images[0], 'RGB').save(f'{outdir}/frames/{prefix}{z_idx:05d}.png')
        
        if save_npy:
          np.save(dnnlib.make_run_dir_path('%s%05d.npy' % (prefix,z_idx)), z)

    cmd="ffmpeg -y -r {} -i {}/frames/{}%05d.png -vcodec libx264 -pix_fmt yuv420p {}/walk-{}-{}fps.mp4".format(framerate,outdir,prefix,outdir,vidname,framerate)
    subprocess.call(cmd, shell=True)
コード例 #19
0
def generate_latent_images(zs, truncation_psi):
    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8,
                                      nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False
    if not isinstance(truncation_psi, list):
        truncation_psi = [truncation_psi] * len(zs)

    # temp_dir = 'frames%06d'%int(1000000*random.random())
    # os.system('mkdir %s'%temp_dir)

    for z_idx, z in enumerate(zs):
        if isinstance(z, list):
            z = np.array(z).reshape(1, 512)
        elif isinstance(z, np.ndarray):
            z.reshape(1, 512)
        print('Generating image for step %d/%d ...' % (z_idx, len(zs)))
        Gs_kwargs.truncation_psi = truncation_psi[z_idx]
        noise_rnd = np.random.RandomState(1)  # fix noise
        tflib.set_vars(
            {var: noise_rnd.randn(*var.shape.as_list())
             for var in noise_vars})  # [height, width]
        images = Gs.run(z, None,
                        **Gs_kwargs)  # [minibatch, height, width, channel]
        PIL.Image.fromarray(images[0], 'RGB').save(
            dnnlib.make_run_dir_path('frame%05d.png' % z_idx))
コード例 #20
0
ファイル: run_generator.py プロジェクト: Jack12xl/stylegan2
def generate_images(network_pkl, seeds, truncation_psi, label=None, output_dir=None):
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    noise_vars = [var for name, var in Gs.components.synthesis.vars.items(
    ) if name.startswith('noise')]

    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(
        func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False
    if truncation_psi is not None:
        Gs_kwargs.truncation_psi = truncation_psi

    if label is not None:
        label = label.reshape([1, -1])
    
    if output_dir is None:
        output_dir = dnnlib.make_run_dir_path()
    os.makedirs(output_dir, exist_ok=True)

    for seed_idx, seed in enumerate(seeds):
        print('[%d/%d] Generating image for seed %d, labels = [%s]...' %
              (seed_idx, len(seeds), seed, _label2str(label, ',')))
        rnd = np.random.RandomState(seed)
        z = rnd.randn(1, *Gs.input_shape[1:])  # [minibatch, component]
        tflib.set_vars({var: rnd.randn(*var.shape.as_list())
                        for var in noise_vars})  # [height, width]
        # [minibatch, height, width, channel]
        images = Gs.run(z, label, **Gs_kwargs)

        PIL.Image.fromarray(images[0], 'RGB').save(
            os.path.join(output_dir, '%s_seed%04d.png' % (_label2str(label), seed)))
コード例 #21
0
ファイル: run_generator.py プロジェクト: nbrawand/stylegan2
def generate_images(network_pkl, seeds, truncation_psi):
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    noise_vars = [
        var for name, var in Gs.components.synthesis.vars.items()
        if name.startswith('noise')
    ]

    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8,
                                      nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False
    if truncation_psi is not None:
        Gs_kwargs.truncation_psi = truncation_psi

    for seed_idx, seed in enumerate(seeds):
        print('Generating image for seed %d (%d/%d) ...' %
              (seed, seed_idx, len(seeds)))
        rnd = np.random.RandomState(seed)
        z = rnd.randn(1, *Gs.input_shape[1:])  # [minibatch, component]
        tflib.set_vars(
            {var: rnd.randn(*var.shape.as_list())
             for var in noise_vars})  # [height, width]
        images = Gs.run(z, None,
                        **Gs_kwargs)  # [minibatch, height, width, channel]
        PIL.Image.fromarray(images[0], 'RGB').save(
            dnnlib.make_run_dir_path('seed%04d.png' % seed))
コード例 #22
0
def truncation_traversal(network_pkl, npys, seed=[0], start=-1.0, stop=1.0, increment=0.1):
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    noise_vars = [var for name, var in Gs.components.synthesis.vars.items(
    ) if name.startswith('noise')]

    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(
        func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False

    count = 1
    trunc = start

    while trunc <= stop:
        Gs_kwargs.truncation_psi = trunc
        print('Generating truncation %0.2f' % trunc)

        rnd = np.random.RandomState(seed)
        z = rnd.randn(1, *Gs.input_shape[1:])  # [minibatch, component]
        tflib.set_vars({var: rnd.randn(*var.shape.as_list())
                        for var in noise_vars})  # [height, width]
        # [minibatch, height, width, channel]
        images = Gs.run(z, None, **Gs_kwargs)
        PIL.Image.fromarray(images[0], 'RGB').save(
            dnnlib.make_run_dir_path('frame%05d.png' % count))

        trunc += increment
        count += 1
コード例 #23
0
def project_generated_images(network_pkl, seeds, num_snapshots,
                             truncation_psi):
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    proj = projector.Projector()
    proj.set_network(Gs)
    noise_vars = [
        var for name, var in Gs.components.synthesis.vars.items()
        if name.startswith('noise')
    ]

    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.randomize_noise = False
    Gs_kwargs.truncation_psi = truncation_psi

    for seed_idx, seed in enumerate(seeds):
        print('Projecting seed %d (%d/%d) ...' % (seed, seed_idx, len(seeds)))
        rnd = np.random.RandomState(seed)
        z = rnd.randn(1, *Gs.input_shape[1:])
        tflib.set_vars(
            {var: rnd.randn(*var.shape.as_list())
             for var in noise_vars})
        images = Gs.run(z, None, **Gs_kwargs)
        project_image(proj,
                      targets=images,
                      labels=None,
                      png_prefix=dnnlib.make_run_dir_path('seed%04d-' % seed),
                      num_snapshots=num_snapshots,
                      save_npy=False,
                      npy_file_prefix='NONAME')
コード例 #24
0
ファイル: model.py プロジェクト: shajen/stylegan2
    def images_beetwen_weights(self, weights, iteration, copy_layers=[]):
        rnd = np.random.RandomState(0)
        tflib.set_vars({
            var: rnd.randn(*var.shape.as_list())
            for var in self.__noise_vars
        })  # [height, width]

        weights = np.array(weights)
        weights = self.__Gs.components.mapping.run(weights, None)
        images = []
        if copy_layers:
            for w in range(weights.shape[0] - 1):
                if len(copy_layers) + 1 == len(weights):
                    weights[w + 1][copy_layers[w]] = weights[w][
                        copy_layers[w]].copy()
                else:
                    weights[w +
                            1][copy_layers] = weights[w][copy_layers].copy()

        for w in range(weights.shape[0] - 1):
            images_weights = []
            w1 = weights[w].copy()
            w2 = weights[w + 1].copy()
            step = (w2 - w1) / iteration
            _weights = w1.copy()
            for _ in range(iteration + 1):
                images_weights.append(_weights.copy())
                _weights += step
            images.append(
                self.__Gs.components.synthesis.run(np.array(images_weights),
                                                   **self.__Gs_kwargs))
        return images
コード例 #25
0
def interpolate_folder(network_pkl, input_dir, input_dir2, count):
    files = glob.glob(input_dir + "/*.png") + glob.glob(input_dir + "/*.jpg")
    L = []
    for f in files:
        metadata = read_metadata(f)
        z = metadata['z']
        truncation_psi = metadata['truncation_psi']
        L.append((z, truncation_psi))
    if input_dir2:
        files2 = glob.glob(input_dir2 + "/*.png") + glob.glob(input_dir2 + "/*.jpg")
        L2 = []
        for f in files2:
            metadata = read_metadata(f)
            z = metadata['z']
            truncation_psi = metadata['truncation_psi']
            L2.append((z, truncation_psi))    
    else:
        L2 = []
    
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]

    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False
    
        
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]

    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False

    random.shuffle(L)
    if L2:
        random.shuffle(L2)
        it = itertools.product(L, L2)    
    else:
        it = itertools.combinations(L, 2)
        
    for x, y in it:  
        psi0, psi1 = x[1], y[1]
        truncation_psi = psi0 + (psi1-psi0)*np.random.random()
        Gs_kwargs.truncation_psi = truncation_psi        
        noise_seed = np.random.randint(0,1000000)
        rnd_noise = np.random.RandomState(noise_seed)
        vars = {var: rnd_noise.randn(*var.shape.as_list()) for var in noise_vars}        
        tflib.set_vars(vars) # [height, width]                   
        for i in range(count):        
            r = (i+1)/(count+1)
            #r = random.random()
            z = (1-r)*x[0] + r*y[0]
            images = Gs.run(z, None, **Gs_kwargs) # [minibatch, height, width, channel]
            im = PIL.Image.fromarray(images[0], 'RGB')  #.save(dnnlib.make_run_dir_path('varysingle_%04d.png' % i))            
            fn = 'if_{:03d}.jpg'.format(int(r*1000))
            metadata = {'z':z, 'truncation_psi':truncation_psi, 'noise_seed':noise_seed}            
            save_with_metadata(im, fn, metadata, True)               
コード例 #26
0
def main():

    tflib.init_tf()
    with open('networks/stylegan2-ffhq-config-f.pkl', "rb") as f:
        generator_network, discriminator_network, Gs_network = pickle.load(f)

    w_avg = Gs_network.get_var('dlatent_avg')
    noise_vars = [
        var for name, var in Gs_network.components.synthesis.vars.items()
        if name.startswith('noise')
    ]
    Gs_syn_kwargs = dnnlib.EasyDict()
    Gs_syn_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8,
                                          nchw_to_nhwc=True)
    Gs_syn_kwargs.randomize_noise = False
    Gs_syn_kwargs.minibatch_size = 1
    truncation_psi = 0.5

    face_latent = read_feature('results/generate_codes/0000.txt')
    z = np.stack(face_latent for _ in range(1))
    tflib.set_vars(
        {var: np.random.randn(*var.shape.as_list())
         for var in noise_vars})  # [height, width]
    w = Gs_network.components.mapping.run(z, None)
    w = w_avg + (w - w_avg) * truncation_psi

    direction_file = [
        'age.npy', 'angle_horizontal.npy', 'emotion_happy.npy',
        'emotion_sad.npy', 'angle_pitch.npy', 'gender.npy'
    ]

    coeffs = [-12., -9., -6., -3., 0., 3., 6., 9., 12.]

    for i_file in direction_file:
        move_latent_and_save(w, i_file, coeffs, Gs_network, Gs_syn_kwargs)
コード例 #27
0
def regenerate_folder(network_pkl, input_dir):
    files = glob.glob(input_dir + "/*.png") + glob.glob(input_dir + "/*.jpg")
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]
    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False
    
    for f in files:
        metadata = read_metadata(f)
        print(metadata.keys())
        z = metadata['z']
        truncation_psi = metadata['truncation_psi']
        if 'noise_vars' in metadata:
            noise_vars = metadata['noise_vars']
            vars = {var:noise_vars[name] for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')}
        else:
            rnd = np.random.RandomState(metadata['noise_seed'])
            vars = {var: rnd.randn(*var.shape.as_list()) for var in noise_vars}        
        Gs_kwargs.truncation_psi = truncation_psi
        tflib.set_vars(vars) # [height, width]           
        images = Gs.run(z, None, **Gs_kwargs) # [minibatch, height, width, channel]            
        im = PIL.Image.fromarray(images[0], 'RGB')  #.save(dnnlib.make_run_dir_path('varysingle_%04d.png' % i))
        fn = os.path.splitext(os.path.basename(f))[0] + '-b.png'
        save_with_metadata(im, fn, metadata, False)
コード例 #28
0
def main():

    # 在这儿选择生成器
    tflib.init_tf()
    with open('networks/generator_yellow-stylegan2-config-f.pkl', "rb") as f:
        generator_network, discriminator_network, Gs_network = pickle.load(f)

    # 这是一些配置参数,不要动它
    w_avg = Gs_network.get_var('dlatent_avg')
    noise_vars = [
        var for name, var in Gs_network.components.synthesis.vars.items()
        if name.startswith('noise')
    ]
    Gs_syn_kwargs = dnnlib.EasyDict()
    Gs_syn_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8,
                                          nchw_to_nhwc=True)
    Gs_syn_kwargs.randomize_noise = False
    Gs_syn_kwargs.minibatch_size = 1
    truncation_psi = 0.5

    # 在这儿选择人物的潜码,注意要与生成器相匹配。潜码来自生成目录下有个generate_codes文件夹里的txt文件。
    face_latent = read_feature('results/generate_codes/0000.txt')
    z = np.stack(face_latent for _ in range(1))
    tflib.set_vars(
        {var: np.random.randn(*var.shape.as_list())
         for var in noise_vars})  # [height, width]
    w = Gs_network.components.mapping.run(z, None)
    w = w_avg + (w - w_avg) * truncation_psi

    # 在这儿选择调整的方向,共有23种调整方式,它们的名称与分别对应的功能如下所示。
    '''
        age.npy - 调整年龄
        angle_horizontal.npy - 在左右方向上调整人脸角度
        angle_vertical.npy - 在上下方向上调整人脸角度
        beauty.npy - 调整颜值
        emotion_angry.npy - 调整此项可增添/减弱一些生气的情绪(调整步幅建议缩小)
        emotion_disgust.npy - 调整此项可增添/减弱一些厌恶的情绪(调整步幅建议缩小)
        emotion_easy.npy - 调整此项可增添/减弱一些平静的情绪(调整步幅建议缩小)
        emotion_fear.npy - 调整此项可增添/减弱一些害怕的情绪(调整步幅建议缩小)
        emotion_happy.npy - 调整此项可增添/减弱一些开心的情绪(调整步幅建议缩小)
        emotion_sad.npy - 调整此项可增添/减弱一些伤心的情绪(调整步幅建议缩小)
        emotion_surprise.npy - 调整此项可增添/减弱一些惊讶的情绪(调整步幅建议缩小)
        eyes_open.npy - 调整眼睛的闭合程度
        face_shape.npy - 调整脸型
        gender.npy - 调整性别
        glasses.npy - 调整是否戴眼镜
        height.npy - 调整脸的高度
        race_black.npy - 调整此项可接近/远离向黑种人变化
        race_white.npy - 调整此项可接近/远离向白种人变化
        race_yellow.npy - 调整此项可接近/远离向黄种人变化
        smile.npy - 调整笑容
        width.npy - 调整脸的宽度
    '''
    direction_file = 'smile.npy'  # 从上面的编辑向量中选择一个

    # 在这儿选择调整的大小,向量里面的值表示调整幅度,可以自行编辑,对于每个值都会生成一张图片并保存。
    coeffs = [-15., -12., -9., -6., -3., 0., 3., 6., 9., 12.]

    # 开始调整并保存图片
    move_latent_and_save(w, direction_file, coeffs, Gs_network, Gs_syn_kwargs)
コード例 #29
0
    def start(self, target_images):
        assert self._Gs is not None

        # Prepare target images.
        self._info('Preparing target images...')
        target_images = np.asarray(target_images, dtype='float32')
        target_images = (target_images + 1) * (255 / 2)
        sh = target_images.shape
        assert sh[0] == self._minibatch_size
        if sh[2] > self._target_images_var.shape[2]:
            factor = sh[2] // self._target_images_var.shape[2]
            target_images = np.reshape(
                target_images,
                [-1, sh[1], sh[2] // factor, factor, sh[3] // factor, factor
                 ]).mean((3, 5))

        # Initialize optimization state.
        self._info('Initializing optimization state...')
        tflib.set_vars({
            self._target_images_var:
            target_images,
            self._dlatents_var:
            np.tile(self._dlatent_avg, [self._minibatch_size, 1, 1])
        })
        tflib.run(self._noise_init_op)
        self._opt.reset_optimizer_state()
        self._cur_step = 0
コード例 #30
0
def save_weights(network_pkl):
    print('Loading networks from "%s"...' % network_pkl)
    _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    noise_vars = [
        var for name, var in Gs.components.synthesis.vars.items()
        if name.startswith('noise')
    ]
    with open("resolution.txt", "w") as f:
        f.write(",".join([str(i) for i in Gs.output_shape[-2:]]))

    #Gs_kwargs = dnnlib.EasyDict()
    #Gs_kwargs.output_transform = dict(func=to_png, nchw_to_nhwc=True) #dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    #Gs_kwargs.randomize_noise = False

    rnd = np.random.RandomState(0)
    z = rnd.randn(1, *Gs.input_shape[1:])  # [minibatch, component]
    tflib.set_vars(
        {var: rnd.randn(*var.shape.as_list())
         for var in noise_vars})  # [height, width]
    #try:
    #    images = Gs.run(z, None, **Gs_kwargs) # [minibatch, height, width, channel]
    #except TypeError:
    #    pass

    saver = tf.train.Saver(var_list=tf.trainable_variables())
    saver.save(sess=tf.get_default_session(), save_path="save_weights")
    """