def main(_, run_eval_loop=True): # Fetch and generate images to run through Inception. with tf.name_scope('inputs'): real_data, num_classes = _get_real_data( FLAGS.num_images_generated, FLAGS.dataset_dir) generated_data = _get_generated_data( FLAGS.num_images_generated, FLAGS.conditional_eval, num_classes) # Compute Frechet Inception Distance. if FLAGS.eval_frechet_inception_distance: fid = util.get_frechet_inception_distance( real_data, generated_data, FLAGS.num_images_generated, FLAGS.num_inception_images) tf.summary.scalar('frechet_inception_distance', fid) # Compute normal Inception scores. if FLAGS.eval_real_images: inc_score = util.get_inception_scores( real_data, FLAGS.num_images_generated, FLAGS.num_inception_images) else: inc_score = util.get_inception_scores( generated_data, FLAGS.num_images_generated, FLAGS.num_inception_images) tf.summary.scalar('inception_score', inc_score) # If conditional, display an image grid of difference classes. if FLAGS.conditional_eval and not FLAGS.eval_real_images: reshaped_imgs = util.get_image_grid( generated_data, FLAGS.num_images_generated, num_classes, FLAGS.num_images_per_class) tf.summary.image('generated_data', reshaped_imgs, max_outputs=1) # Create ops that write images to disk. image_write_ops = None if FLAGS.conditional_eval and FLAGS.write_to_disk: reshaped_imgs = util.get_image_grid( generated_data, FLAGS.num_images_generated, num_classes, FLAGS.num_images_per_class) uint8_images = data_provider.float_image_to_uint8(reshaped_imgs) image_write_ops = tf.write_file( '%s/%s'% (FLAGS.eval_dir, 'conditional_cifar10.png'), tf.image.encode_png(uint8_images[0])) else: if FLAGS.num_images_generated >= 100 and FLAGS.write_to_disk: reshaped_imgs = tfgan.eval.image_reshaper( generated_data[:100], num_cols=FLAGS.num_images_per_class) uint8_images = data_provider.float_image_to_uint8(reshaped_imgs) image_write_ops = tf.write_file( '%s/%s'% (FLAGS.eval_dir, 'unconditional_cifar10.png'), tf.image.encode_png(uint8_images[0])) # For unit testing, use `run_eval_loop=False`. if not run_eval_loop: return tf.contrib.training.evaluate_repeatedly( FLAGS.checkpoint_dir, master=FLAGS.master, hooks=[tf.contrib.training.SummaryAtEndHook(FLAGS.eval_dir), tf.contrib.training.StopAfterNEvalsHook(1)], eval_ops=image_write_ops, max_number_of_evaluations=FLAGS.max_number_of_evaluations)
def main(_, run_eval_loop=True): with tf.name_scope('inputs'): noise, one_hot_labels = _get_generator_inputs( FLAGS.num_images_per_class, NUM_CLASSES, FLAGS.noise_dims) # Generate images. with tf.variable_scope('Generator'): # Same scope as in train job. images = networks.conditional_generator((noise, one_hot_labels)) # Visualize images. reshaped_img = tfgan.eval.image_reshaper( images, num_cols=FLAGS.num_images_per_class) tf.summary.image('generated_images', reshaped_img, max_outputs=1) # Calculate evaluation metrics. tf.summary.scalar('MNIST_Classifier_score', util.mnist_score(images, FLAGS.classifier_filename)) tf.summary.scalar('MNIST_Cross_entropy', util.mnist_cross_entropy( images, one_hot_labels, FLAGS.classifier_filename)) # Write images to disk. image_write_ops = tf.write_file( '%s/%s'% (FLAGS.eval_dir, 'conditional_gan.png'), tf.image.encode_png(data_provider.float_image_to_uint8(reshaped_img[0]))) # For unit testing, use `run_eval_loop=False`. if not run_eval_loop: return tf.contrib.training.evaluate_repeatedly( FLAGS.checkpoint_dir, hooks=[tf.contrib.training.SummaryAtEndHook(FLAGS.eval_dir), tf.contrib.training.StopAfterNEvalsHook(1)], eval_ops=image_write_ops, max_number_of_evaluations=FLAGS.max_number_of_evaluations)
def main(_, run_eval_loop=True): # Fetch real images. with tf.name_scope('inputs'): real_images, _, _ = data_provider.provide_data( 'train', FLAGS.num_images_generated, FLAGS.dataset_dir) image_write_ops = None if FLAGS.eval_real_images: tf.summary.scalar('MNIST_Classifier_score', util.mnist_score(real_images, FLAGS.classifier_filename)) else: # In order for variables to load, use the same variable scope as in the # train job. with tf.variable_scope('Generator'): images = networks.unconditional_generator( tf.random_normal([FLAGS.num_images_generated, FLAGS.noise_dims])) sess = tf.Session() saver = tf.train.Saver() if not restore_from_checkpoint(sess, saver): raise NotImplementedError options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() sess.run(images, options=options, run_metadata=run_metadata) cg = CompGraph('gan_mnist', run_metadata, tf.get_default_graph()) cg_tensor_dict = cg.get_tensors() cg_sorted_keys = sorted(cg_tensor_dict.keys()) cg_sorted_items = [] for cg_key in cg_sorted_keys: cg_sorted_items.append(tf.shape(cg_tensor_dict[cg_key])) cg_sorted_shape = sess.run(cg_sorted_items) cg.op_analysis(dict(zip(cg_sorted_keys, cg_sorted_shape)), 'gan_mnist.pickle') exit(0) #tf.summary.scalar('MNIST_Frechet_distance', # util.mnist_frechet_distance( # real_images, images, FLAGS.classifier_filename)) #tf.summary.scalar('MNIST_Classifier_score', # util.mnist_score(images, FLAGS.classifier_filename)) if FLAGS.num_images_generated >= 100: reshaped_images = tfgan.eval.image_reshaper( images[:100, ...], num_cols=10) uint8_images = data_provider.float_image_to_uint8(reshaped_images) image_write_ops = tf.write_file( '%s/%s'% (FLAGS.eval_dir, 'unconditional_gan.png'), tf.image.encode_png(uint8_images[0])) # For unit testing, use `run_eval_loop=False`. if not run_eval_loop: return tf.contrib.training.evaluate_repeatedly( FLAGS.checkpoint_dir, hooks=[tf.contrib.training.SummaryAtEndHook(FLAGS.eval_dir), tf.contrib.training.StopAfterNEvalsHook(1)], eval_ops=image_write_ops, max_number_of_evaluations=FLAGS.max_number_of_evaluations)
def main(_, run_eval_loop=True): with tf.name_scope('inputs'): images = data_provider.provide_data('validation', FLAGS.batch_size, dataset_dir=FLAGS.dataset_dir, patch_size=FLAGS.patch_size) # In order for variables to load, use the same variable scope as in the # train job. with tf.variable_scope('generator'): reconstructions, _, prebinary = networks.compression_model( images, num_bits=FLAGS.bits_per_patch, depth=FLAGS.model_depth, is_training=False) summaries.add_reconstruction_summaries(images, reconstructions, prebinary) # Visualize losses. pixel_loss_per_example = tf.reduce_mean(tf.abs(images - reconstructions), axis=[1, 2, 3]) pixel_loss = tf.reduce_mean(pixel_loss_per_example) tf.summary.histogram('pixel_l1_loss_hist', pixel_loss_per_example) tf.summary.scalar('pixel_l1_loss', pixel_loss) # Create ops to write images to disk. uint8_images = data_provider.float_image_to_uint8(images) uint8_reconstructions = data_provider.float_image_to_uint8(reconstructions) uint8_reshaped = summaries.stack_images(uint8_images, uint8_reconstructions) image_write_ops = tf.write_file( '%s/%s' % (FLAGS.eval_dir, 'compression.png'), tf.image.encode_png(uint8_reshaped[0])) # For unit testing, use `run_eval_loop=False`. if not run_eval_loop: return tf.contrib.training.evaluate_repeatedly( FLAGS.checkpoint_dir, master=FLAGS.master, hooks=[ tf.contrib.training.SummaryAtEndHook(FLAGS.eval_dir), tf.contrib.training.StopAfterNEvalsHook(1) ], eval_ops=image_write_ops, max_number_of_evaluations=FLAGS.max_number_of_evaluations)
def main(_, run_eval_loop=True): # Fetch real images. # with tf.name_scope('inputs'): # real_images, _, _ = data_provider.provide_data( # 'train', FLAGS.num_images_generated, FLAGS.dataset_dir) image_write_ops = None if FLAGS.eval_real_images: pass ''' tf.summary.scalar('MNIST_Classifier_score', util.mnist_score(real_images, FLAGS.classifier_filename)) ''' else: # In order for variables to load, use the same variable scope as in the # train job. with tf.variable_scope('Generator'): images = networks.unconditional_generator( tf.random_normal( [FLAGS.num_images_generated, FLAGS.noise_dims])) ''' tf.summary.scalar('MNIST_Frechet_distance', util.mnist_frechet_distance( real_images, images, FLAGS.classifier_filename)) tf.summary.scalar('MNIST_Classifier_score', util.mnist_score(images, FLAGS.classifier_filename)) ''' if FLAGS.num_images_generated >= 100: reshaped_images = tfgan.eval.image_reshaper(images[:100, ...], num_cols=10) uint8_images = data_provider.float_image_to_uint8(reshaped_images) image_write_ops = tf.write_file( '%s/%s' % (FLAGS.eval_dir, 'unconditional_gan.png'), tf.image.encode_png(uint8_images[0])) # For unit testing, use `run_eval_loop=False`. if not run_eval_loop: return print("**********{}".format([ tf.contrib.training.SummaryAtEndHook(FLAGS.eval_dir), tf.contrib.training.StopAfterNEvalsHook(1) ])) tf.contrib.training.evaluate_repeatedly( FLAGS.checkpoint_dir, hooks=[ tf.contrib.training.SummaryAtEndHook(FLAGS.eval_dir), tf.contrib.training.StopAfterNEvalsHook(1) ], eval_ops=image_write_ops, max_number_of_evaluations=FLAGS.max_number_of_evaluations)
def main(_, run_eval_loop=True): # Fetch real images. with tf.name_scope('inputs'): real_images, _, _ = data_provider.provide_data( 'train', FLAGS.num_images_generated, FLAGS.dataset_dir) image_write_ops = None if FLAGS.eval_real_images: tf.summary.scalar('MNIST_Classifier_score', util.mnist_score(real_images, FLAGS.classifier_filename)) else: # In order for variables to load, use the same variable scope as in the # train job. with tf.variable_scope('Generator'): images = networks.unconditional_generator( tf.random_normal([FLAGS.num_images_generated, FLAGS.noise_dims]), is_training=False) tf.summary.scalar('MNIST_Frechet_distance', util.mnist_frechet_distance( real_images, images, FLAGS.classifier_filename)) tf.summary.scalar('MNIST_Classifier_score', util.mnist_score(images, FLAGS.classifier_filename)) if FLAGS.num_images_generated >= 100 and FLAGS.write_to_disk: reshaped_images = tfgan.eval.image_reshaper( images[:100, ...], num_cols=10) uint8_images = data_provider.float_image_to_uint8(reshaped_images) image_write_ops = tf.write_file( '%s/%s'% (FLAGS.eval_dir, 'unconditional_gan.png'), tf.image.encode_png(uint8_images[0])) # For unit testing, use `run_eval_loop=False`. if not run_eval_loop: return tf.contrib.training.evaluate_repeatedly( FLAGS.checkpoint_dir, hooks=[tf.contrib.training.SummaryAtEndHook(FLAGS.eval_dir), tf.contrib.training.StopAfterNEvalsHook(1)], eval_ops=image_write_ops, max_number_of_evaluations=FLAGS.max_number_of_evaluations)
def _get_write_image_ops(eval_dir, filename, images): """Create Ops that write images to disk.""" return tf.write_file( '%s/%s' % (eval_dir, filename), tf.image.encode_png(data_provider.float_image_to_uint8(images)))
def main(_, run_eval_loop=True): if FLAGS.conditional_eval: FLAGS.checkpoint_dir = os.path.join(FLAGS.checkpoint_dir, 'conditional') else: FLAGS.checkpoint_dir = os.path.join(FLAGS.checkpoint_dir, 'unconditional') print('Checkpoint dir: {}'.format(FLAGS.checkpoint_dir)) # Fetch and generate images to run through Inception. with tf.name_scope('inputs'): real_data, num_classes = _get_real_data( FLAGS.num_images_generated, FLAGS.dataset_dir) generated_data = _get_generated_data( FLAGS.num_images_generated, FLAGS.conditional_eval, num_classes) model_name = 'gan_cifar_' if FLAGS.conditional_eval: model_name = model_name + 'cond' else: model_name = model_name + 'uncond' sess = tf.Session() saver = tf.train.Saver() if not restore_from_checkpoint(sess, saver): raise NotImplementedError options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() sess.run(generated_data, options=options, run_metadata=run_metadata) cg = CompGraph(model_name, run_metadata, tf.get_default_graph()) cg_tensor_dict = cg.get_tensors() cg_sorted_keys = sorted(cg_tensor_dict.keys()) cg_sorted_items = [] for cg_key in cg_sorted_keys: cg_sorted_items.append(tf.shape(cg_tensor_dict[cg_key])) cg_sorted_shape = sess.run(cg_sorted_items) cg.op_analysis(dict(zip(cg_sorted_keys, cg_sorted_shape)), '{}.pickle'.format(model_name)) exit(0) # Compute Frechet Inception Distance. #if FLAGS.eval_frechet_inception_distance: # fid = util.get_frechet_inception_distance( # real_data, generated_data, FLAGS.num_images_generated, # FLAGS.num_inception_images) # tf.summary.scalar('frechet_inception_distance', fid) # Compute normal Inception scores. #if FLAGS.eval_real_images: # inc_score = util.get_inception_scores( # real_data, FLAGS.num_images_generated, FLAGS.num_inception_images) #else: # inc_score = util.get_inception_scores( # generated_data, FLAGS.num_images_generated, FLAGS.num_inception_images) #tf.summary.scalar('inception_score', inc_score) # If conditional, display an image grid of difference classes. #if FLAGS.conditional_eval and not FLAGS.eval_real_images: # reshaped_imgs = util.get_image_grid( # generated_data, FLAGS.num_images_generated, num_classes, # FLAGS.num_images_per_class) # tf.summary.image('generated_data', reshaped_imgs, max_outputs=1) # Create ops that write images to disk. image_write_ops = None if FLAGS.conditional_eval: reshaped_imgs = util.get_image_grid( generated_data, FLAGS.num_images_generated, num_classes, FLAGS.num_images_per_class) uint8_images = data_provider.float_image_to_uint8(reshaped_imgs) image_write_ops = tf.write_file( '%s/%s'% (FLAGS.eval_dir, 'conditional_cifar10.png'), tf.image.encode_png(uint8_images[0])) else: if FLAGS.num_images_generated >= 100: reshaped_imgs = tfgan.eval.image_reshaper( generated_data[:100], num_cols=FLAGS.num_images_per_class) uint8_images = data_provider.float_image_to_uint8(reshaped_imgs) image_write_ops = tf.write_file( '%s/%s'% (FLAGS.eval_dir, 'unconditional_cifar10.png'), tf.image.encode_png(uint8_images[0])) # For unit testing, use `run_eval_loop=False`. if not run_eval_loop: return tf.contrib.training.evaluate_repeatedly( FLAGS.checkpoint_dir, master=FLAGS.master, hooks=[tf.contrib.training.SummaryAtEndHook(FLAGS.eval_dir), tf.contrib.training.StopAfterNEvalsHook(1)], eval_ops=image_write_ops, max_number_of_evaluations=FLAGS.max_number_of_evaluations)
def cnn_model(features, labels, mode): n_classes = 2 trainable = True learning_rate = 1e-5 images = features['images'] filenames = features['filenames'] # setup batch normalization if mode == tf.estimator.ModeKeys.TRAIN: norm_params = {'is_training': True} else: norm_params = {'is_training': False, 'updates_collections': None} # create the network with tf.contrib.framework.arg_scope( [layers.conv2d, layers.fully_connected], activation_fn=_leaky_relu, normalizer_fn=layers.batch_norm, normalizer_params=norm_params): # activation_fn=tf.nn.leaky_relu, normalizer_fn=None): if FLAGS.hyper_mode == "regular": print("set up the network in regular mode") base_size = 1024 conv1 = layers.conv2d(images, int(base_size / 32), [4, 4], stride=2, trainable=trainable) conv2 = layers.conv2d(conv1, int(base_size / 16), [4, 4], stride=2, trainable=trainable) elif FLAGS.hyper_mode == "tiny": print("set up the network in tiny mode") # bypass first two layer. base_size = int(1024 / 4) conv2 = images conv3 = layers.conv2d(conv2, int(base_size / 8), [4, 4], stride=2, trainable=trainable) conv4 = layers.conv2d(conv3, int(base_size / 4), [4, 4], stride=2, trainable=trainable) conv5 = layers.conv2d(conv4, int(base_size / 2), [4, 4], stride=2, trainable=trainable) flat = layers.flatten(conv5) fc1 = layers.fully_connected(flat, base_size) fc1_dropout = layers.dropout( fc1, is_training=(mode == tf.estimator.ModeKeys.TRAIN)) logits = layers.fully_connected(fc1_dropout, n_classes, activation_fn=None) if FLAGS.hyper_mode == "regular": visual_sample = conv1 elif FLAGS.hyper_mode == "tiny": visual_sample = conv3 predicted_classes = tf.argmax(logits, 1) if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec( mode=mode, predictions={ 'visual_sample': visual_sample, 'class': predicted_classes, 'prob': tf.nn.softmax(logits), 'images': data_provider.float_image_to_uint8(images), 'filenames': filenames }) groundtruth_classes = tf.argmax(labels, 1) loss = tf.losses.softmax_cross_entropy(onehot_labels=labels, logits=logits) if mode == tf.estimator.ModeKeys.TRAIN: optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) with tf.control_dependencies(update_ops): train_op = optimizer.minimize( loss, global_step=tf.train.get_global_step()) return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op) precision = tf.metrics.precision(labels=groundtruth_classes, predictions=predicted_classes) recall = tf.metrics.recall(labels=groundtruth_classes, predictions=predicted_classes) eval_metric_ops = { 'eval/accuracy': tf.metrics.accuracy(labels=groundtruth_classes, predictions=predicted_classes), 'eval/precision': precision, 'eval/recall': recall # 'f1_score': f1_score } return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
def main(_, run_eval_loop=True): # Fetch and generate images to run through Inception. with tf.name_scope('inputs'): real_data, num_classes = _get_real_data( # FLAGS.bs, FLAGS.dataset_dir) FLAGS.bs, FLAGS.dl) generated_data = _get_generated_data(FLAGS.bs, FLAGS.conditional_eval, num_classes) # Compute Frechet Inception Distance. if FLAGS.eval_frechet_inception_distance: fid = util.get_frechet_inception_distance(real_data, generated_data, FLAGS.bs, FLAGS.num_inception_images) tf.summary.scalar('frechet_inception_distance', fid) # Compute normal Inception scores. if FLAGS.eval_real_images: inc_score = util.get_inception_scores(real_data, FLAGS.bs, FLAGS.num_inception_images) else: inc_score = util.get_inception_scores(generated_data, FLAGS.bs, FLAGS.num_inception_images) tf.summary.scalar('inception_score', inc_score) # If conditional, display an image grid of difference classes. if FLAGS.conditional_eval and not FLAGS.eval_real_images: reshaped_imgs = util.get_image_grid(generated_data, FLAGS.bs, num_classes, FLAGS.num_images_per_class) tf.summary.image('generated_data', reshaped_imgs, max_outputs=1) # Create ops that write images to disk. image_write_ops = None if FLAGS.conditional_eval and FLAGS.write_to_disk: reshaped_imgs = util.get_image_grid(generated_data, FLAGS.bs, num_classes, FLAGS.num_images_per_class) uint8_images = data_provider.float_image_to_uint8(reshaped_imgs) image_write_ops = tf.write_file( '%s/%s' % (FLAGS.eval_dir, 'conditional_cifar10.png'), tf.image.encode_png(uint8_images[0])) else: if FLAGS.bs >= 100 and FLAGS.write_to_disk: reshaped_imgs = tfgan.eval.image_reshaper( generated_data[:100], num_cols=FLAGS.num_images_per_class) uint8_images = data_provider.float_image_to_uint8(reshaped_imgs) image_write_ops = tf.write_file( '%s/%s' % (FLAGS.eval_dir, 'unconditional_cifar10.png'), tf.image.encode_png(uint8_images[0])) # For unit testing, use `run_eval_loop=False`. if not run_eval_loop: return checkpoint_path = tf_saver.latest_checkpoint(FLAGS.ckpt) if (checkpoint_path is None): print("error, no checkpoint path") # set mkl env mkl_setup() sess_config = create_config_proto() session_creator = monitored_session.ChiefSessionCreator( scaffold=None, checkpoint_filename_with_path=checkpoint_path, master=FLAGS.master, config=sess_config) with monitored_session.MonitoredSession(session_creator=session_creator, hooks=None) as session: eval_ops = image_write_ops for warmup_i in range(FLAGS.nw): session.run(eval_ops, feed_dict=None) start = time.time() for batch_i in range(FLAGS.nb): session.run(eval_ops, feed_dict=None) end = time.time() inference_time = end - start print("Batch size:", FLAGS.bs, "\nBatches number:", FLAGS.nb) print("Time spent per BATCH: %.4f ms" % (inference_time * 1000 / (FLAGS.nb))) print("Total samples/sec: %.4f samples/s" % (FLAGS.nb * FLAGS.bs / inference_time))
def _get_write_image_ops(eval_dir, filename, images): """Create Ops that write images to disk.""" return tf.write_file( '%s/%s'% (eval_dir, filename), tf.image.encode_png(data_provider.float_image_to_uint8(images)))