将Caffe的train.txt转换为Tensorflow格式

4
我是一个有用的助手,可以为您翻译文本。

我正在尝试将我的Caffe代码适应于TensorFlow。我想知道将我的train.txt和test.txt转换为TensorFlow的最佳方法。

在我的train.txt中,文件看起来像:

/folder/filename1.jpg 1

/folder/filename2.jpg 2
...

第一列是图像名称,第二列是类别标签。谢谢!
1个回答

4
我假设您想获得一批具有数值标签的相同大小的图像。我们将使用 tf.decode_csv() 解析文本,tf.read_file() 加载JPEG数据作为字符串,tf.image.decode_jpeg() 将其解析为密集张量,最后使用tf.train.batch() 构建解析数据成图像批处理。这些函数中有许多选项可供配置,请参阅文档以了解进一步的定制细节。请注意:保留HTML标记。
# Set options here for whether to repeat, etc.
filename_producer = tf.string_input_producer(["train.txt"], ...)

# Read lines from the file, one at a time.
line_reader = tf.TextLineReader()
next_line = line_reader.read(filename_producer)

# Parse line into a filename and an integer label.
image_filename, label = tf.decode_csv(
    next_line, [tf.constant([], dtype=tf.string), tf.constant([], dtype=tf.int32)],
    field_delim=" ")

# Read the image as a string.
image_bytes = tf.read_file(image_filename)

# Convert the image into a 3-D tensor (height x width x channels).
image_tensor = tf.image.decode_jpeg(image_bytes, ...)

# OPTIONAL: Resize your image to a standard size if they are not already.
HEIGHT = ...
WIDTH = ...
image_tensor = tf.image.resize_image_with_crop_or_pad(image_tensor, HEIGHT, WIDTH)

# Create a batch of images.
BATCH_SIZE = 32
images, labels = tf.train.batch([image_tensor, label], BATCH_SIZE, ...)

# [...build the rest of your model...]

这个例子广泛使用了TensorFlow预取来加载样例。TensorFlow文档中有一个如何使用预取功能的教程,但最重要的是你必须在会话开始时调用tf.train.start_queue_runners()来开始预取:
sess = tf.Session()

# You must execute this statement to begin prefetching data.
tf.train.start_queue_runners(sess)

非常感谢你详细的回答,对我非常有帮助。在你的代码中,你使用了 images, labels = tf.train.batch([image_tensor, label], BATCH_SIZE, ...)。我想知道如果我需要获取下一个批次的数据和标签,是不是只需要再次调用 tf.train.batch,并使用相同的参数,还是需要自己实现一个 get_next_batch 函数。谢谢! - Geoffrey Wu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接