FCN全卷积 crop的计算方法

来源:互联网 发布:手机淘宝注册会员名 编辑:程序博客网 时间:2024/05/17 14:18

src:https://groups.google.com/forum/#!topic/caffe-users/YSRYy7Nd9J8

You're right. The cropping layer takes a crop bottom and a reference bottom.


To quickly answer your 2 questions :
1 - How does caffe understand where to crop? Apparently it does not by default. You have to tell it by specifying the offset
2 - What does the offset parameter used for? It is used to tell the cropping layer where exactly to crop.


Let's take an example:
Blobs are 4D : (Batch size , Number of channels/filter , Height, Width) = (N,C,H,W)

1-crop bottom A is (20, 50, 512, 512)
2-reference bottom B is (20, 10, 256, 256)
3-the top blob C (result blob) will be (20, 10, 256, 256) 

In this example we want to crop dimensions 1,2 & 3. But keep dimension 0 fixed.
So we set axis=1 (that will crop 1 and all the following axes)

The other parameter 'offset' specifies where exactly should the crop take place in A. Apparently this parameter has no defaults and have to be specified.

There are 2 modes :
1- Specify 3 offsets, one for each dimension : say offset = (25, 128, 128)
  • So axis=1, offset=(25,128,128)
  • The crop operation in numpy syntax will be : C = A[: , 25: 25+B.shape[1] , 128: 128+B.shape[2] , 128: 128+B.shape[3] ]
  • In words : this will only take filters of A 25 to 35 and ignore the rest. And do a center crop for the spatial dimensions
2- Specify 1 offset that applies for all dimensions : offset = 25
  • This will be exactly like in (1) with offset = (25, 25, 25)

The prototxt for mode (1) is the following :
layer {
  name: "crop_layer"
  type: "Crop"
  bottom: "A"
  bottom: "B"
  top: "C"
  crop_param {
    axis: 1
    offset: 25
    offset: 128
    offset: 128
  }
}


一般情况下,都是中心点裁剪

Yea I've been there too...moving from longjon's branch to the recent caffe with a new crop layer.


Basically if you know you want to do center cropping (99% of the cases) it's easy to calculate the offsets :
To centrally crop a list of size 512 to only the middle 256 values, the offset would be 128.

offset = (Original_length - desired length ) / 2

do this for each dimension. To be able to do that you need to know the exact dimensions of the blobs coming in and out for your layers. Caffe prints these dimensions when the network is constructed.

原创粉丝点击