2016-02-19 21 views
1

爲了數據增強的目的,我想對數據進行n個不同的轉換,並且想要爲批處理中的每個圖像隨機選擇一個並應用其中的一個。例如:如何從張量流中的ops列表中隨機選擇和應用op?

image = tf.apply_random_op(image, [op1, op2, op3]) 
images, label_batch = tf.train.shuffle_batch([image, label]) 

這可能嗎?

Obs:我希望在執行會話時隨機選擇op。

+0

你有沒有考慮生成一個隨機整數和使用if-else語句? – jkschin

+0

這不會滿足我最後的觀察,因爲它不會以符號的方式進行。 – cesarsalgado

+0

您能否詳細說明您最近的觀察?我很抱歉,但我沒有得到它。 – jkschin

回答

2

我想,我想出了一個解決方案:

def apply_random_op(tensor, ops): 
    n = len(ops) 
    rand_idx = tf.floor(tf.random_uniform([], 0, n, dtype=tf.float32)) 
    op_idx = tf.constant(0.0, dtype=tf.float32) 
    chain = tf.cond(tf.equal(op_idx, rand_idx), lambda: ops[0](tensor), lambda: ops[1](tensor)) 
    for i in xrange(2, n): 
    op_idx = tf.constant(float(i), dtype=tf.float32) 
    chain = tf.cond(tf.equal(op_idx, rand_idx), lambda: ops[i](tensor), lambda: chain) 
    return chain