im2sequence_op.h 8.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
G
gongweibao 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

   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
16
#include <string>
17
#include <vector>
18

Y
Yi Wang 已提交
19 20 21
#include "paddle/fluid/framework/data_layout.h"
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
22
#include "paddle/fluid/operators/eigen/eigen_function.h"
Y
Yi Wang 已提交
23
#include "paddle/fluid/operators/math/im2col.h"
24
#include "paddle/phi/kernels/funcs/math_function.h"
G
gongweibao 已提交
25 26 27 28

namespace paddle {
namespace operators {

29
using Tensor = phi::DenseTensor;
30
using LoDTensor = phi::DenseTensor;
W
wanghaoshuang 已提交
31

32 33
inline int Im2SeqOutputSize(
    int input_size, int filter_size, int padding_0, int padding_1, int stride) {
W
wanghaoshuang 已提交
34 35 36
  const int output_size =
      (input_size + padding_0 + padding_1 - filter_size) / stride + 1;
  return output_size;
G
gongweibao 已提交
37 38
}

W
wanghaoshuang 已提交
39
template <typename DeviceContext, typename T>
40
class Im2SequenceKernel : public framework::OpKernel<T> {
G
gongweibao 已提交
41
 public:
G
gongweibao 已提交
42
  void Compute(const framework::ExecutionContext& ctx) const override {
43
    const phi::DenseTensor* in = ctx.Input<phi::DenseTensor>("X");
W
wanghaoshuang 已提交
44
    LoDTensor* out = ctx.Output<LoDTensor>("Out");
G
gongweibao 已提交
45
    auto in_dim = in->dims();
W
wanghaoshuang 已提交
46 47
    int batch_size = in_dim[0];
    int img_channels = in_dim[1];
G
gongweibao 已提交
48 49
    int img_height = in_dim[2];
    int img_width = in_dim[3];
W
wanghaoshuang 已提交
50 51 52
    auto kernels = ctx.Attr<std::vector<int>>("kernels");
    auto strides = ctx.Attr<std::vector<int>>("strides");
    auto paddings = ctx.Attr<std::vector<int>>("paddings");
53
    if (ctx.HasInput("Y") && batch_size > 1) {
54
      const phi::DenseTensor* imgrealsize = ctx.Input<phi::DenseTensor>("Y");
55 56
      auto out_stride = ctx.Attr<std::vector<int>>("out_stride");
      Tensor cpu_shape_tensor;
57 58
      paddle::framework::TensorCopySync(
          *imgrealsize, platform::CPUPlace(), &cpu_shape_tensor);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
      std::vector<int> imgreal_h;
      std::vector<int> imgreal_w;
      std::vector<int> output_height;
      std::vector<int> output_width;
      int result = 0;
      for (int i = 0; i < batch_size; i++) {
        int tmp_real_h = static_cast<int>((cpu_shape_tensor.data<T>())[2 * i]);
        int tmp_real_w =
            static_cast<int>((cpu_shape_tensor.data<T>())[2 * i + 1]);
        if (tmp_real_h % out_stride[0] == 0) {
          tmp_real_h = tmp_real_h / out_stride[0];
        } else {
          tmp_real_h = tmp_real_h / out_stride[0] + 1;
        }
        if (tmp_real_w % out_stride[1] == 0) {
          tmp_real_w = tmp_real_w / out_stride[1];
        } else {
          tmp_real_w = tmp_real_w / out_stride[1] + 1;
        }
        imgreal_h.push_back(tmp_real_h);
        imgreal_w.push_back(tmp_real_w);
        output_height.push_back(Im2SeqOutputSize(
            imgreal_h[i], kernels[0], paddings[0], paddings[2], strides[0]));
        output_width.push_back(Im2SeqOutputSize(
            imgreal_w[i], kernels[1], paddings[1], paddings[3], strides[1]));
        result += output_height[i] * output_width[i];
      }

      out->mutable_data<T>({result, img_channels * kernels[0] * kernels[1]},
                           ctx.GetPlace());

      const std::vector<int> dilations({1, 1});
      int offset_out = 0;
      for (int i = 0; i < batch_size; i++) {
        const Tensor src =
            in->Slice(i, i + 1).Resize({img_channels, img_height, img_width});
        Tensor dst = out->Slice(offset_out,
                                offset_out + output_height[i] * output_width[i])
97 98 99 100 101
                         .Resize({output_height[i],
                                  output_width[i],
                                  img_channels,
                                  kernels[0],
                                  kernels[1]});
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        offset_out += output_height[i] * output_width[i];

        math::Im2ColFunctor<math::ColFormat::kOCF, DeviceContext, T> f;
        auto& dev_ctx = ctx.template device_context<DeviceContext>();
        f(dev_ctx, src, dilations, strides, paddings, &dst);
      }
      framework::LoD lod(1);
      lod[0].reserve(batch_size + 1);
      int offset = 0;
      lod[0].push_back(offset);
      for (int i = 0; i < batch_size; ++i) {
        offset += output_height[i] * output_width[i];
        lod[0].push_back(offset);
      }
      out->set_lod(lod);
    } else {
118 119 120 121
      int output_height = Im2SeqOutputSize(
          img_height, kernels[0], paddings[0], paddings[2], strides[0]);
      int output_width = Im2SeqOutputSize(
          img_width, kernels[1], paddings[1], paddings[3], strides[1]);
L
liuwei1031 已提交
122 123 124 125
      out->mutable_data<T>(
          {static_cast<int64_t>(batch_size) * output_height * output_width,
           static_cast<int64_t>(img_channels) * kernels[0] * kernels[1]},
          ctx.GetPlace());
126 127 128 129 130 131
      const std::vector<int> dilations({1, 1});
      auto out_dims = out->dims();
      out->Resize({batch_size, out->numel() / batch_size});
      for (int i = 0; i < batch_size; i++) {
        const Tensor src =
            in->Slice(i, i + 1).Resize({img_channels, img_height, img_width});
132 133 134 135 136
        Tensor dst = out->Slice(i, i + 1).Resize({output_height,
                                                  output_width,
                                                  img_channels,
                                                  kernels[0],
                                                  kernels[1]});
137 138 139 140 141 142 143 144 145

        math::Im2ColFunctor<math::ColFormat::kOCF, DeviceContext, T> f;
        auto& dev_ctx = ctx.template device_context<DeviceContext>();
        f(dev_ctx, src, dilations, strides, paddings, &dst);
      }
      out->Resize(out_dims);
      framework::LoD lod(1);
      lod[0].reserve(batch_size + 1);
      int offset = 0;
W
wanghaoshuang 已提交
146
      lod[0].push_back(offset);
147 148 149 150 151
      for (int i = 0; i < batch_size; ++i) {
        offset += output_height * output_width;
        lod[0].push_back(offset);
      }
      out->set_lod(lod);
W
wanghaoshuang 已提交
152
    }
G
gongweibao 已提交
153 154 155
  }
};

W
wanghaoshuang 已提交
156
template <typename DeviceContext, typename T>
157
class Im2SequenceGradKernel : public framework::OpKernel<T> {
G
gongweibao 已提交
158 159
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
160 161 162 163
    auto* in = ctx.Input<phi::DenseTensor>("X");
    phi::DenseTensor* d_out = const_cast<phi::DenseTensor*>(
        ctx.Input<phi::DenseTensor>(framework::GradVarName("Out")));
    auto* d_x = ctx.Output<phi::DenseTensor>(framework::GradVarName("X"));
G
gongweibao 已提交
164 165 166
    d_x->mutable_data<T>(ctx.GetPlace());

