from __future__ import division import matplotlib.pyplot as plt import numpy as np import tensorflow as tf imgs = [[[[255, 0, 0], [0, 255, 0], [0, 0, 255]], [[255, 0, 0], [0, 255, 0], [0, 0, 255]], [[255, 0, 0], [0, 255, 0], [0, 0, 255]], [[255, 0, 0], [0, 255, 0], [0, 0, 255]]], [[[255, 0, 0], [0, 255, 0], [0, 0, 255]], [[255, 0, 0], [0, 255, 0], [0, 0, 255]], [[255, 0, 0], [0, 255, 0], [0, 0, 255]], [[255, 0, 0], [0, 255, 0], [0, 0, 255]]]] imgs = tf.reshape(imgs, [2, 4, 3, 3]) coords = [[[[0.2, 0.2], [1.3, 0.2], [1.8, 2.2]], [[0.5, 2], [1.3, 2], [0.2, 1.6]], [[0.2, 0.2], [1.3, 0.2], [1.8, 2.2]], [[0.5, 2], [1.3, 2], [0.2, 1.6]]], [[[-1.2, 0.2], [1.3, 0.2], [1.8, 2.2]], [[0.5, 2], [1.3, 2], [0.2, 1.6]], [[0.2, 0.2], [1.3, 0.2], [1.8, 2.2]], [[0.5, 2], [1.3, 2], [0.2, 1.6]]]] coords = tf.reshape(coords, [2, 4, 3, 2]) cam_coords = [[[[1, 2, 3, 1], [2, 3, 4, 1], [3, 4, 5, 1]], [[4, 5, 6, 1], [7, 8, 9, 1], [10, 11, 12, 1]], [[1, 2, 3, 1], [2, 3, 4, 1], [3, 4, 5, 1]], [[4, 5, 6, 1], [7, 8, 9, 1], [10, 11, 12, 1]]], [[[1, 2, 3, 1], [2, 3, 4, 1], [3, 4, 5, 1]], [[4, 5, 6, 1], [7, 8, 9, 1], [10, 11, 12, 1]], [[2, 2, 3, 1], [3, 3, 4, 1], [4, 4, 5, 1]], [[5, 5, 6, 1], [8, 8, 9, 1], [11, 11, 12, 1]]]] cam_coords = tf.reshape(cam_coords, [2, 4, 3, 4]) cam_coords = tf.transpose(cam_coords, perm=[0, 3, 1, 2]) # def bilinear_sampler(imgs, coords, cam_coords_T): # imgs = tf.tile(imgs, multiples=[1, 100, 50, 1]) # coords = tf.tile(coords, multiples=[1, 100, 50, 1]) # cam_coords_T = tf.tile(cam_coords_T, multiples=[1, 1, 100, 50]) def _repeat(x, n_repeats): # x = tf.cast(tf.range(4), ‘float32‘) * 53248 n_repeats = 53248。 rep = tf.transpose( tf.expand_dims(tf.ones(shape=tf.stack([ n_repeats, ])), 1), [1, 0]) # 最后得到[1,53248]大小的全一矩阵。tf.stack其作用类似于tf.concat,都是拼接两个张量,而不同之处在于,tf.concat拼接的是两个shape完全相同的张量,并且产生的张量的阶数不会发生变化,而tf.stack则会在新的张量阶上拼接,产生的张量的阶数将会增加 rep = tf.cast(rep, ‘float32‘) x = tf.matmul(tf.reshape(x, (-1, 1)), rep) # reshape为一列,得到[[ 0.][ 53248.][106496.][159744.]]*rep,最后得到shape=(4, 53248)的矩阵。 return tf.reshape(x, [-1]) # 最后又化为一列Tensor("Reshape_1:0", shape=(212992,), dtype=float32) with tf.name_scope(‘image_sampling‘): coords_x, coords_y = tf.split(coords, [1, 1], axis=3) inp_size = imgs.get_shape() coord_size = coords.get_shape() out_size = coords.get_shape().as_list() out_size[3] = imgs.get_shape().as_list()[3] coords_x = tf.cast(coords_x, ‘float32‘) coords_y = tf.cast(coords_y, ‘float32‘) x0 = tf.floor(coords_x) x1 = x0 + 1 y0 = tf.floor(coords_y) y1 = y0 + 1 y_max = tf.cast(tf.shape(imgs)[1] - 1, ‘float32‘) x_max = tf.cast(tf.shape(imgs)[2] - 1, ‘float32‘) zero = tf.zeros([1], dtype=‘float32‘) x0_safe = tf.clip_by_value(x0, zero, x_max) y0_safe = tf.clip_by_value(y0, zero, y_max) x1_safe = tf.clip_by_value(x1, zero, x_max) y1_safe = tf.clip_by_value(y1, zero, y_max) ## bilinear interp weights, with points outside the grid having weight 0#判断是否相等,相等为1,不相等为0. ## 以下方式没有提高效果的原因是令重建的对应像素值为0了,但是原图像对应位置还有值,计算误差单纯为原图像的像素值。 # wt_x0 = (x1 - coords_x) * tf.cast(tf.equal(x0, x0_safe), ‘float32‘) # wt_x1 = (coords_x - x0) * tf.cast(tf.equal(x1, x1_safe), ‘float32‘) # wt_y0 = (y1 - coords_y) * tf.cast(tf.equal(y0, y0_safe), ‘float32‘) # wt_y1 = (coords_y - y0) * tf.cast(tf.equal(y1, y1_safe), ‘float32‘) mask_p = tf.logical_and( # 如果四个值都在图像中说明投影点没有落到图像外。这个mask和相邻帧即投影来点的图像大小相等。 tf.logical_and(x0 >= zero, x1 <= x_max), tf.logical_and(y0 >= zero, y1 <= y_max)) mask_p = tf.to_float(mask_p) mask_p = tf.tile(mask_p, multiples=[1, 1, 1, 3]) wt_x0 = x1_safe - coords_x wt_x1 = coords_x - x0_safe wt_y0 = y1_safe - coords_y wt_y1 = coords_y - y0_safe ## indices in the flat image to sample from dim2 = tf.cast(inp_size[2], ‘float32‘) dim1 = tf.cast(inp_size[2] * inp_size[1], ‘float32‘) base = tf.reshape( _repeat( tf.cast(tf.range(coord_size[0]), ‘float32‘) * dim1, coord_size[1] * coord_size[2]), [out_size[0], out_size[1], out_size[2], 1]) # tf.reshape(_repeat(tf.cast(tf.range(4), ‘float32‘) * 128 * 416, 128 * 416), [4, 128, 416, 1]) # 上面最后得base=Tensor("Reshape_2:0", shape=(4, 128, 416, 1), dtype=float32)。中间有[ 0.][ 53248.][106496.][159744.]四种数。 base_y0 = base + y0_safe * dim2 base_y1 = base + y1_safe * dim2 # 考虑进有4个batch,所以不同batch要加上不同的基数。 idx00 = tf.reshape(x0_safe + base_y0, [-1]) # 加上基数之后构成了四个像素值的索引。 idx01 = x0_safe + base_y1 idx10 = x1_safe + base_y0 idx11 = x1_safe + base_y1 ## sample from imgs imgs_flat = tf.reshape(imgs, tf.stack([-1, inp_size[3]])) imgs_flat = tf.cast(imgs_flat, ‘float32‘) im00 = tf.reshape(tf.gather(imgs_flat, tf.cast(idx00, ‘int32‘)), out_size) # 每一个输出都有对应的四个像素点的值参与运算。 im01 = tf.reshape(tf.gather(imgs_flat, tf.cast(idx01, ‘int32‘)), out_size) im10 = tf.reshape(tf.gather(imgs_flat, tf.cast(idx10, ‘int32‘)), out_size) im11 = tf.reshape(tf.gather(imgs_flat, tf.cast(idx11, ‘int32‘)), out_size) w00 = wt_x0 * wt_y0 ######这里横轴和纵轴的距离乘机就算距离了。 w01 = wt_x0 * wt_y1 w10 = wt_x1 * wt_y0 w11 = wt_x1 * wt_y1 output = tf.add_n([ w00 * im00, w01 * im01, w10 * im10, w11 * im11 ]) # 以下为自定义代码 cam_coords = tf.transpose(cam_coords, perm=[0, 2, 3, 1]) batch, height, width, channels = imgs.get_shape().as_list() cam_coords = cam_coords[:, :, :, 0:-1] cam_coords = tf.cast(cam_coords, ‘float32‘) euclidean = tf.sqrt(tf.reduce_sum(tf.square(cam_coords), 3)) euclidean = tf.reshape(euclidean, [batch, -1]) xy00 = tf.concat([x0, y0], axis=3) for i in range(1): euclideani = euclidean[i, :] euclideani = tf.reshape(euclideani, [-1, 1]) xy00_batchi = xy00[i, :, :, :] # 将横纵坐标合在一起,取batch1. xy00_batchi = tf.reshape(xy00_batchi, [-1, 2]) xy00_batchi = tf.cast(xy00_batchi, tf.int32) mask0 = tf.ones(shape=[height * width], dtype=‘float32‘) euclideani_tr = tf.transpose(euclideani, perm=[1, 0]) changdu = 4 batch_h1 = 3 for h1 in range(batch_h1): xy00_batchi_tile1 = tf.tile(tf.expand_dims(xy00_batchi[h1*changdu:(h1+1)*changdu], 1), multiples=[1, height * width, 1]) xy00_batchi_tile2 = tf.tile(tf.expand_dims(xy00_batchi, 0), multiples=[changdu, 1, 1]) xy00_batchi_equal = tf.equal(xy00_batchi_tile1, xy00_batchi_tile2) xy00_batchi_equal = tf.reduce_all(xy00_batchi_equal, axis=2) xy00_batchi_equal = tf.reshape(xy00_batchi_equal, shape=[changdu, height * width]) # euclideani_tile = tf.tile(euclideani[h1*changdu:(h1+1)*changdu, :], multiples=[1, height * width]) euclideani_greater = tf.greater(euclideani_tile,euclideani_tr) xy00_batchi_equal_euclideani_greater = tf.logical_and(xy00_batchi_equal, euclideani_greater) xy00_batchi_equal_euclideani_greater_or = tf.reduce_any(xy00_batchi_equal_euclideani_greater, axis=1) xy00_batchi_equal_euclideani_greater_or = tf.to_float(xy00_batchi_equal_euclideani_greater_or) mask0_part1 = mask0[0:h1*changdu] mask0_part3 = mask0[(h1+1)*changdu:] mask0_part2 = mask0[h1*changdu:(h1+1)*changdu] - xy00_batchi_equal_euclideani_greater_or mask0 = tf.concat([mask0_part1, mask0_part2, mask0_part3], axis=0) mask0 = tf.clip_by_value(mask0, 0, 1) mask0 = tf.reshape(mask0, [height, width]) mask0 = tf.expand_dims(mask0, 0) # if i == 0: # mask0_stack = mask0 # else: # mask0_stack = tf.concat([mask0_stack, mask0], axis=0) # mask0_stack = tf.tile(tf.expand_dims(mask0_stack, 3), multiples=[1, 1, 1, 3]) # return output, mask_p, mask0_stack # output, mask_p, mask0_stack = bilinear_sampler(imgs, coords, cam_coords) # imgs = tf.cast(imgs, ‘float32‘) # imgs_mask = imgs*mask1_stack with tf.Session() as sess: print(sess.run(xy00_batchi_equal_euclideani_greater_or)) print(xy00_batchi_equal_euclideani_greater_or) print(mask0) # # print(sess.run(output)) print(sess.run(mask0)) # print(sess.run(imgs_mask)) # print(mask_p)
原文:https://www.cnblogs.com/sulashi/p/9449217.html