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)) |
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)) |
x2 = tf.reshape(m2, [256, 256, 1]) x3[k, :, :, 1] = x2 |
|
x3 = [] x3.append(x2) |