将 Caffe train.txt 转换为 Tensorflow

convert Caffe train.txt to Tensorflow

我正在尝试使我的 Caffe 代码适应 tensorflow。我想知道转换我的 train.txt 和 test.txt 以便为 tensorflow 工作的最佳方法是什么。

在我的 train.txt 中,文件如下所示:

/folder/filename1.jpg 1

/folder/filename2.jpg 2
...

第一列是图片名称,第二列是class标签

谢谢!!

我假设您想要获得一批大小相同且带有数字标签的图像。我们将使用 tf.decode_csv() to parse the text, tf.read_file() to load the JPEG data as a string, tf.image.decode_jpeg() to parse it into a dense tensor, and finally tf.train.batch() 将解析后的数据构建成一批图像。其中许多功能都有很多选项可供配置,因此请参阅文档以了解更多自定义详细信息。

# 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 文档有一个 how-to that explains how to use the prefetching feature,但最重要的是要注意,您必须在会话开始时调用 tf.train.start_queue_runners() 才能开始预取:

sess = tf.Session()

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