def convert_to_hdf5(hdf5_filename, tfrecord_dir, compress): print('Loading dataset "%s"' % tfrecord_dir) tflib.init_tf() dset = dataset.TFRecordDataset(tfrecord_dir, max_label_size='full', repeat=False, shuffle=False) tflib.init_uninitialized_vars() with HDF5Exporter(hdf5_filename, resolution=dset.shape[1], channels=dset.shape[0], compress=compress) as h5: all_labels = [] while True: images, labels = dset.get_minibatch_np(1) if images is None: break h5.add_images(images) all_labels.append(labels) all_labels = np.concatenate(all_labels) if all_labels.size: h5.add_labels(all_labels)
def run_training(outdir, seed, dry_run, **hyperparam_options): # Setup training options. tflib.init_tf({'rnd.np_random_seed': seed}) run_desc, training_options = setup_training_options(**hyperparam_options) # Pick output directory. prev_run_dirs = [] if os.path.isdir(outdir): prev_run_dirs = [x for x in os.listdir(outdir) if os.path.isdir(os.path.join(outdir, x))] prev_run_ids = [re.match(r'^\d+', x) for x in prev_run_dirs] prev_run_ids = [int(x.group()) for x in prev_run_ids if x is not None] cur_run_id = max(prev_run_ids, default=-1) + 1 training_options.run_dir = os.path.join(outdir, f'{cur_run_id:05d}-{run_desc}') assert not os.path.exists(training_options.run_dir) # Print options. print() print('Training options:') print(json.dumps(training_options, indent=2)) print() print(f'Output directory: {training_options.run_dir}') print(f'Training data: {training_options.train_dataset_args.path}') print(f'Training length: {training_options.total_kimg} kimg') print(f'Resolution: {training_options.train_dataset_args.resolution}') print(f'Number of GPUs: {training_options.num_gpus}') print() # Dry run? if dry_run: print('Dry run; exiting.') return # Kick off training. print('Creating output directory...') os.makedirs(training_options.run_dir) with open(os.path.join(training_options.run_dir, 'training_options.json'), 'wt') as f: json.dump(training_options, f, indent=2) with dnnlib.util.Logger(os.path.join(training_options.run_dir, 'log.txt')): training_loop.training_loop(**training_options)
def main(): tflib.init_tf() model_path = 'QAQ.pkl' image_path = 'data\\*' save_path = 'results\\score.txt' _G, D, _Gs = pickle.load(open('QAQ.pkl', 'rb')) image_filenames = glob.glob(image_path) out = {} temp = len(image_filenames) for i in range(0, temp): img = np.asarray(PIL.Image.open(image_filenames[i])) img = img.reshape(1, 3, 512, 512) score = D.run(img, None) print(image_filenames[i], score[0][0]) out[image_filenames[i]] = score[0][0] print(str(i) + '\n' + str(temp)) out = sorted(out.items(), key=lambda item: item[1]) with open(save_path, mode='w', encoding='UTF-8') as file: for filename, score in out: file.write(filename + '\t' + str(score) + '\n')
def __init__(self): root_dir = 'latent_representations/' listdir = [] sort_listdir = [] num = {} for i, f in enumerate(os.listdir(root_dir)): listdir.append(f) num[i] = int(f.split('_')[0]) num = sorted(num.items(), key=operator.itemgetter(1)) for i in range(len(num)): sort_listdir.append(listdir[num[i][0]]) print(sort_listdir) self.latent_vectors = [np.load(root_dir + f) for f in sort_listdir] self.directions = {'smile': np.load('ffhq_dataset/latent_directions/smile.npy'), 'gender': np.load('ffhq_dataset/latent_directions/gender.npy'), 'age' : np.load('ffhq_dataset/latent_directions/age.npy')} self.new_latent_vector = np.zeros((18,512)) tflib.init_tf() with open('karras2019stylegan-ffhq-1024x1024.pkl', 'rb') as f: generator_network, discriminator_network, Gs_network = pickle.load(f) self.generator = Generator(Gs_network, batch_size=1, randomize_noise=True)
def main(): # Initialize TensorFlow. tflib.init_tf() f = './results/00029-sgan-custom-2gpu/network-snapshot-008040.pkl' #network loading with open(f, 'rb') as pickle_file: _G, _D, Gs = pickle.load(pickle_file) # _G = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run. # _D = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run. # Gs = Long-term average of the generator. Yields higher-quality results than the instantaneous snapshot. # Print network details. #Gs.print_layers() os.makedirs(config.result_dir, exist_ok=True) # Pick latent vector. # for i in range(100): #print(Gs.input_shape) #print(Gs.shape) NUM = 50 #number of images (100 ->00M) rnd = np.random.RandomState(10) latents = rnd.randn(NUM, Gs.input_shape[1]) # Generate image. fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True) images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt) #print(images[0].shape) # Save image. i = 0 for image in images: png_filename = os.path.join(config.result_dir, f'example{i}.png') PIL.Image.fromarray(image, 'RGB').save(png_filename) i += 1
def display(tfrecord_dir): print('Loading dataset "%s"' % tfrecord_dir) tflib.init_tf() dset = dataset.TFRecordDataset(tfrecord_dir, max_label_size='full', repeat=False, shuffle=False) tflib.init_uninitialized_vars() import cv2 # pip install opencv-python idx = 0 while True: images, labels = dset.get_minibatch_np(1) if images is None: break if idx == 0: print('Displaying images') cv2.namedWindow('dataset_tool') print('Press SPACE or ENTER to advance, ESC to exit') print('\nidx = %-8d\nlabel = %s' % (idx, labels[0].tolist())) cv2.imshow('dataset_tool', images[0].transpose(1, 2, 0)[:, :, ::-1]) # CHW => HWC, RGB => BGR idx += 1 if cv2.waitKey() == 27: break print('\nDisplayed %d images.' % idx)
def main(): tflib.init_tf() os.makedirs(config.result_dir, exist_ok=True) #draw_uncurated_result_figure(os.path.join(config.result_dir, 'uncurated_results.png'), load_Gs(model_place), cx=0, cy=0, cw=128, ch=128, rows=5, lods=[0,0,1,1,2,2,2], seed=np.random.randint(0,100000)) #draw_noise_detail_figure(os.path.join(config.result_dir, 'noise-detail.png'), load_Gs(model_place), w=128, h=128, num_samples=100, seeds=[np.random.randint(0,100000),np.random.randint(0,100000), np.random.randint(0,100000),np.random.randint(0,100000)]) #draw_noise_components_figure(os.path.join(config.result_dir, 'noise-components.png'), load_Gs(model_place), w=128, h=128, seeds=[np.random.randint(0,100000), np.random.randint(0,100000)], noise_ranges=[range(0, 18), range(0, 0), range(8, 18), range(0, 8)], flips=[1]) draw_truncation_trick_figure(os.path.join(config.result_dir, 'truncation-trick.png'), load_Gs(model_place), w=128, h=128, seeds=[ np.random.randint(0, 100000), np.random.randint(0, 100000), np.random.randint(0, 100000), np.random.randint(0, 100000), np.random.randint(0, 12345), np.random.randint(0, 12345), np.random.randint(0, 12345) ], psis=[1, 0.9, 0.7, 0.5, 0, -0.5, -1], labels_exist=True)
def generate_grids(network, seeds, latent_pair, n_samples_per=10, bound=2, rot=0, load_gan=False): tflib.init_tf() print('Loading networks from "%s"...' % network) if load_gan: _G, _D, I, G = misc.load_pkl(network) else: E, G = get_return_v(misc.load_pkl(network), 2) G_kwargs = dnnlib.EasyDict() G_kwargs.is_validation = True G_kwargs.randomize_noise = True G_kwargs.minibatch_size=8 for seed_idx, seed in enumerate(seeds): print('Generating images for seed %d (%d/%d) ...' % (seed, seed_idx, len(seeds))) rnd = np.random.RandomState(seed) z = sample_grid_z(rnd, G, latent_pair, n_samples_per, bound, rot) images = get_return_v( G.run(z, None, **G_kwargs), 1) # [n_samples_per*n_samples_per, channel, height, width] images = add_outline(images, width=1) n_samples_square, c, h, w = np.shape(images) assert n_samples_square == n_samples_per * n_samples_per images = np.reshape(images, (n_samples_per, n_samples_per, c, h, w)) images = np.transpose(images, [0, 3, 1, 4, 2]) images = np.reshape(images, (n_samples_per * h, n_samples_per * w, c)) images = misc.adjust_dynamic_range(images, [0, 1], [0, 255]) images = np.rint(images).clip(0, 255).astype(np.uint8) PIL.Image.fromarray(images, 'RGB').save( dnnlib.make_run_dir_path('seed%04d.png' % seed))
def __init__(self): self.network_pkl = './networks/normal_face.pkl' tflib.init_tf() self.session = tf.get_default_session() self.graph = tf.get_default_graph() with open(self.network_pkl, "rb") as f: self.generator_network, self.discriminator_network, self.Gs_network = pickle.load( f) self.noise_vars = [ var for name, var in self.Gs_network.components.synthesis.vars.items() if name.startswith('noise') ] tflib.set_vars({ var: np.random.randn(*var.shape.as_list()) for var in self.noise_vars }) # [height, width] self.Gs_syn_kwargs = dnnlib.EasyDict() self.Gs_syn_kwargs.output_transform = dict( func=tflib.convert_images_to_uint8, nchw_to_nhwc=True) self.Gs_syn_kwargs.randomize_noise = False self.Gs_syn_kwargs.minibatch_size = 1 self.truncation_psi = 0.5 self.Gs_syn_kwargs.truncation_psi = self.truncation_psi self.smile_drt = np.load('latent_directions/smile.npy') self.age_drt = np.load('latent_directions/age.npy') self.gender_drt = np.load('latent_directions/gender.npy') self.beauty_drt = np.load('latent_directions/beauty.npy') self.angleh_drt = np.load('latent_directions/angle_horizontal.npy') self.anglep_drt = np.load('latent_directions/angle_pitch.npy') self.raceblack_drt = np.load('latent_directions/race_black.npy') self.raceyellow_drt = np.load('latent_directions/race_yellow.npy') self.racewhite_drt = np.load('latent_directions/race_white.npy') self.glasses_drt = np.load('latent_directions/glasses.npy') self.angry_drt = np.load('latent_directions/emotion_angry.npy') self.sad_drt = np.load('latent_directions/emotion_sad.npy') self.eye_drt = np.load('latent_directions/eyes_open.npy')
def main(): # Initialize TensorFlow. tflib.init_tf() # Load pre-trained network. # url = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' # karras2019stylegan-ffhq-1024x1024.pkl # with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f: url = 'results/00001-sgan-evan-1024-2gpu/network-snapshot-025000.pkl' with open(url, "rb") as f: _G, _D, Gs = pickle.load(f) # _G = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run. # _D = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run. # Gs = Long-term average of the generator. Yields higher-quality results than the instantaneous snapshot. # Print network details. Gs.print_layers() rnd = np.random.RandomState() N = Gs.input_shape[1] latents = rnd.randn(1, N) delta = 1e-2 direction = rnd.randint(0, N) for i in range(1000): latents[0, direction] += delta # Generate image. fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True) images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt) # Save image. os.makedirs(config.result_dir, exist_ok=True) png_filename = os.path.join(config.result_dir, 'example%04d.png' % i) PIL.Image.fromarray(images[0], 'RGB').save(png_filename)
def main(): # Initialize TensorFlow. tflib.init_tf() # Load pre-trained network. url = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' # karras2019stylegan-ffhq-1024x1024.pkl with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f: _G, _D, Gs = pickle.load(f) # Parameters rnd = np.random.RandomState(1) similarity = 505 # set similarity amount within one 'person' n_persons = 80 n_img_per_person = 1000 os.makedirs(config.result_dir, exist_ok=True) for person_id in tqdm(range(n_persons)): # Set base latent vector for this person. latent_base = rnd.randn(1, Gs.input_shape[1]) for img_id in range(n_img_per_person): # Create variation on base latent vector to create various images. latent = rnd.randn(1, Gs.input_shape[1]) latent[0][:similarity] = latent_base[0][:similarity] # Generate image. fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True) images = Gs.run(latent, None, truncation_psi=0.5, randomize_noise=False, output_transform=fmt) # Save image. png_filename = os.path.join( config.result_dir, 'person_{}-img_{}.png'.format(person_id, img_id)) PIL.Image.fromarray(images[0], 'RGB').resize( (64, 64)).save(png_filename)
def main(): # r=np.random.randint(18,size=30) # X_data=np.load('d:/data.npy') # coef = np.load('./coef.npy') # print(X_data.reshape((-1, 16, 512))[0].reshape((1, 16, 512)).shape) tflib.init_tf() os.makedirs(config.result_dir, exist_ok=True) # # for i in range(9): # for j in range(9): # for i in tqdm(range(17)): # if i == 4 or i==5 or i ==12 or i==13 or i ==9: # continue # move_and_show(load_Gs('url_ffhq'),X_data.reshape((-1, 16, 512))[119:126], coef[i], [-2.5,-1, 0,1,2.5, 5,6],i) # draw_uncurated_result_figure(os.path.join(config.result_dir, 'figure02-uncurated-ffhq.png'), load_Gs(url_ffhq), cx=0, cy=0, cw=512, ch=384, rows=3, lods=[0,1,2,2,3,3], seed=99) # draw_style_mixing_figure(os.path.join(config.result_dir, 'figure03-style-mixing.png'), # load_Gs(url_cars), w=512, h=384, # src_seeds=[1,9,3,5,7], dst_seeds=[144,244,6,3444], # style_ranges=[range(0,4)]*3+[range(4,8)]*2+[range(8,18)]) # j = 0 # # x = [0] * 5 # while j < 16: # # # x = [j]*5 # draw_style_mixing_figure(os.path.join(config.result_dir, '{}.png'.format(str(j))), load_Gs(url_cars), w=512, # h=384, src_seeds=[1, 9, 3, 5,10], dst_seeds=[144, 244, 6, 3444,11], # style_ranges=x) # j += 1 # x = [4,4,4,0] # x.extend([1]*26) # draw_style_mixing_figure2('', load_Gs(url_cars), w=512, # h=384, src_seeds=[1, 9], dst_seeds=[144, 244], # style_ranges='') generate(Gs=load_Gs(url_cars), src_seeds=[random.randint(1, 9999) for x in range(10)])
def truncation_traversal(network_pkl,npys,outdir,class_idx=None, seed=[0],start=-1.0,stop=1.0,increment=0.1,framerate=24): 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(f'{outdir}/frames', exist_ok=True) Gs_kwargs = { 'output_transform': dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True), 'randomize_noise': False } 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 count = 1 trunc = start images = [] 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] image = Gs.run(z, label, **Gs_kwargs) # [minibatch, height, width, channel] images.append(image[0]) PIL.Image.fromarray(image[0], 'RGB').save(f'{outdir}/frames/frame{count:05d}.png') trunc+=increment count+=1 cmd="ffmpeg -y -r {} -i {}/frames/frame%05d.png -vcodec libx264 -pix_fmt yuv420p {}/truncation-traversal-seed{}-start{}-stop{}.mp4".format(framerate,outdir,outdir,seed[0],start,stop) subprocess.call(cmd, shell=True)
def main(): # Initialize TensorFlow. tflib.init_tf() # Load pre-trained network. #url = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' # karras2019stylegan-ffhq-1024x1024.pkl #with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f: with open('network-snapshot-006005.pkl', 'rb') as f: _G, _D, Gs = pickle.load(f) # _G = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run. # _D = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run. # Gs = Long-term average of the generator. Yields higher-quality results than the instantaneous snapshot. # Print network details. Gs.print_layers() Nimages = 10 # Pick latent vector. rnd = np.random.RandomState(5) latents = rnd.randn(Nimages, Gs.input_shape[1]) # Generate image. fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True) images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt) # Save image. os.makedirs(config.result_dir, exist_ok=True) for i in range(Nimages): png_filename = os.path.join(config.result_dir, '%04d_example.png' % (i)) if images[0].shape[-1] == 3: PIL.Image.fromarray(images[i], 'RGB').save(png_filename) else: PIL.Image.fromarray(images[i, :, :, 0], 'L').save(png_filename)
def main(): tflib.init_tf() baseline_network_pkl = '../../results/00015-sgan-ffhq256-1gpu-baseline/network-snapshot-014526.pkl' _G, _D, Gs_baseline = misc.load_pkl(baseline_network_pkl) draw_style_mixing_figure_transition( 'style_mix_baseline.png', Gs_baseline, w=256, h=256, style1_seeds=[12, 25, 1], style2_seed=[45], style_ranges=[list(range(i - 2, i)) for i in range(2, 16, 2)]) no_progan_network_pkl = '../../results/00001-sgan-ffhq256-2gpu-remove-progan/network-snapshot-014800.pkl' _G, _D, Gs_no_progan = misc.load_pkl(no_progan_network_pkl) draw_style_mixing_figure_transition( 'style_mix_no_progan.png', Gs_no_progan, w=256, h=256, style1_seeds=[10, 56, 1], style2_seed=[34], style_ranges=[list(range(i - 2, i)) for i in range(2, 16, 2)])
def evaluate(dir): tflib.init_tf() f = open(dir + 'network-final.pkl', "rb") bin_data = f.read() sio = BytesIO(bin_data) _G, _D, Gs = pickle.load(sio) #fid = dnnlib.EasyDict(func_name='metrics.frechet_inception_distance.FID', name='fid50k', num_images=2788, minibatch_per_gpu=8) #dataset = EasyDict(tfrecord_dir='subset_images_tfr') evaluater = fid.FID(num_images=2788, minibatch_per_gpu=8, name='fid') #run_config = misc.parse_config_for_previous_run(dir) #print(run_config) #evaluater.run = dict(run_config['dataset']) evaluater.run(dir + 'network-final.pkl', run_dir=dir, dataset_args=None, mirror_augment=True, num_gpus=1) evaluater._evaluate(Gs, 1)
def main(): tflib.init_tf() os.makedirs(config.result_dir, exist_ok=True) baseline_network_pkl = 'results/00015-sgan-ffhq256-1gpu/network-snapshot-015326.pkl' no_style_mix_network_pkl = 'results/00023-sgan-ffhq256-2gpu-remove-style-mix/network-snapshot-013726.pkl' _G, _D, Gs_baseline = misc.load_pkl(baseline_network_pkl) _G, _D, Gs_no_style_mix = misc.load_pkl(no_style_mix_network_pkl) # 888,1733 # draw_uncurated_result_figure(os.path.join(config.result_dir, 'figure02-uncurated-ffhq.png'), Gs, cx=0, cy=0, cw=256, ch=256, rows=3, lods=[0,1,2,2,3,3], seed=5) #draw_style_mixing_figure(os.path.join(config.result_dir, 'figure03-style-mixing.png'), Gs, w=256, h=256, src_seeds=[639,701,687,615,2268], dst_seeds=[888,829,1898,1733,1614,845], style_ranges=[range(0,4)]*3+[range(4,8)]*2+[range(8,18)]) # draw_style_mixing_figure(os.path.join(config.result_dir, 'figure03-style-mixing.png'), Gs, w=256, h=256, src_seeds=[123,456,789], dst_seeds=[888,1733]*2, style_ranges=[range(0,4)]*2+[range(4,8)]*2) # draw_style_mixing_figure_transition(os.path.join(config.result_dir, 'no-style-mixing.png'), Gs_baseline, w=256, h=256, style1_seeds=[222, 1733, 4], style2_seed=[888], style_ranges=[list(range(i-2, i)) for i in range(2, 16, 2)]) draw_style_mixing_figure_transition( os.path.join(config.result_dir, 'no-style-mixing.png'), Gs_no_style_mix, w=256, h=256, style1_seeds=[12, 23, 34], style2_seed=[45], style_ranges=[list(range(i - 2, i)) for i in range(2, 16, 2)])
def run(network_pkl, metrics, dataset, data_dir, mirror_augment): print('Evaluating metrics "%s" for "%s"...' % (','.join(metrics), network_pkl)) tflib.init_tf() pkls = [ v for v in os.listdir(network_pkl) if v.startswith('network') and v.endswith('.pkl') ] pkls.sort() for pkl in pkls: net_pkl = pretrained_networks.get_path_or_url( os.path.join(network_pkl, pkl)) print('Process pkl %s' % pkl) dataset_args = dnnlib.EasyDict(tfrecord_dir=dataset, shuffle_mb=0) num_gpus = dnnlib.submit_config.num_gpus metric_group = metric_base.MetricGroup( [metric_defaults[metric] for metric in metrics]) metric_group.run(net_pkl, data_dir=data_dir, dataset_args=dataset_args, mirror_augment=mirror_augment, num_gpus=num_gpus) metric_group.update_autosummaries()
def extract(tfrecord_dir, output_dir): print('Loading dataset "%s"' % tfrecord_dir) tflib.init_tf() dset = dataset.TFRecordDataset(tfrecord_dir, max_label_size=0, repeat=False, shuffle=False) tflib.init_uninitialized_vars() print('Extracting images to "%s"' % output_dir) if not os.path.isdir(output_dir): os.makedirs(output_dir) idx = 0 while True: if idx % 10 == 0: print('%d\r' % idx, end='', flush=True) images, _labels = dset.get_minibatch_np(1) if images is None: break if images.shape[1] == 1: img = PIL.Image.fromarray(images[0][0], 'L') else: img = PIL.Image.fromarray(images[0].transpose(1, 2, 0), 'RGB') img.save(os.path.join(output_dir, 'img%08d.png' % idx)) idx += 1 print('Extracted %d images.' % idx)
def main(): # Initialize TensorFlow. tflib.init_tf() # Load pre-trained network. def load_gs(url): with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f: _G, _D, Gs = pickle.load(f) # _G = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run. # _D = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run. # Gs = Long-term average of the generator. Yields higher-quality results than the instantaneous snapshot. return Gs # # Print network details. # Gs.print_layers() url_faces = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' # karras2019stylegan-ffhq-1024x1024.pkl url_cars = 'https://drive.google.com/uc?id=1MJ6iCfNtMIRicihwRorsM3b7mmtmK9c3' # karras2019stylegan-cars-512x384.pkl url_bedrooms = 'https://drive.google.com/uc?id=1MOSKeGF0FJcivpBI7s63V9YHloUTORiF' # karras2019stylegan-bedrooms-256x256.pkl generate_random_examples(10, load_gs(url_faces), 'faces') generate_random_examples(10, load_gs(url_cars), 'cars') generate_random_examples(10, load_gs(url_bedrooms), 'bedrooms')
def project_real_images(dataset_name, data_dir, num_images, num_snapshots, model_pkl, steps=1000): stream = open(model_pkl, 'rb') tflib.init_tf() with stream: G, D, Gs = pickle.load(stream, encoding='latin1') proj = projector.Projector() proj.set_network(Gs) proj.num_steps = steps print('Loading images from "%s"...' % dataset_name) dataset_obj = training.dataset.load_dataset(data_dir=data_dir, tfrecord_dir=dataset_name, max_label_size=0, verbose=True, repeat=False, shuffle_mb=0) print(dataset_obj.shape) print(Gs.output_shape) assert dataset_obj.shape == Gs.output_shape[1:] for image_idx in range(num_images): print('Projecting image %d/%d ...' % (image_idx, num_images)) images, _labels = dataset_obj.get_minibatch_np(1) images = training.misc.adjust_dynamic_range(images, [0, 255], [-1, 1]) run_projector.project_image(proj, targets=images, png_prefix=dnnlib.make_run_dir_path(f'{img_path}image%04d-' % image_idx), num_snapshots=num_snapshots)
def extract(tfrecord_dir, output_dir): print('Loading dataset "%s"' % tfrecord_dir) tflib.init_tf({'gpu_options.allow_growth': True}) dset = dataset.TFRecordDataset(tfrecord_dir, max_label_size=0, repeat=False, shuffle_mb=0) tflib.init_uninitialized_vars() print('Extracting images to "%s"' % output_dir) if not os.path.isdir(output_dir): os.makedirs(output_dir) idx = 0 while True: if idx % 10 == 0: print('%d\r' % idx, end='', flush=True) try: images, _labels = dset.get_minibatch_np(1) except tf.errors.OutOfRangeError: break if images.shape[1] == 1: img = PIL.Image.fromarray(images[0][0], 'L') else: img = PIL.Image.fromarray(images[0].transpose(1, 2, 0), 'RGB') idx += 1 print('Extracted %d images.' % idx)
def run(model, gpus, output_dir, images_num, truncation_psi, batch_size, ratio): print("Loading networks...") os.environ["CUDA_VISIBLE_DEVICES"] = gpus # Set GPUs tflib.init_tf() # Initialize TensorFlow G, D, Gs = load_networks(model, eval=True) # Load pre-trained network Gs.print_layers() # Print network details print("Generate images...") latents = np.random.randn(images_num, *Gs.input_shape[1:]) # Sample latent vectors images = Gs.run( latents, truncation_psi=truncation_psi, # Generate images batch_size=batch_size, verbose=True)[0] print("Saving images...") os.makedirs(output_dir, exist_ok=True) # Make output directory pattern = "{}/sample_{{:06d}}.png".format( output_dir) # Output images pattern for i, image in tqdm(list(enumerate(images))): # Save images crop(misc.to_pil(image), ratio).save(pattern.format(i))
def main(): """Main function.""" args = parse_args() os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id tf_config = {'rnd.np_random_seed': 1000} tflib.init_tf(tf_config) assert os.path.exists(args.restore_path) E, _, _, Gs, NE = load_pkl(args.restore_path) num_layers = Gs.components.synthesis.input_shape[1] # Building graph real = tf.placeholder('float32', [None, 3, args.image_size, args.image_size], name='real_image') #labels = tf.placeholder('float32', [None,0], name='dummy_labels') encoder_w = E.get_output_for(real, phase=False) print(f'encoder_w size: {encoder_w.shape}') #encoder_z = NE.get_output_for(encoder_w, labels) sess = tf.get_default_session() # Preparing data input_images, _ = preparing_data(im_path=args.data_dir_test, img_type=args.img_type) save_dir = args.output_dir or './outputs/encodings' os.makedirs(save_dir, exist_ok=True) print('Encoding...') temp = [] for it, image_id in tqdm( enumerate(range(0, input_images.shape[0], args.batch_size))): batch_images = input_images[image_id:image_id + args.batch_size] encodings = sess.run(encoder_w, feed_dict={real: batch_images}) for i in range(args.batch_size): id_num = 8 * it + i np.save(os.path.join(save_dir, '%05d_w.npy' % (id_num)), encodings[i])
def main(): # Initialize TensorFlow. tflib.init_tf() # Load pre-trained network. #with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f: #fname = "ffhq_model/karras2019stylegan-ffhq-1024x1024.pkl" #fname = "cats_model/karras2019stylegan-cats-256x256.pkl" fname = "bedroom_model/karras2019stylegan-bedrooms-256x256.pkl" with open(fname, "rb") as f: _G, _D, Gs = pickle.load(f) # _G = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run. # _D = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run. # Gs = Long-term average of the generator. Yields higher-quality results than the instantaneous snapshot. # Print network details. Gs.print_layers() # Pick latent vector. rnd = np.random.RandomState(1) latents = rnd.randn(1, Gs.input_shape[1]) src_dlatents = Gs.components.mapping.run(latents, None) print(src_dlatents.shape) print(src_dlatents[0, 0, :10]) print(src_dlatents[0, 1, :10]) # Generate image. fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True) images = Gs.components.synthesis.run(src_dlatents, truncation_psi=0.7, randomize_noise=True, output_transform=fmt) #images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt) # Save image. os.makedirs(config.result_dir, exist_ok=True) png_filename = os.path.join(config.result_dir, 'example_w.png') PIL.Image.fromarray(images[0], 'RGB').save(png_filename)
def test_d(submit_config, resume_run_id, dataset_args, tf_config = {}, resume_snapshot=None): ctx = dnnlib.RunContext(submit_config, train) tflib.init_tf(tf_config) network_pkl = misc.locate_network_pkl(resume_run_id, resume_snapshot) print('Loading networks from "%s"...' % network_pkl) G, D, Gs = misc.load_pkl(network_pkl) latents_1 = tf.placeholder(tf.float32) labels_1 = None training_set = dataset.load_dataset(data_dir=config.data_dir, verbose=True, **dataset_args) w_1 = Gs.components.mapping.get_output_for(latents_1, labels_1, is_validation=True) fake_image_1_op = Gs.components.synthesis.get_output_for(w_1, is_validation=True, randomize_noise=False) reals, labels = training_set.get_minibatch_tf() lod_in = tf.placeholder(tf.float32, name='lod_in', shape=[]) reals = process_reals(reals, lod_in, False, training_set.dynamic_range, [-1,1]) d_pred_real = D.get_output_for(reals, labels_1) d_pred_fake = D.get_output_for(fake_image_1_op, labels_1) training_set.configure(1, 0) for i in range(15): latents_1_val = np.random.randn(1,*G.input_shape[1:]) # d_pred, fake_image_1 = tflib.run([d_pred_op, fake_image_1_op], feed_dict={latents_1: latents_1_val, lod_in: 0}) d_pred_real_, d_pred_fake_, real_image = tflib.run([d_pred_real, d_pred_fake, reals], feed_dict={latents_1: latents_1_val, lod_in: 0}) print(d_pred_real_, d_pred_fake_) misc.save_mri_image(real_image, os.path.join(submit_config.run_dir,'real_{}.nii.gz'.format(i)), drange=[-1,1])
def transitionAtoB_v2( run_id = 102, # Run ID or network pkl to resume training from, None = start from scratch. snapshot = None, num_frames = 20, interpolate_dim = 350): # Initialize TensorFlow. tflib.init_tf() # Load pre-trained network. network_pkl = misc.locate_network_pkl(run_id, snapshot) print('Loading networks from "%s"...' % network_pkl) G, D, Gs = misc.load_pkl(network_pkl) # Print network details. Gs.print_layers() # Pick latent vector. rnd = np.random.RandomState(10) # seed = 10 init_latent = rnd.randn(1, Gs.input_shape[1])[0] def apply_latent_fudge(fudge): copy = np.copy(init_latent) copy[interpolate_dim] += fudge return copy interpolate = np.linspace(0., 30., num_frames) - 15 latents = np.array(list(map(apply_latent_fudge, interpolate))) fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True) images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt) os.makedirs(os.path.join(config.result_dir, os.path.basename(network_pkl).replace(".mp4","")), exist_ok=True) for idx in range(num_frames): # Save image. png_filename = os.path.join(config.result_dir, os.path.basename(network_pkl).replace(".mp4",""), 'frame_'+'{0:04d}'.format(idx)+'.png') PIL.Image.fromarray(images[idx], 'RGB').save(png_filename)
def main(): # Initialize TensorFlow. tflib.init_tf() # Load pre-trained network. url = os.path.abspath( "results/00004-sgan-poke-1gpu/network-snapshot-015204.pkl") #'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' # karras2019stylegan-ffhq-1024x1024.pkl #with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f: with open(url, 'rb') as f: _G, _D, Gs = pickle.load(f) # _G = Instantaneous snapshot of the generator. Mainly useful for resuming a previous training run. # _D = Instantaneous snapshot of the discriminator. Mainly useful for resuming a previous training run. # Gs = Long-term average of the generator. Yields higher-quality results than the instantaneous snapshot. # Print network details. Gs.print_layers() # Pick latent vector. # Generate image. for i in range(120): rnd = np.random.RandomState(random.randint(1, 1000)) latents = rnd.randn(1, Gs.input_shape[1]) fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True) images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt) # Save image. os.makedirs(config.result_dir, exist_ok=True) png_filename = os.path.join(config.result_dir + "/test", 'example' + str(i) + '.png') PIL.Image.fromarray(images[0], 'RGB').save(png_filename)
def create_initial_pkl( G_args={}, # Options for generator network. D_args={}, # Options for discriminator network. tf_config={}, # Options for tflib.init_tf(). config_id="config-f", # config-f is the only one tested ... num_channels=3, # number of channels, e.g. 3 for RGB resolution_h=1024, # height dimension of real/fake images resolution_w=1024, # height dimension of real/fake images label_size=0, # number of labels for a conditional model ): # Initialize dnnlib and TensorFlow. tflib.init_tf(tf_config) resolution = resolution_h # training_set.shape[1] # Construct or load networks. with tf.device('/gpu:0'): print('Constructing networks...') G = tflib.Network('G', num_channels=num_channels, resolution=resolution, label_size=label_size, **G_args) D = tflib.Network('D', num_channels=num_channels, resolution=resolution, label_size=label_size, **D_args) Gs = G.clone('Gs') # Print layers and generate initial image snapshot. # G.print_layers(); D.print_layers() pkl = 'network-initial-%s-%sx%s-%s.pkl' % (config_id, resolution_w, resolution_h, label_size) misc.save_pkl((G, D, Gs), pkl) print("Saving", pkl)
def main(): # Initialize TensorFlow. tflib.init_tf() # Load pre-trained network: karras2019stylegan-ffhq-1024x1024.pkl url = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ' with dnnlib.util.open_url(url, cache_dir=config.cache_dir) as f: _G, _D, Gs = pickle.load(f) # _G = Instantaneous snapshot of the generator. # Mainly useful for resuming a previous training run. # _D = Instantaneous snapshot of the discriminator. # Mainly useful for resuming a previous training run. # Gs = Long-term average of the generator. # Yields higher-quality results than the instantaneous snapshot. print(Gs, Gs.name, Gs.vars) # Print network details. Gs.print_layers() for i in range(10): # Pick latent vector. rnd = np.random.RandomState(i) latents = rnd.randn(1, Gs.input_shape[1]) # Generate image. fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True) images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt) # Save image. os.makedirs(config.result_dir, exist_ok=True) exp_dir = os.path.join(config.result_dir, 'examples') os.makedirs(exp_dir, exist_ok=True) png_filename = os.path.join(exp_dir, 'example-{}.png'.format(i)) PIL.Image.fromarray(images[0], 'RGB').save(png_filename)