def generate_interpolation_video(run_id, snapshot=None, grid_size=[1,1], image_shrink=1, image_zoom=1, duration_sec=60.0, smoothing_sec=1.0, mp4=None, mp4_fps=30, mp4_codec='libx265', mp4_bitrate='16M', random_seed=1000, minibatch_size=8):
    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if mp4 is None:
        mp4 = misc.get_id_string_for_network_pkl(network_pkl) + '-lerp.mp4'
    num_frames = int(np.rint(duration_sec * mp4_fps))
    random_state = np.random.RandomState(random_seed)

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    print('Generating latent vectors...')
    shape = [num_frames, np.prod(grid_size)] + Gs.input_shape[1:] # [frame, image, channel, component]
    all_latents = random_state.randn(*shape).astype(np.float32)
    all_latents = scipy.ndimage.gaussian_filter(all_latents, [smoothing_sec * mp4_fps] + [0] * len(Gs.input_shape), mode='wrap')
    all_latents /= np.sqrt(np.mean(np.square(all_latents)))

    # Frame generation func for moviepy.
    def make_frame(t):
        frame_idx = int(np.clip(np.round(t * mp4_fps), 0, num_frames - 1))
        latents = all_latents[frame_idx]
        labels = np.zeros([latents.shape[0], 0], np.float32)
        images = Gs.run(latents, labels, minibatch_size=minibatch_size, num_gpus=config.num_gpus, out_mul=127.5, out_add=127.5, out_shrink=image_shrink, out_dtype=np.uint8)
        grid = misc.create_image_grid(images, grid_size).transpose(1, 2, 0) # HWC
        if image_zoom > 1:
            grid = scipy.ndimage.zoom(grid, [image_zoom, image_zoom, 1], order=0)
        if grid.shape[2] == 1:
            grid = grid.repeat(3, 2) # grayscale => RGB
        return grid

    # Generate video.
    import moviepy.editor # pip install moviepy
    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    moviepy.editor.VideoClip(make_frame, duration=duration_sec).write_videofile(os.path.join(result_subdir, mp4), fps=mp4_fps, codec='libx264', bitrate=mp4_bitrate)
    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
def generate_fake_images(run_id,
                         snapshot=None,
                         grid_size=[1, 1],
                         num_pngs=1,
                         image_shrink=1,
                         png_prefix=None,
                         random_seed=1000,
                         minibatch_size=8):
    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if png_prefix is None:
        png_prefix = misc.get_id_string_for_network_pkl(network_pkl) + '-'

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    for png_idx in range(num_pngs):
        print('Generating png %d / %d...' % (png_idx, num_pngs))
        latents = misc.random_latents(np.prod(grid_size),
                                      Gs,
                                      random_state=random_state)
        labels = np.zeros([latents.shape[0], 0], np.float32)
        images = Gs.run(latents,
                        labels,
                        minibatch_size=minibatch_size,
                        num_gpus=config.num_gpus,
                        out_mul=127.5,
                        out_add=127.5,
                        out_shrink=image_shrink,
                        out_dtype=np.uint8)
        misc.save_image_grid(
            images,
            os.path.join(result_subdir, '%s%06d.png' % (png_prefix, png_idx)),
            [0, 255], grid_size)
    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
def generate_interpolation_video(run_id, snapshot=None, grid_size=[1,1], image_shrink=1, image_zoom=1, duration_sec=60.0, smoothing_sec=1.0, mp4=None, mp4_fps=30, mp4_codec='libx265', mp4_bitrate='16M', random_seed=1000, minibatch_size=8):
    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if mp4 is None:
        mp4 = misc.get_id_string_for_network_pkl(network_pkl) + '-lerp.mp4'
    num_frames = int(np.rint(duration_sec * mp4_fps))
    random_state = np.random.RandomState(random_seed)

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    print('Generating latent vectors...')
    shape = [num_frames, np.prod(grid_size)] + Gs.input_shape[1:] # [frame, image, channel, component]
    all_latents = random_state.randn(*shape).astype(np.float32)
    all_latents = scipy.ndimage.gaussian_filter(all_latents, [smoothing_sec * mp4_fps] + [0] * len(Gs.input_shape), mode='wrap')
    all_latents /= np.sqrt(np.mean(np.square(all_latents)))

    # Frame generation func for moviepy.
    def make_frame(t):
        frame_idx = int(np.clip(np.round(t * mp4_fps), 0, num_frames - 1))
        latents = all_latents[frame_idx]
        labels = np.zeros([latents.shape[0], 0], np.float32)
        images = Gs.run(latents, labels, minibatch_size=minibatch_size, num_gpus=config.num_gpus, out_mul=127.5, out_add=127.5, out_shrink=image_shrink, out_dtype=np.uint8)
        grid = misc.create_image_grid(images, grid_size).transpose(1, 2, 0) # HWC
        if image_zoom > 1:
            grid = scipy.ndimage.zoom(grid, [image_zoom, image_zoom, 1], order=0)
        if grid.shape[2] == 1:
            grid = grid.repeat(3, 2) # grayscale => RGB
        return grid

    # Generate video.
    import moviepy.editor # pip install moviepy
    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    moviepy.editor.VideoClip(make_frame, duration=duration_sec).write_videofile(os.path.join(result_subdir, mp4), fps=mp4_fps, codec='libx264', bitrate=mp4_bitrate)
    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
