前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >caffe源码分析-InputLayer

caffe源码分析-InputLayer

作者头像
bear_fish
发布2019-02-25 11:52:14
6110
发布2019-02-25 11:52:14
举报

对于输入层,我们首先分析最简单的InputLayer层,其常作为网络inference时的输入,简单的mnist使用示例如下:

代码语言:javascript
复制
layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 1 dim: 1 dim: 28 dim: 28 } }
}

proto中相关的参数如下:

代码语言:javascript
复制
// 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:

代码语言:javascript
复制
template <typename Dtype>
class InputLayer : public Layer<Dtype> {
public:
    explicit InputLayer(const LayerParameter& param)
            : Layer<Dtype>(param) {}
}

InputLayer没有bottom,因此:

代码语言:javascript
复制
// 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显然无需前向,反向传播,因此对应的函数定义为空:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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));
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年10月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档