此部分为mask-rcnn中clip_boxes_graph()函数的使用。首先利用代码解决基本函数的使用,然后运行代码,其细节如下:
代码如下:
import tensorflow as tf
import numpy as np
import random
sess=tf.Session()
window=[0.0,0.0,1.0,1.0]

box_rand=np.array([round(random.random( ),1) for i in range(32)]).reshape((8,4)) # 随机生成[0,1]之间的数去模拟box坐标
box_rand=box_rand.astype(np.float32)
print('show box_rand=',box_rand)   # 显示模拟的box

wind_split=tf.split(window, 4)
wind_sp=sess.run(wind_split)
print('wind_split=',wind_sp)  # 查看split的分割情况
print(np.array(wind_split).shape)  # 打印维度
y1, x1, y2, x2 = tf.split(box_rand, 4, axis=1)  # 表示延第二个维度分割成4个张量
y=sess.run(y1) # 显示第一个分割内容,其它语气类似
print('show value y1=',y)


def clip_boxes_graph(boxes, window):
    """
    boxes: [N, (y1, x1, y2, x2)]
    window: [4] in the form y1, x1, y2, x2
    """
    # Split
    wy1, wx1, wy2, wx2 = tf.split(window, 4)
    y1, x1, y2, x2 = tf.split(boxes, 4, axis=1)
    # Clip
    # 以y1为例,要求wy1 < y1 < wy2,其余类似,相当与x1/y1/x2/y2取值范围为[0,1]闭区间
    y1 = tf.maximum(tf.minimum(y1, wy2), wy1)
    x1 = tf.maximum(tf.minimum(x1, wx2), wx1)
    y2 = tf.maximum(tf.minimum(y2, wy2), wy1)
    x2 = tf.maximum(tf.minimum(x2, wx2), wx1)
    clipped = tf.concat([y1, x1, y2, x2], axis=1, name="clipped_boxes")
    clipped.set_shape((clipped.shape[0], 4))
    return clipped

clip=clip_boxes_graph(box_rand,window)
clip=sess.run(clip)
print('show function value clipped=',clip)
print('show function value shape clipped=',clip.shape)

结果如下:
show box_rand= [[0. 0.5 0.1 0.7]
 [0.3 0.5 0.6 0.2]
 [0.1 0.6 0.6 0.6]
 [0.8 0.8 0.9 0.6]
 [0.5 0.1 0.8 0.3]
 [0.2 0.2 0.1 0.7]
 [1.  0.3 1.  0.2]
 [0.1 0.8 0.  0.1]]
wind_split= [array([0.], dtype=float32), array([0.], dtype=float32), array([1.], dtype=float32), array([1.], dtype=float32)]
(4,)
show value y1= [[0. ]
 [0.3]
 [0.1]
 [0.8]
 [0.5]
 [0.2]
 [1. ]
 [0.1]]
show function value clipped= [[0.  0.5 0.1 0.7]
 [0.3 0.5 0.6 0.2]
 [0.1 0.6 0.6 0.6]
 [0.8 0.8 0.9 0.6]
 [0.5 0.1 0.8 0.3]
 [0.2 0.2 0.1 0.7]
 [1.  0.3 1.  0.2]
 [0.1 0.8 0.  0.1]]
show function value shape clipped= (8, 4)