avx_docs
commit
ae7452f47b
@ -0,0 +1,123 @@
|
||||
/* 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 "PoolProjection.h"
|
||||
|
||||
namespace paddle {
|
||||
|
||||
REGISTER_PROJECTION_CREATE_FUNC(pool, &PoolProjection::create);
|
||||
|
||||
PoolProjection::PoolProjection(const ProjectionConfig& config,
|
||||
ParameterPtr parameter, bool useGpu)
|
||||
: Projection(config, parameter, useGpu) {
|
||||
const PoolConfig& conf = config_.pool_conf();
|
||||
poolType_ = conf.pool_type();
|
||||
channels_ = conf.channels();
|
||||
sizeX_ = conf.size_x();
|
||||
stride_ = conf.stride();
|
||||
outputX_ = conf.output_x();
|
||||
imgSize_ = conf.img_size();
|
||||
confPadding_ = conf.padding();
|
||||
|
||||
sizeY_ = conf.has_size_y() ? conf.size_y() : conf.size_x();
|
||||
imgSizeY_ = conf.has_img_size_y() ? conf.img_size_y() : conf.img_size();
|
||||
strideY_ = conf.has_stride_y() ? conf.stride_y() : conf.stride();
|
||||
confPaddingY_ = conf.has_padding_y() ? conf.padding_y() : conf.padding();
|
||||
outputY_ = conf.has_output_y() ? conf.output_y() : conf.output_x();
|
||||
}
|
||||
|
||||
size_t PoolProjection::getSize() {
|
||||
imgSizeY_ = in_->getFrameHeight();
|
||||
imgSize_ = in_->getFrameWidth();
|
||||
const PoolConfig& conf = config_.pool_conf();
|
||||
if (imgSizeY_ == 0) {
|
||||
imgSizeY_ = conf.has_img_size_y() ? conf.img_size_y() : conf.img_size();
|
||||
}
|
||||
if (imgSize_ == 0) {
|
||||
imgSize_ = conf.img_size();
|
||||
}
|
||||
outputY_ = outputSize(imgSizeY_, sizeY_, confPaddingY_, strideY_,
|
||||
/* caffeMode */ false);
|
||||
outputX_ = outputSize(imgSize_, sizeX_, confPadding_, stride_,
|
||||
/* caffeMode */ false);
|
||||
|
||||
const_cast<Argument*>(out_)->setFrameHeight(outputY_);
|
||||
const_cast<Argument*>(out_)->setFrameWidth(outputX_);
|
||||
|
||||
return outputY_ * outputX_ * channels_;
|
||||
}
|
||||
|
||||
PoolProjection* PoolProjection::create(const ProjectionConfig& config,
|
||||
ParameterPtr parameter, bool useGpu) {
|
||||
const std::string& pool = config.pool_conf().pool_type();
|
||||
if (pool == "max-projection") {
|
||||
return new MaxPoolProjection(config, parameter, useGpu);
|
||||
} else if (pool == "avg-projection") {
|
||||
return new AvgPoolProjection(config, parameter, useGpu);
|
||||
} else {
|
||||
LOG(FATAL) << "Unknown pool type: " << pool;
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void MaxPoolProjection::forward() {
|
||||
size_t width = getSize();
|
||||
CHECK_EQ(width, out_->value->getWidth());
|
||||
MatrixPtr inputV = in_->value;
|
||||
MatrixPtr outV = out_->value;
|
||||
outV->maxPoolForward(*inputV, imgSizeY_, imgSize_, channels_, sizeX_, sizeY_,
|
||||
strideY_, stride_, outputY_, outputX_, confPaddingY_,
|
||||
confPadding_);
|
||||
}
|
||||
|
||||
void MaxPoolProjection::backward(const UpdateCallback& callback) {
|
||||
(void)callback;
|
||||
MatrixPtr outGrad = out_->grad;
|
||||
MatrixPtr inputV = in_->value;
|
||||
MatrixPtr outV = out_->value;
|
||||
MatrixPtr inputGrad = in_->grad;
|
||||
|
||||
if (NULL == inputGrad) {
|
||||
return;
|
||||
}
|
||||
inputGrad->maxPoolBackward(*inputV, imgSizeY_, imgSize_, *outGrad, *outV,
|
||||
sizeX_, sizeY_, strideY_, stride_, outputY_,
|
||||
outputX_, 1, 1, confPaddingY_, confPadding_);
|
||||
}
|
||||
|
||||
void AvgPoolProjection::forward() {
|
||||
size_t width = getSize();
|
||||
CHECK_EQ(width, out_->value->getWidth());
|
||||
MatrixPtr inputV = in_->value;
|
||||
MatrixPtr outV = out_->value;
|
||||
outV->avgPoolForward(*inputV, imgSizeY_, imgSize_, channels_, sizeX_, sizeY_,
|
||||
strideY_, stride_, outputY_, outputX_, confPaddingY_,
|
||||
confPadding_);
|
||||
}
|
||||
|
||||
void AvgPoolProjection::backward(const UpdateCallback& callback) {
|
||||
(void)callback;
|
||||
|
||||
MatrixPtr outputGrad = out_->grad;
|
||||
MatrixPtr inputGrad = in_->grad;
|
||||
|
||||
if (NULL == inputGrad) {
|
||||
return;
|
||||
}
|
||||
|
||||
inputGrad->avgPoolBackward(*outputGrad, imgSizeY_, imgSize_, sizeX_, sizeY_,
|
||||
strideY_, stride_, outputY_, outputX_, 1, 1,
|
||||
confPaddingY_, confPadding_);
|
||||
}
|
||||
} // namespace paddle
|
@ -0,0 +1,63 @@
|
||||
/* 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 "Projection.h"
|
||||
#include "paddle/math/MathUtils.h"
|
||||
|
||||
namespace paddle {
|
||||
|
||||
class PoolProjection : public Projection {
|
||||
protected:
|
||||
size_t imgSizeY_, imgSize_;
|
||||
size_t outputY_, outputX_;
|
||||
size_t strideY_, stride_;
|
||||
size_t sizeY_, sizeX_;
|
||||
int confPaddingY_, confPadding_;
|
||||
size_t channels_;
|
||||
std::string poolType_;
|
||||
|
||||
public:
|
||||
PoolProjection(const ProjectionConfig& config, ParameterPtr parameter,
|
||||
bool useGpu);
|
||||
|
||||
static PoolProjection* create(const ProjectionConfig& config,
|
||||
ParameterPtr parameter, bool useGpu);
|
||||
|
||||
const std::string& getPoolType() const { return poolType_; }
|
||||
|
||||
size_t getSize();
|
||||
};
|
||||
|
||||
class MaxPoolProjection : public PoolProjection {
|
||||
public:
|
||||
MaxPoolProjection(const ProjectionConfig& config, ParameterPtr parameter,
|
||||
bool useGpu)
|
||||
: PoolProjection(config, parameter, useGpu) {}
|
||||
|
||||
virtual void forward();
|
||||
virtual void backward(const UpdateCallback& callback = nullptr);
|
||||
};
|
||||
|
||||
class AvgPoolProjection : public PoolProjection {
|
||||
public:
|
||||
AvgPoolProjection(const ProjectionConfig& config, ParameterPtr parameter,
|
||||
bool useGpu)
|
||||
: PoolProjection(config, parameter, useGpu) {}
|
||||
|
||||
virtual void forward();
|
||||
virtual void backward(const UpdateCallback& callback = nullptr);
|
||||
};
|
||||
} // namespace paddle
|
@ -0,0 +1,130 @@
|
||||
/* 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 "SpatialPyramidPoolLayer.h"
|
||||
|
||||
namespace paddle {
|
||||
|
||||
REGISTER_LAYER(spp, SpatialPyramidPoolLayer);
|
||||
|
||||
ProjectionConfig SpatialPyramidPoolLayer::getConfig(size_t imgSizeW,
|
||||
size_t imgSizeH,
|
||||
size_t channels,
|
||||
size_t pyramidLevel,
|
||||
std::string& poolType) {
|
||||
ProjectionConfig config;
|
||||
config.set_type("pool");
|
||||
PoolConfig* conf = config.mutable_pool_conf();
|
||||
conf->set_channels(channels);
|
||||
conf->set_img_size(imgSizeW);
|
||||
conf->set_img_size_y(imgSizeH);
|
||||
conf->set_pool_type(poolType);
|
||||
|
||||
int numBins = std::pow(2, pyramidLevel);
|
||||
|
||||
int sizeH = std::ceil(imgSizeH / static_cast<double>(numBins));
|
||||
int paddingH = (sizeH * numBins - imgSizeH + 1) / 2;
|
||||
int outSizeH = outputSize(imgSizeH, sizeH, paddingH, sizeH, true);
|
||||
|
||||
int sizeW = std::ceil(imgSizeW / static_cast<double>(numBins));
|
||||
int paddingW = (sizeW * numBins - imgSizeW + 1) / 2;
|
||||
int outSizeW = outputSize(imgSizeW, sizeW, paddingW, sizeW, true);
|
||||
|
||||
conf->set_stride(sizeW);
|
||||
conf->set_stride_y(sizeH);
|
||||
conf->set_size_x(sizeW);
|
||||
conf->set_size_y(sizeH);
|
||||
conf->set_padding(paddingW);
|
||||
conf->set_padding_y(paddingH);
|
||||
conf->set_output_x(outSizeW);
|
||||
conf->set_output_y(outSizeH);
|
||||
config.set_output_size(outSizeH * outSizeW * channels);
|
||||
return config;
|
||||
}
|
||||
|
||||
size_t SpatialPyramidPoolLayer::getSize() {
|
||||
CHECK_EQ(inputLayers_.size(), 1UL);
|
||||
size_t layerSize = 0;
|
||||
const SppConfig& sppConf = config_.inputs(0).spp_conf();
|
||||
imgSizeH_ = inputLayers_[0]->getOutput().getFrameHeight();
|
||||
imgSizeW_ = inputLayers_[0]->getOutput().getFrameWidth();
|
||||
if (imgSizeH_ == 0) {
|
||||
imgSizeH_ = sppConf.has_img_size_y() ? sppConf.img_size_y() : imgSizeW_;
|
||||
}
|
||||
if (imgSizeW_ == 0) {
|
||||
imgSizeW_ = sppConf.img_size();
|
||||
}
|
||||
|
||||
size_t outputH = 1;
|
||||
size_t outputW = (std::pow(4, pyramidHeight_) - 1) / (4 - 1);
|
||||
|
||||
layerSize = outputH * outputW * channels_;
|
||||
return layerSize;
|
||||
}
|
||||
|
||||
bool SpatialPyramidPoolLayer::init(const LayerMap& layerMap,
|
||||
const ParameterMap& parameterMap) {
|
||||
Layer::init(layerMap, parameterMap);
|
||||
CHECK_EQ(config_.inputs_size(), 1);
|
||||
|
||||
const SppConfig& sppConf = config_.inputs(0).spp_conf();
|
||||
pyramidHeight_ = sppConf.pyramid_height();
|
||||
poolType_ = sppConf.pool_type();
|
||||
|
||||
channels_ = sppConf.channels();
|
||||
imgSizeW_ = sppConf.img_size();
|
||||
imgSizeH_ = sppConf.has_img_size_y() ? sppConf.img_size_y() : imgSizeW_;
|
||||
poolProjections_.reserve(pyramidHeight_);
|
||||
projCol_.reserve(pyramidHeight_);
|
||||
projOutput_.resize(pyramidHeight_);
|
||||
|
||||
size_t startCol = 0;
|
||||
size_t endCol = 0;
|
||||
for (size_t i = 0; i < pyramidHeight_; i++) {
|
||||
poolProjections_.emplace_back(PoolProjection::create(
|
||||
getConfig(imgSizeW_, imgSizeH_, channels_, i, poolType_), nullptr,
|
||||
useGpu_));
|
||||
endCol += poolProjections_[i]->getOutputSize();
|
||||
projCol_.push_back(std::make_pair(startCol, endCol));
|
||||
startCol = endCol;
|
||||
}
|
||||
CHECK_EQ(endCol, getSize());
|
||||
return true;
|
||||
}
|
||||
|
||||
void SpatialPyramidPoolLayer::forward(PassType passType) {
|
||||
Layer::forward(passType);
|
||||
|
||||
int batchSize = getInput(0).getBatchSize();
|
||||
resetOutput(batchSize, getSize());
|
||||
for (size_t i = 0; i < pyramidHeight_; i++) {
|
||||
size_t startCol = projCol_[i].first;
|
||||
size_t endCol = projCol_[i].second;
|
||||
projOutput_[i].value = output_.value->subColMatrix(startCol, endCol);
|
||||
projOutput_[i].grad = output_.grad->subColMatrix(startCol, endCol);
|
||||
}
|
||||
for (size_t i = 0; i < pyramidHeight_; i++) {
|
||||
poolProjections_[i]->forward(&getInput(0), &projOutput_[i], passType);
|
||||
}
|
||||
}
|
||||
|
||||
void SpatialPyramidPoolLayer::backward(const UpdateCallback& callback) {
|
||||
for (size_t i = 0; i < pyramidHeight_; i++) {
|
||||
if (poolProjections_[i]) {
|
||||
poolProjections_[i]->backward(callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace paddle
|
@ -0,0 +1,57 @@
|
||||
/* 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 "Layer.h"
|
||||
#include "PoolProjection.h"
|
||||
#include "paddle/math/MathUtils.h"
|
||||
#include "paddle/utils/Logging.h"
|
||||
|
||||
namespace paddle {
|
||||
/**
|
||||
* @brief A layer for spatial pyramid pooling on the input image by taking
|
||||
* the max, average, etc. within regions, so that the result vector of
|
||||
* different sized images are of the same size.
|
||||
*
|
||||
* The config file api is spp_layer.
|
||||
*/
|
||||
|
||||
class SpatialPyramidPoolLayer : public Layer {
|
||||
protected:
|
||||
size_t channels_;
|
||||
size_t imgSizeW_;
|
||||
size_t imgSizeH_;
|
||||
size_t pyramidHeight_;
|
||||
std::string poolType_;
|
||||
|
||||
std::vector<std::unique_ptr<PoolProjection>> poolProjections_;
|
||||
std::vector<Argument> projOutput_;
|
||||
std::vector<std::pair<size_t, size_t>> projCol_;
|
||||
|
||||
public:
|
||||
explicit SpatialPyramidPoolLayer(const LayerConfig& config) : Layer(config) {}
|
||||
|
||||
~SpatialPyramidPoolLayer() {}
|
||||
|
||||
virtual bool init(const LayerMap& layerMap, const ParameterMap& parameterMap);
|
||||
|
||||
ProjectionConfig getConfig(size_t sizeX_, size_t sizeY_, size_t channels,
|
||||
size_t pyamidLevel_, std::string& poolType_);
|
||||
size_t getSize();
|
||||
|
||||
virtual void forward(PassType passType);
|
||||
virtual void backward(const UpdateCallback& callback = nullptr);
|
||||
};
|
||||
} // namespace paddle
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue