def fvd(generated_embd, reference_embd): with tf.Graph().as_default(): generated_embd = tf.convert_to_tensor(generated_embd, dtype=np.float32) reference_embd = tf.convert_to_tensor(reference_embd, dtype=np.float32) result = calculate_fvd(generated_embd, reference_embd) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.tables_initializer()) print("FVD is: %.2f." % sess.run(result))
def fvd_session(predictions, target): # 40, 10, 1, h, w # with tf.Graph().as_default(): NUMBER_OF_VIDEOS, VIDEO_LENGTH, FRAME_WIDTH, FRAME_HEIGHT, C = predictions.shape # with values in 0-255 assert C == 3 result = fvd.calculate_fvd( fvd.create_id3_embedding(fvd.preprocess(predictions, (224, 224))), fvd.create_id3_embedding(fvd.preprocess(target, (224, 224)))) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.tables_initializer()) print("FVD is: %.2f." % sess.run(result)) return result
def main(argv): del argv with tf.Graph().as_default(): first_set_of_videos = tf.zeros( [NUMBER_OF_VIDEOS, VIDEO_LENGTH, 64, 64, 3]) second_set_of_videos = tf.ones( [NUMBER_OF_VIDEOS, VIDEO_LENGTH, 64, 64, 3]) * 255 result = calculate_fvd( create_id3_embedding(preprocess(first_set_of_videos, (224, 224))), create_id3_embedding(preprocess(second_set_of_videos, (224, 224)))) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.tables_initializer()) print("FVD is: %.2f." % sess.run(result))
def main(argv): video1 = torch.zeros(16, 15, 64, 64, 3) video2 = torch.ones(16, 15, 64, 64, 3) * 255 del argv with tf.Graph().as_default(): first_set_of_videos = tf.convert_to_tensor(video1.numpy(), np.uint8) # first_set_of_videos = tf.zeros([NUMBER_OF_VIDEOS, VIDEO_LENGTH, 64, 64, 3]) second_set_of_videos = tf.convert_to_tensor(video2.numpy(), np.uint8) # second_set_of_videos = tf.ones([NUMBER_OF_VIDEOS, VIDEO_LENGTH, 64, 64, 3]) * 255 result = fvd.calculate_fvd( fvd.create_id3_embedding( fvd.preprocess(first_set_of_videos, (224, 224))), fvd.create_id3_embedding( fvd.preprocess(second_set_of_videos, (224, 224)))) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.tables_initializer()) print("FVD is: %.2f." % sess.run(result))
def fvd(generated_images, reference_images): with tf.Graph().as_default(): print (generated_images.shape) print (reference_images.shape) print (type(generated_images)) print (type(reference_images)) generated_images = tf.convert_to_tensor(generated_images, dtype=np.uint8) print("Converted to tensor generated_images ") reference_images = tf.convert_to_tensor(reference_images, dtype=np.uint8) print("Converted to tensor reference images") print (tf.shape(generated_images)) print (tf.shape(reference_images)) result = calculate_fvd( create_id3_embedding(preprocess(generated_images, (256, 256))), create_id3_embedding(preprocess(reference_images, (256, 256)))) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.tables_initializer()) print("FVD is: %.2f." % sess.run(result))
import train_vae import utils # Number of videos must be divisible by 16. NUMBER_OF_VIDEOS = 16 VIDEO_LENGTH = 32 rn = "golf" data_loader = dataloader.DataLoader() rp = data_loader.shuffle_data() vid, np_idx_new = data_loader.get_batch(0, rp) videos = vid.permute(0, 1, 3, 4, 2).float() videos = vid.permute(0, 1, 3, 4, 2).float().cpu().detach().numpy() videos = tf.convert_to_tensor(videos, dtype=tf.float32) generated = train_vae.main() #Choose a model generated_vid = generated.permute(0, 1, 3, 4, 2).float().cpu().detach().numpy() generated_vid = tf.slice(generated_vid, [0, 0, 0, 0, 0], [NUMBER_OF_VIDEOS, 32, 64, 64, 3]) generated_vid = tf.convert_to_tensor(generated_vid, dtype=tf.float32) result = fvd.calculate_fvd( fvd.create_id3_embedding(fvd.preprocess(videos, (224, 224))), fvd.create_id3_embedding(fvd.preprocess(generated_vid, (224, 224)))) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.tables_initializer()) print("FVD is: %.2f." % sess.run(result))
def main(argv): args = argv[0] # read file lists from directories dir0_gif_paths = [f for f in os.listdir(args.dir0) if f.endswith('.gif')] dir0_gif_paths = [os.path.join(args.dir0, f) for f in dir0_gif_paths] dir1_gif_paths = [f for f in os.listdir(args.dir1) if f.endswith('.gif')] dir1_gif_paths = [os.path.join(args.dir1, f) for f in dir1_gif_paths] # assert number of videos to be divisible by 16 remainder_dir0 = len(dir0_gif_paths) % VIDEO_BATCH_SIZE dir0_gif_paths = dir0_gif_paths[:-remainder_dir0] remainder_dir1 = len(dir1_gif_paths) % VIDEO_BATCH_SIZE dir1_gif_paths = dir1_gif_paths[:-remainder_dir1] # loop over video dirs in batches of 16, compute and assemble activations (id3_embedding) dir0_embeddings, dir1_embeddings = [], [] # graph0_initialized, graph1_initialized = False, False dir0_embeddings_file = os.path.join(args.dir0, 'id3_embeddings.npy') dir1_embeddings_file = os.path.join(args.dir1, 'id3_embeddings.npy') # --- dir0 ID3 embeddings if os.path.exists(dir0_embeddings_file): with open(dir0_embeddings_file, 'rb') as fp: dir0_embeddings = np.load(fp) print( f">>> Found stored ID3 activations for videos in {args.dir0} in {dir0_embeddings_file}." ) else: print(f">>> Computing ID3 activations for videos in {args.dir0}...") for batch_start_idx in tqdm( range(0, len(dir0_gif_paths), VIDEO_BATCH_SIZE)): with tf.Graph().as_default(): # load batch of videos from GIFs and represent as tensor dir0_videos = tf.stack( [tf.io.decode_gif(tf.io.read_file(f)) \ for f in dir0_gif_paths[batch_start_idx:batch_start_idx+VIDEO_BATCH_SIZE]]) with tf.Session() as sess: dir0_tensor = sess.run(dir0_videos) # define placeholder for subsequent feeding ph_dir0_videos = tf.placeholder(shape=[*dir0_tensor.shape], dtype=tf.uint8) # calculate embeddings id3_embeddings = fvd.create_id3_embedding( fvd.preprocess(ph_dir0_videos, (224, 224))) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.tables_initializer()) dir0_embeddings.append( sess.run(id3_embeddings, feed_dict={ph_dir0_videos: dir0_tensor})) dir0_embeddings = np.concatenate(dir0_embeddings, axis=0) with open(dir0_embeddings_file, 'wb') as fp: np.save(fp, dir0_embeddings) print( f">>> Saved ID3 embeddings for lookup in {dir0_embeddings_file}" ) print(f">>> Embedding matrix: {dir0_embeddings.shape}") # --- dir1 ID3 embeddings if os.path.exists(dir1_embeddings_file): with open(dir1_embeddings_file, 'rb') as fp: dir1_embeddings = np.load(fp) print( f">>> Found stored ID3 activations for videos in {args.dir1} in {dir1_embeddings_file}." ) else: print(f">>> Computing ID3 activations for videos in {args.dir1}...") for batch_start_idx in tqdm( range(0, len(dir1_gif_paths), VIDEO_BATCH_SIZE)): with tf.Graph().as_default(): # load batch of videos from GIFs and represent as tensor dir1_videos = tf.stack( [tf.io.decode_gif(tf.io.read_file(f)) \ for f in dir1_gif_paths[batch_start_idx:batch_start_idx+VIDEO_BATCH_SIZE]]) with tf.Session() as sess: dir1_tensor = sess.run(dir1_videos) # define placeholder for subsequent feeding ph_dir1_videos = tf.placeholder(shape=[*dir1_tensor.shape], dtype=tf.uint8) # calculate embeddings id3_embeddings = fvd.create_id3_embedding( fvd.preprocess(ph_dir1_videos, (224, 224))) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.tables_initializer()) dir1_embeddings.append( sess.run(id3_embeddings, feed_dict={ph_dir1_videos: dir1_tensor})) dir1_embeddings = np.concatenate(dir1_embeddings, axis=0) with open(dir1_embeddings_file, 'wb') as fp: np.save(fp, dir1_embeddings) print( f">>> Saved ID3 embeddings for lookup in {dir1_embeddings_file}" ) print(f">>> Embedding matrix: {dir1_embeddings.shape}") # --- final FVD with tf.Graph().as_default(): print(">>> Computing FVD...") result = fvd.calculate_fvd(dir0_embeddings, dir1_embeddings) with tf.Session() as sess: print(">>> FVD is: %.2f." % sess.run(result))