张量流中cnn的两个分支之间的点积

时间:2017-03-05 03:12:24

标签: python tensorflow deep-learning backpropagation dot-product

我试图使用tensorflow来实现一个cnn,当给出两个图像时,它可以找到两个图像最相似的位置。这里描述了什么:Efficient Deep Learning for Stereo Matching

我分享了cnn的两个分支的变量,然后我想做左图像激活的点积与右边图像中的所有可用位置。 我的问题是:如果我手动执行此过程(例如,在for循环中),张量流是否仍会通过过滤器反向传播渐变?我得到了一个非常不稳定和奇怪的损失值,我不确定我是否做错了。

感谢您提前提供任何帮助。

编辑: 下面是一些代码如何初始化cnn以及如何计算点积。为简单起见,我没有发布转换层初始化代码。我认为问题出在dot_product函数中。

def init_new_cnn(x_left,x_right,y,nclasses,receptive_field=1,batch_norm=True,padding="VALID"):
    shape=[3,3,3,64]
    conv1_left=cnn.conv_layer_init("conv1",x_left,shape,batch_norm=batch_norm,padding=padding,w_init="xavier")
    shape=[3,3,64,64]
    conv2_left=cnn.conv_layer_init("conv2",conv1_left,shape,batch_norm=batch_norm,padding=padding,w_init="xavier")
    conv3_left=cnn.conv_layer_init("conv3",conv2_left,shape,batch_norm=batch_norm,padding=padding,w_init="xavier")
    conv4_left=cnn.conv_layer_init("conv4",conv3_left,shape,activation=False,batch_norm=batch_norm,padding=padding,w_init="xavier")

    conv1_right=cnn.conv_layer_reuse("conv1",x_right,padding=padding,batch_norm=batch_norm)
    conv2_right=cnn.conv_layer_reuse("conv2",conv1_right,padding=padding,batch_norm=batch_norm)
    conv3_right=cnn.conv_layer_reuse("conv3",conv2_right,padding=padding,batch_norm=batch_norm)
    conv4_right=cnn.conv_layer_reuse("conv4",conv3_right,activation=False,batch_norm=batch_norm,padding=padding)
    image_rows=x_left.get_shape()[1]
    left_cols=x_left.get_shape()[2]
    right_cols=x_right.get_shape()[2]
    batch=x_left.get_shape()[0]
    match=cnn.dot_product(conv4_left, conv4_right, receptive_field,batch,image_rows,left_cols,right_cols,nclasses)
    #match=tf.nn.l2_normalize(match, 3)
    loss=cnn.loss("loss", match,y,loss_type="softmax")
    return loss,match


def dot_product(deconv_left, deconv_right, receptive_field,batch,image_rows,left_cols,right_cols,nclasses):
    res=[]

        for j in range(nclasses-1,-1,-1):
            multiply=tf.mul(deconv_left[n,:,:,:],deconv_right[n,:,j:j+receptive_field,:],name="multiply")#
            dotprod=tf.reduce_sum(multiply,name="sumdotprod")
            res.append(dotprod)

    res=tf.pack(res,name='concat')
    reshaped= tf.reshape(res,(int(batch),1,1,int(nclasses)))
    return reshaped

EDIT2:我还添加了图表: enter image description here

0 个答案:

没有答案