commit
8295eb91bf
@ -0,0 +1,95 @@
|
|||||||
|
/* 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 "BilinearInterpLayer.h"
|
||||||
|
#include "paddle/utils/Logging.h"
|
||||||
|
#include "paddle/utils/Stat.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
|
||||||
|
REGISTER_LAYER(bilinear_interp, BilinearInterpLayer);
|
||||||
|
|
||||||
|
size_t BilinearInterpLayer::getSize() {
|
||||||
|
inImgH_ = inputLayers_[0]->getOutput().getFrameHeight();
|
||||||
|
inImgW_ = inputLayers_[0]->getOutput().getFrameWidth();
|
||||||
|
|
||||||
|
const BilinearInterpConfig& conf = config_.inputs(0).bilinear_interp_conf();
|
||||||
|
if (inImgH_ == 0) {
|
||||||
|
inImgH_ = conf.img_size_y();
|
||||||
|
}
|
||||||
|
if (inImgW_ == 0) {
|
||||||
|
inImgW_ = conf.img_size_x();
|
||||||
|
}
|
||||||
|
|
||||||
|
outImgH_ = conf.out_size_y();
|
||||||
|
outImgW_ = conf.out_size_x();
|
||||||
|
numChannels_ = conf.num_channels();
|
||||||
|
|
||||||
|
CHECK(outImgH_ > 0 && outImgW_ > 0);
|
||||||
|
CHECK(inImgH_ > 0 && inImgW_ > 0);
|
||||||
|
CHECK(numChannels_);
|
||||||
|
|
||||||
|
ratioH_ = (outImgH_ > 1) ?
|
||||||
|
static_cast<real>(inImgH_ - 1) / (outImgH_ - 1) : 0.f;
|
||||||
|
ratioW_ = (outImgW_ > 1) ?
|
||||||
|
static_cast<real>(inImgW_ - 1) / (outImgW_ - 1) : 0.f;
|
||||||
|
|
||||||
|
getOutput().setFrameHeight(outImgH_);
|
||||||
|
getOutput().setFrameWidth(outImgW_);
|
||||||
|
return outImgH_ * outImgW_ * numChannels_;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BilinearInterpLayer::init(const LayerMap& layerMap,
|
||||||
|
const ParameterMap& parameterMap) {
|
||||||
|
/* Initialize the basic parent class */
|
||||||
|
Layer::init(layerMap, parameterMap);
|
||||||
|
|
||||||
|
CHECK_EQ(1, config_.inputs_size());
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BilinearInterpLayer::forward(PassType passType) {
|
||||||
|
Layer::forward(passType);
|
||||||
|
|
||||||
|
size_t batchSize = getInput(0).getBatchSize();
|
||||||
|
size_t size = getSize();
|
||||||
|
{
|
||||||
|
REGISTER_TIMER_INFO("FwResetTimer", getName().c_str());
|
||||||
|
resetOutput(batchSize, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
MatrixPtr inV = getInputValue(0);
|
||||||
|
MatrixPtr outV = getOutputValue();
|
||||||
|
{
|
||||||
|
REGISTER_TIMER_INFO("FwBilinearInterpTimer", getName().c_str());
|
||||||
|
outV->bilinearForward(*inV, inImgH_, inImgW_, outImgH_, outImgW_,
|
||||||
|
numChannels_, ratioH_, ratioW_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BilinearInterpLayer::backward(const UpdateCallback& callback) {
|
||||||
|
(void) callback;
|
||||||
|
|
||||||
|
MatrixPtr inputG = getInputGrad(0);
|
||||||
|
MatrixPtr outG = getOutputGrad();
|
||||||
|
{
|
||||||
|
REGISTER_TIMER_INFO("BwBilinearInterpTimer", getName().c_str());
|
||||||
|
if (inputG) {
|
||||||
|
inputG->bilinearBackward(*outG, outImgH_, outImgW_, inImgH_, inImgW_,
|
||||||
|
numChannels_, ratioH_, ratioW_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // namespace paddle
|
@ -0,0 +1,46 @@
|
|||||||
|
/* 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 "paddle/math/Matrix.h"
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief A layer for bilinear interpolation which is
|
||||||
|
* used on conv layer output.
|
||||||
|
*
|
||||||
|
* @note The config file api is bilinear_interp_layer.
|
||||||
|
*/
|
||||||
|
class BilinearInterpLayer : public Layer {
|
||||||
|
protected:
|
||||||
|
size_t outImgH_, outImgW_;
|
||||||
|
size_t inImgH_, inImgW_;
|
||||||
|
real ratioH_, ratioW_;
|
||||||
|
size_t numChannels_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit BilinearInterpLayer(const LayerConfig& config) : Layer(config) {}
|
||||||
|
|
||||||
|
virtual ~BilinearInterpLayer() {}
|
||||||
|
|
||||||
|
size_t getSize();
|
||||||
|
bool init(const LayerMap& layerMap, const ParameterMap& parameterMap);
|
||||||
|
void forward(PassType passType);
|
||||||
|
void backward(const UpdateCallback& callback = nullptr);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace paddle
|
@ -0,0 +1,123 @@
|
|||||||
|
type: "nn"
|
||||||
|
layers {
|
||||||
|
name: "data"
|
||||||
|
type: "data"
|
||||||
|
size: 2304
|
||||||
|
active_type: ""
|
||||||
|
}
|
||||||
|
layers {
|
||||||
|
name: "__conv_0__"
|
||||||
|
type: "exconv"
|
||||||
|
size: 36864
|
||||||
|
active_type: ""
|
||||||
|
inputs {
|
||||||
|
input_layer_name: "data"
|
||||||
|
input_parameter_name: "___conv_0__.w0"
|
||||||
|
conv_conf {
|
||||||
|
filter_size: 3
|
||||||
|
channels: 1
|
||||||
|
stride: 1
|
||||||
|
padding: 1
|
||||||
|
groups: 1
|
||||||
|
filter_channels: 1
|
||||||
|
output_x: 48
|
||||||
|
img_size: 48
|
||||||
|
caffe_mode: true
|
||||||
|
filter_size_y: 3
|
||||||
|
padding_y: 1
|
||||||
|
stride_y: 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bias_parameter_name: "___conv_0__.wbias"
|
||||||
|
num_filters: 16
|
||||||
|
shared_biases: true
|
||||||
|
}
|
||||||
|
layers {
|
||||||
|
name: "__bilinear_interp_layer_0__"
|
||||||
|
type: "bilinear_interp"
|
||||||
|
size: 65536
|
||||||
|
active_type: ""
|
||||||
|
inputs {
|
||||||
|
input_layer_name: "__conv_0__"
|
||||||
|
bilinear_interp_conf {
|
||||||
|
out_size_x: 64
|
||||||
|
out_size_y: 64
|
||||||
|
num_channels: 16
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
layers {
|
||||||
|
name: "__pool_0__"
|
||||||
|
type: "pool"
|
||||||
|
size: 16384
|
||||||
|
active_type: ""
|
||||||
|
inputs {
|
||||||
|
input_layer_name: "__bilinear_interp_layer_0__"
|
||||||
|
pool_conf {
|
||||||
|
pool_type: "max-projection"
|
||||||
|
channels: 4
|
||||||
|
size_x: 2
|
||||||
|
stride: 2
|
||||||
|
output_x: 64
|
||||||
|
img_size: 128
|
||||||
|
padding: 0
|
||||||
|
size_y: 2
|
||||||
|
stride_y: 2
|
||||||
|
output_y: 64
|
||||||
|
img_size_y: 128
|
||||||
|
padding_y: 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
layers {
|
||||||
|
name: "__fc_layer_0__"
|
||||||
|
type: "fc"
|
||||||
|
size: 384
|
||||||
|
active_type: "tanh"
|
||||||
|
inputs {
|
||||||
|
input_layer_name: "__pool_0__"
|
||||||
|
input_parameter_name: "___fc_layer_0__.w0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parameters {
|
||||||
|
name: "___conv_0__.w0"
|
||||||
|
size: 144
|
||||||
|
initial_mean: 0.0
|
||||||
|
initial_std: 0.471404520791
|
||||||
|
initial_strategy: 0
|
||||||
|
initial_smart: false
|
||||||
|
}
|
||||||
|
parameters {
|
||||||
|
name: "___conv_0__.wbias"
|
||||||
|
size: 16
|
||||||
|
initial_mean: 0.0
|
||||||
|
initial_std: 0.0
|
||||||
|
dims: 16
|
||||||
|
dims: 1
|
||||||
|
initial_strategy: 0
|
||||||
|
initial_smart: false
|
||||||
|
}
|
||||||
|
parameters {
|
||||||
|
name: "___fc_layer_0__.w0"
|
||||||
|
size: 6291456
|
||||||
|
initial_mean: 0.0
|
||||||
|
initial_std: 0.0078125
|
||||||
|
dims: 16384
|
||||||
|
dims: 384
|
||||||
|
initial_strategy: 0
|
||||||
|
initial_smart: true
|
||||||
|
}
|
||||||
|
input_layer_names: "data"
|
||||||
|
output_layer_names: "__fc_layer_0__"
|
||||||
|
sub_models {
|
||||||
|
name: "root"
|
||||||
|
layer_names: "data"
|
||||||
|
layer_names: "__conv_0__"
|
||||||
|
layer_names: "__bilinear_interp_layer_0__"
|
||||||
|
layer_names: "__pool_0__"
|
||||||
|
layer_names: "__fc_layer_0__"
|
||||||
|
input_layer_names: "data"
|
||||||
|
output_layer_names: "__fc_layer_0__"
|
||||||
|
is_recurrent_layer_group: false
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
|||||||
|
from paddle.trainer_config_helpers import *
|
||||||
|
|
||||||
|
settings(
|
||||||
|
batch_size=1000,
|
||||||
|
learning_rate=1e-5
|
||||||
|
)
|
||||||
|
|
||||||
|
data = data_layer(name='data', size=2304)
|
||||||
|
|
||||||
|
conv = img_conv_layer(input=data,
|
||||||
|
filter_size = 3,
|
||||||
|
num_channels=1,
|
||||||
|
num_filters=16,
|
||||||
|
padding=1,
|
||||||
|
act=LinearActivation(),
|
||||||
|
bias_attr=True)
|
||||||
|
|
||||||
|
bilinear = bilinear_interp_layer(input=conv,
|
||||||
|
out_size_x=64,
|
||||||
|
out_size_y=64)
|
||||||
|
|
||||||
|
pool = img_pool_layer(input=bilinear,
|
||||||
|
num_channels=4,
|
||||||
|
pool_size=2,
|
||||||
|
stride=2,
|
||||||
|
pool_type=MaxPooling())
|
||||||
|
|
||||||
|
fc = fc_layer(input=pool, size=384, bias_attr=False)
|
||||||
|
|
||||||
|
outputs(fc)
|
Loading…
Reference in new issue