TensorFlow:培训我自己的形象

VIC*_*TOR 38 python conv-neural-network tensorflow tensorflow-datasets

我是TensorFlow的新手.我正在寻找有关图像识别的帮助,我可以在那里训练自己的图像数据集.

有没有训练新数据集的例子?

Oli*_*rot 71

如果您对如何在TensorFlow中输入自己的数据感兴趣,可以查看本教程.
我也写与CS230的最佳做法指南在斯坦福这里.


新答案(带tf.data)和标签

随着引进tf.datar1.4,我们可以创建一个批处理图像没有占位符的,没有队列.步骤如下:

  1. 创建一个列表,其中包含图像的文件名和相应的标签列表
  2. 创建tf.data.Dataset读取这些文件名和标签
  3. 预处理数据
  4. 从中创建一个迭代器tf.data.Dataset将产生下一批

代码是:

# 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()
Run Code Online (Sandbox Code Playgroud)

现在我们可以直接运行sess.run([images, labels])而无需通过占位符提供任何数据.


旧答案(使用TensorFlow队列)

总结一下,你有多个步骤:

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

最简单的代码是:

# 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)
Run Code Online (Sandbox Code Playgroud)

  • 如果你有训练图像的标签,你也应该把它们作为输入并用图像批处理:`image_batch,label_batch = tf.train.batch([resized_image,label],batch_size = 8)`.然后你必须构建一个模型,其中图像作为输入,标签作为输出,参考[本教程](https://www.tensorflow.org/versions/r0.8/tutorials/mnist/tf/index.html)更多信息. (6认同)
  • 你是对的,`tf.train.batch`或`tf.train.shuffle_batch`的第一个参数应该是一个列表`[image]`而不仅仅是`image`.我在代码中修复了它. (2认同)

Mad*_*yar 7

基于@olivier-moindrot 的回答,但对于 Tensorflow 2.0+:

# 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))

def im_file_to_tensor(file, label):
    def _im_file_to_tensor(file, label):
        path = f"../foo/bar/{file.numpy().decode()}"
        im = tf.image.decode_jpeg(tf.io.read_file(path), channels=3)
        im = tf.cast(image_decoded, tf.float32) / 255.0
        return im, label
    return tf.py_function(_im_file_to_tensor, 
                          inp=(file, label), 
                          Tout=(tf.float32, tf.uint8))

dataset = dataset.map(im_file_to_tensor)
Run Code Online (Sandbox Code Playgroud)

如果您遇到类似以下问题:

ValueError:无法获取未知等级的形状的长度

将 tf.data.Dataset 张量传递给 model.fit 时,请查看https://github.com/tensorflow/tensorflow/issues/24520。上面代码片段的修复方法是:

def im_file_to_tensor(file, label):
    def _im_file_to_tensor(file, label):
        path = f"../foo/bar/{file.numpy().decode()}"
        im = tf.image.decode_jpeg(tf.io.read_file(path), channels=3)
        im = tf.cast(image_decoded, tf.float32) / 255.0
        return im, label

    file, label = tf.py_function(_im_file_to_tensor, 
                                 inp=(file, label), 
                                 Tout=(tf.float32, tf.uint8))
    file.set_shape([192, 192, 3])
    label.set_shape([])
    return (file, label)
Run Code Online (Sandbox Code Playgroud)