如何使用tensorflow cifar10模型对图像进行分类

时间:2016-03-09 15:53:50

标签: python classification tensorflow

我正在尝试使用cifar10训练模型对图像进行分类(这是整个事物的目的,对吗?!)但直到现在我还没有成功。

在cifar10_eval.py中,我进行了以下调整:

1)在evaluate()中,我调用自定义函数“test_image(saver,summary_writer,logits,summary_op)”:

def test_image(saver, summary_writer, logits, summary_op):
  """Run Eval once.

  Args:
    saver: Saver.
    summary_writer: Summary writer.
    top_k_op: Top K op.
    summary_op: Summary op.
  """
  x = tf.placeholder("float", shape=[1, 3072])

  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

    width = 32
    height = 32
    images = np.zeros((1, 3072))
    img = cv2.imread("/home/prtricardo/tese_ws/open_cv/acacia_model/acacia_.jpg")
    resized_image = cv2.resize(img, (width, height))
    arr = np.uint8([0 for x in range(width*height*3)])


    arr_cnt = 0
    for y in range(0, width):
      for x in range(0, height):
          arr[arr_cnt] = np.uint8(resized_image[x, y, 2])  # R
          arr[arr_cnt + 1024] = np.uint8(resized_image[x, y, 1])  # G
          arr[arr_cnt + 2048] = np.uint8(resized_image[x, y, 0])  # B

          arr_cnt += 1

    images[0] = arr

    classification = sess.run(tf.argmax(logits, 1), feed_dict={x: [images[0]]})
    print 'Neural Network predicted', classification[0]

对于mnist模型教程的相同问题,这是一种类似的方法。在mnist教程中,我可以成功对数字进行分类,如我的other question:中所述。

有没有人尝试过类似的东西?谢谢你的帮助!

0 个答案:

没有答案