im2sequence_kernel.cpp 3.0 KB
Newer Older
Y
yaokun01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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. */

Y
Yao,kun 已提交
15 16
#ifdef IM2SEQUENCE_OP

Y
yaokun01 已提交
17 18 19 20 21
#include "operators/kernel/im2sequence_kernel.h"

namespace paddle_mobile {
namespace operators {

Y
Yao,kun 已提交
22
template <>
N
nhzlx 已提交
23
bool Im2SequenceKernel<CPU, float>::Init(Im2SequenceParam<CPU> *para) {
Y
Yao,kun 已提交
24 25
  return true;
}
Y
Yao,kun 已提交
26

Y
yaokun01 已提交
27 28 29 30 31 32 33 34
inline int Im2SeqOutputSize(int input_size, int filter_size, int padding_0,
                            int padding_1, int stride) {
  const int output_size =
      (input_size + padding_0 + padding_1 - filter_size) / stride + 1;
  return output_size;
}

template <>
Y
Yao,kun 已提交
35
void Im2SequenceKernel<CPU, float>::Compute(
L
liuruilong 已提交
36
    const Im2SequenceParam<CPU> &param) {
Y
yaokun01 已提交
37
  const Tensor *in_x = param.Input();
E
eclipsess 已提交
38
  framework::LoDTensor *out = param.Output();
Y
yaokun01 已提交
39
  out->mutable_data<float>();
Y
yaokun01 已提交
40 41 42 43 44

  std::vector<int> kernels = param.Kernels();
  std::vector<int> strides = param.Strides();
  std::vector<int> paddings = param.Paddings();

Y
yaokun01 已提交
45 46 47 48 49
  auto in_x_dim = in_x->dims();
  const int batch_size = static_cast<int>(in_x_dim[0]);
  const int img_channels = static_cast<int>(in_x_dim[1]);
  const int img_height = static_cast<int>(in_x_dim[2]);
  const int img_width = static_cast<int>(in_x_dim[3]);
Y
yaokun01 已提交
50 51 52 53 54 55

  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]);

E
eclipsess 已提交
56 57 58
  out->mutable_data<float>({batch_size * output_height * output_width,
                            img_channels * kernels[0] * kernels[1]});
  const std::vector<int> dilations({1, 1});
L
liuruilong 已提交
59
  // TODO(): verify
Y
yaokun01 已提交
60 61
  auto out_dims = out->dims();
  out->Resize({batch_size, out->numel() / batch_size});
Y
yaokun01 已提交
62 63
  for (int i = 0; i < batch_size; i++) {
    const Tensor src =
Y
Yao,kun 已提交
64
        in_x->Slice(i, i + 1).Resize({img_channels, img_height, img_width});
Y
yaokun01 已提交
65
    Tensor dst = out->Slice(i, i + 1).Resize(
Y
Yao,kun 已提交
66
        {output_height, output_width, img_channels, kernels[0], kernels[1]});
Y
yaokun01 已提交
67 68 69
    math::Im2ColFunctor<math::ColFormat::kOCF, CPU, float> f;
    f(src, dilations, strides, paddings, &dst);
  }
Y
yaokun01 已提交
70
  out->Resize(out_dims);
E
eclipsess 已提交
71 72 73 74 75 76 77 78 79
  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 * output_width;
    lod[0].push_back(offset);
  }
  out->set_lod(lod);
Y
yaokun01 已提交
80 81 82 83 84 85
}

template class Im2SequenceKernel<CPU, float>;

}  // namespace operators
}  // namespace paddle_mobile
Y
Yao,kun 已提交
86 87

#endif