unpool_op.h 3.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
S
sweetsky0901 已提交
2 3 4

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
5
Indicesou may obtain a copy of the License at
S
sweetsky0901 已提交
6 7 8 9 10 11 12 13 14 15 16

    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

17 18
#include <string>
#include <vector>
Y
Yi Wang 已提交
19 20 21
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/operators/math/unpooling.h"
S
sweetsky0901 已提交
22 23 24

namespace paddle {
namespace operators {
Q
QI JUN 已提交
25
template <typename DeviceContext, typename T>
S
sweetsky0901 已提交
26 27 28
class UnpoolKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
S
sweetsky0901 已提交
29
    const framework::Tensor* in_x = context.Input<framework::Tensor>("X");
30
    const framework::Tensor* in_y = context.Input<framework::Tensor>("Indices");
S
sweetsky0901 已提交
31
    auto* out = context.Output<framework::Tensor>("Out");
S
sweetsky0901 已提交
32
    std::string unpooling_type = context.Attr<std::string>("unpooling_type");
S
sweetsky0901 已提交
33 34 35
    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");
S
sweetsky0901 已提交
36
    T* output_data = out->mutable_data<T>(context.GetPlace());
Q
QI JUN 已提交
37
    auto& dev_ctx = context.template device_context<DeviceContext>();
S
sweetsky0901 已提交
38
    if (output_data) {
Q
QI JUN 已提交
39 40
      math::SetConstant<DeviceContext, T> set_zero;
      set_zero(dev_ctx, out, static_cast<T>(0));
S
sweetsky0901 已提交
41
    }
Q
QI JUN 已提交
42 43
    math::Unpool2dMaxFunctor<DeviceContext, T> unpool2d_max_forward;
    unpool2d_max_forward(dev_ctx, *in_x, *in_y, out);
S
sweetsky0901 已提交
44 45
  }
};
Q
QI JUN 已提交
46
template <typename DeviceContext, typename T>
S
sweetsky0901 已提交
47 48 49
class UnpoolGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
S
sweetsky0901 已提交
50
    const framework::Tensor* in_x = context.Input<framework::Tensor>("X");
51
    const framework::Tensor* in_y = context.Input<framework::Tensor>("Indices");
S
sweetsky0901 已提交
52 53 54 55
    const framework::Tensor* out = context.Input<framework::Tensor>("Out");
    const framework::Tensor* out_grad =
        context.Input<framework::Tensor>(framework::GradVarName("Out"));
    framework::Tensor* in_x_grad =
S
sweetsky0901 已提交
56
        context.Output<framework::Tensor>(framework::GradVarName("X"));
S
sweetsky0901 已提交
57
    std::string unpooling_type = context.Attr<std::string>("unpooling_type");
S
sweetsky0901 已提交
58 59 60 61
    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");

Q
QI JUN 已提交
62 63
    auto& device_ctx = context.template device_context<DeviceContext>();
    math::SetConstant<DeviceContext, T> zero;
L
liuwei1031 已提交
64 65 66 67

    in_x_grad->mutable_data<T>(context.GetPlace());
    zero(device_ctx, in_x_grad, static_cast<T>(0));

Q
QI JUN 已提交
68 69
    math::Unpool2dMaxGradFunctor<DeviceContext, T> unpool2d_max_backward;
    unpool2d_max_backward(device_ctx, *in_x, *in_y, *out, *out_grad, in_x_grad);
S
sweetsky0901 已提交
70 71 72 73
  }
};
}  // namespace operators
}  // namespace paddle