ConvOp.h 2.9 KB
Newer Older
H
hedaoyuan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

15 16
#pragma once

H
hedaoyuan 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "Function.h"

namespace paddle {

/*
 * Function Arguments:
 *
 * \param inputs[0]  Input image data, is NCHW format, where N is batch size,
 *                   C is the number of channels, H and W is the height and
 *                   width of input image.
 * \param inputs[1]  Filter data, is MCHW, where M is the number of output
 *                   channels, C is the number of input channels, H and W
 *                   is height and width of filter.
 * \param outputs[0] Output image data, is NCHW format, where N is batch size,
 *                   C is the number of channels, H and W is the height and
 *                   width of output image.
 *
 * \note Implemented based on the ConvFunctionBase class only supports
 *       input data in the NCHW format.
 */
class ConvFunctionBase : public FunctionBase {
public:
  void init(const FuncConfig& config) override {
    // function arguments
41 42
    strides_ = config.get<std::vector<size_t>>("strides");
    paddings_ = config.get<std::vector<size_t>>("paddings");
43
    groups_ = config.get<size_t>("groups");
H
hedaoyuan 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

    // number of inputs and outputs
    numInputs_ = 2;
    numOutputs_ = 1;
  }

  virtual void calc(const BufferArgs& inputs, const BufferArgs& outputs) {}

  void check(const BufferArgs& inputs, const BufferArgs& outputs) override {
    CHECK_EQ(numInputs_, inputs.size());
    CHECK_EQ(numOutputs_, outputs.size());

    CHECK_EQ(inputs[0].shape().ndims(), (size_t)4);
    CHECK_EQ(inputs[1].shape().ndims(), (size_t)4);
    CHECK_EQ(outputs[0].shape().ndims(), (size_t)4);

    CHECK(inputs[0].shape()[0] == outputs[0].shape()[0]);
H
Bug fix  
hedaoyuan 已提交
61
    CHECK(inputs[0].shape()[1] / groups_ == inputs[1].shape()[1]);
H
hedaoyuan 已提交
62 63 64 65
    CHECK(outputs[0].shape()[1] == inputs[1].shape()[0]);
  }

protected:
66 67
  std::vector<size_t> strides_;
  std::vector<size_t> paddings_;
68 69 70 71 72
  /// Group size, refer to grouped convolution in
  /// Alex Krizhevsky's paper: when group=2, the first half of the
  /// filters are only connected to the first half of the input channels,
  /// and the second half only connected to the second half.
  size_t groups_;
73 74 75 76 77 78 79
  inline int strideH() const { return strides_[0]; }

  inline int strideW() const { return strides_[1]; }

  inline int paddingH() const { return paddings_[0]; }

  inline int paddingW() const { return paddings_[1]; }
H
hedaoyuan 已提交
80 81 82
};

}  // namespace paddle