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

匆匆过客 提交于 2020-12-25 04:23:59

问题


Intro

I am using a modified version of the Tensorflow tutorial "Deep MNIST for experts" with the Python API for a medical images classification project using convolutionnal networks.

I want to artificially increase the size of my training set by applying random modifications on the images of my training set.

Problem

When I run the line :

flipped_images = tf.image.random_flip_left_right(images)

I get de following error :

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

My Tensor "images" is an ndarray (shape=[batch, im_size, im_size, channels]) of "batch" ndarrays (shape=[im_size, im_size, channels]).

Just to check if my input data was packed in the right shape and type, I have tried to apply this simple function in the (not modified) tutorial "Tensorflow Mechanics 101" and I get the same error.

Finally, I still get the same error trying to use the following functions :

  • tf.image.random_flip_up_down()
  • tf.image.random_brightness()
  • tf.image.random_contrast()

Questions

As input data is usually carried in Tensorflow as ndarrays, I would like to know :

  1. Is it a bug of Tensorflow Python API or is it my "fault" because of the type/shape of my input data?
  2. How could I get it to work and be able to apply tf.image.random_flip_left_right to my training set?

回答1:


This seems like an inconsistency in the TensorFlow API, since almost all other op functions accept NumPy arrays wherever a tf.Tensor is expected. I've filed an issue to track the fix.

Fortunately, there is a simple workaround, using tf.convert_to_tensor(). Replace your code with the following:

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


来源:https://stackoverflow.com/questions/35824798/tensorflow-error-using-tf-image-random-numpy-ndarray-object-has-no-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!