maurizio maurizio - 1 month ago 9
Python Question

Can't pass random variable to tf.image.central_crop() in Tensorflow

In Tensorflow I am training from a set of PNG files and I wish to apply data augmentation. I have successfully used

tf.image.random_flip_left_right()


But I get an error when I try to use
tf.image.central_crop()
.
basically I would like the central_fraction to be drawn from a uniform distribution (0.8,1.0].

Here is my code. Where did I go wrong? Should
frac
be a
tf.random_uniform()
?

filename_queue = tf.train.string_input_producer( tf.train.match_filenames_once("./images/*.png"))
image_reader = tf.WholeFileReader() # Read an entire image file
_, image_file = image_reader.read(filename_queue)
image = tf.image.decode_png(image_file, channels=3, dtype=tf.uint8, name="PNGDecompressor")
image.set_shape([800,400,3])

frac = random.uniform(0.8,1.0)
image = tf.image.central_crop(image, central_fraction = frac) # THIS FAILS
# image = tf.image.central_crop(image, central_fraction = 0.8) # THIS WORKS

image = tf.image.resize_images(image, [256, 128])
image.set_shape([256,128,3])
image = tf.cast(image, tf.float32) * (1. / 255) - 0.5 # Convert from [0, 255] -> [-0.5, 0.5] floats.
image = tf.image.per_image_whitening(image)
image = tf.image.random_flip_left_right(image, seed=42)
# Start a new session to show example output.
with tf.Session() as sess:
tf.initialize_all_variables().run()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
t_image= sess.run([image])
[...]
coord.request_stop()
coord.join(threads)


Fails with error:

TypeError: Fetch argument 0.9832154064713503 has invalid type <class 'float'>, must be a string or Tensor. (Can not convert a float into a Tensor or Operation.)

Answer

I solved my own problem defining the following function. I adjusted the code provided in tf.image.central_crop(image, central_fraction). The function RandomCrop will crop an image taking a central_fraction drawn from a uniform distribution. You can just specify the min and max fraction you want. You can replace random_uniform distribution to a different one obviously.

def RandomCrop(image,fMin, fMax):
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import array_ops
  from tensorflow.python.framework import ops
  image = ops.convert_to_tensor(image, name='image')

  if fMin <= 0.0 or fMin > 1.0:
    raise ValueError('fMin must be within (0, 1]')
  if fMax <= 0.0 or fMax > 1.0:
    raise ValueError('fMin must be within (0, 1]')

  img_shape = array_ops.shape(image)
  depth = image.get_shape()[2]
  my_frac2 = tf.random_uniform([1], minval=fMin, maxval=fMax, dtype=tf.float32, seed=42, name="uniform_dist") 
  fraction_offset = tf.cast(math_ops.div(1.0 , math_ops.div(math_ops.sub(1.0,my_frac2[0]), 2.0)),tf.int32)
  bbox_h_start = math_ops.div(img_shape[0], fraction_offset)
  bbox_w_start = math_ops.div(img_shape[1], fraction_offset)
  bbox_h_size = img_shape[0] - bbox_h_start * 2
  bbox_w_size = img_shape[1] - bbox_w_start * 2

  bbox_begin = array_ops.pack([bbox_h_start, bbox_w_start, 0])
  bbox_size = array_ops.pack([bbox_h_size, bbox_w_size, -1])
  image = array_ops.slice(image, bbox_begin, bbox_size)

  # The first two dimensions are dynamic and unknown.
  image.set_shape([None, None, depth])
  return(image)
Comments