Esempio n. 4
0
def generate_interpolation_video(run_id, snapshot=None, grid_size=[1,1], image_shrink=1, image_zoom=1, duration_sec=60.0, smoothing_sec=1.0, mp4=None, mp4_fps=30, mp4_codec='libx265', mp4_bitrate='16M', random_seed=1000, minibatch_size=8):

    interpolate_dim=500

    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if mp4 is None:
        # mp4 = misc.get_id_string_for_network_pkl(network_pkl) + '-lerp.mp4'
        mp4 = misc.get_id_string_for_network_pkl(network_pkl) + '-lerp' + '-interpolate-dim-' + str(interpolate_dim) + '.mp4'
    num_frames = int(np.rint(duration_sec * mp4_fps))
    random_state = np.random.RandomState(random_seed)

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    print('Generating latent vectors...')
    ## shape=[1800, 1, 512]; grid_size=[1, 1]; Gs.input_shape=[None, 512]
    shape = [num_frames, np.prod(grid_size)] + Gs.input_shape[1:] # [frame, image, channel, component]
    all_latents = random_state.randn(*shape).astype(np.float32)
    all_latents = scipy.ndimage.gaussian_filter(all_latents, [smoothing_sec * mp4_fps] + [0] * len(Gs.input_shape), mode='wrap')
    all_latents /= np.sqrt(np.mean(np.square(all_latents)))

    latent_dims=512         # TODO: Hardcoded
    for frame in range(num_frames):
        all_latents[frame][0][0:interpolate_dim]=-0.5
        all_latents[frame][0][interpolate_dim+1:latent_dims]=-0.5

    # Frame generation func for moviepy.
    def make_frame(t):
        frame_idx = int(np.clip(np.round(t * mp4_fps), 0, num_frames - 1))
        latents = all_latents[frame_idx]
        labels = np.zeros([latents.shape[0], 0], np.float32)
        images = Gs.run(latents, labels, minibatch_size=minibatch_size, num_gpus=config.num_gpus, out_mul=127.5, out_add=127.5, out_shrink=image_shrink, out_dtype=np.uint8)
        grid = misc.create_image_grid(images, grid_size).transpose(1, 2, 0) # HWC
        if image_zoom > 1:
            grid = scipy.ndimage.zoom(grid, [image_zoom, image_zoom, 1], order=0)
        if grid.shape[2] == 1:
            grid = grid.repeat(3, 2) # grayscale => RGB
        return grid

    # Generate video.
    import moviepy.editor # pip install moviepy
    # result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    result_subdir = misc.create_result_subdir(config.result_dir, config.desc + '-interpolate-dim-' + str(interpolate_dim))
    moviepy.editor.VideoClip(make_frame, duration=duration_sec).write_videofile(os.path.join(result_subdir, mp4), fps=mp4_fps, codec='libx264', bitrate=mp4_bitrate)

    # aSk
    # np.set_printoptions(threshold=np.inf)
    # with open(os.path.join(result_subdir, 'all_latents_mod.txt'), 'w') as fp:
    #     fp.write(str(all_latents))

    with open(os.path.join(result_subdir, 'latents_interpolate_dim_' + str(interpolate_dim) + '.txt'), 'w') as fp:
        for frame in range(num_frames):
            for latent_dim in range(latent_dims):
                fp.write(str(all_latents[frame][0][latent_dim]) + ', ')
            fp.write('\n')
    # _aSk

    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
def generate_fake_images_cond2(run_id, x, y, snapshot=None, grid_size=[1,1], num_pngs=1, image_shrink=1, png_prefix=None, random_seed=1000, minibatch_size=1):
    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if png_prefix is None:
        png_prefix = misc.get_id_string_for_network_pkl(network_pkl) + '-'
    
    
    
    training_set = dataset.load_dataset(data_dir=config.data_dir, shuffle_mb=0, verbose=True, **config.dataset)
    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)
    real, label = training_set.get_minibatch_np(num_pngs)
    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    size=128
    grid= np.zeros((3, x*256, y* 256))
    image=[]
    for i in range(1, 18): 
      filename = 'cond4/'+str(i)+'.png'
      if os.path.isfile(filename):
          im=Image.open(filename)
          im.load()
          im = np.asarray(im, dtype=np.float32 )
          im=np.transpose(im, (2, 0, 1))
          image.append(im)
      print(image[i-1].shape)
    print(len(image))
    for i in range (1, x):
        grid[:, (i)*256:(i)*256+256, 0:256]= image[i]
    print(i)
    for j in range (i, 16):
        grid[:, 0:256, (j-i)*256:(j-i)*256+256]= image[j]
    
    for j in range (128, y*256-128, 128):
        for i in range (128, x*256-128, 128):
        
            real = grid[:,i:i+256, j:j+256]
    
            real1= real[:, :(size),:(size)]
            real2= real[:, (size):,:(size)]
            real3= real[:, :(size),(size):]
            real1=(real1.astype(np.float32)-127.5)/127.5
            real2=(real2.astype(np.float32)-127.5)/127.5
            real3=(real3.astype(np.float32)-127.5)/127.5
            latents = np.random.randn(3, 128, 128)
            left = np.concatenate((real1, real2), axis=1)
            print('left:'+str(left.shape))
            right = np.concatenate((real3, latents), axis=1)
            lat_and_cond = np.concatenate((left, right), axis=2)
            lat_and_cond = lat_and_cond[np.newaxis]
            fake_images_out_small = Gs.get_output_for(lat_and_cond, is_training=False)
            fake_images_out_small = (fake_images_out_small.eval()*127.5)+127.5
            print(fake_images_out_small.shape)
            fake_images_out_small=fake_images_out_small[0, :,:,:]
            grid[:,i+128:i+256, j+128:j+256]=fake_images_out_small
        
    images = grid[np.newaxis]
    misc.save_image_grid(images, os.path.join(result_subdir, 'grid.png'), [0,255], grid_size)
    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
def generate_training_video(run_id, duration_sec=20.0, time_warp=1.5, mp4=None, mp4_fps=30, mp4_codec='libx265', mp4_bitrate='16M'):
    src_result_subdir = misc.locate_result_subdir(run_id)
    if mp4 is None:
        mp4 = os.path.basename(src_result_subdir) + '-train.mp4'

    # Parse log.
    times = []
    snaps = [] # [(png, kimg, lod), ...]
    with open(os.path.join(src_result_subdir, 'log.txt'), 'rt') as log:
        for line in log:
            k = re.search(r'kimg ([\d\.]+) ', line)
            l = re.search(r'lod ([\d\.]+) ', line)
            t = re.search(r'time (\d+d)? *(\d+h)? *(\d+m)? *(\d+s)? ', line)
            if k and l and t:
                k = float(k.group(1))
                l = float(l.group(1))
                t = [int(t.group(i)[:-1]) if t.group(i) else 0 for i in range(1, 5)]
                t = t[0] * 24*60*60 + t[1] * 60*60 + t[2] * 60 + t[3]
                png = os.path.join(src_result_subdir, 'fakes%06d.png' % int(np.floor(k)))
                if os.path.isfile(png):
                    times.append(t)
                    snaps.append((png, k, l))
    assert len(times)

    # Frame generation func for moviepy.
    png_cache = [None, None] # [png, img]
    def make_frame(t):
        wallclock = ((t / duration_sec) ** time_warp) * times[-1]
        png, kimg, lod = snaps[max(bisect.bisect(times, wallclock) - 1, 0)]
        if png_cache[0] == png:
            img = png_cache[1]
        else:
            img = scipy.misc.imread(png)
            # Check if the img is greyscale. If so, it needs to be converted to RGB, since MoviePy expects it in that format
            if img.ndim == 2:
                tmpimg = img
                img = np.zeros([tmpimg.shape[0], tmpimg.shape[1], 3], tmpimg.dtype)
                img[:,:,0] = tmpimg
                img[:,:,1] = tmpimg
                img[:,:,2] = tmpimg
            while img.shape[1] > 1920 or img.shape[0] > 1080:
                img = img.astype(np.float32).reshape(img.shape[0]//2, 2, img.shape[1]//2, 2, -1).mean(axis=(1,3))
            png_cache[:] = [png, img]
        img = misc.draw_text_label(img, 'lod %.2f' % lod, 16, img.shape[0]-4, alignx=0.0, aligny=1.0)
        img = misc.draw_text_label(img, misc.format_time(int(np.rint(wallclock))), img.shape[1]//2, img.shape[0]-4, alignx=0.5, aligny=1.0)
        img = misc.draw_text_label(img, '%.0f kimg' % kimg, img.shape[1]-16, img.shape[0]-4, alignx=1.0, aligny=1.0)
        return img

    # Generate video.
    import moviepy.editor # pip install moviepy
    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    moviepy.editor.VideoClip(make_frame, duration=duration_sec).write_videofile(os.path.join(result_subdir, mp4), fps=mp4_fps, codec='libx264', bitrate=mp4_bitrate)
    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
Esempio n. 7
0
def generate_fake_interpolate_midle_images(run_id,
                                           snapshot=None,
                                           grid_size=[1, 1],
                                           num_pngs=1,
                                           image_shrink=1,
                                           png_prefix=None,
                                           random_seed=1000,
                                           minibatch_size=8,
                                           middle_img=10):
    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if png_prefix is None:
        png_prefix = misc.get_id_string_for_network_pkl(network_pkl) + '-'
    random_state = np.random.RandomState(random_seed)

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    for png_idx in range(num_pngs):
        latents = misc.random_latents(middle_img + 2,
                                      Gs,
                                      random_state=random_state)
        from_to_tensor = latents[middle_img + 1] - latents[0]
        from_z = latents[0]
        #between_x_list = [from_x]
        counter = 0
        for alpha in np.linspace(-0.5, 0.5, middle_img +
                                 2):  #np.linspace(0, 1, middle_img + 1):
            print('alpha: ', alpha, 'counter= ', counter)
            between_z = from_z + alpha * from_to_tensor
            latents[counter] = between_z
            counter += 1
        labels = np.zeros([latents.shape[0], 0], np.float32)
        images = Gs.run(latents,
                        labels,
                        minibatch_size=minibatch_size,
                        num_gpus=config.num_gpus,
                        out_mul=127.5,
                        out_add=127.5,
                        out_shrink=image_shrink,
                        out_dtype=np.uint8)
        #grid_size_1=[middle_img+2,1]
        grid_size_1 = [middle_img + 1, 1]
        #png_prefix=0

        misc.save_image_grid(
            images[1:, :, :, :],
            os.path.join(result_subdir, '%s%06d.png' % (png_prefix, png_idx)),
            [0, 255], grid_size_1)
    '''
Esempio n. 8
0
def find_latent_with_query_image(run_id, snapshot=None, grid_size=[1,1], num_pngs=1, image_shrink=1, png_prefix=None, random_seed=4123, minibatch_size=8):
    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if png_prefix is None:
        png_prefix = misc.get_id_string_for_network_pkl(network_pkl) + '-'
    random_state = np.random.RandomState(random_seed)

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    
    # Create query image - tensorflow constant
    query_image = cv2.imread('../../data/ACDC/training/patient001/cardiac_cycles/0/0.png')
    query_image = cv2.resize(query_image, (256, 256))
    print('Saving query image to "%s"...' % result_subdir)
    cv2.imwrite(result_subdir+'/query_image.png', query_image)
    query_image = query_image.transpose(2,0,1)
    query_image = query_image[np.newaxis]
    x = tf.constant(query_image, dtype=tf.float32, name='query_image')
    # Create G(z) - tensorflow variable and label
    latent = misc.random_latents(np.prod(grid_size), Gs, random_state=random_state)
    initial = tf.constant(latent, dtype=tf.float32)
    z = tf.Variable(initial_value=initial, dtype=tf.float32, name='latent_space')
    label = np.zeros([latent.shape[0], 5], np.float32)
    label[:,4] = 1 # | 0 -> NOR | 1 -> DCM | 2 -> HCM | 3 -> MINF | 4 -> RV | 
    gz = Gs.run(latent, label, minibatch_size=minibatch_size, num_gpus=config.num_gpus, out_mul=127.5, out_add=127.5, out_shrink=image_shrink, out_dtype=np.float32)
    gz = tf.Variable(gz, dtype=tf.float32)
    # Define a loss function
    residual_loss = tf.losses.absolute_difference(x, gz)
    # Define an optimizer
    train_op = tf.train.AdamOptimizer(learning_rate=0.01).minimize(residual_loss)
    
    zs, gzs, step = [], [], 1
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        _, loss_value = sess.run([train_op, residual_loss])
        while (loss_value > 2e-04 and step <= 50000):
            _, loss_value = sess.run([train_op, residual_loss])
            step += 1
            if step % 10000 == 0:
                print('Step {}, Loss value: {}'.format(step, loss_value))
                gzs.append(sess.run(gz))
                zs.append(sess.run(z))
                
    for png_idx, image in enumerate(gzs):
        misc.save_image_grid(image, os.path.join(result_subdir, '%s%06d.png' % (png_prefix, png_idx)), [0,255], grid_size)
        
    np.save(result_subdir+'/zs.npy', np.asarray(zs))
def generate_fake_images(run_id, snapshot=None, grid_size=[1,1], num_pngs=1, image_shrink=1, png_prefix=None, random_seed=1000, minibatch_size=8):
    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if png_prefix is None:
        png_prefix = misc.get_id_string_for_network_pkl(network_pkl) + '-'
    random_state = np.random.RandomState(random_seed)

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    for png_idx in range(num_pngs):
        print('Generating png %d / %d...' % (png_idx, num_pngs))
        latents = misc.random_latents(np.prod(grid_size), Gs, random_state=random_state)
        labels = np.zeros([latents.shape[0], 0], np.float32)
        images = Gs.run(latents, labels, minibatch_size=minibatch_size, num_gpus=config.num_gpus, out_mul=127.5, out_add=127.5, out_shrink=image_shrink, out_dtype=np.uint8)
        misc.save_image_grid(images, os.path.join(result_subdir, '%s%06d.png' % (png_prefix, png_idx)), [0,255], grid_size)
    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
def generate_training_video(run_id, duration_sec=20.0, time_warp=1.5, mp4=None, mp4_fps=30, mp4_codec='libx265', mp4_bitrate='16M'):
    src_result_subdir = misc.locate_result_subdir(run_id)
    if mp4 is None:
        mp4 = os.path.basename(src_result_subdir) + '-train.mp4'

    # Parse log.
    times = []
    snaps = [] # [(png, kimg, lod), ...]
    with open(os.path.join(src_result_subdir, 'log.txt'), 'rt') as log:
        for line in log:
            k = re.search(r'kimg ([\d\.]+) ', line)
            l = re.search(r'lod ([\d\.]+) ', line)
            t = re.search(r'time (\d+d)? *(\d+h)? *(\d+m)? *(\d+s)? ', line)
            if k and l and t:
                k = float(k.group(1))
                l = float(l.group(1))
                t = [int(t.group(i)[:-1]) if t.group(i) else 0 for i in range(1, 5)]
                t = t[0] * 24*60*60 + t[1] * 60*60 + t[2] * 60 + t[3]
                png = os.path.join(src_result_subdir, 'fakes%06d.png' % int(np.floor(k)))
                if os.path.isfile(png):
                    times.append(t)
                    snaps.append((png, k, l))
    assert len(times)

    # Frame generation func for moviepy.
    png_cache = [None, None] # [png, img]
    def make_frame(t):
        wallclock = ((t / duration_sec) ** time_warp) * times[-1]
        png, kimg, lod = snaps[max(bisect.bisect(times, wallclock) - 1, 0)]
        if png_cache[0] == png:
            img = png_cache[1]
        else:
            img = scipy.misc.imread(png)
            while img.shape[1] > 1920 or img.shape[0] > 1080:
                img = img.astype(np.float32).reshape(img.shape[0]//2, 2, img.shape[1]//2, 2, -1).mean(axis=(1,3))
            png_cache[:] = [png, img]
        img = misc.draw_text_label(img, 'lod %.2f' % lod, 16, img.shape[0]-4, alignx=0.0, aligny=1.0)
        img = misc.draw_text_label(img, misc.format_time(int(np.rint(wallclock))), img.shape[1]//2, img.shape[0]-4, alignx=0.5, aligny=1.0)
        img = misc.draw_text_label(img, '%.0f kimg' % kimg, img.shape[1]-16, img.shape[0]-4, alignx=1.0, aligny=1.0)
        return img

    # Generate video.
    import moviepy.editor # pip install moviepy
    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    moviepy.editor.VideoClip(make_frame, duration=duration_sec).write_videofile(os.path.join(result_subdir, mp4), fps=mp4_fps, codec='libx264', bitrate=mp4_bitrate)
    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
def generate_test_image_with_corresponding_z(run_id,
                                             snapshot=None,
                                             grid_size=[1, 1],
                                             image_shrink=1,
                                             image_zoom=1,
                                             num_frames=None,
                                             random_seed=1000,
                                             minibatch_size=8):
    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if num_frames is None:
        num_frames = 4
    random_state = np.random.RandomState(random_seed)

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    print('Generating latent vectors...')
    shape = [num_frames, np.prod(grid_size)
             ] + Gs.input_shape[1:]  # [frame, image, channel, component]
    all_latents = random_state.randn(*shape).astype(np.float32)
    #all_latents = scipy.ndimage.gaussian_filter(all_latents, [smoothing_sec * mp4_fps] + [0] * len(Gs.input_shape), mode='wrap')
    all_latents /= np.sqrt(np.mean(np.square(all_latents)))

    # Generate images.
    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    for idx, latent in enumerate(all_latents):
        labels = np.zeros([latent.shape[0], 0], np.float32)
        images = Gs.run(latent,
                        labels,
                        minibatch_size=minibatch_size,
                        num_gpus=config.num_gpus,
                        out_mul=127.5,
                        out_add=127.5,
                        out_shrink=image_shrink,
                        out_dtype=np.uint8)
        grid = misc.create_image_grid(images, grid_size).transpose(1, 2,
                                                                   0)  # HWC
        if image_zoom > 1:
            grid = scipy.ndimage.zoom(grid, [image_zoom, image_zoom, 1],
                                      order=0)
        if grid.shape[2] == 1:
            grid = grid.repeat(3, 2)  # grayscale => RGB
        np.save(os.path.join(result_subdir, 'z_' + str(idx)), latent)
        filename = 'im_' + str(idx) + '.png'
        scipy.misc.imsave(os.path.join(result_subdir, filename), grid)
def generate_fake_images_cond(run_id, snapshot=None, grid_size=[1,1], num_pngs=1, image_shrink=1, png_prefix=None, random_seed=1000, minibatch_size=1):
    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if png_prefix is None:
        png_prefix = misc.get_id_string_for_network_pkl(network_pkl) + '-'
    random_state = np.random.RandomState(random_seed)
    
    
    training_set = dataset.load_dataset(data_dir=config.data_dir,   verbose=True, **config.dataset)
    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)
    real, label = training_set.get_minibatch_np(num_pngs)
    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    size=128

    for png_idx in range(num_pngs):
        

        real1= real[png_idx,:, :(size),:(size)]
        real2= real[png_idx,:, (size):,:(size)]
        real3= real[png_idx,:, :(size),(size):]
        real1=(real1.astype(np.float32)-127.5)/127.5
        real2=(real2.astype(np.float32)-127.5)/127.5
        real3=(real3.astype(np.float32)-127.5)/127.5
        latents = np.random.randn(3, 128, 128)
        left = np.concatenate((real1, real2), axis=1)
        print('left:'+str(left.shape))
        right = np.concatenate((real3, latents), axis=1)
        lat_and_cond = np.concatenate((left, right), axis=2)
        lat_and_cond = lat_and_cond[np.newaxis]
        fake_images_out_small = Gs.get_output_for(lat_and_cond, is_training=False)
        fake_images_out_small = (fake_images_out_small.eval()*127.5)+127.5
        print(fake_images_out_small.shape)
        fake_images_out_small=fake_images_out_small[0, :,:,:]
        
        real1=(real1.astype(np.float32)*127.5)+127.5
        real2=(real2.astype(np.float32)*127.5)+127.5
        real3=(real3.astype(np.float32)*127.5)+127.5     
        fake_image_out_right =np.concatenate((real3, fake_images_out_small), axis=1)                 
        fake_image_out_left = np.concatenate((real1, real2), axis=1)
        images = np.concatenate((fake_image_out_left, fake_image_out_right), axis=2)
        images = images[np.newaxis]
        misc.save_image_grid(images, os.path.join(result_subdir, '%s%06d.png' % (png_prefix, png_idx)), [0,255], grid_size)
    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
def generate_training_video(
    run_id,
    duration_sec=20.0,
    time_warp=1.5,
    mp4=None,
    mp4_fps=30,
    mp4_codec="libx265",
    mp4_bitrate="16M",
):
    src_result_subdir = misc.locate_result_subdir(run_id)
    if mp4 is None:
        mp4 = os.path.basename(src_result_subdir) + "-train.mp4"

    # Parse log.
    times = []
    snaps = []  # [(png, kimg, lod), ...]
    with open(os.path.join(src_result_subdir, "log.txt"), "rt") as log:
        for line in log:
            k = re.search(r"kimg ([\d\.]+) ", line)
            l = re.search(r"lod ([\d\.]+) ", line)
            t = re.search(r"time (\d+d)? *(\d+h)? *(\d+m)? *(\d+s)? ", line)
            if k and l and t:
                k = float(k.group(1))
                l = float(l.group(1))
                t = [
                    int(t.group(i)[:-1]) if t.group(i) else 0
                    for i in range(1, 5)
                ]
                t = t[0] * 24 * 60 * 60 + t[1] * 60 * 60 + t[2] * 60 + t[3]
                png = os.path.join(src_result_subdir,
                                   "fakes%06d.png" % int(np.floor(k)))
                if os.path.isfile(png):
                    times.append(t)
                    snaps.append((png, k, l))
    assert len(times)

    # Frame generation func for moviepy.
    png_cache = [None, None]  # [png, img]

    def make_frame(t):
        wallclock = ((t / duration_sec)**time_warp) * times[-1]
        png, kimg, lod = snaps[max(bisect.bisect(times, wallclock) - 1, 0)]
        if png_cache[0] == png:
            img = png_cache[1]
        else:
            img = scipy.misc.imread(png)
            while img.shape[1] > 1920 or img.shape[0] > 1080:
                img = (img.astype(np.float32).reshape(img.shape[0] // 2, 2,
                                                      img.shape[1] // 2, 2,
                                                      -1).mean(axis=(1, 3)))
            png_cache[:] = [png, img]
        img = misc.draw_text_label(img,
                                   "lod %.2f" % lod,
                                   16,
                                   img.shape[0] - 4,
                                   alignx=0.0,
                                   aligny=1.0)
        img = misc.draw_text_label(
            img,
            misc.format_time(int(np.rint(wallclock))),
            img.shape[1] // 2,
            img.shape[0] - 4,
            alignx=0.5,
            aligny=1.0,
        )
        img = misc.draw_text_label(
            img,
            "%.0f kimg" % kimg,
            img.shape[1] - 16,
            img.shape[0] - 4,
            alignx=1.0,
            aligny=1.0,
        )
        return img

    # Generate video.
    import moviepy.editor  # pip install moviepy

    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    moviepy.editor.VideoClip(make_frame,
                             duration=duration_sec).write_videofile(
                                 os.path.join(result_subdir, mp4),
                                 fps=mp4_fps,
                                 codec="libx264",
                                 bitrate=mp4_bitrate,
                             )
    open(os.path.join(result_subdir, "_done.txt"), "wt").close()
Esempio n. 14
0
def generate_interpolation_images(run_id,
                                  snapshot=None,
                                  grid_size=[1, 1],
                                  image_shrink=1,
                                  image_zoom=1,
                                  duration_sec=60.0,
                                  smoothing_sec=1.0,
                                  mp4=None,
                                  mp4_fps=30,
                                  mp4_codec='libx265',
                                  mp4_bitrate='16M',
                                  random_seed=1000,
                                  minibatch_size=8):

    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if mp4 is None:
        mp4 = misc.get_id_string_for_network_pkl(network_pkl) + '-lerp.mp4'
    num_frames = int(np.rint(duration_sec * mp4_fps))
    random_state = np.random.RandomState(random_seed)

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    print('Generating latent vectors...')
    shape = [num_frames, np.prod(grid_size)
             ] + Gs.input_shape[1:]  # [frame, image, channel, component]
    all_latents = random_state.randn(*shape).astype(np.float32)
    all_latents = scipy.ndimage.gaussian_filter(
        all_latents, [smoothing_sec * mp4_fps] + [0] * len(Gs.input_shape),
        mode='wrap')
    all_latents /= np.sqrt(np.mean(np.square(all_latents)))

    lsfm_model = m3io.import_lsfm_model(
        '/home/baris/Projects/faceganhd/models/all_all_all.mat')
    lsfm_tcoords = \
    mio.import_pickle('/home/baris/Projects/team members/stelios/UV_spaces_V2/UV_dicts/full_face/512_UV_dict.pkl')[
        'tcoords']
    lsfm_params = []
    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    for png_idx in range(int(num_frames / minibatch_size)):
        start = time.time()
        print('Generating png %d-%d / %d... in ' %
              (png_idx * minibatch_size,
               (png_idx + 1) * minibatch_size, num_frames),
              end='')
        latents = all_latents[png_idx * minibatch_size:(png_idx + 1) *
                              minibatch_size, 0]
        labels = np.zeros([latents.shape[0], 0], np.float32)
        images = Gs.run(latents,
                        labels,
                        minibatch_size=minibatch_size,
                        num_gpus=config.num_gpus,
                        out_shrink=image_shrink)
        for i in range(minibatch_size):
            texture = Image(np.clip(images[i, 0:3] / 2 + 0.5, 0, 1))
            mesh_raw = from_UV_2_3D(Image(images[i, 3:6]))
            normals = images[i, 6:9]
            normals_norm = (normals - normals.min()) / (normals.max() -
                                                        normals.min())
            mesh = lsfm_model.reconstruct(mesh_raw)
            lsfm_params.append(lsfm_model.project(mesh_raw))
            t_mesh = TexturedTriMesh(mesh.points, lsfm_tcoords.points, texture,
                                     mesh.trilist)
            m3io.export_textured_mesh(
                t_mesh,
                os.path.join(result_subdir,
                             '%06d.obj' % (png_idx * minibatch_size + i)),
                texture_extension='.png')
            mio.export_image(
                Image(normals_norm),
                os.path.join(result_subdir,
                             '%06d_nor.png' % (png_idx * minibatch_size + i)))
        print('%0.2f seconds' % (time.time() - start))
    mio.export_pickle(lsfm_params,
                      os.path.join(result_subdir, 'lsfm_params.pkl'))
    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
Esempio n. 15
0
def generate_fake_images(run_id,
                         snapshot=None,
                         grid_size=[1, 1],
                         batch_size=8,
                         num_pngs=1,
                         image_shrink=1,
                         png_prefix=None,
                         random_seed=1000,
                         minibatch_size=8):
    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if png_prefix is None:
        png_prefix = misc.get_id_string_for_network_pkl(network_pkl) + '-'
    random_state = np.random.RandomState(random_seed)

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    lsfm_model = m3io.import_lsfm_model(
        '/home/baris/Projects/faceganhd/models/all_all_all.mat')
    lsfm_tcoords = \
    mio.import_pickle('/home/baris/Projects/team members/stelios/UV_spaces_V2/UV_dicts/full_face/512_UV_dict.pkl')[
        'tcoords']
    lsfm_params = []

    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    for png_idx in range(int(num_pngs / batch_size)):
        start = time.time()
        print('Generating png %d-%d / %d... in ' %
              (png_idx * batch_size, (png_idx + 1) * batch_size, num_pngs),
              end='')
        latents = misc.random_latents(np.prod(grid_size) * batch_size,
                                      Gs,
                                      random_state=random_state)
        labels = np.zeros([latents.shape[0], 0], np.float32)
        images = Gs.run(latents,
                        labels,
                        minibatch_size=minibatch_size,
                        num_gpus=config.num_gpus,
                        out_shrink=image_shrink)
        for i in range(batch_size):
            if images.shape[1] == 3:
                mio.export_pickle(
                    images[i],
                    os.path.join(
                        result_subdir,
                        '%s%06d.pkl' % (png_prefix, png_idx * batch_size + i)))
                # misc.save_image(images[i], os.path.join(result_subdir, '%s%06d.png' % (png_prefix, png_idx*batch_size+i)), [0,255], grid_size)
            elif images.shape[1] == 6:
                mio.export_pickle(images[i][3:6],
                                  os.path.join(
                                      result_subdir, '%s%06d.pkl' %
                                      (png_prefix, png_idx * batch_size + i)),
                                  overwrite=True)
                misc.save_image(
                    images[i][0:3],
                    os.path.join(
                        result_subdir,
                        '%s%06d.png' % (png_prefix, png_idx * batch_size + i)),
                    [-1, 1], grid_size)
            elif images.shape[1] == 9:
                texture = Image(np.clip(images[i, 0:3] / 2 + 0.5, 0, 1))
                mesh_raw = from_UV_2_3D(Image(images[i, 3:6]))
                normals = images[i, 6:9]
                normals_norm = (normals - normals.min()) / (normals.max() -
                                                            normals.min())
                mesh = lsfm_model.reconstruct(mesh_raw)
                lsfm_params.append(lsfm_model.project(mesh_raw))
                t_mesh = TexturedTriMesh(mesh.points, lsfm_tcoords.points,
                                         texture, mesh.trilist)
                m3io.export_textured_mesh(
                    t_mesh,
                    os.path.join(result_subdir,
                                 '%06d.obj' % (png_idx * minibatch_size + i)),
                    texture_extension='.png')
                mio.export_image(
                    Image(normals_norm),
                    os.path.join(
                        result_subdir,
                        '%06d_nor.png' % (png_idx * minibatch_size + i)))
                shape = images[i, 3:6]
                shape_norm = (shape - shape.min()) / (shape.max() -
                                                      shape.min())
                mio.export_image(
                    Image(shape_norm),
                    os.path.join(
                        result_subdir,
                        '%06d_shp.png' % (png_idx * minibatch_size + i)))
                mio.export_pickle(
                    t_mesh,
                    os.path.join(result_subdir,
                                 '%06d.pkl' % (png_idx * minibatch_size + i)))

        print('%0.2f seconds' % (time.time() - start))

    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
Esempio n. 16
0
def generate_fake_images(run_id,
                         snapshot=None,
                         grid_size=[1, 1],
                         num_pngs=1,
                         image_shrink=1,
                         png_prefix=None,
                         random_seed=1000,
                         minibatch_size=8):

    embeddings_contant = False
    labels_constant = False
    latents_constant = False

    idx = random.randint(0, 56880)
    df = pandas.read_csv('datasets/50k_sorted_tf/50k_index_sorted.csv')
    print('embeddings_contant : ' + str(embeddings_contant))
    print('labels_constant : ' + str(labels_constant))
    print('latents_constant : ' + str(latents_constant))

    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if png_prefix is None:
        png_prefix = misc.get_id_string_for_network_pkl(network_pkl) + '-'
    random_state = np.random.RandomState(random_seed)

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    result_subdir = misc.create_result_subdir(config.result_dir + '/' + run_id,
                                              config.desc)

    if latents_constant:
        latents = misc.random_latents(np.prod(grid_size),
                                      Gs,
                                      random_state=None)
    #embeddings = np.zeros([1, 300], dtype=np.float32)
    #labels = np.zeros([1, 32], dtype=np.float32)
    embeddings = np.load(
        'datasets/50k_sorted_tf/sum_embedding_title.embeddings')
    embeddings = embeddings.astype('float32')

    labels = np.load(
        'datasets/50k_sorted_tf/sum_embedding_category_average.labels')
    labels = labels.astype('float32')
    name1 = ''
    if labels_constant:
        label = labels[idx]
        name1 = name1 + ' ' + df.at[idx, 'category1']
        label = label.reshape(1, label.shape[0])

    if embeddings_contant:
        embedding = embeddings[idx]
        title = df.at[idx, 'title']
        name1 = name1 + ' ' + title[:10]
        embedding = embedding.reshape(1, embedding.shape[0])

    #print(latents.shape)
    for png_idx in range(num_pngs):
        name = ''
        name = name + name1
        print('Generating png %d / %d...' % (png_idx, num_pngs))
        rand = random.randint(0, 56880)
        #rand = png_idx * 1810
        #labels = sess.run(classes[0])
        if not latents_constant:
            latents = misc.random_latents(np.prod(grid_size),
                                          Gs,
                                          random_state=random_state)
        if not labels_constant:
            label = labels[rand]
            label = label.reshape(1, label.shape[0])
            name = name + ' ' + df.at[rand, 'category1']
        if not embeddings_contant:
            embedding = embeddings[rand]
            title = df.at[rand, 'title']
            name = name + ' ' + title[:10]
            embedding = embedding.reshape(1, embedding.shape[0])

        #print(labels.shape)
        images = Gs.run(latents,
                        label,
                        embedding,
                        minibatch_size=minibatch_size,
                        num_gpus=config.num_gpus,
                        out_mul=127.5,
                        out_add=127.5,
                        out_shrink=image_shrink,
                        out_dtype=np.uint8)
        misc.save_image_grid(
            images, os.path.join(result_subdir,
                                 '%s%06d.png' % (name, png_idx)), [0, 255],
            grid_size)
    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
Esempio n. 17
0
def find_dir_latent_with_query_image(run_id, snapshot=None, grid_size=[1,1], num_pngs=1, image_shrink=1, png_prefix=None, random_seed=4123, minibatch_size=8, dir_path='../../data/ACDC/latents/cleaned_testing/'):
    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if png_prefix is None:
        png_prefix = misc.get_id_string_for_network_pkl(network_pkl) + '-'
    random_state = np.random.RandomState(random_seed)

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
    replicate_folder_structure(dir_path, result_subdir+'/')

    train_patients = sorted_nicely(glob.glob(dir_path+'*'))

    for patient in train_patients:
        cardiac_cycles = sorted_nicely(glob.glob(patient+'/*/*/*.png'))
        cfg = open(patient+'/Info.cfg')
        label = condition_to_onehot(cfg.readlines()[2][7:])
        cont = 0
        for cycle in cardiac_cycles:
            # Get folder containing the image
            supfolder = sup_folder(cycle)
            latent_subir = result_subdir + '/' + supfolder

            # Create query image - tensorflow constant
            query_image = cv2.imread(cycle) # read frame
            query_image = cv2.resize(query_image, (256, 256))
            query_image = query_image.transpose(2,0,1)
            query_image = query_image[np.newaxis]
            x = tf.constant(query_image, dtype=tf.float32, name='query_image')

            # Create G(z) - tensorflow variable and label
            latent = misc.random_latents(np.prod(grid_size), Gs, random_state=random_state)
            initial = tf.constant(latent, dtype=tf.float32)
            z = tf.Variable(initial_value=initial, dtype=tf.float32, name='latent_space')
            gz = Gs.run(latent, label, minibatch_size=minibatch_size, num_gpus=config.num_gpus, out_mul=127.5, out_add=127.5, out_shrink=image_shrink, out_dtype=np.float32)
            gz = tf.Variable(gz, dtype=tf.float32)

            # Define a loss function
            residual_loss = tf.losses.absolute_difference(x, gz)
            # Define an optimizer
            train_op = tf.train.AdamOptimizer(learning_rate=0.1).minimize(residual_loss)

            zs, gzs, step = [], [], 1
    
            with tf.Session() as sess:
                sess.run(tf.global_variables_initializer())
                _, loss_value = sess.run([train_op, residual_loss])
                while (loss_value > 2e-04 and step <= 5000):
                    _, loss_value = sess.run([train_op, residual_loss])
                    step += 1
                    if step % 1000 == 0:
                        print('Step {}, Loss value: {}'.format(step, loss_value))
                        gzs.append(sess.run(gz))
                        zs.append(sess.run(z))
            
            # save last image
            print('Image saved at {}'.format(os.path.join(latent_subir, '%s.png' % (cont))))
            misc.save_image_grid(gzs[-1], os.path.join(latent_subir, '%02d.png' % (cont)), [0,255], grid_size)
            print('Latent vectors saved at {}'.format(os.path.join(latent_subir, 'latent_%02d.npy' % (cont))))
            np.save(os.path.join(latent_subir, 'latent_%02d.npy' % (cont)), zs[-1])
            print('Labels saved at {}'.format(os.path.join(latent_subir, 'label_%02d.npy' % (cont))))
            np.save(os.path.join(latent_subir, 'label_%02d.npy' % (cont)), label)
            cont+=1

        cfg.close()
        cont = 0
Esempio n. 18
0
def generate_interpolation_images(run_id,
                                  snapshot=None,
                                  grid_size=[1, 1],
                                  image_shrink=1,
                                  image_zoom=1,
                                  duration_sec=60.0,
                                  smoothing_sec=1.0,
                                  mp4=None,
                                  mp4_fps=30,
                                  mp4_codec='libx265',
                                  mp4_bitrate='16M',
                                  random_seed=1000,
                                  minibatch_size=8):

    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if mp4 is None:
        mp4 = misc.get_id_string_for_network_pkl(network_pkl) + '-lerp.mp4'
    num_frames = int(np.rint(duration_sec * mp4_fps))
    random_state = np.random.RandomState(random_seed)

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    print('Generating latent vectors...')
    shape = [num_frames, np.prod(grid_size)] + [
        Gs.input_shape[1:][0] + Gs.input_shapes[1][1:][0]
    ]  # [frame, image, channel, component]
    all_latents = random_state.randn(*shape).astype(np.float32)
    all_latents = scipy.ndimage.gaussian_filter(
        all_latents, [smoothing_sec * mp4_fps] + [0] * len(Gs.input_shape),
        mode='wrap')
    all_latents /= np.sqrt(np.mean(np.square(all_latents)))

    #10 10 10 10 5 3 10
    # model = mio.import_pickle('../models/lsfm_shape_model_fw.pkl')
    # facesoft_model = mio.import_pickle('../models/facesoft_id_and_exp_3d_face_model.pkl')['shape_model']
    # lsfm_model = m3io.import_lsfm_model('/home/baris/Projects/faceganhd/models/all_all_all.mat')
    # model_mean = lsfm_model.mean().copy()
    # mask = mio.import_pickle('../UV_spaces_V2/mask_full_2_crop.pkl')
    lsfm_tcoords = \
    mio.import_pickle('512_UV_dict.pkl')['tcoords']
    lsfm_params = []
    result_subdir = misc.create_result_subdir(config_test.result_dir,
                                              config_test.desc)
    for png_idx in range(int(num_frames / minibatch_size)):
        start = time.time()
        print('Generating png %d-%d / %d... in ' %
              (png_idx * minibatch_size,
               (png_idx + 1) * minibatch_size, num_frames),
              end='')
        latents = all_latents[png_idx * minibatch_size:(png_idx + 1) *
                              minibatch_size, 0, :Gs.input_shape[1:][0]]
        labels = all_latents[png_idx * minibatch_size:(png_idx + 1) *
                             minibatch_size, 0, Gs.input_shape[1:][0]:]
        labels_softmax = softmax(labels) * np.array([10, 10, 10, 10, 5, 3, 10])
        images = Gs.run(latents,
                        labels_softmax,
                        minibatch_size=minibatch_size,
                        num_gpus=config_test.num_gpus,
                        out_shrink=image_shrink)
        for i in range(minibatch_size):
            texture = Image(np.clip(images[i, 0:3] / 2 + 0.5, 0, 1))
            img_shape = ndimage.gaussian_filter(images[i, 3:6],
                                                sigma=(0, 3, 3),
                                                order=0)
            mesh_raw = from_UV_2_3D(Image(img_shape),
                                    topology='full',
                                    uv_layout='oval')
            # model_mean.points[mask,:] = mesh_raw.points
            normals = images[i, 6:9]
            normals_norm = (normals - normals.min()) / (normals.max() -
                                                        normals.min())
            mesh = mesh_raw  #facesoft_model.reconstruct(model_mean).from_mask(mask)
            # lsfm_params.append(lsfm_model.project(mesh_raw))
            t_mesh = TexturedTriMesh(mesh.points, lsfm_tcoords.points, texture,
                                     mesh.trilist)
            m3io.export_textured_mesh(
                t_mesh,
                os.path.join(result_subdir,
                             '%06d.obj' % (png_idx * minibatch_size + i)),
                texture_extension='.png')
            fix_obj(
                os.path.join(result_subdir,
                             '%06d.obj' % (png_idx * minibatch_size + i)))
            mio.export_image(
                Image(normals_norm),
                os.path.join(result_subdir,
                             '%06d_nor.png' % (png_idx * minibatch_size + i)))
        print('%0.2f seconds' % (time.time() - start))
    mio.export_pickle(lsfm_params,
                      os.path.join(result_subdir, 'lsfm_params.pkl'))
    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
Esempio n. 19
0
def generate_fake_images(run_id,
                         snapshot=None,
                         grid_size=[1, 1],
                         batch_size=8,
                         num_pngs=1,
                         image_shrink=1,
                         png_prefix=None,
                         random_seed=1000,
                         minibatch_size=8):
    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    if png_prefix is None:
        png_prefix = misc.get_id_string_for_network_pkl(network_pkl) + '-'
    random_state = np.random.RandomState(random_seed)

    print('Loading network from "%s"...' % network_pkl)
    G, D, Gs = misc.load_network_pkl(run_id, snapshot)

    result_subdir = misc.create_result_subdir(config_test.result_dir,
                                              config_test.desc)
    for png_idx in range(int(num_pngs / batch_size)):
        start = time.time()
        print('Generating png %d-%d / %d... in ' %
              (png_idx * batch_size, (png_idx + 1) * batch_size, num_pngs),
              end='')
        latents = misc.random_latents(np.prod(grid_size) * batch_size,
                                      Gs,
                                      random_state=random_state)
        labels = np.zeros([latents.shape[0], 7], np.float32)
        images = Gs.run(latents,
                        labels,
                        minibatch_size=minibatch_size,
                        num_gpus=config_test.num_gpus,
                        out_shrink=image_shrink)
        for i in range(batch_size):
            if images.shape[1] == 3:
                mio.export_pickle(
                    images[i],
                    os.path.join(
                        result_subdir,
                        '%s%06d.pkl' % (png_prefix, png_idx * batch_size + i)))
                # misc.save_image(images[i], os.path.join(result_subdir, '%s%06d.png' % (png_prefix, png_idx*batch_size+i)), [0,255], grid_size)
            elif images.shape[1] == 6:
                mio.export_pickle(images[i][3:6],
                                  os.path.join(
                                      result_subdir, '%s%06d.pkl' %
                                      (png_prefix, png_idx * batch_size + i)),
                                  overwrite=True)
                misc.save_image(
                    images[i][0:3],
                    os.path.join(
                        result_subdir,
                        '%s%06d.png' % (png_prefix, png_idx * batch_size + i)),
                    [-1, 1], grid_size)
            elif images.shape[1] == 9:
                mio.export_pickle(images[i][3:6],
                                  os.path.join(
                                      result_subdir, '%s%06d_shp.pkl' %
                                      (png_prefix, png_idx * batch_size + i)),
                                  overwrite=True)
                mio.export_pickle(images[i][6:9],
                                  os.path.join(
                                      result_subdir, '%s%06d_nor.pkl' %
                                      (png_prefix, png_idx * batch_size + i)),
                                  overwrite=True)
                misc.save_image(
                    images[i][0:3],
                    os.path.join(
                        result_subdir,
                        '%s%06d.png' % (png_prefix, png_idx * batch_size + i)),
                    [-1, 1], grid_size)
        print('%0.2f seconds' % (time.time() - start))

    open(os.path.join(result_subdir, '_done.txt'), 'wt').close()