block_expand_op.h 3.3 KB
Newer Older
G
gongweibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 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. */

#pragma once

#include "paddle/operators/math/math_function.h"

#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
G
gongweibao 已提交
21
#include "paddle/operators/math/img2col.h"
G
gongweibao 已提交
22 23 24 25

namespace paddle {
namespace operators {

G
gongweibao 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
inline void get_blockexpand_output_shape(int imgHeight, int imgWidth,
                                         int blockHeight, int blockWidth,
                                         int strideHeight, int strideWidth,
                                         int paddingHeight, int paddingWidth,
                                         int& outputHeight, int& outputWidth) {
  outputHeight =
      1 +
      (imgHeight + 2 * paddingHeight - blockHeight + strideHeight - 1) /
          strideHeight;

  outputWidth = 1 +
                (imgWidth + 2 * paddingWidth - blockWidth + strideWidth - 1) /
                    strideWidth;
}

G
gongweibao 已提交
41 42 43
template <typename Place, typename T>
class BlockExpandKernel : public framework::OpKernel<T> {
 public:
G
gongweibao 已提交
44
  void Compute(const framework::ExecutionContext& ctx) const override {
G
gongweibao 已提交
45
    using namespace framework;
G
gongweibao 已提交
46 47 48 49 50 51 52
    const Tensor* in = ctx.Input<Tensor>("input");
    Tensor* out = ctx.Output<Tensor>("Out");
    out->mutable_data<T>(ctx.GetPlace());

    auto in_dim = in->dims();
    int N = in_dim[0];
    int C = in_dim[1];
G
gongweibao 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    int imgHeight = in_dim[2];
    int imgWidth = in_dim[3];

    int blockHeight = ctx.Attr<int>("blockHeight");
    int blockWidth = ctx.Attr<int>("blockWidth");
    int strideHeight = ctx.Attr<int>("strideHeight");
    int strideWidth = ctx.Attr<int>("strideWidth");
    int paddingHeight = ctx.Attr<int>("paddingHeight");
    int paddingWidth = ctx.Attr<int>("paddingWidth");

    int outputHeight = 0;
    int outputWidth = 0;

    get_blockexpand_output_shape(imgHeight, imgWidth, blockHeight, blockWidth,
                                 strideHeight, strideWidth, paddingHeight,
                                 paddingWidth, outputHeight, outputWidth);

    for (int i = 0; i < N; i++) {
      Tensor src = in->Slice<T>(i, i + 1).Resize(C, imgHeight, imgWidth);
      Tensor dst = out->Slice<T>(i, i + 1).Resize(outputHeight, outputWidth, C,
                                                  blockHeight, blockWidth);
      math::Im2ColFunctor<kOCF, ctx->GetPlace(), T>(ctx, src, dst, strideHeight,
                                                    strideWidth, paddingHeight,
                                                    paddingWidth);
G
gongweibao 已提交
77 78 79 80 81 82 83 84
    }
  }
};

template <typename Place, typename T>
class BlockExpandGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
G
gongweibao 已提交
85
    using namespace framework;
G
gongweibao 已提交
86 87 88 89 90
  }
};

}  // namespace operators
}  // namespace paddle