【问题标题】:Classifying images using pre-trained (Tensorflow) CNN使用预训练 (Tensorflow) CNN 对图像进行分类
【发布时间】:2017-07-18 08:02:13
【问题描述】:

我已经在自己的数据集上训练了 alexnet_v2,现在想在另一个应用程序中使用它。这应该很简单,我尝试以多种方式实现它,但要么我遇到无法解决的错误,要么(在下面的代码的情况下)它无限期挂起。

理想情况下,我希望在 C++ 中使用它(但 C++ API 似乎不可靠,或者至少在许多地方有过时的文档,所以 python 是可以接受的),我想对大量图像进行分类(对于示例:为程序提供 80 张动物图像,并返回其中是否有猫)。

我是否使用下面的代码以正确的方式解决这个问题?如果是这样,我该如何解决它。

如果没有,有没有更好方法的工作示例?

非常感谢。

import tensorflow as tf

#Using preprocessing and alexnet_v2 net from the slim examples

from nets import nets_factory
from preprocessing import preprocessing_factory

#Checkpoint file from training on binary dataset

checkpoint_path = '/home/ubuntu/tensorflow/models/slim/data/checkpoint.ckpt'

slim = tf.contrib.slim

number_of_classes = 2


image_filename = '/home/ubuntu/tensorflow/models/slim/data/images/neg_sample_123459.jpg'

image_filename_placeholder = tf.placeholder(tf.string)

image_tensor = tf.read_file(image_filename_placeholder)

image_tensor = tf.image.decode_jpeg(image_tensor, channels=3)

image_batch_tensor = tf.expand_dims(image_tensor, axis=0)

#Use slim's alexnet_v2 implementation

network_fn = nets_factory.get_network_fn('alexnet_v2',num_classes=2,is_training=False)

#Use inception preprocessing

preprocessing_name = 'inception'
image_preprocessing_fn= preprocessing_factory.get_preprocessing(preprocessing_name,is_training=False)

image_tensor=image_preprocessing_fn(image_tensor,network_fn.default_image_size,network_fn.default_image_size)

label=3
images,labels=tf.train.batch(
    [image_tensor,label],
    batch_size=2,
    num_threads=1,
    capacity=10)

pred,_=network_fn(images)

initializer = tf.local_variables_initializer()

init_fn=slim.assign_from_checkpoint_fn(
    checkpoint_path,
    slim.get_model_variables('alexnet_v2'))

with tf.Session() as sess:

    sess.run(initializer)
    init_fn(sess)
    tf.train.start_queue_runners(sess)
    image_np, pred_np = sess.run([image_tensor, pred], feed_dict={image_filename_placeholder: image_filename})

编辑:添加粗体行后,程序不再挂起。但是我收到一个占位符错误:

InvalidArgumentError:您必须为占位符张量提供一个值 'Placeholder' 与 dtype 字符串 [[Node: Placeholder = 占位符类型=DT_STRING,形状=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]

我已经仔细检查了拼写,据我所知,我输入正确。怎么了?

【问题讨论】:

    标签: tensorflow neural-network classification conv-neural-network tf-slim


    【解决方案1】:

    tf.train.batch() 函数使用后台线程来预取示例,但您需要添加显式命令 (tf.train.start_queue_runners(sess)) 来启动这些线程。如下重写代码的最后一部分应该停止它挂起:

    with tf.Session() as sess:
      sess.run(initializer)
      init_fn(sess)
    
      # Starts background threads for input preprocessing.
      tf.train.start_queue_runners(sess)
    
      image_np, pred_np = sess.run(
          [image_tensor, pred],
          feed_dict={image_filename_placeholder: image_filename})
    

    【讨论】:

    • 我认为问题在于队列运行器使用了image_filename_placeholder(通过在内部调用sess.run() 填充用于批处理图像的队列),但队列运行器不知道什么文件名喂。更仔细地查看您的程序,如果您一次对一张图像进行分类,则根本不需要tf.train.batch():您可以简单地将tf.decode_jpeg() 的结果传递给network_fn()(也许在对其进行整形之后) .
    • 在最终程序中,我打算每次调用网络对大约 80 张图像进行分类,这就是为什么我需要传递一批(我只是简化了一个原始问题很少)。
    猜你喜欢
    • 1970-01-01
    • 2016-04-19
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 2018-09-25
    • 2020-10-05
    • 2018-12-15
    相关资源
    最近更新 更多