python基础教程:tensorflow对图像进行拼接的例子

@本文来源于公众号:csdn2299,喜欢可以关注公众号 程序员学府
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))
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)

非常感谢你的阅读
大学的时候选择了自学python,工作了发现吃了计算机基础不好的亏,学历不行这是
没办法的事,只能后天弥补,于是在编码之外开启了自己的逆袭之路,不断的学习python核心知识,深入的研习计算机基础知识,整理好了,如果你也不甘平庸,那就与我一起在编码之外,不断成长吧!
其实这里不仅有技术,更有那些技术之外的东西,比如,如何做一个精致的程序员,而不是“屌丝”,程序员本身就是高贵的一种存在啊,难道不是吗?[点击加入]想做你自己想成为高尚人,加油!

发布了45 篇原创文章 · 获赞 16 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/chengxun03/article/details/105522080