    auto x_v = framework::EigenVector<T>::Flatten(*d_x);
W
wanghaoshuang 已提交
167
    auto& place = *ctx.template device_context<DeviceContext>().eigen_device();
168
    EigenConstant<std::decay_t<decltype(place)>, T, 1>::Eval(place, x_v, 0.0);
G
add gpu  
gongweibao 已提交
169 170

    auto in_dim = in->dims();
W
wanghaoshuang 已提交
171 172
    int batch_size = in_dim[0];
    int img_channels = in_dim[1];
G
add gpu  
gongweibao 已提交
173 174 175
    int img_height = in_dim[2];
    int img_width = in_dim[3];

W
wanghaoshuang 已提交
176 177 178
    auto kernels = ctx.Attr<std::vector<int>>("kernels");
    auto strides = ctx.Attr<std::vector<int>>("strides");
    auto paddings = ctx.Attr<std::vector<int>>("paddings");
179 180 181 182
    int output_height = Im2SeqOutputSize(
        img_height, kernels[0], paddings[0], paddings[2], strides[0]);
    int output_width = Im2SeqOutputSize(
        img_width, kernels[1], paddings[1], paddings[3], strides[1]);
W
wanghaoshuang 已提交
183 184 185 186 187 188 189 190 191

    const std::vector<int> dilations({1, 1});

    auto d_out_dims = d_out->dims();
    d_out->Resize({batch_size, d_out->numel() / batch_size});
    for (int i = 0; i < batch_size; i++) {
      Tensor dst =
          d_x->Slice(i, i + 1).Resize({img_channels, img_height, img_width});
      const Tensor src = d_out->Slice(i, i + 1).Resize(
W
wanghaoshuang 已提交
192
          {output_height, output_width, img_channels, kernels[0], kernels[1]});
W
wanghaoshuang 已提交
193 194 195
      math::Col2ImFunctor<math::ColFormat::kOCF, DeviceContext, T> f;
      auto& dev_ctx = ctx.template device_context<DeviceContext>();
      f(dev_ctx, src, dilations, strides, paddings, &dst);
G
add gpu  
gongweibao 已提交
196
    }
W
wanghaoshuang 已提交
197
    d_out->Resize(d_out_dims);
G
gongweibao 已提交
198 199 200 201 202
  }
};

}  // namespace operators
}  // namespace paddle