Merge pull request #3504 from chengduoZH/Add_3DPooling
Add 3DPoolingLayerrevert-3824-remove_grad_op_type
commit
fcad0a3a4b
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,178 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. 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 "Pool3DLayer.h"
|
||||
#include "PoolProjectionLayer.h"
|
||||
#include "paddle/utils/Logging.h"
|
||||
|
||||
namespace paddle {
|
||||
|
||||
REGISTER_LAYER(pool3d, Pool3DLayer);
|
||||
|
||||
bool Pool3DLayer::init(const LayerMap& layerMap,
|
||||
const ParameterMap& parameterMap) {
|
||||
Layer::init(layerMap, parameterMap);
|
||||
|
||||
/* the size of inputs for pool-layer is 1 */
|
||||
CHECK_EQ(config_.inputs_size(), 1);
|
||||
|
||||
const PoolConfig& conf = config_.inputs(0).pool_conf();
|
||||
poolType_ = conf.pool_type();
|
||||
channels_ = conf.channels();
|
||||
|
||||
sizeX_ = conf.size_x();
|
||||
sizeY_ = conf.size_y();
|
||||
sizeZ_ = conf.size_z();
|
||||
|
||||
strideW_ = conf.stride();
|
||||
strideH_ = conf.stride_y();
|
||||
strideD_ = conf.stride_z();
|
||||
|
||||
imgSizeW_ = conf.img_size();
|
||||
imgSizeH_ = conf.img_size_y();
|
||||
imgSizeD_ = conf.img_size_z();
|
||||
|
||||
paddingW_ = conf.padding();
|
||||
paddingH_ = conf.padding_y();
|
||||
paddingD_ = conf.padding_z();
|
||||
|
||||
outputW_ = conf.output_x();
|
||||
outputH_ = conf.output_y();
|
||||
outputD_ = conf.output_z();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t Pool3DLayer::getSize() {
|
||||
CHECK_EQ(inputLayers_.size(), 1UL);
|
||||
|
||||
size_t layerSize = 0;
|
||||
outputD_ = outputSize(imgSizeD_, sizeZ_, paddingD_, strideD_, false);
|
||||
outputH_ = outputSize(imgSizeH_, sizeY_, paddingH_, strideH_, false);
|
||||
outputW_ = outputSize(imgSizeW_, sizeX_, paddingW_, strideW_, false);
|
||||
|
||||
layerSize = outputD_ * outputH_ * outputW_ * channels_;
|
||||
getOutput().setFrameHeight(outputH_);
|
||||
getOutput().setFrameWidth(outputW_);
|
||||
getOutput().setFrameDepth(outputD_);
|
||||
return layerSize;
|
||||
}
|
||||
|
||||
void Pool3DLayer::forward(PassType passType) {
|
||||
Layer::forward(passType);
|
||||
const MatrixPtr& inMat = inputLayers_[0]->getOutputValue();
|
||||
size_t batchSize = inMat->getHeight();
|
||||
size_t outWidth = getSize();
|
||||
resetOutput(batchSize, outWidth);
|
||||
Matrix::resizeOrCreate(maxPoolIdx_, batchSize, outWidth, false, useGpu_);
|
||||
const MatrixPtr outMat = getOutputValue();
|
||||
|
||||
if (poolType_ == "avg") {
|
||||
outMat->avgPool3DForward(*inMat,
|
||||
channels_,
|
||||
imgSizeD_,
|
||||
imgSizeH_,
|
||||
imgSizeW_,
|
||||
outputD_,
|
||||
outputH_,
|
||||
outputW_,
|
||||
sizeZ_,
|
||||
sizeY_,
|
||||
sizeX_,
|
||||
strideD_,
|
||||
strideH_,
|
||||
strideW_,
|
||||
paddingD_,
|
||||
paddingH_,
|
||||
paddingW_);
|
||||
} else if (poolType_ == "max") {
|
||||
outMat->maxPool3DForward(*inMat,
|
||||
*maxPoolIdx_,
|
||||
channels_,
|
||||
imgSizeD_,
|
||||
imgSizeH_,
|
||||
imgSizeW_,
|
||||
outputD_,
|
||||
outputH_,
|
||||
outputW_,
|
||||
sizeZ_,
|
||||
sizeY_,
|
||||
sizeX_,
|
||||
strideD_,
|
||||
strideH_,
|
||||
strideW_,
|
||||
paddingD_,
|
||||
paddingH_,
|
||||
paddingW_);
|
||||
} else {
|
||||
LOG(FATAL) << "Unknown pool type: " << poolType_;
|
||||
}
|
||||
forwardActivation();
|
||||
}
|
||||
|
||||
void Pool3DLayer::backward(const UpdateCallback& callback) {
|
||||
backwardActivation();
|
||||
|
||||
(void)callback;
|
||||
if (NULL == getInputGrad(0)) return;
|
||||
MatrixPtr inMat = inputLayers_[0]->getOutputValue();
|
||||
MatrixPtr inGradMat = inputLayers_[0]->getOutputGrad();
|
||||
MatrixPtr outMat = getOutputValue();
|
||||
MatrixPtr outGradMat = getOutputGrad();
|
||||
|
||||
if (poolType_ == "avg") {
|
||||
inGradMat->avgPool3DBackward(*outGradMat,
|
||||
imgSizeD_,
|
||||
imgSizeH_,
|
||||
imgSizeW_,
|
||||
outputD_,
|
||||
outputH_,
|
||||
outputW_,
|
||||
sizeZ_,
|
||||
sizeY_,
|
||||
sizeZ_,
|
||||
strideD_,
|
||||
strideH_,
|
||||
strideW_,
|
||||
paddingD_,
|
||||
paddingH_,
|
||||
paddingW_,
|
||||
1.0,
|
||||
1.0);
|
||||
} else if (poolType_ == "max") {
|
||||
inGradMat->maxPool3DBackward(*outGradMat,
|
||||
*maxPoolIdx_,
|
||||
imgSizeD_,
|
||||
imgSizeH_,
|
||||
imgSizeW_,
|
||||
outputD_,
|
||||
outputH_,
|
||||
outputW_,
|
||||
sizeZ_,
|
||||
sizeY_,
|
||||
sizeZ_,
|
||||
strideD_,
|
||||
strideH_,
|
||||
strideW_,
|
||||
paddingD_,
|
||||
paddingH_,
|
||||
paddingW_,
|
||||
1.0,
|
||||
1.0);
|
||||
} else {
|
||||
LOG(FATAL) << "Unknown pool type: " << poolType_;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace paddle
|
@ -0,0 +1,49 @@
|
||||
/* Copyright (c) 2016 PaddlePaddle Authors. 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 "Layer.h"
|
||||
#include "paddle/math/MathUtils.h"
|
||||
#include "paddle/math/Matrix.h"
|
||||
|
||||
namespace paddle {
|
||||
|
||||
/**
|
||||
* @brief Basic parent layer of pooling
|
||||
* Pools the input within regions
|
||||
*/
|
||||
class Pool3DLayer : public Layer {
|
||||
public:
|
||||
explicit Pool3DLayer(const LayerConfig& config) : Layer(config) {}
|
||||
~Pool3DLayer() {}
|
||||
|
||||
bool init(const LayerMap& layerMap,
|
||||
const ParameterMap& parameterMap) override;
|
||||
void forward(PassType passType) override;
|
||||
void backward(const UpdateCallback& callback) override;
|
||||
size_t getSize();
|
||||
|
||||
protected:
|
||||
int channels_;
|
||||
int sizeX_, sizeY_, sizeZ_;
|
||||
int strideW_, strideH_, strideD_;
|
||||
int paddingW_, paddingH_, paddingD_;
|
||||
int imgSizeW_, imgSizeH_, imgSizeD_;
|
||||
int outputW_, outputH_, outputD_;
|
||||
std::string poolType_;
|
||||
MatrixPtr maxPoolIdx_;
|
||||
};
|
||||
} // namespace paddle
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,123 @@
|
||||
type: "nn"
|
||||
layers {
|
||||
name: "data_2d"
|
||||
type: "data"
|
||||
size: 6000
|
||||
active_type: ""
|
||||
height: 20
|
||||
width: 10
|
||||
}
|
||||
layers {
|
||||
name: "pool___2d"
|
||||
type: "pool"
|
||||
size: 840
|
||||
active_type: ""
|
||||
inputs {
|
||||
input_layer_name: "data_2d"
|
||||
pool_conf {
|
||||
pool_type: "avg-projection"
|
||||
channels: 30
|
||||
size_x: 5
|
||||
stride: 3
|
||||
output_x: 4
|
||||
img_size: 10
|
||||
padding: 1
|
||||
size_y: 5
|
||||
stride_y: 3
|
||||
output_y: 7
|
||||
img_size_y: 20
|
||||
padding_y: 1
|
||||
}
|
||||
}
|
||||
height: 7
|
||||
width: 4
|
||||
}
|
||||
layers {
|
||||
name: "data_3d_1"
|
||||
type: "data"
|
||||
size: 60000
|
||||
active_type: ""
|
||||
height: 20
|
||||
width: 10
|
||||
depth: 10
|
||||
}
|
||||
layers {
|
||||
name: "pool_3d_1"
|
||||
type: "pool3d"
|
||||
size: 3360
|
||||
active_type: ""
|
||||
inputs {
|
||||
input_layer_name: "data_3d_1"
|
||||
pool_conf {
|
||||
pool_type: "avg-projection"
|
||||
channels: 30
|
||||
size_x: 5
|
||||
stride: 3
|
||||
output_x: 4
|
||||
img_size: 10
|
||||
padding: 1
|
||||
size_y: 5
|
||||
stride_y: 3
|
||||
output_y: 7
|
||||
img_size_y: 20
|
||||
padding_y: 1
|
||||
size_z: 5
|
||||
stride_z: 3
|
||||
output_z: 4
|
||||
img_size_z: 10
|
||||
padding_z: 1
|
||||
}
|
||||
}
|
||||
height: 7
|
||||
width: 4
|
||||
depth: 4
|
||||
}
|
||||
layers {
|
||||
name: "pool_3d_2"
|
||||
type: "pool3d"
|
||||
size: 3360
|
||||
active_type: ""
|
||||
inputs {
|
||||
input_layer_name: "data_3d_1"
|
||||
pool_conf {
|
||||
pool_type: "max-projection"
|
||||
channels: 30
|
||||
size_x: 5
|
||||
stride: 3
|
||||
output_x: 4
|
||||
img_size: 10
|
||||
padding: 1
|
||||
size_y: 5
|
||||
stride_y: 3
|
||||
output_y: 7
|
||||
img_size_y: 20
|
||||
padding_y: 1
|
||||
size_z: 5
|
||||
stride_z: 3
|
||||
output_z: 4
|
||||
img_size_z: 10
|
||||
padding_z: 1
|
||||
}
|
||||
}
|
||||
height: 7
|
||||
width: 4
|
||||
depth: 4
|
||||
}
|
||||
input_layer_names: "data_2d"
|
||||
output_layer_names: "pool___2d"
|
||||
output_layer_names: "pool_3d_1"
|
||||
output_layer_names: "pool_3d_2"
|
||||
sub_models {
|
||||
name: "root"
|
||||
layer_names: "data_2d"
|
||||
layer_names: "pool___2d"
|
||||
layer_names: "data_3d_1"
|
||||
layer_names: "pool_3d_1"
|
||||
layer_names: "pool_3d_2"
|
||||
input_layer_names: "data_2d"
|
||||
output_layer_names: "pool___2d"
|
||||
output_layer_names: "pool_3d_1"
|
||||
output_layer_names: "pool_3d_2"
|
||||
is_recurrent_layer_group: false
|
||||
}
|
||||
|
@ -0,0 +1,38 @@
|
||||
from paddle.trainer_config_helpers import *
|
||||
|
||||
settings(batch_size=100, learning_rate=1e-5)
|
||||
|
||||
data_2d = data_layer(name='data_2d', size=6000, height=20, width=10)
|
||||
|
||||
pool_2d = img_pool_layer(
|
||||
name="pool___2d",
|
||||
input=data_2d,
|
||||
num_channels=30,
|
||||
pool_size=5,
|
||||
stride=3,
|
||||
padding=1,
|
||||
pool_type=AvgPooling())
|
||||
outputs(pool_2d)
|
||||
|
||||
data_3d = data_layer(
|
||||
name='data_3d_1', size=60000, depth=10, height=20, width=10)
|
||||
|
||||
pool_3d_1 = img_pool3d_layer(
|
||||
name="pool_3d_1",
|
||||
input=data_3d,
|
||||
num_channels=30,
|
||||
pool_size=5,
|
||||
stride=3,
|
||||
padding=1,
|
||||
pool_type=AvgPooling())
|
||||
outputs(pool_3d_1)
|
||||
|
||||
pool_3d_2 = img_pool3d_layer(
|
||||
name="pool_3d_2",
|
||||
input=data_3d,
|
||||
num_channels=30,
|
||||
pool_size=[5, 5, 5],
|
||||
stride=[3, 3, 3],
|
||||
padding=[1, 1, 1],
|
||||
pool_type=MaxPooling())
|
||||
outputs(pool_3d_2)
|
Loading…
Reference in new issue