|
| 1 | +/* Copyright (c) 2016 Baidu, Inc. 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 "RotateLayer.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | + |
| 19 | +REGISTER_LAYER(rotate, RotateLayer); |
| 20 | + |
| 21 | +bool RotateLayer::init(const LayerMap& layerMap, |
| 22 | + const ParameterMap& parameterMap) { |
| 23 | + Layer::init(layerMap, parameterMap); |
| 24 | + |
| 25 | + CHECK_EQ(inputLayers_.size(), 1UL); |
| 26 | + height_ = config_.height(); |
| 27 | + width_ = config_.width(); |
| 28 | + CHECK_GT(height_, 0); |
| 29 | + CHECK_GT(width_, 0); |
| 30 | + return true; |
| 31 | +} |
| 32 | + |
| 33 | +void RotateLayer::forward(PassType passType) { |
| 34 | + Layer::forward(passType); |
| 35 | + |
| 36 | + MatrixPtr input = getInputValue(0); |
| 37 | + batchSize_ = input->getHeight(); |
| 38 | + size_ = input->getWidth(); |
| 39 | + CHECK_GE(size_, height_ * width_); |
| 40 | + CHECK_EQ(size_ % (height_ * width_), 0) |
| 41 | + << "total size_ is not dividable by (height_ * width_), i.e., " |
| 42 | + << "channel number should be an integer"; |
| 43 | + channels_ = size_ / (height_ * width_); |
| 44 | + |
| 45 | + resizeOutput(batchSize_, size_); |
| 46 | + |
| 47 | + MatrixPtr outV = getOutputValue(); |
| 48 | + for (int b = 0; b < batchSize_; b++) { // for each input feat map |
| 49 | + for (int c = 0; c < channels_; c++) { // for each feat channel |
| 50 | + MatrixPtr inputSample = |
| 51 | + Matrix::create(input->getData() + b * size_ + c * height_ * width_, |
| 52 | + height_, |
| 53 | + width_, |
| 54 | + false, |
| 55 | + useGpu_); |
| 56 | + MatrixPtr outputSample = |
| 57 | + Matrix::create(outV->getData() + b * size_ + c * height_ * width_, |
| 58 | + width_, |
| 59 | + height_, |
| 60 | + false, |
| 61 | + useGpu_); |
| 62 | + inputSample->rotate(outputSample, false, true /* clock-wise */); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (getInputGrad(0)) { |
| 67 | + zeroGrad(); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +void RotateLayer::backward(const UpdateCallback& callback) { |
| 72 | + (void)callback; |
| 73 | + |
| 74 | + MatrixPtr outputGrad = getOutputGrad(); |
| 75 | + if (outputGrad == NULL) { |
| 76 | + return; |
| 77 | + } |
| 78 | + // the grad should be rotated in the reverse direction |
| 79 | + MatrixPtr preGrad = getInputGrad(0); |
| 80 | + |
| 81 | + for (int b = 0; b < batchSize_; b++) { // for each input feat map |
| 82 | + for (int c = 0; c < channels_; c++) { // for each feat channel |
| 83 | + MatrixPtr inputSampleGrad = |
| 84 | + Matrix::create(preGrad->getData() + b * size_ + c * height_ * width_, |
| 85 | + height_, |
| 86 | + width_, |
| 87 | + false, |
| 88 | + useGpu_); |
| 89 | + MatrixPtr outputSampleGrad = Matrix::create( |
| 90 | + outputGrad->getData() + b * size_ + c * height_ * width_, |
| 91 | + width_, |
| 92 | + height_, |
| 93 | + false, |
| 94 | + useGpu_); |
| 95 | + MatrixPtr tmpGrad = nullptr; |
| 96 | + outputSampleGrad->rotate(tmpGrad, true, false /* anti clock-wise */); |
| 97 | + inputSampleGrad->add(*tmpGrad); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +} // namespace paddle |
0 commit comments