tensorflow對圖像進(jìn)行拼接的例子
tensorflow對圖像進(jìn)行多個塊的行列拼接tf.concat(), tf.stack()
在深度學(xué)習(xí)過程中,通過卷積得到的圖像塊大小是8×8×1024的圖像塊,對得到的圖像塊進(jìn)行reshape得到[8×8]×[32×32],其中[8×8]是圖像塊的個數(shù),[32×32]是小圖像的大小。通過tf.concat對小塊的圖像進(jìn)行拼接。
-在做圖像卷積的過程中,做了這樣一個比較麻煩的拼接,現(xiàn)在還沒想到更好的拼接方法,因為是塊拼接,開始的時候使用了reshape,但是得到的結(jié)果不對,需要確定清楚數(shù)據(jù)的維度,對于數(shù)據(jù)的維度很是問題。
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大小的圖像。
對[batch_size, height, weight, channel] 的圖像進(jìn)行1一樣的圖像塊拼接:
在深度神經(jīng)網(wǎng)絡(luò)中,會有batch_size個圖像大小[256×256×1]的圖像進(jìn)行塊的拼接,對于多了一個維度的圖像拼接起來,由[batch_size, 8, 8, 1024]拼接為[batch_size,256, 256, 1]。在做著部分時batch_size這部分實在是不知道怎么處理,所以還是用了本辦法,使用的函數(shù)是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))
在學(xué)習(xí)過程中對tensor不能直接賦值,比如不能寫:
x2 = tf.reshape(m2, [256, 256, 1]) x3[k, :, :, 1] = x2
這樣的代碼,會出現(xiàn)錯誤:'Tensor' object does not support item assignment
對于帶有類似索引的賦值,參考的辦法是:
x3 = [] x3.append(x2)
這時候得到的是list的格式,所以接下來將list轉(zhuǎn)化為array,使用的是tf.stack(x3)
以上這篇tensorflow對圖像進(jìn)行拼接的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實現(xiàn)的隨機(jī)森林算法與簡單總結(jié)
這篇文章主要介紹了Python實現(xiàn)的隨機(jī)森林算法,結(jié)合實例形式詳細(xì)分析了隨機(jī)森林算法的概念、原理、實現(xiàn)技巧與相關(guān)注意事項,需要的朋友可以參考下2018-01-01深入講解Python函數(shù)中參數(shù)的使用及默認(rèn)參數(shù)的陷阱
這篇文章主要介紹了Python函數(shù)中參數(shù)的使用及默認(rèn)參數(shù)的陷阱,文中將函數(shù)的參數(shù)分為必選參數(shù)、默認(rèn)參數(shù)、可變參數(shù)和關(guān)鍵字參數(shù)來講,要的朋友可以參考下2016-03-03Python?Excel數(shù)據(jù)處理之xlrd/xlwt/xlutils模塊詳解
在復(fù)雜的Excel業(yè)務(wù)數(shù)據(jù)處理中,三兄弟扮演的角色缺一不可。如何能夠使用xlrd/xlwt/xlutils三個模塊來實現(xiàn)數(shù)據(jù)處理就是今天的內(nèi)容,希望對大家有所幫助2023-03-03