【问题标题】:Failed to convert object of type <class 'function'> to Tensor无法将 <class 'function'> 类型的对象转换为张量
【发布时间】:2019-04-29 16:44:04
【问题描述】:

我正在尝试使用 tensorflow 的 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:无法将类型对象转换为张量。内容: 。考虑将元素转换为支持的类型。ROR:

让我知道我遗漏了什么或者是否需要更多信息。

【问题讨论】:

  • 您的代码中的image 是什么? (在tf.cond 之前)
  • @jdehesa 是一个 float32 维度的张量(height,width,3) 使用 tf.image.decode_image() 解码,原来是 jpeg_image

标签: tensorflow deep-learning data-augmentation


【解决方案1】:

来自documentation

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

参数:

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

【讨论】:

  • 我不认为这是问题所在,TensorFlow 会自动将其每个函数中的参数转换为张量。
  • 是的,感谢您恢复 max,但我相信这不是这里的问题,因为在我之前的代码中它是并且没有这样的错误,只有在引入 tf.cond() 之后我才面临错误。正如@jdehesa 指出的那样,tensorflow 会解决这个问题。
猜你喜欢
  • 2021-01-15
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多