Exemplo n.º 1
0
def main(_):

    FLAGS.batch_size = 1
    pic_set = [
        "F:/TensorFlowDev/PythonWorksp/TensorFlow/furniture/bed/baby-bed/baby-bed356.jpg",
        "F:/TensorFlowDev/PythonWorksp/TensorFlow/furniture/bed/hammock/hammock1476.jpg",
        "F:/TensorFlowDev/PythonWorksp/TensorFlow/RetrainInception/flower_photos/tulips/3150964108_24dbec4b23_m.jpg",
        "F:/TensorFlowDev/PythonWorksp/TensorFlow/RetrainInception/flower_photos/tulips/3105702091_f02ce75226.jpg",
        "F:/TensorFlowDev/www/upload-files/6454_14991605780.jpg",
        "F:/TensorFlowDev/www/upload-files/4589_14991507381.jpg",
        "F:/TensorFlowDev/www/upload-files/7255_14991507381.jpg",
        "F:/TensorFlowDev/PythonWorksp/TensorFlow/furniture/bed/bunk-bed/bunk-bed576.jpg",
        "F:/TensorFlowDev/www/upload-files/1501723500_05517.jpg",
        "F:/TensorFlowDev/www/upload-files/1501723499_19721-n.jpg"
    ]
    with tf.name_scope('input'):  #
        img_path = pic_set[9]
        filename_queue = tf.train.string_input_producer([img_path])  #
        reader = tf.WholeFileReader()
        key, value = reader.read(filename_queue)
        orig_img = tf.image.decode_jpeg(value)
        print('here1')
        resized_img = tf.image.resize_images(orig_img,
                                             [IMAGE_SIZE, IMAGE_SIZE])
        resized_img.set_shape((IMAGE_SIZE, IMAGE_SIZE, 3))

        float_img = tf.image.per_image_standardization(resized_img)
        image = tf.expand_dims(float_img, 0)
        print('here2')
        tf.summary.image('input-image', image)

    #logits = general.inference(image)
    action = general.inference(image)

    # Restore the moving average version of the learned variables for eval.
    variable_averages = tf.train.ExponentialMovingAverage(
        general.MOVING_AVERAGE_DECAY)
    variables_to_restore = variable_averages.variables_to_restore(
    )  #moving_avg_variables=tf.moving_average_variables() still lack somthing
    #del variables_to_restore['input/vis-image/ExponentialMovingAverage']
    #del variables_to_restore['input/vis-image/Adam_1']
    #del variables_to_restore['train/beta2_power']
    #del variables_to_restore['train/beta1_power']
    #del variables_to_restore['input/vis-image/Adam']
    saver = tf.train.Saver(variables_to_restore)

    merged_summary = tf.summary.merge_all()
    summary_writer = tf.summary.FileWriter(
        FLAGS.vis_dir + img_path[img_path.rfind('/'):img_path.rfind('.')])

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            saver.restore(sess, ckpt.model_checkpoint_path)
            # Assuming model_checkpoint_path looks something like:
            #   /my-favorite-path/cifar10_train/model.ckpt-0,
            # extract global_step from it.
            global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                '-')[-1]
        else:
            print('No checkpoint file found')
            return

        summary_writer.add_graph(sess.graph)
        #print(sess.run(image))
        #hang forever?

        # Start the queue runners.
        coord = tf.train.Coordinator()
        try:
            threads = []
            for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
                threads.extend(
                    qr.create_threads(sess,
                                      coord=coord,
                                      daemon=True,
                                      start=True))
                print(sess.run(action))
                '''
			pool1, argmax = sess.run(action)

			raw = [[0 for x in range(FLAGS.batch_size)] for y in range(IMAGE_SIZE*IMAGE_SIZE*64)] 
			for i in range(int(IMAGE_SIZE/2)*int(IMAGE_SIZE/2)*64):
				for j in range(FLAGS.batch_size):
					raw[j][argmax[j][i]] = pool1[j][i]

			unpooled = tf.conver_to_tensor(raw)
			unpooled = tf.reshape(unpooled, [FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE, 64])
			unpooled_trans = general.deconv1(unpooled, kernel1)
			tf.summary.image("reverse_pool1_discrete", unpooled_trans, max_outputs=16)
			#neurons = tf.split(argmax, 64, axis=3)
			#neurons = tf.squeeze(neurons)
			#tf.summary.image("argmax_pool1", )
			'''
            summary = sess.run(merged_summary)
            summary_writer.add_summary(summary, 0)

        except Exception as e:  # pylint: disable=broad-except
            coord.request_stop(e)

        coord.request_stop()
        coord.join(threads, stop_grace_period_secs=10)

        print('here3')

    return
