2016-10-17 72 views
3

在Tensorflow中,我正在從一組PNG文件進行培訓,並希望應用數據增強。我已成功使用tf.image.random_flip_left_right()無法在Tensorflow中將隨機變量傳遞給tf.image.central_crop()

但是當我嘗試使用tf.image.central_crop()時出現錯誤。 基本上我想從一個均勻分佈(0.8,1.0]繪製的central_fraction。

這裏是我的代碼。我有什麼錯嗎?應該fractf.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.) 
+0

我試着在v0.10上運行腳本,但沒有遇到任何問題。你能否提一下你使用'import tensoflow as tf; print tf .__ git_version__'的TF版本?另外,在您的數據中提供一些指向png的鏈接可能會有所幫助。 –

+0

對於TF版本: – maurizio

+0

對於TF版本,如果我運行:python3 -c'import tensorflow as tf; print(tf .__ version__)'我得到:0.11.0rc0。我在Python 3.5上。這是一個圖像的例子。謝謝! – maurizio

回答

0

我解決我的定義下列函數自己的問題,我調整了tf.image.central_crop提供的代碼(圖像,central_fraction) 功能RandomCrop將CR。選擇一個採用均勻分佈繪製的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)