Merge pull request #3348 from chengduoZH/develop
Add Conv3DLayer, DeConv3DLayerrevert-3824-remove_grad_op_type
commit
1e6c9926db
@ -0,0 +1,244 @@
|
||||
/* 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. */
|
||||
|
||||
#include "Conv3DLayer.h"
|
||||
#include "paddle/utils/Logging.h"
|
||||
#include "paddle/utils/Stat.h"
|
||||
|
||||
namespace paddle {
|
||||
|
||||
REGISTER_LAYER(conv3d, Conv3DLayer);
|
||||
|
||||
bool Conv3DLayer::init(const LayerMap &layerMap,
|
||||
const ParameterMap ¶meterMap) {
|
||||
if (!ConvBaseLayer::init(layerMap, parameterMap)) return false;
|
||||
int index = 0;
|
||||
for (auto &inputConfig : config_.inputs()) {
|
||||
const ConvConfig &conf = inputConfig.conv_conf();
|
||||
M_.push_back(numFilters_ / conf.groups());
|
||||
K_.push_back(filterPixels_[index] * filterChannels_[index]);
|
||||
|
||||
// create a new weight
|
||||
size_t height, width;
|
||||
width = filterPixels_[index] * filterChannels_[index];
|
||||
height = numFilters_;
|
||||
CHECK_EQ(parameters_[index]->getSize(), width * height);
|
||||
Weight *w = new Weight(height, width, parameters_[index]);
|
||||
weights_.emplace_back(w);
|
||||
++index;
|
||||
}
|
||||
if (biasParameter_.get()) {
|
||||
if (sharedBiases_) {
|
||||
CHECK_EQ((size_t)numFilters_, biasParameter_->getSize());
|
||||
biases_ =
|
||||
std::unique_ptr<Weight>(new Weight(1, numFilters_, biasParameter_));
|
||||
} else {
|
||||
biases_ =
|
||||
std::unique_ptr<Weight>(new Weight(1, getSize(), biasParameter_));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t Conv3DLayer::getSize() {
|
||||
CHECK_NE(inputLayers_.size(), 0UL);
|
||||
outputH_.clear();
|
||||
outputW_.clear();
|
||||
outputD_.clear();
|
||||
N_.clear();
|
||||
size_t layerSize = 0;
|
||||
for (size_t i = 0; i < inputLayers_.size(); ++i) {
|
||||
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_;
|
||||
}
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (nullptr != this->biasParameter_) {
|
||||
REGISTER_TIMER_INFO("FwBiasTimer", getName().c_str());
|
||||
this->addBias();
|
||||
}
|
||||
forwardActivation();
|
||||
}
|
||||
|
||||
void Conv3DLayer::backward(const UpdateCallback &callback) {
|
||||
backwardActivation();
|
||||
|
||||
if (biases_ && biases_->getWGrad()) {
|
||||
bpropBiases();
|
||||
biases_->getParameterPtr()->incUpdate(callback);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i != inputLayers_.size(); ++i) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void Conv3DLayer::bpropWeights(int i) {
|
||||
int M = M_[i];
|
||||
int N = N_[i];
|
||||
int K = K_[i];
|
||||
const MatrixPtr &inMat = getInputValue(i);
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void Conv3DLayer::bpropBiases() {
|
||||
MatrixPtr outGradMat = getOutputGrad();
|
||||
if (this->sharedBiases_) {
|
||||
biases_->getWGrad()->collectSharedBias(*outGradMat, 1.0f);
|
||||
} else {
|
||||
biases_->getWGrad()->collectBias(*outGradMat, 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
void Conv3DLayer::addBias() {
|
||||
MatrixPtr outMat = getOutputValue();
|
||||
if (this->sharedBiases_) {
|
||||
outMat->addSharedBias(*(biases_->getW()), 1.0f);
|
||||
} else {
|
||||
outMat->addBias(*(biases_->getW()), 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace paddle
|
@ -0,0 +1,51 @@
|
||||
/* 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. */
|
||||
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include "ConvBaseLayer.h"
|
||||
#include "paddle/math/MathUtils.h"
|
||||
#include "paddle/math/Matrix.h"
|
||||
|
||||
namespace paddle {
|
||||
|
||||
/**
|
||||
* @brief A subclass of convolution layer.
|
||||
* This layer expands input and use matrix multiplication to
|
||||
* calculate convolution operation.
|
||||
*/
|
||||
class Conv3DLayer : public ConvBaseLayer {
|
||||
public:
|
||||
explicit Conv3DLayer(const LayerConfig& config) : ConvBaseLayer(config) {}
|
||||
~Conv3DLayer() {}
|
||||
|
||||
bool init(const LayerMap& layerMap, const ParameterMap& parameterMap);
|
||||
|
||||
void forward(PassType passType);
|
||||
void addBias();
|
||||
void backward(const UpdateCallback& callback);
|
||||
void bpropBiases();
|
||||
void bpropData(int i);
|
||||
void bpropWeights(int i);
|
||||
size_t getSize();
|
||||
|
||||
protected:
|
||||
// Figure out the dimensions for individual gemms.
|
||||
IntV M_; /// numFilters_ / filter_group_;
|
||||
IntV N_; /// channels_ * filterSizeZ_ * filterSize_ * filterSizeY_
|
||||
IntV K_; /// outputD_ * outputH_ * outputW_
|
||||
MatrixPtr colBuf_;
|
||||
};
|
||||
|
||||
} // namespace paddle
|
@ -0,0 +1,212 @@
|
||||
/* 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. */
|
||||
|
||||
#include "DeConv3DLayer.h"
|
||||
#include "paddle/utils/Logging.h"
|
||||
#include "paddle/utils/Stat.h"
|
||||
|
||||
namespace paddle {
|
||||
|
||||
REGISTER_LAYER(deconv3d, DeConv3DLayer);
|
||||
|
||||
bool DeConv3DLayer::init(const LayerMap &layerMap,
|
||||
const ParameterMap ¶meterMap) {
|
||||
if (!ConvBaseLayer::init(layerMap, parameterMap)) return false;
|
||||
// for Deconv, the dimension of Kernel is
|
||||
// channel * output * depth * height * weigth
|
||||
// Matrix storage format: (output * depth * height * weigth) x channel
|
||||
for (int index = 0; index < config_.inputs().size(); ++index) {
|
||||
M_.push_back(filterChannels_[index]);
|
||||
K_.push_back(filterPixels_[index] * (numFilters_ / groups_[index]));
|
||||
|
||||
// create a new weight
|
||||
size_t height, width;
|
||||
height = filterPixels_[index] * numFilters_;
|
||||
width = filterChannels_[index];
|
||||
CHECK_EQ(parameters_[index]->getSize(), width * height);
|
||||
Weight *w = new Weight(height, width, parameters_[index]);
|
||||
weights_.emplace_back(w);
|
||||
}
|
||||
if (biasParameter_.get()) {
|
||||
if (sharedBiases_) {
|
||||
CHECK_EQ((size_t)numFilters_, biasParameter_->getSize());
|
||||
biases_ =
|
||||
std::unique_ptr<Weight>(new Weight(1, numFilters_, biasParameter_));
|
||||
} else {
|
||||
biases_ =
|
||||
std::unique_ptr<Weight>(new Weight(1, getSize(), biasParameter_));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t DeConv3DLayer::getSize() {
|
||||
CHECK_NE(inputLayers_.size(), 0UL);
|
||||
outputH_.clear();
|
||||
outputW_.clear();
|
||||
outputD_.clear();
|
||||
N_.clear();
|
||||
NOut_.clear();
|
||||
size_t layerSize = 0;
|
||||
for (size_t i = 0; i < inputLayers_.size(); ++i) {
|
||||
outputW_.push_back(
|
||||
imageSize(imgSizeW_[i], filterSize_[i], padding_[i], stride_[i], true));
|
||||
outputH_.push_back(imageSize(
|
||||
imgSizeH_[i], filterSizeY_[i], paddingY_[i], strideY_[i], true));
|
||||
outputD_.push_back(imageSize(
|
||||
imgSizeD_[i], filterSizeZ_[i], paddingZ_[i], strideZ_[i], true));
|
||||
NOut_.push_back(outputD_[i] * outputH_[i] * outputW_[i]);
|
||||
N_.push_back(imgSizeD_[i] * imgSizeH_[i] * imgSizeW_[i]);
|
||||
CHECK(layerSize == 0 || N_[i] * size_t(numFilters_) == layerSize);
|
||||
layerSize += NOut_[i] * numFilters_;
|
||||
}
|
||||
getOutput().setFrameHeight(outputH_[0]);
|
||||
getOutput().setFrameWidth(outputW_[0]);
|
||||
getOutput().setFrameDepth(outputD_[0]);
|
||||
return layerSize;
|
||||
}
|
||||
|
||||
void DeConv3DLayer::forward(PassType passType) {
|
||||
Layer::forward(passType);
|
||||
int batchSize = inputLayers_[0]->getOutputValue()->getHeight();
|
||||
int outWidth = getSize();
|
||||
resetOutput(batchSize, outWidth);
|
||||
const MatrixPtr outMat = getOutputValue();
|
||||
|
||||
for (size_t i = 0; i != inputLayers_.size(); ++i) {
|
||||
REGISTER_TIMER_INFO("FwdDeConv3D", getName().c_str());
|
||||
const MatrixPtr &inMat = getInputValue(i);
|
||||
int M = M_[i];
|
||||
int N = N_[i];
|
||||
int K = K_[i];
|
||||
MatrixPtr wMat = weights_[i]->getW();
|
||||
Matrix::resizeOrCreate(colBuf_, K * groups_[i], N, false, useGpu_);
|
||||
for (int n = 0; n < batchSize; ++n) {
|
||||
real *inData = inMat->getData() + n * inMat->getStride();
|
||||
for (int g = 0; g < groups_[i]; ++g) {
|
||||
MatrixPtr inMatSub = Matrix::create(inData, M, N, false, useGpu_);
|
||||
MatrixPtr wMatSub = wMat->subMatrix(g * K, K);
|
||||
MatrixPtr colBufDataSub = colBuf_->subMatrix(g * K, K);
|
||||
colBufDataSub->mul(*wMatSub, *inMatSub, 1.0, 0.0);
|
||||
inData += M * N;
|
||||
}
|
||||
colBuf_->col2Vol(outMat->getData() + n * outMat->getStride(),
|
||||
numFilters_,
|
||||
outputD_[i],
|
||||
outputH_[i],
|
||||
outputW_[i],
|
||||
filterSizeZ_[i],
|
||||
filterSizeY_[i],
|
||||
filterSize_[i],
|
||||
strideZ_[i],
|
||||
strideY_[i],
|
||||
stride_[i],
|
||||
paddingZ_[i],
|
||||
paddingY_[i],
|
||||
padding_[i],
|
||||
1.0,
|
||||
1.0);
|
||||
}
|
||||
}
|
||||
if (nullptr != this->biasParameter_) {
|
||||
REGISTER_TIMER_INFO("FwBiasTimer", getName().c_str());
|
||||
this->addBias();
|
||||
}
|
||||
forwardActivation();
|
||||
}
|
||||
|
||||
void DeConv3DLayer::backward(const UpdateCallback &callback) {
|
||||
backwardActivation();
|
||||
int batchSize = getOutputGrad()->getHeight();
|
||||
if (biases_ && biases_->getWGrad()) {
|
||||
bpropBiases();
|
||||
biases_->getParameterPtr()->incUpdate(callback);
|
||||
}
|
||||
for (size_t i = 0; i < inputLayers_.size(); ++i) {
|
||||
if (weights_[i]->getWGrad() || this->needGradient_) {
|
||||
int M = M_[i];
|
||||
int N = N_[i];
|
||||
int K = K_[i];
|
||||
REGISTER_TIMER_INFO("BwdDeConv3D", getName().c_str());
|
||||
Matrix::resizeOrCreate(colBuf_, K * groups_[i], N, false, useGpu_);
|
||||
const MatrixPtr &inMat = getInputValue(i);
|
||||
for (int n = 0; n < batchSize; ++n) {
|
||||
colBuf_->vol2Col(
|
||||
getOutputGrad()->getData() + n * getOutputGrad()->getStride(),
|
||||
numFilters_,
|
||||
outputD_[i],
|
||||
outputH_[i],
|
||||
outputW_[i],
|
||||
filterSizeZ_[i],
|
||||
filterSizeY_[i],
|
||||
filterSize_[i],
|
||||
strideZ_[i],
|
||||
strideY_[i],
|
||||
stride_[i],
|
||||
paddingZ_[i],
|
||||
paddingY_[i],
|
||||
padding_[i]);
|
||||
if (weights_[i]->getWGrad()) {
|
||||
real *inData = inMat->getData() + n * inMat->getStride();
|
||||
for (int g = 0; g < groups_[i]; ++g) {
|
||||
MatrixPtr colBufDataSub = colBuf_->subMatrix(g * K, K);
|
||||
MatrixPtr wGradMatSub =
|
||||
weights_[i]->getWGrad()->subMatrix(g * K, K);
|
||||
MatrixPtr inMatSub = Matrix::create(inData, M, N, false, useGpu_);
|
||||
wGradMatSub->mul(
|
||||
*colBufDataSub, *(inMatSub->getTranspose()), 1.0, 1.0);
|
||||
inData += M * N;
|
||||
}
|
||||
}
|
||||
if (getInputGrad(i)) {
|
||||
real *preGrad =
|
||||
getInputGrad(i)->getData() + n * getInputGrad(i)->getStride();
|
||||
for (int g = 0; g < groups_[i]; ++g) {
|
||||
MatrixPtr w = weights_[i]->getW()->subMatrix(g * K, K);
|
||||
MatrixPtr outGradMat = colBuf_->subMatrix(g * K, K);
|
||||
MatrixPtr inGradMatSub =
|
||||
Matrix::create(preGrad, M, N, false, useGpu_);
|
||||
inGradMatSub->mul(*(w->getTranspose()), *outGradMat, 1.0, 1.0);
|
||||
preGrad += M * N;
|
||||
}
|
||||
}
|
||||
}
|
||||
REGISTER_TIMER_INFO("WeightUpdate", getName().c_str());
|
||||
weights_[i]->getParameterPtr()->incUpdate(callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
void DeConv3DLayer::bpropWeights(int i) {}
|
||||
void DeConv3DLayer::bpropData(int i) {}
|
||||
|
||||
void DeConv3DLayer::bpropBiases() {
|
||||
const MatrixPtr &outGradMat = getOutputGrad();
|
||||
|
||||
if (this->sharedBiases_) {
|
||||
biases_->getWGrad()->collectSharedBias(*outGradMat, 1.0f);
|
||||
} else {
|
||||
biases_->getWGrad()->collectBias(*outGradMat, 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
void DeConv3DLayer::addBias() {
|
||||
MatrixPtr outMat = getOutputValue();
|
||||
if (this->sharedBiases_) {
|
||||
outMat->addSharedBias(*(biases_->getW()), 1.0f);
|
||||
} else {
|
||||
outMat->addBias(*(biases_->getW()), 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace paddle
|
@ -0,0 +1,52 @@
|
||||
/* 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. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "ConvBaseLayer.h"
|
||||
#include "paddle/math/MathUtils.h"
|
||||
#include "paddle/math/Matrix.h"
|
||||
|
||||
namespace paddle {
|
||||
|
||||
/**
|
||||
* @brief A subclass of deconvolution3D layer.
|
||||
* This layer expands input and use matrix multiplication to
|
||||
* calculate deconvolution3D operation.
|
||||
*/
|
||||
class DeConv3DLayer : public ConvBaseLayer {
|
||||
public:
|
||||
explicit DeConv3DLayer(const LayerConfig& config) : ConvBaseLayer(config) {}
|
||||
~DeConv3DLayer() {}
|
||||
bool init(const LayerMap& layerMap, const ParameterMap& parameterMap);
|
||||
|
||||
void forward(PassType passType);
|
||||
void addBias();
|
||||
void backward(const UpdateCallback& callback);
|
||||
void bpropBiases();
|
||||
void bpropData(int i);
|
||||
void bpropWeights(int i);
|
||||
size_t getSize();
|
||||
|
||||
protected:
|
||||
// Figure out the dimensions for individual gemms.
|
||||
IntV M_; /// numFilters_ / filter_group_;
|
||||
IntV N_; /// channels_ * filterSizeZ_ * filterSize_ * filterSizeY_
|
||||
IntV K_; /// outputD_ * outputH_ * outputW_
|
||||
IntV NOut_;
|
||||
MatrixPtr colBuf_;
|
||||
};
|
||||
|
||||
} // namespace paddle
|
@ -0,0 +1,132 @@
|
||||
type: "nn"
|
||||
layers {
|
||||
name: "data"
|
||||
type: "data"
|
||||
size: 36288
|
||||
active_type: ""
|
||||
height: 48
|
||||
width: 42
|
||||
depth: 6
|
||||
}
|
||||
layers {
|
||||
name: "conv3d_1"
|
||||
type: "conv3d"
|
||||
size: 24192
|
||||
active_type: ""
|
||||
inputs {
|
||||
input_layer_name: "data"
|
||||
input_parameter_name: "_conv3d_1.w0"
|
||||
conv_conf {
|
||||
filter_size: 3
|
||||
channels: 3
|
||||
stride: 2
|
||||
padding: 1
|
||||
groups: 1
|
||||
filter_channels: 3
|
||||
output_x: 21
|
||||
img_size: 42
|
||||
caffe_mode: true
|
||||
filter_size_y: 3
|
||||
padding_y: 1
|
||||
stride_y: 2
|
||||
output_y: 24
|
||||
img_size_y: 48
|
||||
filter_size_z: 3
|
||||
padding_z: 1
|
||||
stride_z: 2
|
||||
output_z: 3
|
||||
img_size_z: 6
|
||||
}
|
||||
}
|
||||
bias_parameter_name: "_conv3d_1.wbias"
|
||||
num_filters: 16
|
||||
shared_biases: true
|
||||
height: 24
|
||||
width: 21
|
||||
depth: 3
|
||||
}
|
||||
layers {
|
||||
name: "conv3d_2"
|
||||
type: "conv3d"
|
||||
size: 24192
|
||||
active_type: ""
|
||||
inputs {
|
||||
input_layer_name: "data"
|
||||
input_parameter_name: "_conv3d_2.w0"
|
||||
conv_conf {
|
||||
filter_size: 3
|
||||
channels: 3
|
||||
stride: 2
|
||||
padding: 1
|
||||
groups: 1
|
||||
filter_channels: 3
|
||||
output_x: 21
|
||||
img_size: 42
|
||||
caffe_mode: true
|
||||
filter_size_y: 3
|
||||
padding_y: 1
|
||||
stride_y: 2
|
||||
output_y: 24
|
||||
img_size_y: 48
|
||||
filter_size_z: 3
|
||||
padding_z: 1
|
||||
stride_z: 2
|
||||
output_z: 3
|
||||
img_size_z: 6
|
||||
}
|
||||
}
|
||||
bias_parameter_name: "_conv3d_2.wbias"
|
||||
num_filters: 16
|
||||
shared_biases: true
|
||||
height: 24
|
||||
width: 21
|
||||
depth: 3
|
||||
}
|
||||
parameters {
|
||||
name: "_conv3d_1.w0"
|
||||
size: 1296
|
||||
initial_mean: 0.0
|
||||
initial_std: 0.272165526976
|
||||
initial_strategy: 0
|
||||
initial_smart: false
|
||||
}
|
||||
parameters {
|
||||
name: "_conv3d_1.wbias"
|
||||
size: 16
|
||||
initial_mean: 0.0
|
||||
initial_std: 0.0
|
||||
dims: 16
|
||||
dims: 1
|
||||
initial_strategy: 0
|
||||
initial_smart: false
|
||||
}
|
||||
parameters {
|
||||
name: "_conv3d_2.w0"
|
||||
size: 1296
|
||||
initial_mean: 0.0
|
||||
initial_std: 0.272165526976
|
||||
initial_strategy: 0
|
||||
initial_smart: false
|
||||
}
|
||||
parameters {
|
||||
name: "_conv3d_2.wbias"
|
||||
size: 16
|
||||
initial_mean: 0.0
|
||||
initial_std: 0.0
|
||||
dims: 16
|
||||
dims: 1
|
||||
initial_strategy: 0
|
||||
initial_smart: false
|
||||
}
|
||||
input_layer_names: "data"
|
||||
output_layer_names: "conv3d_2"
|
||||
sub_models {
|
||||
name: "root"
|
||||
layer_names: "data"
|
||||
layer_names: "conv3d_1"
|
||||
layer_names: "conv3d_2"
|
||||
input_layer_names: "data"
|
||||
output_layer_names: "conv3d_2"
|
||||
is_recurrent_layer_group: false
|
||||
}
|
||||
|
@ -0,0 +1,132 @@
|
||||
type: "nn"
|
||||
layers {
|
||||
name: "data"
|
||||
type: "data"
|
||||
size: 36288
|
||||
active_type: ""
|
||||
height: 48
|
||||
width: 42
|
||||
depth: 6
|
||||
}
|
||||
layers {
|
||||
name: "deconv3d_1"
|
||||
type: "deconv3d"
|
||||
size: 1387760
|
||||
active_type: ""
|
||||
inputs {
|
||||
input_layer_name: "data"
|
||||
input_parameter_name: "_deconv3d_1.w0"
|
||||
conv_conf {
|
||||
filter_size: 3
|
||||
channels: 3
|
||||
stride: 2
|
||||
padding: 1
|
||||
groups: 1
|
||||
filter_channels: 16
|
||||
output_x: 42
|
||||
img_size: 83
|
||||
caffe_mode: true
|
||||
filter_size_y: 3
|
||||
padding_y: 1
|
||||
stride_y: 2
|
||||
output_y: 48
|
||||
img_size_y: 95
|
||||
filter_size_z: 3
|
||||
padding_z: 1
|
||||
stride_z: 2
|
||||
output_z: 6
|
||||
img_size_z: 11
|
||||
}
|
||||
}
|
||||
bias_parameter_name: "_deconv3d_1.wbias"
|
||||
num_filters: 16
|
||||
shared_biases: true
|
||||
height: 95
|
||||
width: 83
|
||||
depth: 11
|
||||
}
|
||||
layers {
|
||||
name: "deconv3d_2"
|
||||
type: "deconv3d"
|
||||
size: 1387760
|
||||
active_type: ""
|
||||
inputs {
|
||||
input_layer_name: "data"
|
||||
input_parameter_name: "_deconv3d_2.w0"
|
||||
conv_conf {
|
||||
filter_size: 3
|
||||
channels: 3
|
||||
stride: 2
|
||||
padding: 1
|
||||
groups: 1
|
||||
filter_channels: 16
|
||||
output_x: 42
|
||||
img_size: 83
|
||||
caffe_mode: true
|
||||
filter_size_y: 3
|
||||
padding_y: 1
|
||||
stride_y: 2
|
||||
output_y: 48
|
||||
img_size_y: 95
|
||||
filter_size_z: 3
|
||||
padding_z: 1
|
||||
stride_z: 2
|
||||
output_z: 6
|
||||
img_size_z: 11
|
||||
}
|
||||
}
|
||||
bias_parameter_name: "_deconv3d_2.wbias"
|
||||
num_filters: 16
|
||||
shared_biases: true
|
||||
height: 95
|
||||
width: 83
|
||||
depth: 11
|
||||
}
|
||||
parameters {
|
||||
name: "_deconv3d_1.w0"
|
||||
size: 6912
|
||||
initial_mean: 0.0
|
||||
initial_std: 0.272165526976
|
||||
initial_strategy: 0
|
||||
initial_smart: false
|
||||
}
|
||||
parameters {
|
||||
name: "_deconv3d_1.wbias"
|
||||
size: 16
|
||||
initial_mean: 0.0
|
||||
initial_std: 0.0
|
||||
dims: 16
|
||||
dims: 1
|
||||
initial_strategy: 0
|
||||
initial_smart: false
|
||||
}
|
||||
parameters {
|
||||
name: "_deconv3d_2.w0"
|
||||
size: 6912
|
||||
initial_mean: 0.0
|
||||
initial_std: 0.272165526976
|
||||
initial_strategy: 0
|
||||
initial_smart: false
|
||||
}
|
||||
parameters {
|
||||
name: "_deconv3d_2.wbias"
|
||||
size: 16
|
||||
initial_mean: 0.0
|
||||
initial_std: 0.0
|
||||
dims: 16
|
||||
dims: 1
|
||||
initial_strategy: 0
|
||||
initial_smart: false
|
||||
}
|
||||
input_layer_names: "data"
|
||||
output_layer_names: "deconv3d_2"
|
||||
sub_models {
|
||||
name: "root"
|
||||
layer_names: "data"
|
||||
layer_names: "deconv3d_1"
|
||||
layer_names: "deconv3d_2"
|
||||
input_layer_names: "data"
|
||||
output_layer_names: "deconv3d_2"
|
||||
is_recurrent_layer_group: false
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue