【问题标题】:Tensorflow: How to set tensor shape after reading image from tfrecord file for data augmentation?Tensorflow:如何在从 tfrecord 文件读取图像以进行数据增强后设置张量形状?
【发布时间】:2021-01-29 16:02:17
【问题描述】:

我有一个tf.data.Dataset,我从tfrecords 文件中读取,如下所示:

import tensorflow as tf

# given an existing record_file

raw_dataset = tf.data.TFRecordDataset(record_file)
example_description = {
        "height": tf.io.FixedLenFeature([], tf.int64),
        "width": tf.io.FixedLenFeature([], tf.int64),
        "channels": tf.io.FixedLenFeature([], tf.int64),
        "image": tf.io.FixedLenFeature([], tf.string),
    }
dataset = raw_dataset.map(
    lambda example: tf.io.parse_single_example(example, example_description)
)

接下来,我将这些特征组合成一个图像,如下所示:

dataset = dataset.map(_extract_image_from_sample)

# and

def _extract_image_from_sample(sample):
    height = tf.cast(sample["height"], tf.int32) # always 1038
    width = tf.cast(sample["width"], tf.int32) # always 1366
    depth = tf.cast(sample["channels"], tf.int32) # always 3
    shape = [height, width, depth]

    image = sample["image"]
    image = decode_tf_image(image)
    image = tf.reshape(image, shape)

    return image

此时,数据集中的任何图像都具有(None, None, None) 的形状(这让我感到惊讶,因为我重塑了它们)。 当我尝试使用 tf.keras.preprocessing.image.ImageDataGenerator 扩充数据集时,我认为这是导致错误的原因:

augmented_dataset = dataset.map(random_image_augmentation)

# and

image_data_generator = tf.keras.preprocessing.image.ImageDataGenerator(
    rotation_range=45,
    width_shift_range=0.1,
    height_shift_range=0.1,
    shear_range=5.0,
    zoom_range=[0.9, 1.2],
    fill_mode="reflect",
    horizontal_flip=True,
    vertical_flip=True,
)

def random_image_augmentation(image: tf.Tensor) -> tf.Tensor:
    transform = image_data_generator.get_random_transform(img_shape=image.shape)
    image = image_data_generator.apply_transform(image, transform)
    return image

这会导致错误消息:

TypeError: in user code:
    # ...
    C:\Users\[PATH_TO_ENVIRONMENT]\lib\site-packages\keras_preprocessing\image\image_data_generator.py:778 get_random_transform  *
        tx *= img_shape[img_row_axis]

    TypeError: unsupported operand type(s) for *=: 'float' and 'NoneType'

但是,如果我不使用图形模式,而是使用渴望模式,这就像一个魅力:

it = iter(dataset)
for i in range(3):
    image = it.next()
    image = random_image_augmentation(image.numpy())

这使我得出结论,主要错误是在读取数据集后缺少形状信息。但我不知道如何比我已经做的更明确地定义它。有什么想法吗?

【问题讨论】:

  • 您希望进行什么样的增强?
  • 看看第三个代码块:随机旋转、移位、剪切、缩放和翻转(暂时)。
  • 你应该看看 Chris Deotte 的一些工作,他提供了一些很棒的代码来在获取 tfrecord 图像数据时进行转换 --> kaggle.com/cdeotte/rotation-augmentation-gpu-tpu-0-96
  • 感谢您的提示。肯定有一些有趣的方法。但是,它们与我的问题没有直接关系,并且比上面的方法做的更多。我们的想法是真正尽可能多地利用 tensorflow 的预定义方法。

标签: tensorflow keras tfrecord image-preprocessing tf.data.dataset


【解决方案1】:

使用tf.py_function 包装要求张量具有如下形状的预处理函数:

augmented_dataset = dataset.map(
    lambda x: tf.py_function(random_image_augmentation, inp=[x], Tout=tf.float32),
    num_parallel_calls=tf.data.experimental.AUTOTUNE
)

# and

def random_image_augmentation(image: tf.Tensor) -> tf.Tensor:
    image = image.numpy()  # now we can do this, because tensors have this function in eager mode
    transform = image_data_generator.get_random_transform(img_shape=image.shape)
    image = image_data_generator.apply_transform(image, transform)
    return image

这对我有用,但我不确定它是否是唯一甚至最好的解决方案。

【讨论】:

    猜你喜欢
    • 2016-04-20
    • 1970-01-01
    • 2019-02-02
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 2018-11-06
    相关资源
    最近更新 更多