Siamese Network final convolution operation 代码实现 (tf, keras)

本文介绍了一个特定的卷积操作实现,该操作应用于两个不同尺寸的输入张量,并详细解释了如何使用TensorFlow来完成这一过程。通过tf.nn.convolution函数进行卷积计算,并通过tf.map_fn函数将操作应用于所有输入样本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码实现的功能为下图中最后的两层输出的卷积操作

这里写图片描述


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

结束!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值