TensorFlow:用我自己的图像训练 [英] TensorFlow: training on my own image
问题描述
我是TensorFlow的新手。我正在寻找有关图像识别的帮助,可以在其中训练自己的图像数据集。
I am new to TensorFlow. I am looking for the help on the image recognition where I can train my own image dataset.
是否有用于训练新数据集的示例? ?
Is there any example for training the new dataset?
推荐答案
如果您对如何在TensorFlow中输入自己的数据感兴趣,可以查看本教程。
我还为斯坦福大学CS230编写了最佳实践指南< a href = https://cs230-stanford.github.io/tensorflow-input-data.html#building-an-image-data-pipeline rel = noreferrer>此处。
If you are interested in how to input your own data in TensorFlow, you can look at this tutorial.
I've also written a guide with best practices for CS230 at Stanford here.
在 r1.4
中引入了 tf.data
之后,我们可以创建一批没有占位符且没有队列的图像。步骤如下:
With the introduction of tf.data
in r1.4
, we can create a batch of images without placeholders and without queues. The steps are the following:
- 创建一个包含图像文件名和标签列表的列表
- 创建一个
tf.data.Dataset
读取这些文件名和标签 - 预处理数据
- 从
tf.data.Dataset
创建一个迭代器,该迭代器将产生下一批
- Create a list containing the filenames of the images and a corresponding list of labels
- Create a
tf.data.Dataset
reading these filenames and labels - Preprocess the data
- Create an iterator from the
tf.data.Dataset
which will yield the next batch
代码为:
# step 1
filenames = tf.constant(['im_01.jpg', 'im_02.jpg', 'im_03.jpg', 'im_04.jpg'])
labels = tf.constant([0, 1, 0, 1])
# step 2: create a dataset returning slices of `filenames`
dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
# step 3: parse every image in the dataset using `map`
def _parse_function(filename, label):
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_string, channels=3)
image = tf.cast(image_decoded, tf.float32)
return image, label
dataset = dataset.map(_parse_function)
dataset = dataset.batch(2)
# step 4: create iterator and final input tensor
iterator = dataset.make_one_shot_iterator()
images, labels = iterator.get_next()
现在我们可以直接运行 sess.run([图像,标签])
,而无需通过占位符提供任何数据。
Now we can run directly sess.run([images, labels])
without feeding any data through placeholders.
总而言之,您可以执行多个步骤:
To sum it up you have multiple steps:
- 创建文件名列表(例如:图像的路径)
- 创建TensorFlow 文件名队列
- 读取并解码每个图像,将它们调整为固定大小(对于批处理而言是必需的)
- 输出一批这些图像
- Create a list of filenames (ex: the paths to your images)
- Create a TensorFlow filename queue
- Read and decode each image, resize them to a fixed size (necessary for batching)
- Output a batch of these images
最简单的代码是:
The simplest code would be:
# step 1
filenames = ['im_01.jpg', 'im_02.jpg', 'im_03.jpg', 'im_04.jpg']
# step 2
filename_queue = tf.train.string_input_producer(filenames)
# step 3: read, decode and resize images
reader = tf.WholeFileReader()
filename, content = reader.read(filename_queue)
image = tf.image.decode_jpeg(content, channels=3)
image = tf.cast(image, tf.float32)
resized_image = tf.image.resize_images(image, [224, 224])
# step 4: Batching
image_batch = tf.train.batch([resized_image], batch_size=8)
这篇关于TensorFlow:用我自己的图像训练的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!