tensorflow对图像进行多个块的行列拼接tf.concat(), tf.stack()

在深度学习过程中,通过卷积得到的图像块大小是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大小的图像。

对[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)

以上这篇tensorflow对图像进行拼接的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

以上就是【tensorflow对图像进行拼接的例子】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)
发表我的评论

最新评论

  1. 暂无评论