From 6245fed240abb80ccaed43e08f6ad2b327e12cc4 Mon Sep 17 00:00:00 2001 From: Haonan Date: Thu, 2 Feb 2017 11:28:45 -0800 Subject: [PATCH] rotate_layer python interface fixes --- paddle/gserver/layers/FeatureMapExpandLayer.cpp | 3 +++ paddle/gserver/layers/GruStepLayer.cpp | 4 ++-- paddle/gserver/layers/RotateLayer.cpp | 2 ++ python/paddle/trainer/config_parser.py | 6 +++--- python/paddle/trainer_config_helpers/layers.py | 14 ++++++++------ .../tests/layers_test_config.py | 2 +- 6 files changed, 19 insertions(+), 12 deletions(-) diff --git a/paddle/gserver/layers/FeatureMapExpandLayer.cpp b/paddle/gserver/layers/FeatureMapExpandLayer.cpp index d023074c521..4b685812aac 100644 --- a/paddle/gserver/layers/FeatureMapExpandLayer.cpp +++ b/paddle/gserver/layers/FeatureMapExpandLayer.cpp @@ -95,6 +95,9 @@ void FeatureMapExpandLayer::forward(PassType passType) { void FeatureMapExpandLayer::backward(const UpdateCallback& callback) { MatrixPtr inGrad = getInputGrad(0); + if (NULL == inGrad) { + return; + } MatrixPtr outGrad = getOutputGrad(); size_t batchSize = getInput(0).getBatchSize(); int imgSize = inGrad->getWidth(); diff --git a/paddle/gserver/layers/GruStepLayer.cpp b/paddle/gserver/layers/GruStepLayer.cpp index ce692c49088..4a1006aa941 100644 --- a/paddle/gserver/layers/GruStepLayer.cpp +++ b/paddle/gserver/layers/GruStepLayer.cpp @@ -68,8 +68,8 @@ bool GruStepLayer::init(const LayerMap& layerMap, if (!Layer::init(layerMap, parameterMap)) return false; CHECK_EQ(2U, inputLayers_.size()); - CHECK_EQ(getSize() * getSize() * 3, parameters_[1]->getSize()); - weight_.reset(new Weight(getSize(), getSize() * 3, parameters_[1])); + CHECK_EQ(getSize() * getSize() * 3, parameters_[0]->getSize()); + weight_.reset(new Weight(getSize(), getSize() * 3, parameters_[0])); if (biasParameter_.get() != NULL) { CHECK_EQ(getSize() * 3, biasParameter_->getSize()); diff --git a/paddle/gserver/layers/RotateLayer.cpp b/paddle/gserver/layers/RotateLayer.cpp index 1cdd8366d5d..d928f1d93de 100644 --- a/paddle/gserver/layers/RotateLayer.cpp +++ b/paddle/gserver/layers/RotateLayer.cpp @@ -25,6 +25,8 @@ bool RotateLayer::init(const LayerMap& layerMap, CHECK_EQ(inputLayers_.size(), 1UL); height_ = config_.height(); width_ = config_.width(); + CHECK_GT(height_, 0); + CHECK_GT(width_, 0); return true; } diff --git a/python/paddle/trainer/config_parser.py b/python/paddle/trainer/config_parser.py index 62efe5fca0c..b02af991dc5 100644 --- a/python/paddle/trainer/config_parser.py +++ b/python/paddle/trainer/config_parser.py @@ -1969,13 +1969,13 @@ class ResizeLayer(LayerBase): @config_layer('rotate') class RotateLayer(LayerBase): - def __init__(self, name, inputs, height, device=None): + def __init__(self, name, inputs, height, width, device=None): super(RotateLayer, self).__init__( name, 'rotate', 0, inputs=inputs, device=device) config_assert( len(self.inputs) == 1, 'RotateLayer must have one and only one input') - self.config.height = height + self.set_layer_height_width(height, width) self.set_layer_size(self.get_input_layer(0).size) @@ -3007,7 +3007,7 @@ class GruStepLayer(LayerBase): config_assert(input_layer1.size == size, 'input_layer1.size != layer.size') self.config.active_gate_type = active_gate_type - self.create_input_parameter(1, size * size * 3, [size, size * 3]) + self.create_input_parameter(0, size * size * 3, [size, size * 3]) self.create_bias_parameter(bias, size * 3) diff --git a/python/paddle/trainer_config_helpers/layers.py b/python/paddle/trainer_config_helpers/layers.py index 19f7ec31865..2636f6cc1a1 100755 --- a/python/paddle/trainer_config_helpers/layers.py +++ b/python/paddle/trainer_config_helpers/layers.py @@ -1677,22 +1677,23 @@ def trans_layer(input, name=None, layer_attr=None): @wrap_name_default() @layer_support() -def rotate_layer(input, height, name=None, layer_attr=None): +def rotate_layer(input, height, width, name=None, layer_attr=None): """ - A layer for rotation (clock-wise), usually used when the input sample - is some image or map. + A layer for rotating 90 degrees (clock-wise), usually used when the input sample + is some image or feature map. .. math:: - y(j,i) = x(M-i-1,j) + y(j,i,:) = x(M-i-1,j,:) - where :math:`x` is (M x N) input, and :math:`y` is (N x M) output. + where :math:`x` is (M x N x C) input, and :math:`y` is (N x M x C) output. The example usage is: .. code-block:: python rot = rotate_layer(input=layer, - height=100) + height=100, + width=100) :param input: Input layer. :type input: LayerOutput @@ -1709,6 +1710,7 @@ def rotate_layer(input, height, name=None, layer_attr=None): l = Layer( name=name, height=height, + width=width, type=LayerType.ROTATE_LAYER, inputs=[input.name], **ExtraLayerAttribute.to_kwargs(layer_attr)) diff --git a/python/paddle/trainer_config_helpers/tests/layers_test_config.py b/python/paddle/trainer_config_helpers/tests/layers_test_config.py index 1964e6f113e..e6cd35ee761 100644 --- a/python/paddle/trainer_config_helpers/tests/layers_test_config.py +++ b/python/paddle/trainer_config_helpers/tests/layers_test_config.py @@ -39,7 +39,7 @@ z1 = mixed_layer( assert z1.size > 0 y2 = fc_layer(input=y, size=15) -z2 = rotate_layer(input=y2, height=5) +z2 = rotate_layer(input=y2, height=5, width=3) cos1 = cos_sim(a=x1, b=y1) cos3 = cos_sim(a=x1, b=y2, size=3) -- GitLab