python - 如何在 Tensorflow 上测试自己的图像到 Cifar-10 教程?

标签 python tensorflow

我训练了 Tensorflow Cifar10 模型,我想为其提供自己的单个图像(32*32,jpg/png)。

我想看到标签和每个标签的概率作为输出,但我对此遇到了一些麻烦..

搜索堆栈溢出后,我发现了一些帖子,其中this我修改了 cifar10_eval.py。

但是根本不起作用。

错误消息是:

InvalidArgumentErrorTraceback (most recent call last) in () ----> 1 evaluate()

in evaluate() 86 # Restores from checkpoint 87 print("ckpt.model_checkpoint_path ", ckpt.model_checkpoint_path) ---> 88 saver.restore(sess, ckpt.model_checkpoint_path) 89 # Assuming model_checkpoint_path looks something like: 90 # /my-favorite-path/cifar10_train/model.ckpt-0,

/home/huray/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/training/saver.pyc in restore(self, sess, save_path) 1127 raise ValueError("Restore called with invalid save path %s" % save_path)
1128 sess.run(self.saver_def.restore_op_name, -> 1129 {self.saver_def.filename_tensor_name: save_path}) 1130 1131 @staticmethod

/home/huray/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata) 380 try: 381 result = self._run(None, fetches, feed_dict, options_ptr, --> 382 run_metadata_ptr) 383 if run_metadata: 384 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/home/huray/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata) 653 movers = self._update_with_movers(feed_dict_string, feed_map) 654 results = self._do_run(handle, target_list, unique_fetches, --> 655 feed_dict_string, options, run_metadata) 656 657 # User may have fetched the same tensor multiple times, but we

/home/huray/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata) 721 if handle is None: 722 return self._do_call(_run_fn, self._session, feed_dict, fetch_list, --> 723 target_list, options, run_metadata) 724 else: 725 return self._do_call(_prun_fn, self._session, handle, feed_dict,

/home/huray/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args) 741 except KeyError: 742 pass --> 743 raise type(e)(node_def, op, message) 744 745 def _extend_graph(self):

InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [18,384] rhs shape= [2304,384] [[Node: save/Assign_5 = Assign[T=DT_FLOAT, _class=["loc:@local3/weights"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/cpu:0"](local3/weights, save/restore_slice_5)]]

任何有关 Cifar10 的帮助将不胜感激。

这是迄今为止已实现的代码,但存在编译问题:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from datetime import datetime
import math
import time

import numpy as np
import tensorflow as tf
import cifar10

FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string('eval_dir', '/tmp/cifar10_eval',
                           """Directory where to write event logs.""")
tf.app.flags.DEFINE_string('eval_data', 'test',
                           """Either 'test' or 'train_eval'.""")
tf.app.flags.DEFINE_string('checkpoint_dir', '/tmp/cifar10_train',
                           """Directory where to read model checkpoints.""")
tf.app.flags.DEFINE_integer('eval_interval_secs', 5,
                            """How often to run the eval.""")
tf.app.flags.DEFINE_integer('num_examples', 1,
                            """Number of examples to run.""")
tf.app.flags.DEFINE_boolean('run_once', False,
                         """Whether to run eval only once.""")

def eval_once(saver, summary_writer, top_k_op, summary_op):
  """Run Eval once.

  Args:
    saver: Saver.
    summary_writer: Summary writer.
    top_k_op: Top K op.
    summary_op: Summary op.
  """
  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
    print("Check point : %s" % ckpt.model_checkpoint_path)

    # 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.batch_size))
      true_count = 0  # Counts the number of correct predictions.
      total_sample_count = num_iter * FLAGS.batch_size
      step = 0
      while step < num_iter and not coord.should_stop():
        predictions = sess.run([top_k_op])
        true_count += np.sum(predictions)
        step += 1

      # Compute precision @ 1.
      precision = true_count / total_sample_count
      print('%s: precision @ 1 = %.3f' % (datetime.now(), precision))

      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)


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 = cifar10.inputs(eval_data=eval_data)

    # TEST CODE
    img_path = "/TEST_IMAGEPATH/image.png"
    input_img = tf.image.decode_png(tf.read_file(img_path), channels=3)
    casted_image = tf.cast(input_img, tf.float32)

    reshaped_image = tf.image.resize_image_with_crop_or_pad(casted_image, 24, 24)
    float_image = tf.image.per_image_withening(reshaped_image)
    images = tf.expand_dims(reshaped_image, 0) 

    logits = cifar10.inference(images)
    _, top_k_pred = tf.nn.top_k(logits, k=1)


    with tf.Session() as sess:
        saver = tf.train.Saver()
        ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
        if ckpt and ckpt.model_checkpoint_path:
          print("ckpt.model_checkpoint_path ", ckpt.model_checkpoint_path)
          saver.restore(sess, ckpt.model_checkpoint_path)
          global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
        else:
          print('No checkpoint file found')
          return

        print("Check point : %s" % ckpt.model_checkpoint_path)
        top_indices = sess.run([top_k_pred])
        print ("Predicted ", top_indices[0], " for your input image.")

evaluate()

最佳答案

视频https://youtu.be/d9mSWqfo0Xw显示了对单个图像进行分类的示例。

网络经过 python cifar10_train.py 训练后,我们评估 CIFAR-10 数据库的单个图像 deer6.png 和火柴盒自己的照片。 TF教程原始源码最重要的修改如下:

首先需要将这些图像转换为 cifar10_input.py 可以读取的二进制形式。使用 How to create dataset similar to cifar-10 中的代码片段可以轻松完成此操作。

然后为了读取转换后的图像(称为input.bin),我们需要修改cifar10_input.py中的函数input():

  else:
    #filenames = [os.path.join(data_dir, 'test_batch.bin')]
    filenames = [os.path.join(data_dir, 'input.bin')]

(data_dir 等于“./”)

最后为了获得标签,我们修改了源 cifar10_eval.py 中的函数 eval_once():

      #while step < num_iter and not coord.should_stop():
      #  predictions = sess.run([top_k_op])
      print(sess.run(logits[0]))
      classification = sess.run(tf.argmax(logits[0], 0))
      cifar10classes = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]
      print(cifar10classes[classification])

      #true_count += np.sum(predictions)
      step += 1

      # Compute precision @ 1.
      precision = true_count / total_sample_count
      # print('%s: precision @ 1 = %.3f' % (datetime.now(), precision))

当然,您需要进行一些小的修改。

关于python - 如何在 Tensorflow 上测试自己的图像到 Cifar-10 教程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39848610/

相关文章:

python - Tensorflow:修改隐藏状态的计算

python - 海湾合作委员会错误 : command 'gcc-4.0' failed with exit status 1

python - Celery 连接错误

python - tensorflow 层中StringLookup层和embedding层的区别

python stackdriver google 函数 webhook 监听器

python - 动态拆分Python中的复杂字符串

tensorflow 在稀疏变量上做梯度

python - 为什么 `tf.matmul` 不能与转置张量一起使用?

python-3.x - 当我在 Windows 10 上构建 tensorflow 时出现错误 "Can not get Python include directory. Is distutils installed?"

python - Keras:TensorFlow 1.3 模型在 TensorFlow 1.4 或更高版本下失败(错误预测)