TensorFlow:在我自己的图像上训练 [英] TensorFlow: training on my own image

查看:44
本文介绍了TensorFlow:在我自己的图像上训练的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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 最佳实践的指南 此处.

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.4tf.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:

  1. 创建一个包含图像文件名和相应标签列表的列表
  2. 创建一个 tf.data.Dataset 读取这些文件名和标签
  3. 预处理数据
  4. tf.data.Dataset 创建一个迭代器,它将产生下一批
  1. Create a list containing the filenames of the images and a corresponding list of labels
  2. Create a tf.data.Dataset reading these filenames and labels
  3. Preprocess the data
  4. 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([images, labels]) 而无需通过占位符提供任何数据.

Now we can run directly sess.run([images, labels]) without feeding any data through placeholders.

总而言之,您有多个步骤:

To sum it up you have multiple steps:

  1. 创建文件名列表(例如:图像的路径)
  2. 创建一个 TensorFlow 文件名队列
  3. 读取和解码每个图像,将它们调整为固定大小(批处理所需)
  4. 输出一批这些图像

<小时>

最简单的代码是:


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