如何将jpeg数据加载,标记和提供给Tensorflow? [英] How do you load, label, and feed jpeg data into Tensorflow?
问题描述
我一直在尝试将1750 * 1750图像输入Tensorflow,但在使用tf.image.decode_jpeg()函数将图像转换为Tensor后,我不知道如何标记和提供数据。
I have been trying to feed 1750 * 1750 images into Tensorflow, but I do not know how to label and feed the data after I convert the images into a Tensor using the tf.image.decode_jpeg() function.
目前,我的代码是:
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教程文件,但是他们将标签存储在一个文件中,我打算不这样做。
Now, as I said earlier, I need to feed and label the data. I have seen the CIFAR-10 tutorial files, but they stored the labels in a file and I plan on not doing that way.
我对Tensorflow很新,所以请保持尽可能详细的回复。
I am quite new to Tensorflow so please keep the response as detailed as possible.
谢谢!
推荐答案
根据您的目的,有几个方向需要考虑。
Depending on what you are trying to do, there are several directions to consider.
-
如果您只是希望对任意JPEG文件(即不需要标签),然后你可以按照classify_image.py的例子,它将JPEG图像输入预先训练好的Inception网络:
If you just wish to run inference on an arbitrary JPEG file (i.e. labels are not required), then you can follow the example of classify_image.py which feeds in a JPEG image into a pre-trained Inception network:
github.com/tensorflow/models/blob/master/ tutorials / image / imagenet / classify_image.py
如果您希望在上训练(或微调)模型小的自定义JPEG图像数据集,然后看看这个关于如何从一小组JPEG图像中训练模型的示例。
If you do wish to train (or fine-tune) a model on a small custom data set of JPEG images, then take a look at this example for how to train a model off a small set of JPEG images.
github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/image_retraining/retrain.py
如果您希望在大型自定义JPEG图像数据集上训练(或微调)模型,那么读取许多单独的JPEG文件将效率低下,培训速度极慢。
If you do wish to train (or fine-tune) a model on a large custom data set of JPEG images, then reading many individual JPEG files will be inefficient and slow down training tremendously.
我建议按照初始/模型库中描述的程序进行操作将JPEG图像目录转换为包含序列化JPEG图像的分片RecordIO。
I would suggest following the procedure of described in the inception/ model library that converts a directory of JPEG images into sharded RecordIO containing serialized JPEG images.
github.com/tensorflow/models/blob/master/research/inception/inception/data/build_im age_data.py
此处提供了运行转换脚本的说明:
Instructions for running the conversion script are available here:
运行转换后,您可以使用/复制初始/模型使用的图像预处理管道。
After running the conversion, you may then employ/copy the image preprocessing pipeline used by the inception/ model.
github.com/tensorflow/models/blob/master/research/inception/inception/image_processing.py
这篇关于如何将jpeg数据加载,标记和提供给Tensorflow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!