代码实现的功能为下图中最后的两层输出的卷积操作
def cross_correlation(inputs): #([None,22,22,128],[None,6,6,128])
x = inputs[0] #[22,22,128]
#print(x.shape.as_list())
x = tf.reshape(x, [1] + x.shape.as_list()) #[1,22,22,128]
z = inputs[1] #[6,6,128]
z = tf.reshape(z, z.shape.as_list() + [1]) #[6,6,128,1]
#print(x.shape.as_list())
#print(z.shape.as_list())
#return tf.nn.convolution(x, z, padding='VALID', strides=(1,1))
a= tf.nn.convolution(x, z, padding='VALID', strides=(1,1)) #[1,17,17,1]
return a
def x_corr_map(inputs):
# Note that dtype MUST be specified,
# otherwise TF will assert that the input and output structures are the same,
# which they most certainly are NOT.
return K.reshape(tf.map_fn(cross_correlation, inputs, dtype=tf.float32, infer_shape=False), shape=(-1,17,17))
# [None,17,17]
def x_corr_layer():
return Lambda(x_corr_map, output_shape=(17,17))
x=x_corr_layer()([s_output,t_output])
print(x.shape.as_list()) #[None,17,17]
代码解释
s_output 为搜索区域 branch 输出 size: (None, W1, H1, C1) e.g. (None, 22, 22, 128)
t_output为目标区域branch输出 size: (None, W2, H2, C2) e.g. (None, 6, 6, 128)
层x_corr_layer()
接受输入 inputs [s_output, t_output]
tf.map_fn
实现将 inputs 在张量第一维上展开口应用到 cross_correlation
函数上。
每一步的输出都体现在代码后面。
Attention
一定要注意tensor大小,输入的两个branche可以自定义,根据输出大小进行卷积操作。可以更改strides。
注意最终输出shape
函数解释
tf.nn.convolution(x,z,padding,strides...)
x卷积区域 (1,22,22,512)
z卷积函数 (6,6,512,1) 大小6*6, 输出维度512 输出维度1
结束!