def main(args): # load the dataset dataset = mnist.get_split('train', FLAGS.data_dir) # load batch of dataset images, labels = load_batch(dataset, FLAGS.batch_size, is_training=True) # run the image through the model logits, endpoints = lenet.lenet(images, num_classes=10, is_training=True, dropout_keep_prob=0.5) # get the cross-entropy loss one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes) slim.losses.softmax_cross_entropy(logits, one_hot_labels) total_loss = slim.losses.get_total_loss() tf.summary.scalar('loss', total_loss) # use RMSProp to optimize optimizer = tf.train.RMSPropOptimizer(0.001, 0.9) # create train op train_op = slim.learning.create_train_op(total_loss, optimizer, summarize_gradients=True) # run training slim.learning.train(train_op, FLAGS.log_dir, save_summaries_secs=20, number_of_steps=FLAGS.num_steps)
def main(args): # load the dataset dataset = mnist.get_split('test', FLAGS.data_dir) # load batch images, labels = load_batch( dataset, FLAGS.batch_size, is_training=False) # get the model prediction predictions = lenet(images) # convert prediction values for each class into single class prediction predictions = tf.to_int64(tf.argmax(predictions, 1)) # streaming metrics to evaluate metrics_to_values, metrics_to_updates = metrics.aggregate_metric_map({ 'mse': metrics.streaming_mean_squared_error(predictions, labels), 'accuracy': metrics.streaming_accuracy(predictions, labels), }) # write the metrics as summaries for metric_name, metric_value in metrics_to_values.iteritems(): tf.summary.scalar(metric_name, metric_value) # evaluate on the model saved at the checkpoint directory # evaluate every eval_interval_secs slim.evaluation.evaluation_loop( '', FLAGS.checkpoint_dir, FLAGS.log_dir, num_evals=FLAGS.num_evals, eval_op=metrics_to_updates.values(), eval_interval_secs=FLAGS.eval_interval_secs)
def main(args): # load the dataset dataset = mnist.get_split('test', FLAGS.data_dir) # load batch images, labels = load_batch(dataset, FLAGS.batch_size, is_training=False) # get the model prediction predictions = lenet(images) # convert prediction values for each class into single class prediction predictions = tf.to_int64(tf.argmax(predictions, 1)) # streaming metrics to evaluate metrics_to_values, metrics_to_updates = metrics.aggregate_metric_map({ 'mse': metrics.streaming_mean_squared_error(predictions, labels), 'accuracy': metrics.streaming_accuracy(predictions, labels), }) # write the metrics as summaries for metric_name, metric_value in metrics_to_values.iteritems(): tf.summary.scalar(metric_name, metric_value) # evaluate on the model saved at the checkpoint directory # evaluate every eval_interval_secs slim.evaluation.evaluation_loop( '', FLAGS.checkpoint_dir, FLAGS.log_dir, num_evals=FLAGS.num_evals, eval_op=metrics_to_updates.values(), eval_interval_secs=FLAGS.eval_interval_secs)
def main(args): dataset = mnist.get_split('train', '/tmp/mnist') images, labels = load_batch(dataset, BATCHSIZE, is_training=True) with slim.arg_scope(lenet.lenet_arg_scope()): logits, end_points = lenet.lenet(images, is_training=True) one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes) tf.losses.softmax_cross_entropy(one_hot_labels, logits) total_loss = tf.losses.get_total_loss() * LOSS_SCALING_FACTOR tf.summary.scalar('loss', total_loss / LOSS_SCALING_FACTOR) optimiser = tf.train.GradientDescentOptimizer(LEARNING_RATE) train_op = tf.contrib.training.create_train_op( total_loss, optimiser, summarize_gradients=True, transform_grads_fn=scale_down_grads) for i in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES): print(i) slim.learning.train( train_op, './log/train_3', save_summaries_secs=2, #session_wrapper=tf_debug.LocalCLIDebugWrapperSession )
def main(args): # load the dataset dataset = mnist.get_split('train', FLAGS.data_dir) # load batch of dataset images, labels = load_batch(dataset, FLAGS.batch_size, is_training=True) network_fn = nets_factory.get_network_fn("lenet", num_classes=10, is_training=True) # run the image through the model # predictions,_ = lenet.lenet(images) predictions, _ = network_fn(images) # slim.model_analyzer.analyze_ops(tf.get_default_graph(), print_info=True) variables = slim.get_model_variables() for var in variables: tf.summary.histogram(var.op.name, var) slim.model_analyzer.analyze_vars(variables, print_info=True) # get the cross-entropy loss one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes) tf.losses.softmax_cross_entropy(one_hot_labels, predictions) total_loss = tf.losses.get_total_loss() tf.summary.scalar('loss', total_loss) # use RMSProp to optimize optimizer = tf.train.RMSPropOptimizer(0.001, 0.9) # create train op train_op = slim.learning.create_train_op(total_loss, optimizer) # run training slim.learning.train(train_op, FLAGS.log_dir, save_summaries_secs=20, save_interval_secs=60 * 2)
def main(args): tf.logging.set_verbosity(tf.logging.DEBUG) # load the dataset dataset = mnist.get_split('test', FLAGS.data_dir) # load batch images, labels = load_batch( dataset, FLAGS.batch_size, is_training=False) print(images,labels) # get the model prediction predictions,_ = lenet.lenet(images) # convert prediction values for each class into single class prediction predictions = tf.to_int64(tf.argmax(predictions, 1)) # streaming metrics to evaluate metrics_to_values, metrics_to_updates = metrics.aggregate_metric_map({ 'mse': metrics.streaming_mean_squared_error(predictions, labels), 'accuracy': metrics.streaming_accuracy(predictions, labels), # 'Recall_3': slim.metrics.streaming_recall_at_k(predictions, labels, 3), }) # write the metrics as summaries for metric_name, metric_value in metrics_to_values.items(): summary_name = 'eval/%s' % metric_name tf.summary.scalar(summary_name, metric_value) # for name, value in metrics_to_values.items(): # summary_name = 'eval/%s' % name # op = tf.summary.scalar(summary_name, value, collections=[]) # op = tf.Print(op, [value], summary_name) # tf.add_to_collection(tf.GraphKeys.SUMMARIES, op) # evaluate on the model saved at the checkpoint directory # evaluate every eval_interval_secs # slim.evaluation.evaluation_loop( # '', # FLAGS.checkpoint_dir, # FLAGS.log_dir, # num_evals=FLAGS.num_evals, # eval_op=list(metrics_to_updates.values()), # eval_interval_secs=FLAGS.eval_interval_secs) checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_dir) num_batches = math.ceil(10000 / float(FLAGS.batch_size)) metric_values =slim.evaluation.evaluate_once( master ='', checkpoint_path =checkpoint_path, logdir =FLAGS.log_dir, num_evals=num_batches, eval_op=list(metrics_to_updates.values()), final_op=list(metrics_to_values.values()) ) for metric, value in zip(metrics_to_values.keys(), metric_values): print("%s: %f" %(metric, value))
def main(args): config = model_deploy.DeploymentConfig(num_clones=2) with tf.device(config.variables_device()): global_step = tf.train.get_or_create_global_step() with tf.device(config.inputs_device()): # load the dataset dataset = mnist.get_split('train', FLAGS.data_dir) # load batch of dataset images, labels = load_batch(dataset, FLAGS.batch_size, width=300, height=300, is_training=True) queue = prefetch_queue([images, labels], capacity=10) with tf.device(config.optimizer_device()): optimizer = tf.train.RMSPropOptimizer(0.001, 0.9) def model_fn(queue): images, labels = queue.dequeue() predictions = lenet(images) one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes) slim.losses.softmax_cross_entropy(predictions, one_hot_labels) model_dp = model_deploy.deploy(config, model_fn, [queue], optimizer=optimizer) sess_conf = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) sess = tf.Session(config=sess_conf) tf.train.start_queue_runners(sess=sess) sess.run(tf.global_variables_initializer()) s = time.time() for i in range(100): var = sess.run([model_dp.train_op, global_step]) if i % 10 == 0: print("{}, {}".format(i, var)) print("last : {}, {}s elapsed".format(var, time.time() - s))
def evaluate(): """Eval CIFAR-10 for a number of steps.""" with tf.Graph().as_default() as g: # Get images and labels for CIFAR-10. # eval_data = FLAGS.eval_data == 'test' # images, labels = LeNet.inputs(eval_data=eval_data) dataset = mnist.get_split('test', './tmp/LeNet_data') # Creates a TF-Slim DataProvider which reads the dataset in the background # during both training and testing. provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=10, shuffle=True) image, label = provider.get(['image', 'label']) # batch up some training data images, labels = tf.train.batch([image, label], batch_size=LeNet.BATCH_SIZE) print(images.shape) images = tf.cast(images, tf.float32) # Build a Graph that computes the logits predictions from the # inference model. logits = LeNet.inference(images) # Calculate predictions. top_k_op = tf.nn.in_top_k(logits, labels, 1) # Restore the moving average version of the learned variables for eval. variable_averages = tf.train.ExponentialMovingAverage( LeNet.MOVING_AVERAGE_DECAY) variables_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variables_to_restore) # Build the summary operation based on the TF collection of Summaries. summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(FLAGS.eval_dir, g) while True: eval_once(saver, summary_writer, top_k_op, summary_op) if FLAGS.run_once: break time.sleep(FLAGS.eval_interval_secs)
def main(args): dataset = mnist.get_split('train', FLAGS.data_dir) # load batch of dataset images, labels = load_batch(dataset, FLAGS.batch_size, is_training=True) # run the model predictions = lenet(images) # cross-entropy loss one_hot_labels = slim.one_hot_encoding(labels, dataset.num_classes) slim.losses.softmax_cross_entropy(predictions, one_hot_labels) total_loss = slim.losses.get_total_loss() optimizer = tf.train.AdamOptimizer() train_op = slim.learning.create_train_op(total_loss, optimizer, summarize_gradients=True) slim.learning.train(train_op, FLAGS.log_dir, save_summaries_secs=20)
def main(args): # load the dataset dataset = mnist.get_split('train', FLAGS.data_dir) # load batch of dataset images, labels = load_batch( dataset, FLAGS.batch_size, is_training=True) # run the image through the model predictions = lenet(images) # get the cross-entropy loss one_hot_labels = slim.one_hot_encoding( labels, dataset.num_classes) slim.losses.softmax_cross_entropy( predictions, one_hot_labels) total_loss = slim.losses.get_total_loss() tf.summary.scalar('loss', total_loss) # use RMSProp to optimize optimizer = tf.train.RMSPropOptimizer(0.001, 0.9) # create train op train_op = slim.learning.create_train_op( total_loss, optimizer, summarize_gradients=True) # run training slim.learning.train( train_op, FLAGS.log_dir, save_summaries_secs=20)
def train(): """Train LeNet for a number of steps.""" with tf.Graph().as_default(): global_step = tf.contrib.framework.get_or_create_global_step() dataset = mnist.get_split('train', './tmp/LeNet_data') # Creates a TF-Slim DataProvider which reads the dataset in the background # during both training and testing. provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=10, shuffle=True) image, label = provider.get(['image', 'label']) # batch up some training data images, labels = tf.train.batch([image, label], batch_size=LeNet.BATCH_SIZE) print(images.shape) images = tf.cast(images, tf.float32) # images=tf.transpose(images,[1,2,3,0])#tf.reshape(images,[28,28,1,64]) print(images.shape) # labels=tf.reshape(labels,[128,]) # print (images.shape) logits = LeNet.inference(images) print("logits shape:", logits.shape) # Calculate loss. print("label shape", labels.shape) loss = LeNet.loss(logits, labels) # Build a Graph that trains the model with one batch of examples and # updates the model parameters. train_op = LeNet.train(loss, global_step) # Parse pruning hyperparameters pruning_hparams = pruning.get_pruning_hparams().parse( FLAGS.pruning_hparams) # Create a pruning object using the pruning hyperparameters pruning_obj = pruning.Pruning(pruning_hparams, global_step=global_step) # Use the pruning_obj to add ops to the training graph to update the masks # The conditional_mask_update_op will update the masks only when the # training step is in [begin_pruning_step, end_pruning_step] specified in # the pruning spec proto mask_update_op = pruning_obj.conditional_mask_update_op() # Use the pruning_obj to add summaries to the graph to track the sparsity # of each of the layers pruning_obj.add_pruning_summaries() class _LoggerHook(tf.train.SessionRunHook): """Logs loss and runtime.""" def begin(self): self._step = -1 def before_run(self, run_context): self._step += 1 self._start_time = time.time() return tf.train.SessionRunArgs(loss) # Asks for loss value. def after_run(self, run_context, run_values): duration = time.time() - self._start_time loss_value = run_values.results if self._step % 10 == 0: num_examples_per_step = 128 examples_per_sec = num_examples_per_step / duration sec_per_batch = float(duration) format_str = ( '%s: step %d, loss = %.2f (%.1f examples/sec; %.3f ' 'sec/batch)') print(format_str % (datetime.datetime.now(), self._step, loss_value, examples_per_sec, sec_per_batch)) with tf.train.MonitoredTrainingSession( checkpoint_dir=FLAGS.train_dir, hooks=[ tf.train.StopAtStepHook(last_step=FLAGS.max_steps), tf.train.NanTensorHook(loss), _LoggerHook() ], config=tf.ConfigProto(log_device_placement=FLAGS. log_device_placement)) as mon_sess: while not mon_sess.should_stop(): mon_sess.run(train_op) # Update the masks mon_sess.run(mask_update_op)
def distorted_inputs(data_dir, batch_size): """Construct distorted input for CIFAR training using the Reader ops. Args: data_dir: Path to the CIFAR-10 data directory. batch_size: Number of images per batch. Returns: images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size. labels: Labels. 1D tensor of [batch_size] size. """ filenames = [ os.path.join(data_dir, 'data_batch_%d.bin' % i) for i in xrange(1, 6) ] for f in filenames: if not tf.gfile.Exists(f): raise ValueError('Failed to find file: ' + f) # Create a queue that produces the filenames to read. filename_queue = tf.train.string_input_producer(filenames) # Read examples from files in the filename queue. dataset = mnist.get_split('train', './tmp/LeNet_data') # Creates a TF-Slim DataProvider which reads the dataset in the background # during both training and testing. provider = slim.dataset_data_provider.DatasetDataProvider(dataset) images, labels = provider.get(['image', 'label']) reshaped_image = tf.cast(images, tf.float32) height = IMAGE_SIZE width = IMAGE_SIZE # Image processing for training the network. Note the many random # distortions applied to the image. # Randomly crop a [height, width] section of the image. distorted_image = tf.random_crop(reshaped_image, [height, width, 1]) # Randomly flip the image horizontally. distorted_image = tf.image.random_flip_left_right(distorted_image) # Because these operations are not commutative, consider randomizing # the order their operation. distorted_image = tf.image.random_brightness(distorted_image, max_delta=63) distorted_image = tf.image.random_contrast(distorted_image, lower=0.2, upper=1.8) # Subtract off the mean and divide by the variance of the pixels. float_image = tf.image.per_image_standardization(distorted_image) # Set the shapes of tensors. float_image.set_shape([height, width, 1]) read_input.label.set_shape([1]) # Ensure that the random shuffling has good mixing properties. min_fraction_of_examples_in_queue = 0.4 min_queue_examples = int(NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN * min_fraction_of_examples_in_queue) print('Filling queue with %d CIFAR images before starting to train. ' 'This will take a few minutes.' % min_queue_examples) # Generate a batch of images and labels by building up a queue of examples. return _generate_image_and_label_batch(float_image, read_input.label, min_queue_examples, batch_size, shuffle=True)