def get_inception_score(images, sess, batch_size, splits, pred_op, verbose=False): # assert(type(x) == list) assert (type(images[0]) == np.ndarray) assert (len(images[0].shape) == 3) assert (np.max(images[0]) > 10) assert (np.min(images[0]) >= 0.0) preds = [] num_examples = len(images) n_batches = int(math.floor(float(num_examples) / float(batch_size))) indices = list(np.arange(num_examples)) np.random.shuffle(indices) for i in range(n_batches): inp = [] for j in range(batch_size): if (i * batch_size + j) == num_examples: break img = images[indices[i * batch_size + j]] img = prep_incep_img(img) inp.append(img) if verbose: print("\rPropagating batch %d/%d" % (i + 1, n_batches)) pred = sess.run(pred_op, {'inputs:0': inp}) preds.append(pred) preds = np.concatenate(preds, 0) return get_inception_from_predictions(preds, splits)
def compute_imd(sess, real_img, gen_img, act_op, verbose=False): assert (len(real_img) == len(gen_img)) assert (type(real_img[0]) == np.ndarray) assert (type(gen_img[0]) == np.ndarray) assert (len(real_img[0].shape) == 3) assert (len(gen_img[0].shape) == 3) assert (np.max(real_img[0]) > 10) assert (np.min(gen_img[0]) >= 0.0) batch_size = FLAGS.batch_size d0 = len(real_img) if batch_size > d0: msg = "batch size is bigger than the data size" raise RuntimeError(msg) n_batches = d0 // batch_size n_used_imgs = n_batches * batch_size distances = np.empty(n_used_imgs) for i in range(n_batches): if verbose: print("\rComputing batch %d/%d" % (i + 1, n_batches), end="", flush=True) start = i * batch_size end = start + batch_size r_img_batch = [] g_img_batch = [] for j in range(start, end): r_img_batch.append(prep_incep_img(real_img[j])) g_img_batch.append(prep_incep_img(gen_img[j])) pred_real = sess.run(act_op, {'inputs:0': r_img_batch}) pred_gen = sess.run(act_op, {'inputs:0': g_img_batch}) distances[start:end] = get_cosine_dist(pred_real, pred_gen) if verbose: print(" done") return print('Mean {}, Std: {}'.format(np.mean(distances), np.std(distances)))
def get_activations(images, sess, batch_size, act_op, verbose=False): """Calculates the activations of the pool_3 layer for all x. Params: -- x : Numpy array of dimension (n_images, hi, wi, 3). The values must lie between 0 and 256. -- sess : current session -- batch_size : the x numpy array is split into batches with batch size batch_size. A reasonable batch size depends on the disposable hardware. -- verbose : If set to True and parameter out_step is given, the number of calculated batches is reported. Returns: -- A numpy array of dimension (num x, 2048) that contains the activations of the given tensor when feeding inception with the query tensor. """ assert (type(images[0]) == np.ndarray) assert (len(images[0].shape) == 3) assert (np.max(images[0]) > 10) assert (np.min(images[0]) >= 0.0) d0 = len(images) if batch_size > d0: msg = "batch size is bigger than the data size" raise RuntimeError(msg) n_batches = d0 // batch_size n_used_imgs = n_batches * batch_size pred_arr = np.empty((n_used_imgs, 2048)) for i in range(n_batches): if verbose: print("\rPropagating batch %d/%d" % (i + 1, n_batches), end="", flush=True) start = i * batch_size end = start + batch_size batch = [] for j in range(start, end): batch.append(prep_incep_img(images[j])) pred = sess.run(act_op, {'inputs:0': batch}) pred_arr[start:end] = pred if verbose: print(" done") return pred_arr
def evaluate_inception(self): incep_batch_size = self.cfg.EVAL.INCEP_BATCH_SIZE logits, _ = load_inception_inference(self.sess, self.cfg.EVAL.NUM_CLASSES, incep_batch_size, self.cfg.EVAL.INCEP_CHECKPOINT_DIR) pred_op = tf.nn.softmax(logits) z = tf.placeholder(tf.float32, [self.bs, self.model.stagei.z_dim], name='z') cond = tf.placeholder(tf.float32, [self.bs] + [self.model.stagei.embed_dim], name='cond') stagei_gen, _, _ = self.model.stagei.generator(z, cond, reuse=False, is_training=False) eval_gen, _, _ = self.model.generator(stagei_gen, cond, reuse=False, is_training=False) self.Retrieval.eval(self.bs) saver = tf.train.Saver(tf.global_variables('g_net')+tf.global_variables('vf_')+tf.global_variables('sf_')+ tf.global_variables('att')) could_load, _ = load(saver, self.sess, self.model.stagei.cfg.CHECKPOINT_DIR) if could_load: print(" [*] Load SUCCESS") else: print(" [!] Load failed...") raise RuntimeError('Could not load the checkpoints of stage I') saver = tf.train.Saver(tf.global_variables('stageII_g_net')) could_load, _ = load(saver, self.sess, self.cfg.CHECKPOINT_DIR) if could_load: print(" [*] Load SUCCESS") else: print(" [!] Load failed...") raise RuntimeError('Could not load the checkpoints of stage II') print('Generating batches...') size = self.cfg.EVAL.SIZE n_batches = size // self.bs all_preds = [] for i in range(n_batches): print("\rGenerating batch %d/%d" % (i + 1, n_batches), end="", flush=True) sample_z = np.random.normal(0, 1, size=(self.bs, self.model.z_dim)) # _, _, embed, _, _ = self.dataset.test.next_batch(self.bs, 4, embeddings=True) _, _, embed, _, _ = self.dataset.test.next_batch(self.bs, 1, embeddings=True) im_feats, sent_feats, labels = self.test_data_loader.get_batch(i, self.bs, phase = 'incep') # Generate a batch and scale it up for inception sent_emb = self.sess.run(self.Retrieval.sent_embed_tensor, feed_dict={ self.Retrieval.image_placeholder_test: im_feats, self.Retrieval.sent_placeholder_test: sent_feats, }) gen_batch = self.sess.run(eval_gen, feed_dict={z: sample_z, cond: sent_emb}) samples = denormalize_images(gen_batch) incep_samples = np.empty((self.bs, 299, 299, 3)) for sample_idx in range(self.bs): incep_samples[sample_idx] = prep_incep_img(samples[sample_idx]) # Run prediction for current batch pred = self.sess.run(pred_op, feed_dict={'inputs:0': incep_samples}) all_preds.append(pred) # Get rid of the first dimension all_preds = np.concatenate(all_preds, 0) print('\nComputing inception score...') mean, std = inception_score.get_inception_from_predictions(all_preds, 10) print('Inception Score | mean:', "%.2f" % mean, 'std:', "%.2f" % std)
for i in range(n_batches): print("\rGenerating batch %d/%d" % (i + 1, n_batches), end="", flush=True) sample_z = np.random.normal(0, 1, size=(batch_size, sample_size)) _, _, embed, _, _ = dataset.test.next_batch(batch_size, 4, embeddings=True) # Generate a batch and scale it up for inception gen_batch = sess.run(gen_op, feed_dict={z: sample_z, cond: embed}) gen_batch = np.clip(gen_batch, -1., 1.) samples = denormalize_images(gen_batch) incep_samples = np.empty((batch_size, 299, 299, 3)) for sample_idx in range(batch_size): incep_samples[sample_idx] = prep_incep_img(samples[sample_idx]) # Run prediction for current batch pred = sess.run(pred_op, feed_dict={'inputs:0': incep_samples}) all_preds.append(pred) # Get rid of the first dimension all_preds = np.concatenate(all_preds, 0) print('\nComputing inception score...') mean, std = inception_score.get_inception_from_predictions( all_preds, 10) print('Inception Score | mean:', "%.2f" % mean, 'std:', "%.2f" % std)