【问题标题】:How do you load, label, and feed jpeg data into Tensorflow?您如何将 jpeg 数据加载、标记和输入到 Tensorflow?
【发布时间】:2016-07-04 16:08:55
【问题描述】:

我一直在尝试将 1750 * 1750 图像输入到 Tensorflow,但在使用 tf.image.decode_jpeg() 函数将图像转换为张量后,我不知道如何标记和输入数据。

目前,我的代码是:

import tensorflow as tf
import numpy as np

import imageflow
import os, glob

sess = tf.InteractiveSession()

def read_jpeg(filename_queue):
 reader = tf.WholeFileReader()
 key, value = reader.read(filename_queue)

 my_img = tf.image.decode_jpeg(value)
 my_img.set_shape([1750, 1750, 1])
 print(value)
 return my_img

#####################################################
def read_image_data():
 jpeg_files = []
 images_tensor = []

 i = 1
 WORKING_PATH = "/Users/Zanhuang/Desktop/NNP/DATA"
 jpeg_files_path = glob.glob(os.path.join(WORKING_PATH, '*.jpeg'))

 for filename in jpeg_files_path:
    print(i)
    i += 1
    jpeg_files.append(filename)


 filename_queue = tf.train.string_input_producer(jpeg_files)

 mlist = [read_jpeg(filename_queue) for _ in range(len(jpeg_files))]

 init = tf.initialize_all_variables()

 sess = tf.Session()
 sess.run(init)

 images_tensor = tf.convert_to_tensor(images_tensor)


 sess.close()

现在,正如我之前所说,我需要输入和标记数据。我看过 CIFAR-10 教程文件,但他们将标签存储在一个文件中,我打算不这样做。

我是 Tensorflow 的新手,所以请尽可能详细地回复。

谢谢!

【问题讨论】:

  • 用同样格式的MINST_data,用input_data.py读取怎么样?

标签: python image-processing machine-learning jpeg tensorflow


【解决方案1】:

根据您想要做的事情,有几个方向需要考虑。

  1. 如果您只想对任意 JPEG 文件进行推理(即不需要标签),那么您可以按照分类图像.py 的示例将 JPEG 图像馈送到预训练的 Inception 网络:

    github.com/tensorflow/models/blob/master/tutorials/image/imagenet/classify_image.py

  2. 如果您确实希望在 small JPEG 图像的自定义数据集上训练(或微调)模型,那么请查看此示例,了解如何在小范围内训练模型一组 JPEG 图像。

    github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/image_retraining/retrain.py

  3. 如果您确实希望在 大型 JPEG 图像的自定义数据集上训练(或微调)模型,那么读取许多单独的 JPEG 文件将效率低下并减慢训练速度非常好。

我建议遵循 inception/ 模型库中描述的过程,将 JPEG 图像目录转换为包含序列化 JPEG 图像的分片 RecordIO。

github.com/tensorflow/models/blob/master/research/inception/inception/data/build_image_data.py

此处提供了运行转换脚本的说明:

github.com/tensorflow/models/blob/master/research/inception/README.md#how-to-construct-a-new-dataset-for-retraining

运行转换后,您可以使用/复制 inception/ 模型使用的图像预处理管道。

github.com/tensorflow/models/blob/master/research/inception/inception/image_processing.py

【讨论】:

  • 这里介绍的方法是可以用于任何其他 tensorflow 模型而不仅仅是初始网络的通用方法吗?谢谢。
  • 是的,这些都是通用方法,尽管上面的脚本更适合基于图像识别的问题。
  • 您的初始链接是否转换为 RecordIO 或 TFRecords?似乎是后者。
猜你喜欢
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多