【发布时间】:2021-12-14 14:30:54
【问题描述】:
我正在尝试做一些数据增强,但我对张量不太熟悉。 这是我开始的代码:
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
有人知道我做错了什么吗?
【问题讨论】:
标签: python numpy tensorflow deep-learning tensorflow-datasets