无法将类型<class'function'=“”>的对象转换为Tensor

时间:2019-04-29 16:44:05

标签: tensorflow deep-learning data-augmentation

我正在尝试使用张量流的left_right和up_down增强函数将翻转增强随机化。我通过tf.cond()

根据布尔条件映射函数时出错
random_number=tf.random_uniform([],seed=seed)
print_random_number=tf.print(random_number)
flip_strategy=tf.less(random_number,0.5)

版本0.1

image=tf.cond
        (
            flip_strategy,
            tf.image.flip_left_right(image),
            tf.image.flip_up_down(image),
        )

0.2版

image=tf.cond
            (
                flip_strategy,
                lambda: tf.image.flip_left_right(image),
                lambda: tf.image.flip_up_down(image),
            )

错误

TypeError:无法将类型的对象转换为Tensor。内容:。考虑将元素强制转换为受支持的类型。ROR:

让我知道我在想什么,或者是否需要更多信息。

1 个答案:

答案 0 :(得分:0)

来自documentation

  

tf.math.less(       X,       y,       名称=无   )

     

Args:

x: A Tensor. Must be one of the following types: float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64.

y: A Tensor. Must have the same type as x.

name: A name for the operation (optional).

因此tf.less期望两个张量,但是您传递的参数之一是一个numpy数组。您可以像这样在张量中转换numpy数组


random_number=tf.random_uniform([],seed=seed)
print_random_number=tf.print(random_number)
random_numer=tf.convert_to_tensor(random_number,dtype=tf.float32)
flip_strategy=tf.less(random_number,0.5)

image=tf.cond`
  (
  flip_strategy,
  tf.image.flip_left_right(image),
  tf.image.flip_up_down(image),
  )