亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

tensorflow對圖像進行拼接的例子

 更新時間:2020年02月05日 14:56:53   作者:J_PrCz  
今天小編就為大家分享一篇tensorflow對圖像進行拼接的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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

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

-在做圖像卷積的過程中,做了這樣一個比較麻煩的拼接,現(xiàn)在還沒想到更好的拼接方法,因為是塊拼接,開始的時候使用了reshape,但是得到的結果不對,需要確定清楚數(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] 的圖像進行1一樣的圖像塊拼接:

在深度神經網(wǎng)絡中,會有batch_size個圖像大小[256×256×1]的圖像進行塊的拼接,對于多了一個維度的圖像拼接起來,由[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))

在學習過程中對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轉化為array,使用的是tf.stack(x3)

以上這篇tensorflow對圖像進行拼接的例子就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論