tensorflow對圖像進行拼接

tensorflow對圖像進行多個塊的行列拼接tf.concat(), tf.stack()

  1. 在深度學習過程中,通過卷積得到的圖像塊大小是8×8×1024的圖像塊,對得到的圖像塊進行reshape得到[8×8]×[32×32],其中[8×8]是圖像塊的個數,[32×32]是小圖像的大小。通過tf.concat對小塊的圖像進行拼接。

    -在做圖像卷積的過程中,做了這樣一個比較麻煩的拼接,現在還沒想到更好的拼接方法,因爲是塊拼接,開始的時候使用了reshape,但是得到的結果不對,需要確定清楚數據的維度,對於數據的維度很是問題。

import tensorflow as tf
def tensor_concat(f, axis):
    x1 = f[0, :, :]
    for i in range(1, 8):
        x1 = tf.concat([x1, f[i, :, :]], axis=axis)
    return x1

def block_to_image(f): 
    x1 = tf.reshape(f, [64, 1024])
    x1 = tf.reshape(x1, [64, 32, 32])
    m2 = tensor_concat(x1[0:8, :, :], axis=1)
    for i in range(1, 8):
        m1 = tensor_concat(x1[i*8:(i+1)*8, :, :], axis=1)
        m2 = tf.concat([m2, m1], axis=0)
    x2 = tf.reshape(m2, [256, 256, 1])
    return x2

x = tf.random_normal([ 8, 8, 1024])
with tf.Session() as sess:
    m = sess.run(x)
    m1 = sess.run(block_to_image(m))

最後通過行拼接和列拼接得到圖像大小爲256×256×1大小的圖像。

  1. 對[batch_size, height, weight, channel] 的圖像進行1一樣的圖像塊拼接:
    在深度神經網絡中,會有batch_size個圖像大小[256×256×1]的圖像進行塊的拼接,對於多了一個維度的圖像拼接起來,由[batch_size, 8, 8, 1024]拼接爲[batch_size,256, 256, 1]。在做着部分時batch_size這部分實在是不知道怎麼處理,所以還是用了本辦法,使用的函數是append和tf.stack()
def tensor_concat(f, axis):
    x1 = f[0, :, :]
    for i in range(1, 8):
        x1 = tf.concat([x1, f[i, :, :]], axis=axis)
    return x1

def block_to_image(f):
    x3 =[]
    for k in range(f.shape[0]):
        x = f[k, :, :, :]
        x1 = tf.reshape(x, [64, 1024])
        x1 = tf.reshape(x1, [64, 32, 32])
        m2 = tensor_concat(x1[0:8, :, :], axis=1)
        for i in range(1, 8):
            m1 = tensor_concat(x1[i*8:(i+1)*8, :, :], axis=1)
            m2 = tf.concat([m2, m1], axis=0)
        x2 = tf.reshape(m2, [256, 256, 1])
        x3.append(x2)
        x4 = tf.stack(x3)
    return x4   
x = tf.random_normal([10, 8, 8, 1024])
with tf.Session() as sess:
   m = sess.run(x)
   m1 = sess.run(block_to_image1(m))

在學習過程中對tensor不能直接賦值,比如不能寫:
x2 = tf.reshape(m2, [256, 256, 1])
x3[k, :, :, 1] = x2
這樣的代碼,會出現錯誤:’Tensor’ object does not support item assignment
對於帶有類似索引的賦值,參考的辦法是:
x3 = []
x3.append(x2)
這時候得到的是list的格式,所以接下來將list轉化爲array,使用的是tf.stack(x3)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章