无法将随机变量传递给Tensorflow中的tf.image.central_crop()

时间:2016-10-17 00:33:51

标签: python tensorflow

在Tensorflow中,我正在训练一组PNG文件,我希望应用数据扩充。我已成功使用tf.image.random_flip_left_right()

但是当我尝试使用tf.image.central_crop()时出现错误。 基本上我希望central_fraction来自均匀分布(0.8,1.0)。

这是我的代码。我哪里做错了? frac应该是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)

失败并显示错误:

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

1 个答案:

答案 0 :(得分:0)

我解决了我自己定义以下功能的问题。我调整了tf.image.central_crop(image,central_fraction)中提供的代码。 函数RandomCrop将裁剪图像,该图像采用从均匀分布绘制的central_fraction。您只需指定所需的最小和最大分数即可。 你可以明显地将random_uniform分布替换为另一个。

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)