tensorflow数据集tfds数据加载中的数据扩充,导致TypError或AttributeError

Data augmentation in the data loading of tensorflow dataset, tfds, resulting in TypError or AttributeError

我正在尝试做一些数据扩充,但我对张量不太熟悉。 这是我开始的代码:

def _random_apply(func, x, p):
  return tf.cond(tf.less(tf.random.uniform([], minval=0, maxval=1, dtype=tf.float32),
                  tf.cast(p, tf.float32)),
        lambda: func(x),
        lambda: x)
def _resize_with_pad(image):
  image = tf.image.resize_with_pad(image, target_height=IMG_S, target_width=IMG_S)   
  return image

def augment(image, label):
  img = _random_apply(tf.image.flip_left_right(image), image, p=0.2)
  img = _random_apply(_resize_with_pad(img), img, p=1)
  return img, label
train_dataset = (
    train_ds
    .shuffle(1000)
    .map(augment, num_parallel_calls=tf.data.AUTOTUNE)
    .prefetch(tf.data.AUTOTUNE)
)

导致以下错误。

----> 4     .map(augment, num_parallel_calls=tf.data.AUTOTUNE)

TypeError: 'Tensor' object is not callable

然后我想如果我把它转换成 numpy 可能会起作用。

def _random_apply(func, x, p):
  return tf.cond(tf.less(tf.random.uniform([], minval=0, maxval=1, dtype=tf.float32),
                  tf.cast(p, tf.float32)),
        lambda: func(x),
        lambda: x)
def _resize_with_pad(image):
  image = image.numpy()
  image = tf.image.resize_with_pad(image, target_height=IMG_S, target_width=IMG_S).numpy()  
  return image

def augment(image, label):
  image = image.numpy()
  img = _random_apply(tf.image.flip_left_right(image).numpy(), image, p=0.2)
  img = _random_apply(_resize_with_pad(img), img, p=1)
  return img, label
train_dataset = (
    train_ds
    .shuffle(1000)
    .map(augment, num_parallel_calls=tf.data.AUTOTUNE)
    .prefetch(tf.data.AUTOTUNE)
)

但现在我得到了这个错误。

----> 4     .map(augment, num_parallel_calls=tf.data.AUTOTUNE)

 AttributeError: 'Tensor' object has no attribute 'numpy'

我试图在这个 answer 中做类似的事情,现在我没有直接得到错误,而是在下一个代码块中:

for image, _ in train_dataset.take(9):
etc
InvalidArgumentError 
----> 1 for image, _ in train_dataset.take(9):

InvalidArgumentError: TypeError: 'tensorflow.python.framework.ops.EagerTensor' object is not callable

有人知道我做错了什么吗?

augment 中,您将张量传递给 _random_applytf.image.flip_left_right(image) returns 张量。然后,在 _random_apply 中,您像使用函数一样使用该张量。您需要将 tf.flip_left_right 作为可调用对象传递:

def augment(image):
    img = _random_apply(tf.image.flip_left_right, image, p=0.2)
    img = _random_apply(_resize_with_pad, img, p=1)
    return img

完整的工作示例:

import tensorflow as tf

train_ds = tf.data.Dataset.from_tensor_slices(tf.random.uniform((100, 224, 224, 3)))


def _random_apply(func, x, p):
    return tf.cond(tf.less(tf.random.uniform([], minval=0, maxval=1, dtype=tf.float32),
                           tf.cast(p, tf.float32)),
                   lambda: func(x),
                   lambda: x)


def _resize_with_pad(image):
    image = tf.image.resize_with_pad(image, target_height=200, target_width=200)
    return image


def augment(image):
    img = _random_apply(tf.image.flip_left_right, image, p=0.2)
    img = _random_apply(_resize_with_pad, img, p=1)
    return img


train_dataset = train_ds.map(augment)

batch = next(iter(train_dataset))