Exemplo n.º 2
0
def main(_):
  ps_hosts = FLAGS.ps_hosts.split(",")
  worker_hosts = FLAGS.worker_hosts.split(",")

  # Create a cluster from the parameter server and worker hosts.
  cluster = tf.train.ClusterSpec({"ps": ps_hosts, "worker": worker_hosts})

  # Create and start a server for the local task.
  server = tf.train.Server(cluster,
                           job_name=FLAGS.job_name,
                           task_index=FLAGS.task_index)

  if FLAGS.job_name == "ps":
    server.join()
  elif FLAGS.job_name == "worker":

    # Assigns ops to the local worker by default.
    with tf.device(tf.train.replica_device_setter(
        worker_device="/job:worker/task:%d" % FLAGS.task_index,
        cluster=cluster)):
      images, labels = general.distorted_inputs()

      # Build a Graph that computes the logits predictions from the
      # inference model.
      logits = general.inference(images)

      # Calculate loss.
      loss = general.loss(logits, labels)

      global_step = tf.contrib.framework.get_or_create_global_step()

      train_op = tf.train.AdagradOptimizer(0.01).minimize(
          loss, global_step=global_step)

    class _LoggerHook(tf.train.SessionRunHook):
      """Logs loss and runtime."""

      def begin(self):
        self._step = -1
        self._start_time = time.time()

      def before_run(self, run_context):
        self._step += 1
        return tf.train.SessionRunArgs([loss])  # Asks for loss value.

      def after_run(self, run_context, run_values):
        if self._step % 20 == 0:
          current_time = time.time()
          duration = current_time - self._start_time
          self._start_time = current_time

          loss_value = run_values.results[0]
          examples_per_sec = 20 * 128 / duration
          sec_per_batch = float(duration / 20)

          format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                        'sec/batch)')
          print (format_str % (datetime.now(), self._step, loss_value,
                               examples_per_sec, sec_per_batch))

    # The StopAtStepHook handles stopping after running given steps.
    hooks=[tf.train.StopAtStepHook(last_step=1000),_LoggerHook()]

    # The MonitoredTrainingSession takes care of session initialization,
    # restoring from a checkpoint, saving to a checkpoint, and closing when done
    # or an error occurs.
    with tf.train.MonitoredTrainingSession(master=server.target,
                                           is_chief=(FLAGS.task_index == 0),
                                           checkpoint_dir="./logs/train_logs",
                                           hooks=hooks) as mon_sess:
      while not mon_sess.should_stop():
        # Run a training step asynchronously.
        # See `tf.train.SyncReplicasOptimizer` for additional details on how to
        # perform *synchronous* training.
        # mon_sess.run handles AbortedError in case of preempted PS.
        mon_sess.run(train_op)
Exemplo n.º 3
0
def build_and_save(images, builder, output_graph):
    # Build a Graph that computes the logits predictions from the
    # inference model.
    logits = general.inference(images)

    # Restore the moving average version of the learned variables for eval.
    variable_averages = tf.train.ExponentialMovingAverage(
        general.MOVING_AVERAGE_DECAY)
    variables_to_restore = variable_averages.variables_to_restore()
    saver = tf.train.Saver(variables_to_restore)

    merged_summary = tf.summary.merge_all()
    # Build the summary operation based on the TF collection of Summaries.
    #summary_op = tf.summary.merge_all()

    # Before exporting our graph, we need to precise what is our output node
    # This is how TF decides what part of the Graph he has to keep and what part it can dump
    # NOTE: this variable is plural, because you can have multiple output nodes
    output_node_names = "softmax_linear/softmax_linear"

    # We clear devices to allow TensorFlow to control on which device it will load operations
    clear_devices = True

    summary_writer = tf.summary.FileWriter(FLAGS.save_dir)
    graph = tf.get_default_graph()
    input_graph_def = graph.as_graph_def()

    with tf.Session() as sess:
        ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
        if ckpt and ckpt.model_checkpoint_path:
            # Restores from checkpoint
            saver.restore(sess, ckpt.model_checkpoint_path)
            # Assuming model_checkpoint_path looks something like:
            #   /my-favorite-path/cifar10_train/model.ckpt-0,
            # extract global_step from it.
            global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                '-')[-1]
        else:
            print('No checkpoint file found')
            return

        summary_writer.add_graph(sess.graph)

        # Start the queue runners.
        coord = tf.train.Coordinator()
        try:
            threads = []
            for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
                threads.extend(
                    qr.create_threads(sess,
                                      coord=coord,
                                      daemon=True,
                                      start=True))

            # We use a built-in TF helper to export variables to constants
            output_graph_def = graph_util.convert_variables_to_constants(
                sess,  # The session is used to retrieve the weights
                input_graph_def,  # The graph_def is used to retrieve the nodes 
                output_node_names.split(
                    ","
                )  # The output node names are used to select the usefull nodes
            )

            # Finally we serialize and dump the output graph to the filesystem
            with tf.gfile.GFile(output_graph, "wb") as f:
                f.write(output_graph_def.SerializeToString())
            print("%d ops in the final graph." % len(output_graph_def.node))

            #什么也不干
            builder.add_meta_graph_and_variables(
                sess, [tf.saved_model.tag_constants.SERVING])
            #builder.save(True)
            builder.save()
        except Exception as e:  # pylint: disable=broad-except
            coord.request_stop(e)

        coord.request_stop()
        coord.join(threads, stop_grace_period_secs=10)
