Python basic tutorial: tensorflow example of stitching images

@This article comes from the public number: csdn2299, like to follow the public number programmer
academic institute tensorflow to stitch the rows and columns of multiple blocks on the image tf.concat (), tf.stack ()

In the deep learning process, the image block size obtained by convolution is an image block of 8 × 8 × 1024, and the obtained image block is reshaped to obtain [8 × 8] × [32 × 32], where [8 × 8] Is the number of image blocks, and [32 × 32] is the size of the small image. Use tf.concat to stitch the small images.

-In the process of image convolution, I made such a troublesome stitching. I haven't thought of a better stitching method because it is a block stitching. I used reshape at the beginning, but the results obtained are not correct, so I need to make sure The dimension of data is very problematic for the dimension of data.

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

Finally, an image with a size of 256 × 256 × 1 is obtained by row stitching and column stitching.

Perform the same image block stitching on the images of [batch_size, height, weight, channel]:

In the deep neural network, there will be batch_size images with an image size of [256 × 256 × 1] for block splicing. For images with an additional dimension, the splicing will be done by [batch_size, 8, 8, 1024] as [batch_size , 256, 256, 1]. When doing the part, the batch_size part really does not know how to deal with it, so this method is still used, and the functions used are append and 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))

During the learning process, the tensor cannot be directly assigned, for example, it cannot be written:

x2 = tf.reshape(m2, [256, 256, 1]) 
 
x3[k, :, :, 1] = x2 

Such code, there will be an error: 'Tensor' object does not support item assignment

For assignments with similar indexes, the method of reference is:

x3 = [] 
 
x3.append(x2) 

At this time, the format of the list is obtained, so the next step is to convert the list into an array, using tf.stack (x3)

Thank you very much for reading
. When I chose to study python at university, I found that I ate a bad computer foundation. I did n’t have an academic qualification. This is
nothing to do. I can only make up for it, so I started my own counterattack outside of coding. The road, continue to learn the core knowledge of python, in-depth study of computer basics, sorted out, if you are not willing to be mediocre, then join me in coding, and continue to grow!
In fact, there are not only technology here, but also things beyond those technologies. For example, how to be an exquisite programmer, rather than "cock silk", the programmer itself is a noble existence, isn't it? [Click to join] Want to be yourself, want to be a noble person, come on!

Published 45 original articles · praised 16 · 20,000+ views

Guess you like

Origin blog.csdn.net/chengxun03/article/details/105522080