简体   繁体   English

在Caffe中训练连体网络

[英]Training Siamese Network in Caffe

I am trying to build a siamese network for comparing two image samples. 我正在尝试建立一个暹罗网络来比较两个图像样本。 I followed the MNIST example in caffe . 我在caffe中遵循了MNIST的示例。

What I am trying to do is not to use Fully connected layers but rather Fully Convolutional Siamese network. 我想做的不是使用完全连接的层,而是使用完全卷积的暹罗网络。 I am just doing this for learning and understanding deep learning. 我这样做只是为了学习和理解深度学习。

I created my own custom network, that takes 32 x 32 size RGB image patch and runs through several layers of the network defined in the attached Prototxt file. 我创建了自己的自定义网络,该网络需要32 x 32大小的RGB图像补丁,并贯穿附加Prototxt文件中定义的网络的多个层。 Note to keep it short, I deleted the other half of the network which is just a mirror. 请注意,为了简短起见,我删除了网络的另一半,这只是一个镜像。 Also I am trying to learn how to use Padding in Convolutional Layers so I am also trying that in my example here. 我也在尝试学习如何在卷积层中使用填充,因此我也在这里的示例中尝试。 You will see that I put a padding of 1 on conv3 layer. 您将看到在conv3层上放置了1的填充。

label1 and label2 are same so I used silent layer to block label2 label1label2相同,所以我使用了无声层来阻止label2

layer {
  name: "data1"
  type: "Data"
  top: "data1"
  top: "label"
  include {
    phase: TRAIN
  }
  data_param {
    source: "Desktop/training/lmdb/train_1"
    batch_size: 512
    backend: LMDB
  }
}

layer {
  name: "data2"
  type: "Data"
  top: "data2"
  top: "label2"
  include {
    phase: TRAIN
  }
  data_param {
    source: "/Desktop/training/lmdb/train_2"
    batch_size: 512
    backend: LMDB
  }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data1"
  top: "conv1"
  param {
    name: "conv1_w"
    lr_mult: 1
  }
  param {
    name: "conv1_b"
    lr_mult: 2
  }
  convolution_param {
    num_output: 32
    pad: 0
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
      std: 0.03
    }
    bias_filler {
      type: "constant"
      value: 0.2
    }
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "conv1"
  top: "conv1"
}
layer {
  name: "pool1"
  type: "Pooling"
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "norm1"
  type: "LRN"
  bottom: "pool1"
  top: "norm1"
  lrn_param {
    local_size: 5
    alpha: 0.0001
    beta: 0.75
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "norm1"
  top: "conv2"
  param {
    name: "conv2_w"
    lr_mult: 1
  }
  param {
    name: "conv2_b"
    lr_mult: 2
  }
  convolution_param {
    num_output: 64
    pad: 0
    kernel_size: 1
    stride: 1
    weight_filler {
      type: "xavier"
      std: 0.03
    }
    bias_filler {
      type: "constant"
      value: 0.2
    }
  }
}
layer {
  name: "relu2"
  type: "ReLU"
  bottom: "conv2"
  top: "conv2"
}
layer {
  name: "conv3"
  type: "Convolution"
  bottom: "conv2"
  top: "conv3"
  param {
    name: "conv3_w"
    lr_mult: 1
  }
  param {
    name: "conv3_b"
    lr_mult: 2
  }
  convolution_param {
    num_output: 128
    pad: 1
    kernel_size: 3
    stride: 2
    weight_filler {
      type: "xavier"
      std: 0.03
    }
    bias_filler {
      type: "constant"
      value: 0.2
    }
  }
}
layer {
  name: "relu3"
  type: "ReLU"
  bottom: "conv3"
  top: "conv3"
}
# layer {
#   name: "dropout"
#   type: "Dropout"
#   bottom: "conv3"
#   top: "dropout"
#   dropout_param {
#     dropout_ratio: 0.5
#   }
# }
layer {
  name: "conv4"
  type: "Convolution"
  bottom: "conv3"
  top: "conv4"
  param {
    name: "conv4_w"
    lr_mult: 1
  }
  param {
    name: "conv4_b"
    lr_mult: 2
  }
  convolution_param {
    num_output: 1
    pad: 0
    kernel_size: 1
    stride: 1
    weight_filler {
      type: "xavier"
      std: 0.03
    }
    bias_filler {
      type: "constant"
      value: 0.2
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv4"
  top: "pool2"
  pooling_param {
    pool: AVE
    kernel_size: 7
    stride: 1


     }
    }

#################
layer {
  name: "loss"
  type: "ContrastiveLoss"
  bottom: "pool2"
  bottom: "pool2_p"
  bottom: "label"
  top: "loss"
  contrastive_loss_param {
    margin: 1
  }
  include { 
    phase: TRAIN 
    }
}

There are few things I am confused with: 有几件事让我感到困惑:

  1. Is it safe to add padding on the convolutional layer or can it have destructive effect? 在卷积层上添加填充是安全的还是具有破坏性的效果?
  2. In some papers I read on siamaese network, they use L2-Normalization after Fully connected layer. 在一些关于siamaese网络的文章中,他们在完全连接层之后使用L2-Normalization。 I didnt find any L2-Normalization layer on caffe but I support LRN can do the same thing by setting alpha = 1 and beta = 0.5 . 我没有在caffe上找到任何L2归一化层,但我支持LRN可以通过设置alpha = 1beta = 0.5来做同样的事情。
  3. In my network, I just average pooled the conv4 layer and used that to compute the loss using ContrastiveLoss . 在我的网络中,我平均地conv4conv4层,并使用ContrastiveLoss来计算损失。 Can that work, or I need to normalize the output of conv4 or am I doing something completely wrong here. 那行得通吗,或者我需要规范conv4的输出,或者我在这里做错了什么。
  4. Can outputs of convolutional layer be directly fed into loss functions? 卷积层的输出可以直接输入损失函数吗?

I would really appreciate you help in showing me the right direction. 我非常感谢您的帮助,向我展示正确的方向。 Besides I am using sample images of about 50K patches of some cells that I cannot publish as it is classified. 此外,我正在使用某些细胞的约50K色块的样本图像,这些图像在分类时无法发布。 The patch size is about 25x25 so I resize to 32x32 补丁大小约为25x25因此我将尺寸调整为32x32

Yes, it is safe to add padding to conv layers. 是的,将填充添加到转换层是安全的。 I think you can use LRN layers for L2 normalization the way it is described in the documentation. 我认为您可以按照文档中所述的方式将LRN层用于L2标准化。 Yes, outputs of CNN layers can be used directly in loss functions, nothing wrong with that, its just a blob. 是的,CNN层的输出可以直接用于损失函数,这没什么错,只是斑点。 In fully convolutional networks, it is always the case. 在完全卷积的网络中,情况总是如此。 Atleast in theory, your output need not be constrained for contrastive loss to work as it is a margin based loss. 从理论上讲,您的输出不必受对比损失的影响,因为它是基于边际的损失。 Typically, changing contrastive loss to a binary classification problem with softmax loss generally works and doesn't have normalization issues. 通常,将对比性损失更改为具有softmax损失的二进制分类问题通常是可行的,并且没有标准化问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM