使用 tf.image.random 的 Tensorflow 错误:'numpy.ndarray' 对象没有属性 'get_shape'

Tensorflow error using tf.image.random : 'numpy.ndarray' object has no attribute 'get_shape'

简介

我正在使用带有 Python API 的 Tensorflow 教程 "Deep MNIST for experts" 的修改版本,用于使用卷积网络的医学图像分类项目。

我想通过对训练集的图像应用随机修改来人为地增加训练集的大小。

问题

当我 运行 行时:

flipped_images = tf.image.random_flip_left_right(images)

我收到以下错误:

AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'

我的 Tensor "images" 是 "batch" 个 ndarrays (shape=[im_size, im_size, channels]).

的一个 ndarray (shape=[batch, im_size, im_size, channels])

只是为了检查我的输入数据是否以正确的形状和类型打包,我尝试在(未修改的)教程中应用这个简单的函数 "Tensorflow Mechanics 101" 但我得到了同样的错误。

最后,我在尝试使用以下函数时仍然遇到同样的错误:

问题

由于输入数据通常以 ndarrays 的形式在 Tensorflow 中进行,我想知道:

  1. 是 Tensorflow 的 bug Python API 还是我的 "fault" 因为 我的输入数据的type/shape?
  2. 我怎样才能让它工作并能够将 tf.image.random_flip_left_right 应用到我的训练集?

这似乎是 TensorFlow API 中的一个不一致之处,因为几乎所有其他运算函数都在需要 tf.Tensor 的地方接受 NumPy 数组。我已提交 an issue 以跟踪修复。

幸运的是,有一个简单的解决方法,使用 tf.convert_to_tensor()。用以下内容替换您的代码:

flipped_images = tf.image.random_flip_left_right(tf.convert_to_tensor(images))