Exemplo n.º 4
0
def train():
    """Train CIFAR-10 for a number of steps."""
    with tf.Graph().as_default():
        global_step = tf.contrib.framework.get_or_create_global_step()

        # Get images and labels for CIFAR-10.
        # Force input pipeline to CPU:0 to avoid operations sometimes ending up on
        # GPU and resulting in a slow down.
        with tf.device('/cpu:0'):
            images, labels = general.distorted_inputs()

        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits = general.inference(images)

        # Calculate loss.
        loss = general.loss(logits, labels)

        #Calculate accuracy
        accuracy = general.accuracy(logits, labels)

        # updates the model parameters.
        train_op = general.train(loss, global_step)

        #builder = tf.saved_model.builder.SavedModelBuilder(general.MODEL_DIR) //can't save later in end() because the graph will be frozen after begin()

        class _LoggerHook(tf.train.SessionRunHook):
            """Logs loss and runtime."""
            def begin(self):
                self._step = -1
                self._start_time = time.time()

            def before_run(self, run_context):
                self._step += 1
                return tf.train.SessionRunArgs([loss, accuracy
                                                ])  # Asks for loss value.

            def after_run(self, run_context, run_values):
                if self._step % FLAGS.log_frequency == 0:
                    current_time = time.time()
                    duration = current_time - self._start_time
                    self._start_time = current_time

                    loss_value = run_values.results[0]
                    examples_per_sec = FLAGS.log_frequency * FLAGS.batch_size / duration
                    sec_per_batch = float(duration / FLAGS.log_frequency)

                    format_str = (
                        '%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                        'sec/batch)')
                    print(format_str % (datetime.now(), self._step, loss_value,
                                        examples_per_sec, sec_per_batch))
                if self._step % FLAGS.eval_frequency == 0:
                    accuracy = run_values.results[1]
                    print('%s: precision @ 1 = %.3f' %
                          (datetime.now(), accuracy))
                #if self._step == FLAGS.max_steps -1:
                #builder = run_values.results[2]

            #def end(self, session):

        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)
Exemplo n.º 5
0
def evaluate():
    """Run Eval once.

  Args:
    saver: Saver.
    summary_writer: Summary writer.
    top_k_op: Top K op.
    summary_op: Summary op.
  """
    with tf.Graph().as_default() as g:
        # Get images and labels for CIFAR-10.
        eval_data = FLAGS.eval_data == 'test'
        images, labels = read_image.inputs(eval_data, FLAGS.eval_batch_size)

        #这样居然也可以
        FLAGS.batch_size = FLAGS.eval_batch_size

        # Build a Graph that computes the logits predictions from the
        # inference model.
        logits = general.inference(images)

        accuracy = general.accuracy(logits, labels)

        # 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(
            general.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)

        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
            if ckpt and ckpt.model_checkpoint_path:
                # Restores from checkpoint
                saver.restore(sess, ckpt.model_checkpoint_path)
                # Assuming model_checkpoint_path looks something like:
                #   /my-favorite-path/cifar10_train/model.ckpt-0,
                # extract global_step from it.
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
            else:
                print('No checkpoint file found')
                return

            summary_writer.add_graph(sess.graph)

            # Start the queue runners.
            coord = tf.train.Coordinator()
            try:
                threads = []
                for qr in tf.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
                    threads.extend(
                        qr.create_threads(sess,
                                          coord=coord,
                                          daemon=True,
                                          start=True))

                num_iter = int(
                    math.ceil(FLAGS.num_examples / FLAGS.eval_batch_size))
                true_count = 0  # Counts the number of correct predictions.
                total_sample_count = num_iter * FLAGS.batch_size
                step = 0
                accuracy_sum = 0

                while step < num_iter and not coord.should_stop():
                    predictions = sess.run([top_k_op])
                    accuracy_sum += sess.run(accuracy)
                    true_count += np.sum(predictions)
                    step += 1

                # Compute precision @ 1.
                precision = true_count / total_sample_count
                accuracy_avg = accuracy_sum / step
                print('%s: precision @ 1 = %.4f' % (datetime.now(), precision))
                print('%s: accuracy @ 1 = %.4f' %
                      (datetime.now(), accuracy_avg))
                '''
        2017-06-29 17:44:36.120575: precision @ 1 = 0.396
        2017-06-29 17:44:36.120575: accuracy @ 1 = 0.411
        真的不一样耶,为什么呢?随机吗?
        '''
                summary = tf.Summary()
                summary.ParseFromString(sess.run(summary_op))
                summary.value.add(tag='Precision @ 1', simple_value=precision)
                summary_writer.add_summary(summary, global_step)

            except Exception as e:  # pylint: disable=broad-except
                coord.request_stop(e)

            coord.request_stop()
            coord.join(threads, stop_grace_period_secs=10)