unpool_op.h 3.1 KB
Newer Older
S
sweetsky0901 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/* 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/framework/op_registry.h"
#include "paddle/operators/math/math_function.h"
#include "paddle/operators/math/unpooling.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

template <typename Place, typename T>
class UnpoolKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    const Tensor* in_x = context.Input<Tensor>("X");
    const Tensor* in_y = context.Input<Tensor>("Y");
    Tensor* out = context.Output<Tensor>("Out");
S
sweetsky0901 已提交
33
    std::string unpoolingtype = context.Attr<std::string>("unpoolingtype");
S
sweetsky0901 已提交
34 35 36 37 38
    std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
    std::vector<int> strides = context.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
    switch (ksize.size()) {
    case 2: {
S
sweetsky0901 已提交
39
      if (unpoolingtype == "max") {
S
sweetsky0901 已提交
40
        math::Unpool2dMaxFunctor<Place, T> unpool2d_max_forward;
S
sweetsky0901 已提交
41
        unpool2d_max_forward(context.device_context(), *in_x, *in_y, out);
S
sweetsky0901 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
      }
    } break;
    default: { PADDLE_THROW("Pool op only supports 2D input."); }
    }
  }
};

template <typename Place, typename T>
class UnpoolGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    const Tensor* in_x = context.Input<Tensor>("X");
    const Tensor* in_y = context.Input<Tensor>("Y");
    const Tensor* out = context.Input<Tensor>("Out");
    const Tensor* out_grad =
        context.Input<Tensor>(framework::GradVarName("Out"));
    Tensor* in_x_grad = context.Output<Tensor>(framework::GradVarName("X"));
S
sweetsky0901 已提交
59
    std::string unpoolingtype = context.Attr<std::string>("unpoolingtype");
S
sweetsky0901 已提交
60 61 62 63 64 65 66 67 68 69 70 71
    std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
    std::vector<int> strides = context.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");

    auto& device_ctx = context.device_context();
    math::SetConstant<Place, T> zero;
    if (in_x_grad) {
      in_x_grad->mutable_data<T>(context.GetPlace());
      zero(device_ctx, in_x_grad, static_cast<T>(0.0));
          }
    switch (ksize.size()) {
    case 2: {
S
sweetsky0901 已提交
72
    if (unpoolingtype == "max") {
S
sweetsky0901 已提交
73
      math::Unpool2dMaxGradFunctor<Place, T> unpool2d_max_backward;
S
sweetsky0901 已提交
74 75
      unpool2d_max_backward(context.device_context(), *in_x, *in_y, in_x_grad,
                            *out, *out_grad);
S
sweetsky0901 已提交
76 77
      }
    } break;
S
sweetsky0901 已提交
78
    default: { PADDLE_THROW("Unpool op only supports 2D input."); }
S
sweetsky0901 已提交
79 80 81 82 83 84
    }
  }
};

}  // namespace operators
}  // namespace paddle