|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "CropOp.h" |
| 16 | +#include "paddle/function/TensorShape.h" |
| 17 | +#include "paddle/math/Vector.h" |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | + |
| 21 | +template <> |
| 22 | +void Crop<DEVICE_TYPE_CPU>(real* outputs, |
| 23 | + const real* inputs, |
| 24 | + const TensorShape inShape, |
| 25 | + const TensorShape outShape, |
| 26 | + const FuncConfig& conf) { |
| 27 | + std::vector<uint32_t> crop_corner = |
| 28 | + conf.get<std::vector<uint32_t>>("crop_corner"); |
| 29 | + int cCrop = crop_corner[1]; |
| 30 | + int hCrop = crop_corner[2]; |
| 31 | + int wCrop = crop_corner[3]; |
| 32 | + |
| 33 | + int num = inShape[0]; |
| 34 | + int inC = inShape[1]; |
| 35 | + int inH = inShape[2]; |
| 36 | + int inW = inShape[3]; |
| 37 | + |
| 38 | + int outC = outShape[1]; |
| 39 | + int outH = outShape[2]; |
| 40 | + int outW = outShape[3]; |
| 41 | + |
| 42 | + for (int n = 0; n < num; n++) { |
| 43 | + for (int c = 0; c < outC; c++) { |
| 44 | + for (int h = 0; h < outH; h++) { |
| 45 | + int outoff = ((n * outC + c) * outH + h) * outW; |
| 46 | + int inoff = ((n * inC + c + cCrop) * inH + h + hCrop) * inW + wCrop; |
| 47 | + memcpy(outputs + outoff, inputs + inoff, outW * sizeof(real)); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +template <> |
| 54 | +void CropGrad<DEVICE_TYPE_CPU>(const real* inGrad, |
| 55 | + real* outGrad, |
| 56 | + const TensorShape inShape, |
| 57 | + const TensorShape outShape, |
| 58 | + const FuncConfig& conf) { |
| 59 | + std::vector<uint32_t> crop_corner = |
| 60 | + conf.get<std::vector<uint32_t>>("crop_corner"); |
| 61 | + int cCrop = crop_corner[1]; |
| 62 | + int hCrop = crop_corner[2]; |
| 63 | + int wCrop = crop_corner[3]; |
| 64 | + |
| 65 | + int num = outShape[0]; |
| 66 | + int outC = outShape[1]; |
| 67 | + int outH = outShape[2]; |
| 68 | + int outW = outShape[3]; |
| 69 | + |
| 70 | + int inC = inShape[1]; |
| 71 | + int inH = inShape[2]; |
| 72 | + int inW = inShape[3]; |
| 73 | + |
| 74 | + for (int n = 0; n < num; n++) { |
| 75 | + for (int c = 0; c < inC; c++) { |
| 76 | + for (int h = 0; h < inH; h++) { |
| 77 | + int outoff = ((n * outC + c + cCrop) * outH + h + hCrop) * outW + wCrop; |
| 78 | + int inoff = ((n * inC + c) * inH + h) * inW; |
| 79 | + CpuVector inG = CpuVector(inW, const_cast<real*>(inGrad + inoff)); |
| 80 | + CpuVector outG = CpuVector(inW, outGrad + outoff); |
| 81 | + outG += inG; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * \brief Crop input according to the specify corner and shape. |
| 89 | + * The input and output is a 4D tensor. In CropFunc, we only |
| 90 | + * crop the 2nd to 4th dimension. |
| 91 | + * |
| 92 | + * Argument in this Function: |
| 93 | + * \param pad_ A struct object contains the cropping corner and shape. |
| 94 | + * \param inputs A 4D tensor, only one input. |
| 95 | + * \param outputs A 4D tensor, the output value after cropping. |
| 96 | + * |
| 97 | + * For example, |
| 98 | + * Input(2,2,2,3) = [ |
| 99 | + * [ [[1,2,3], [3,4,5]], |
| 100 | + * [[2,3,5], [1,6,7]] ], |
| 101 | + * [ [[4,3,1], [1,8,7]], |
| 102 | + * [[3,8,9], [2,3,5]] ] |
| 103 | + * ] # the input shape is (2,2,2,3) |
| 104 | + * |
| 105 | + * pad_: if corner = (0,1,1) and crop_shape = (2,1,2) |
| 106 | + * Output(2,2,1,2) = [ |
| 107 | + * [ [[4,5]], |
| 108 | + * [[6,7]] ], |
| 109 | + * [ [[8,7]], |
| 110 | + * [[3,5]] ] |
| 111 | + * ] # the input shape is (2,2,2,3) |
| 112 | + */ |
| 113 | +template <DeviceType Device> |
| 114 | +class CropFunc : public FunctionBase { |
| 115 | +public: |
| 116 | + void init(const FuncConfig& config) override { conf_ = config; } |
| 117 | + |
| 118 | + void calc(const BufferArgs& inputs, const BufferArgs& outputs) override { |
| 119 | + CHECK_EQ(1UL, inputs.size()); |
| 120 | + CHECK_EQ(1UL, outputs.size()); |
| 121 | + CHECK_EQ(outputs[0].getArgType(), ASSIGN_TO); |
| 122 | + |
| 123 | + TensorShape inShape = inputs[0].shape(); |
| 124 | + TensorShape outShape = outputs[0].shape(); |
| 125 | + |
| 126 | + Crop<Device>(outputs[0].data<real>(), |
| 127 | + inputs[0].data<real>(), |
| 128 | + inShape, |
| 129 | + outShape, |
| 130 | + conf_); |
| 131 | + } |
| 132 | + |
| 133 | +private: |
| 134 | + FuncConfig conf_; |
| 135 | +}; |
| 136 | + |
| 137 | +/** |
| 138 | + * \brief The backward propagation of cropping Function. |
| 139 | + * |
| 140 | + * Argument in this Function: |
| 141 | + * \param crop_ The same meaning as it in CropFunc. |
| 142 | + * \param inputs The gradient with respect to the output value of CropFunc. |
| 143 | + * \param outputs The gradient with respect to the input value of CropFunc. |
| 144 | + */ |
| 145 | + |
| 146 | +template <DeviceType Device> |
| 147 | +class CropGradFunc : public FunctionBase { |
| 148 | +public: |
| 149 | + void init(const FuncConfig& config) override { conf_ = config; } |
| 150 | + |
| 151 | + void calc(const BufferArgs& inputs, const BufferArgs& outputs) override { |
| 152 | + CHECK_EQ(1UL, inputs.size()); |
| 153 | + CHECK_EQ(1UL, outputs.size()); |
| 154 | + CHECK_EQ(outputs[0].getArgType(), ADD_TO); |
| 155 | + |
| 156 | + TensorShape outShape = outputs[0].shape(); |
| 157 | + TensorShape inShape = inputs[0].shape(); |
| 158 | + |
| 159 | + CropGrad<Device>(inputs[0].data<real>(), |
| 160 | + outputs[0].data<real>(), |
| 161 | + inShape, |
| 162 | + outShape, |
| 163 | + conf_); |
| 164 | + } |
| 165 | + |
| 166 | +private: |
| 167 | + FuncConfig conf_; |
| 168 | +}; |
| 169 | + |
| 170 | +REGISTER_TYPED_FUNC(Crop, CPU, CropFunc); |
| 171 | +REGISTER_TYPED_FUNC(CropGrad, CPU, CropGradFunc); |
| 172 | +#ifndef PADDLE_ONLY_CPU |
| 173 | +REGISTER_TYPED_FUNC(Crop, GPU, CropFunc); |
| 174 | +REGISTER_TYPED_FUNC(CropGrad, GPU, CropGradFunc); |
| 175 | +#endif |
| 176 | + |
| 177 | +} // namespace paddle |
0 commit comments