Conv3DLayer.cpp 8.2 KB
Newer Older
C
chengduoZH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

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

C
chengduoZH 已提交
15
#include "Conv3DLayer.h"
C
chengduoZH 已提交
16 17 18 19 20 21 22 23 24
#include "paddle/utils/Logging.h"
#include "paddle/utils/Stat.h"

namespace paddle {

REGISTER_LAYER(conv3d, Conv3DLayer);

bool Conv3DLayer::init(const LayerMap &layerMap,
                       const ParameterMap &parameterMap) {
C
chengduoZH 已提交
25
  if (!ConvBaseLayer::init(layerMap, parameterMap)) return false;
C
chengduoZH 已提交
26 27
  int index = 0;
  for (auto &inputConfig : config_.inputs()) {
C
chengduoZH 已提交
28 29 30 31 32 33 34
    const ConvConfig &conf = inputConfig.conv_conf();
    M_.push_back(numFilters_ / conf.groups());
    K_.push_back(filterPixels_[index] * filterChannels_[index]);
    if (nullptr != weights_[index]->getW())
      weights_[index]->getW()->reshape(weights_[index]->getW()->getWidth(),
                                       weights_[index]->getW()->getHeight());
    if (nullptr != weights_[index]->getWGrad())
C
chengduoZH 已提交
35
      weights_[index]->getWGrad()->reshape(
C
chengduoZH 已提交
36 37 38
          weights_[index]->getWGrad()->getWidth(),
          weights_[index]->getWGrad()->getHeight());
    ++index;
C
chengduoZH 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  }
  CHECK(inputLayers_.size() == parameters_.size());
  return true;
}

size_t Conv3DLayer::getSize() {
  CHECK_NE(inputLayers_.size(), 0UL);
  // imgSizeH_.clear();
  // imgSizeW_.clear();
  // imgSizeD_.clear();
  outputH_.clear();
  outputW_.clear();
  outputD_.clear();
  N_.clear();
  size_t layerSize = 0;
  for (size_t i = 0; i < inputLayers_.size(); ++i) {
C
chengduoZH 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67
    // imgSizeH_.push_back(inputLayers_[i]->getOutput().getFrameHeight());
    // imgSizeW_.push_back(inputLayers_[i]->getOutput().getFrameWidth());
    // imgSizeD_.push_back(inputLayers_[i]->getOutput().getFrameDepth());
    outputW_.push_back(outputSize(
        imgSizeW_[i], filterSize_[i], padding_[i], stride_[i], true));
    outputH_.push_back(outputSize(
        imgSizeH_[i], filterSizeY_[i], paddingY_[i], strideY_[i], true));
    outputD_.push_back(outputSize(
        imgSizeD_[i], filterSizeZ_[i], paddingZ_[i], strideZ_[i], true));

    N_.push_back(outputD_[i] * outputH_[i] * outputW_[i]);
    CHECK(layerSize == 0 || N_[i] * size_t(numFilters_) == layerSize);
    layerSize += N_[i] * numFilters_;
C
chengduoZH 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  }
  getOutput().setFrameHeight(outputH_[0]);
  getOutput().setFrameWidth(outputW_[0]);
  getOutput().setFrameDepth(outputD_[0]);
  return layerSize;
}

void Conv3DLayer::forward(PassType passType) {
  Layer::forward(passType);

  int batchSize = inputLayers_[0]->getOutputValue()->getHeight();
  int outWidth = getSize();
  resetOutput(batchSize, outWidth);

  for (size_t i = 0; i != inputLayers_.size(); ++i) {
C
chengduoZH 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    REGISTER_TIMER_INFO("FwdConv3D", getName().c_str());
    const MatrixPtr &inMat = getInputValue(i);
    const MatrixPtr &outMat = getOutputValue();
    int M = M_[i];
    int N = N_[i];
    int K = K_[i];
    Matrix::resizeOrCreate(colBuf_, K * groups_[i], N, false, useGpu_);
    MatrixPtr wMat = weights_[i]->getW();
    for (int n = 0; n < batchSize; ++n) {
      colBuf_->vol2Col(inMat->getData() + n * inMat->getStride(),
                       channels_[i],
                       imgSizeD_[i],
                       imgSizeH_[i],
                       imgSizeW_[i],
                       filterSizeZ_[i],
                       filterSizeY_[i],
                       filterSize_[i],
                       strideZ_[i],
                       strideY_[i],
                       stride_[i],
                       paddingZ_[i],
                       paddingY_[i],
                       padding_[i]);

      real *outData = outMat->getData() + n * outMat->getStride();
      MatrixPtr outMatSub =
          Matrix::create(outData, groups_[i] * M, N, false, useGpu_);
      for (int g = 0; g < groups_[i]; g++) {
        MatrixPtr wMatSub = wMat->subMatrix(g * M, M);
        MatrixPtr in = colBuf_->subMatrix(g * K, K);
        MatrixPtr out = outMatSub->subMatrix(g * M, M);
        out->mul(*wMatSub, *in, 1.0, 1.0);
C
chengduoZH 已提交
115
      }
C
chengduoZH 已提交
116
    }
C
chengduoZH 已提交
117 118
  }
  if (nullptr != this->biasParameter_) {
C
chengduoZH 已提交
119 120
    REGISTER_TIMER_INFO("FwBiasTimer", getName().c_str());
    this->addBias();
C
chengduoZH 已提交
121 122 123 124 125 126 127 128
  }
  forwardActivation();
}

void Conv3DLayer::backward(const UpdateCallback &callback) {
  backwardActivation();

  if (biases_ && biases_->getWGrad()) {
C
chengduoZH 已提交
129 130
    bpropBiases();
    biases_->getParameterPtr()->incUpdate(callback);
C
chengduoZH 已提交
131 132 133
  }

  for (size_t i = 0; i != inputLayers_.size(); ++i) {
C
chengduoZH 已提交
134 135 136 137 138 139 140 141 142
    REGISTER_TIMER_INFO("BwdConv3D", getName().c_str());
    if (weights_[i]->getWGrad()) {
      bpropWeights(i);
    }
    if (getInputGrad(i)) {
      bpropData(i);
    }
    REGISTER_TIMER_INFO("WeightUpdate", getName().c_str());
    weights_[i]->getParameterPtr()->incUpdate(callback);
C
chengduoZH 已提交
143 144 145 146 147 148 149
  }
}

void Conv3DLayer::bpropWeights(int i) {
  int M = M_[i];
  int N = N_[i];
  int K = K_[i];
C
chengduoZH 已提交
150
  const MatrixPtr &inMat = getInputValue(i);
C
chengduoZH 已提交
151 152 153 154
  Matrix::resizeOrCreate(colBuf_, K * groups_[i], N, false, useGpu_);
  MatrixPtr wGradMat = weights_[i]->getWGrad();
  int batchSize = inputLayers_[0]->getOutputValue()->getHeight();
  for (int n = 0; n < batchSize; ++n) {
C
chengduoZH 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    colBuf_->vol2Col(inMat->getData() + n * inMat->getStride(),
                     channels_[i],
                     imgSizeD_[i],
                     imgSizeH_[i],
                     imgSizeW_[i],
                     filterSizeZ_[i],
                     filterSizeY_[i],
                     filterSize_[i],
                     strideZ_[i],
                     strideY_[i],
                     stride_[i],
                     paddingZ_[i],
                     paddingY_[i],
                     padding_[i]);

    real *outGradData =
        getOutputGrad()->getData() + n * getOutputGrad()->getStride();
    MatrixPtr outGradSub =
        Matrix::create(outGradData, groups_[i] * M, N, false, useGpu_);
    for (int g = 0; g < groups_[i]; ++g) {
      MatrixPtr inMatSub = colBuf_->subMatrix(g * K, K);
      MatrixPtr outG = outGradSub->subMatrix(g * M, M);
      MatrixPtr wGradSub = wGradMat->subMatrix(g * M, M);
      wGradSub->mul(*outG, *(inMatSub->getTranspose()), 1.0, 1.0);
    }
C
chengduoZH 已提交
180 181 182 183 184 185 186 187 188 189 190
  }
}

void Conv3DLayer::bpropData(int i) {
  int M = M_[i];
  int N = N_[i];
  int K = K_[i];
  Matrix::resizeOrCreate(colBuf_, K * groups_[i], N, false, useGpu_);
  MatrixPtr wMat = weights_[i]->getW();
  int batchSize = inputLayers_[0]->getOutputValue()->getHeight();
  for (int n = 0; n < batchSize; ++n) {
C
chengduoZH 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    real *outGradData =
        getOutputGrad()->getData() + n * getOutputGrad()->getStride();
    real *preGradData =
        getInputGrad(i)->getData() + n * getInputGrad(i)->getStride();
    MatrixPtr outGradSub =
        Matrix::create(outGradData, M * groups_[i], N, false, useGpu_);
    for (int g = 0; g < groups_[i]; ++g) {
      MatrixPtr wMatSub = wMat->subMatrix(g * M, M);
      MatrixPtr outG = outGradSub->subMatrix(g * M, M);
      MatrixPtr inGradMatSub = colBuf_->subMatrix(g * K, K);
      inGradMatSub->mul(*(wMatSub->getTranspose()), *outG, 1.0, 0.0);
    }
    colBuf_->col2Vol(preGradData,
                     channels_[i],
                     imgSizeD_[i],
                     imgSizeH_[i],
                     imgSizeW_[i],
                     filterSizeZ_[i],
                     filterSizeY_[i],
                     filterSize_[i],
                     strideZ_[i],
                     strideY_[i],
                     stride_[i],
                     paddingZ_[i],
                     paddingY_[i],
                     padding_[i],
                     1.0,
                     1.0);
C
chengduoZH 已提交
219 220 221 222 223 224
  }
}

void Conv3DLayer::bpropBiases() {
  MatrixPtr outGradMat = getOutputGrad();
  if (this->sharedBiases_) {
C
chengduoZH 已提交
225
    biases_->getWGrad()->collectSharedBias(*outGradMat, 1.0f);
C
chengduoZH 已提交
226
  } else {
C
chengduoZH 已提交
227
    biases_->getWGrad()->collectBias(*outGradMat, 1.0f);
C
chengduoZH 已提交
228 229 230 231 232 233
  }
}

void Conv3DLayer::addBias() {
  MatrixPtr outMat = getOutputValue();
  if (this->sharedBiases_) {
C
chengduoZH 已提交
234
    outMat->addSharedBias(*(biases_->getW()), 1.0f);
C
chengduoZH 已提交
235
  } else {
C
chengduoZH 已提交
236
    outMat->addBias(*(biases_->getW()), 1.0f);
C
chengduoZH 已提交
237 238 239 240
  }
}

}  // namespace paddle