1. 程式人生 > >caffe原始碼分析-InputLayer

caffe原始碼分析-InputLayer

對於輸入層,我們首先分析最簡單的InputLayer層,其常作為網路inference時的輸入,簡單的mnist使用示例如下:

layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 1 dim: 1 dim: 28 dim: 28 } }
}

proto中相關的引數如下:

// Specifies the shape (dimensions) of a Blob.
message BlobShape {
  repeated int64 dim = 1 [packed = true];
}
message InputParameter {
  // This layer produces N >= 1 top blob(s) to be assigned manually.
  // Define N shapes to set a shape for each top.
  // Define 1 shape to set the same shape for every top.
  // Define no shape to defer to reshaping manually.
  repeated BlobShape shape = 1;
}
message LayerParameter {
//.....
optional InputParameter input_param = 143;
}

InputLayer繼承自Layer:


template <typename Dtype>
class InputLayer : public Layer<Dtype> {
public:
    explicit InputLayer(const LayerParameter& param)
            : Layer<Dtype>(param) {}
}

InputLayer沒有bottom,因此:


// Data layers have no bottoms, so reshaping is trivial.
virtual void
Reshape(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {} virtual inline const char* type() const { return "Input"; } virtual inline int ExactNumBottomBlobs() const { return 0; } virtual inline int MinTopBlobs() const { return
1; }

InputLayer顯然無需前向,反向傳播,因此對應的函式定義為空:


virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
                         const vector<Blob<Dtype>*>& top) { }
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
                          const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { }

接下來是LayerSetUp初始化top的shape:


template <typename Dtype>
void InputLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
                                   const vector<Blob<Dtype>*>& top) {
    const int num_top = top.size();
    const InputParameter& param = this->layer_param_.input_param();
    const int num_shape = param.shape_size();
    CHECK(num_shape == 0 || num_shape == 1 || num_shape == num_top)
    << "Must specify 'shape' once, once per top blob, or not at all: "
    << num_top << " tops vs. " << num_shape << " shapes.";
    if (num_shape > 0) {
        for (int i = 0; i < num_top; ++i) {
            const int shape_index = (param.shape_size() == 1) ? 0 : i;
            top[i]->Reshape(param.shape(shape_index));
        }
    }
}