The crop layer in caffe

tags: caffe

inFully Convolutional Networks(FCN)In, will useCrop layer, His main role is to cut. Below we give an example to illustrate how to use the Crop layer.

 

   The data in Caffe is in the form of blobs, and blobs are four-dimensional data, namely (Batch size, number of Chennels, Height, Width)=(N, C, H, W). ---(0,1,2,3)

 

Crop layer has two inputs (bottom blobs), let us assume A and B, and output (top) is C.

  • A is the bottom to be cut, and its size is (20,50,512,512)
  • B is the reference input for cutting, and his size is (20,10,256,256)
  • C is the output (top blob), cut from A, then his size is (20,10,256,256)

  

   In this example, the dimension of axis 0 remains unchanged, we only need to cut the axis 1, 2, 3 of the blob, so we set axis=1, which means we will cut the axis 1 and all axes after it.

   has two cutting modes:

Mode 1---gives 3 offsets, each for a dimension, offset=(25,128,128)

  • axis=1,offset=(25,128,128)
  • crop operation: C = A[: , 25: 25+B.shape[1] , 128: 128+B.shape[2] , 128: 128+B.shape[3] ]
  • In other words, for axis 1 of A, 25-35 are cut symmetrically
  • Symmetrical cutting: offset = (Original_length-desired length) / 2

 

Mode 2---gives 1 offset, applicable to three dimensions, offset=25

  • Then it is equivalent to mode 1 offset=(25,25,25)

axis=0,1,2,3 are represented as N, C, H, W respectively. The default axis is equal to 2, that is, cropping starts from H by default (cutting H and W). Offset represents the size of the crop

void CropLayer<Dtype>::crop_copy(const vector<Blob<Dtype>*>& bottom,// bottom[0]
             const vector<Blob<Dtype>*>& top,
             const vector<int>& offsets,
             vector<int> indices,//all 0 at initialization
                           int cur_dim,//starts from 0 by default
             const Dtype* src_data,
             Dtype* dest_data,
             bool is_forward) {
  if (cur_dim + 1 < top[0]->num_axes()) {
    // We are not yet at the final dimension, call copy recursively
	 // Before reaching the last dimension, call crop_copy() recursively
    for (int i = 0; i < top[0]->shape(cur_dim); ++i) {
      indices[cur_dim] = i;
      crop_copy(bottom, top, offsets, indices, cur_dim+1,
                src_data, dest_data, is_forward);
    }
  } else {
         // We are at the last dimensions, which is stored continously(continuous) in memory
    for (int i = 0; i < top[0]->shape(cur_dim); ++i) {
             // prepare index vector reduced(red) and with offsets(off) prepare index vector
             std::vector<int> ind_red(cur_dim, 0); //The offset vector of the top level
             std::vector<int> ind_off(cur_dim+1, 0);//The offset vector of the bottom layer
             for (int j = 0; j <cur_dim; ++j) {//Note that cur_dim=3 here, so j is at most 2, and ind_red[0] is 0 during initialization
          ind_red[j] = indices[j];
          ind_off[j] = indices[j] + offsets[j];
      }
             ind_off[cur_dim] = offsets[cur_dim];//ind_off the last dimension
             // do the copy copy operation
      if (is_forward) {
        caffe_copy(top[0]->shape(cur_dim),
            src_data + bottom[0]->offset(ind_off),
            dest_data + top[0]->offset(ind_red));
      } else {
        // in the backwards pass the src_data is top_diff
        // and the dest_data is bottom_diff
		 // Backward process src_data is top_diff, dest_data is bottom_diff
        caffe_copy(top[0]->shape(cur_dim),
            src_data + top[0]->offset(ind_red),
            dest_data + bottom[0]->offset(ind_off));
      }
    }
  }
}

Mode 1 prototxt is written as follows:


1
2
3
4
5
6
7
8
9
10
11
12
13
layer {
  name: "crop_layer"
  type: "Crop"
  bottom: "A"
  bottom: "B"
  top: "C"
  crop_param {
    axis: 1
    offset: 25
    offset: 128
    offset: 128
  }
}

Intelligent Recommendation

Introduction to the Layer layer in Caffe

Author: knowing users link: https://www.zhihu.com/question/2798282/answer/39350629 First knowledge Caffe 1.1. Advances and disadvantages of caffe relative to other DL frames: Advantages: high speed. T...

[Caffe] network layer meaning in Caffe

RESHAPE layer: (Change BLOB shape, N, C, W, H) Reshape layerOnly change the dimension of the input data, but the content is constantThere is no data replication process, similar to Flatten Layer. The ...

Use crop_size to crop training pictures in caffe

Transfer from: The following is a simple example. [python] view plain copy print? layer {     name: ”data”     type: ”Data&rdqu...

Caffe layer series: Eltwise Layer

Eltwise Layer is to perform operation calculation on multiple bottoms and assign the result to top. General characteristics: multiple inputs and one output First look at the parameters of the Eltwise ...

Caffe layer series: Slice Layer

The role of the Slice Layer is to divide the bottom into multiple tops as needed. The general feature is: one input and multiple outputs. First let's take a look at the slice layer written in prototxt...

More Recommendation

Caffe common layer: Reduction layer

Layer type: Reduction Header location: ./include/caffe/layers/reduction_layer.hpp CPU execution source file location: ./src/caffe/layers/reduction_layer.cpp CUDA GPU execution source file location: ./...

caffe rpn layer of reshape layer

Reshape layer:(Changing the shape of the blob, N, C, W, H)         Reshape layer only changes the dimension of the input data, but the content is the same, there is no process data...

[A] Layer Caffe codes network layer

Layer functions: Is the base class for all of the network layer, which. It defines the common interface, such as feed-forward. Feedback. reshape, setup and so on. // feedforward interface must be impl...

Add layer python layer caffe

Recent want to take advantage of using python write layer, but also in the use of the process into a lot of pits, record it, in order to deepen the impression; First, under the python files under pyth...

The bn layer and scale layer in caffe

Normalization is data standardization (normalization, normalization). Batch can be understood as batch, which is batch standardization. Let me talk about how Batch is determined. In CNN, Batch is the ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top