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

3
在TensorFlow中,我正在从一组PNG文件进行训练,并希望应用数据增强。我已经成功地使用了tf.image.random_flip_left_right()
但是,当我尝试使用tf.image.central_crop()时出现错误。基本上,我想要从均匀分布(0.8,1.0]中获得central_fraction。
以下是我的代码。我哪里做错了吗?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.)

我在v0.10上尝试运行了您的脚本,并没有遇到任何问题。请问您正在使用哪个版本的TF import tensoflow as tf;print tf.__git_version__?此外,提供一些数据中png的链接可能会有所帮助。 - Saurabh Saxena
针对TF版本: - maurizio
对于TF版本,如果我运行:python3 -c 'import tensorflow as tf; print(tf.version)',我得到的是:0.11.0rc0。我使用的是Python 3.5。这是一个图像的例子。谢谢! - maurizio
示例图像:http://tinypic.com/r/2ziu1pj/9 - maurizio
1个回答

0
我通过定义以下函数解决了自己的问题。 我调整了提供的代码 tf.image.central_crop(image, central_fraction)。 RandomCrop 函数将裁剪图像,取自均匀分布的中心分数。 您只需指定所需的最小和最大分数即可。 您可以替换 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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接