From 86543f7f6a8f0fc073977794abee9ae5b033f78e Mon Sep 17 00:00:00 2001 From: yangyaming Date: Tue, 4 Jul 2017 16:40:00 +0800 Subject: [PATCH] Follow comments. --- doc/api/v2/config/layer.rst | 2 +- paddle/gserver/layers/DetectionOutputLayer.h | 8 +- paddle/gserver/layers/MultiBoxLossLayer.cpp | 6 +- paddle/gserver/layers/MultiBoxLossLayer.h | 2 +- .../paddle/trainer_config_helpers/layers.py | 20 +++-- .../test_detection_output_layer.protostr | 66 ++++++++++++++++ .../test_multibox_loss_layer.protostr | 79 +++++++++++++++++++ 7 files changed, 164 insertions(+), 19 deletions(-) create mode 100644 python/paddle/trainer_config_helpers/tests/configs/protostr/test_detection_output_layer.protostr create mode 100644 python/paddle/trainer_config_helpers/tests/configs/protostr/test_multibox_loss_layer.protostr diff --git a/doc/api/v2/config/layer.rst b/doc/api/v2/config/layer.rst index 0a8465919d9..4f4a9187bcb 100644 --- a/doc/api/v2/config/layer.rst +++ b/doc/api/v2/config/layer.rst @@ -478,6 +478,6 @@ Detection output Layer ====================== detection_output ---- +---------------- .. autoclass:: paddle.v2.layer.detection_output :noindex: diff --git a/paddle/gserver/layers/DetectionOutputLayer.h b/paddle/gserver/layers/DetectionOutputLayer.h index 9cc568219ca..a232af0a691 100644 --- a/paddle/gserver/layers/DetectionOutputLayer.h +++ b/paddle/gserver/layers/DetectionOutputLayer.h @@ -22,14 +22,14 @@ limitations under the License. */ namespace paddle { /** - * The detection output layer for a SSD detection task. This layer apply the - * Non-maximum suppression to the all predicted bounding box and keep the + * The detection output layer for a SSD detection task. This layer applies the + * Non-maximum suppression to the all predicted bounding box and keeps the * Top-K bounding boxes. - * - Input: This layer needs three input layers: This first input layer + * - Input: This layer needs three input layers: The first input layer * is the priorbox layer. The rest two input layers are convolution * layers for generating bbox location offset and the classification * confidence. - * - Output: The predict bounding box location. + * - Output: The predict bounding box locations. */ class DetectionOutputLayer : public Layer { diff --git a/paddle/gserver/layers/MultiBoxLossLayer.cpp b/paddle/gserver/layers/MultiBoxLossLayer.cpp index f2d7b8eb1da..bbf1166dced 100644 --- a/paddle/gserver/layers/MultiBoxLossLayer.cpp +++ b/paddle/gserver/layers/MultiBoxLossLayer.cpp @@ -258,8 +258,7 @@ void MultiBoxLossLayer::forward(PassType passType) { } real loss = locLoss_ + confLoss_; MatrixPtr outV = getOutputValue(); - std::vector tmp(batchSize, loss); - outV->copyFrom(&tmp[0], batchSize); + outV->assign(loss); } void MultiBoxLossLayer::backward(const UpdateCallback& callback) { @@ -336,6 +335,9 @@ void MultiBoxLossLayer::backward(const UpdateCallback& callback) { const MatrixPtr inLocG = getInputGrad(*getLocInputLayer(n)); const MatrixPtr inConfG = getInputGrad(*getConfInputLayer(n)); size_t height = getInput(*getLocInputLayer(n)).getFrameHeight(); + // only for unittest, there are no width and height information + // when constructing matrix in unittest, so we should + // set the shape in configuration if (!height) height = layerConf.height(); size_t width = getInput(*getLocInputLayer(n)).getFrameWidth(); if (!width) width = layerConf.width(); diff --git a/paddle/gserver/layers/MultiBoxLossLayer.h b/paddle/gserver/layers/MultiBoxLossLayer.h index 9767fed7f1c..9935da56446 100644 --- a/paddle/gserver/layers/MultiBoxLossLayer.h +++ b/paddle/gserver/layers/MultiBoxLossLayer.h @@ -30,7 +30,7 @@ namespace paddle { * The loss is composed by the location loss and the confidence loss. * The location loss is a smooth L1 loss and the confidence loss is * a softmax loss. - * - Input: This layer need four input layers: This first input layer + * - Input: This layer needs four input layers: The first input layer * is the priorbox layer and the second layer is a label layer. * The rest two input layers are convolution layers for generating * bbox location offset and the classification confidence. diff --git a/python/paddle/trainer_config_helpers/layers.py b/python/paddle/trainer_config_helpers/layers.py index 1286ed198e0..86e91e2c57f 100755 --- a/python/paddle/trainer_config_helpers/layers.py +++ b/python/paddle/trainer_config_helpers/layers.py @@ -1072,10 +1072,10 @@ def multibox_loss_layer(input_loc, :param name: The Layer Name. :type name: basestring - :param input_loc: The input predict location. - :type input_loc: LayerOutput + :param input_loc: The input predict locations. + :type input_loc: LayerOutput | List of LayerOutput :param input_conf: The input priorbox confidence. - :type input_conf: LayerOutput + :type input_conf: LayerOutput | List of LayerOutput :param priorbox: The input priorbox location and the variance. :type priorbox: LayerOutput :param label: The input label. @@ -1146,10 +1146,10 @@ def detection_output_layer(input_loc, :param name: The Layer Name. :type name: basestring - :param input_loc: The input predict location. - :type input_loc: LayerOutput + :param input_loc: The input predict locations. + :type input_loc: LayerOutput | List of LayerOutput. :param input_conf: The input priorbox confidence. - :type input_conf: LayerOutput + :type input_conf: LayerOutput | List of LayerOutput. :param priorbox: The input priorbox location and the variance. :type priorbox: LayerOutput :param num_classes: The number of the classification. @@ -1166,22 +1166,20 @@ def detection_output_layer(input_loc, :type background_id: int :return: LayerOutput """ - input_loc_num = 0 - input_conf_num = 0 - if isinstance(input_loc, LayerOutput): input_loc = [input_loc] assert isinstance(input_loc, collections.Sequence) # list or tuple for each in input_loc: assert isinstance(each, LayerOutput) - input_loc_num += 1 + input_loc_num = len(input_loc) if isinstance(input_conf, LayerOutput): input_conf = [input_conf] assert isinstance(input_conf, collections.Sequence) # list or tuple for each in input_conf: assert isinstance(each, LayerOutput) - input_conf_num += 1 + input_conf_num = len(input_conf) + # Check the input layer number. assert input_loc_num == input_conf_num diff --git a/python/paddle/trainer_config_helpers/tests/configs/protostr/test_detection_output_layer.protostr b/python/paddle/trainer_config_helpers/tests/configs/protostr/test_detection_output_layer.protostr new file mode 100644 index 00000000000..6690f9852a3 --- /dev/null +++ b/python/paddle/trainer_config_helpers/tests/configs/protostr/test_detection_output_layer.protostr @@ -0,0 +1,66 @@ +type: "nn" +layers { + name: "input_loc" + type: "data" + size: 16 + active_type: "" + height: 16 + width: 1 +} +layers { + name: "input_conf" + type: "data" + size: 8 + active_type: "" + height: 1 + width: 8 +} +layers { + name: "priorbox" + type: "data" + size: 32 + active_type: "" + height: 4 + width: 8 +} +layers { + name: "test_detection_output" + type: "detection_output" + size: 1400 + active_type: "" + inputs { + input_layer_name: "priorbox" + detection_output_conf { + num_classes: 21 + nms_threshold: 0.45 + nms_top_k: 400 + background_id: 0 + input_num: 1 + keep_top_k: 200 + confidence_threshold: 0.01 + } + } + inputs { + input_layer_name: "input_loc" + } + inputs { + input_layer_name: "input_conf" + } +} +input_layer_names: "priorbox" +input_layer_names: "input_loc" +input_layer_names: "input_conf" +output_layer_names: "test_detection_output" +sub_models { + name: "root" + layer_names: "input_loc" + layer_names: "input_conf" + layer_names: "priorbox" + layer_names: "test_detection_output" + input_layer_names: "priorbox" + input_layer_names: "input_loc" + input_layer_names: "input_conf" + output_layer_names: "test_detection_output" + is_recurrent_layer_group: false +} + diff --git a/python/paddle/trainer_config_helpers/tests/configs/protostr/test_multibox_loss_layer.protostr b/python/paddle/trainer_config_helpers/tests/configs/protostr/test_multibox_loss_layer.protostr new file mode 100644 index 00000000000..0ba84dcc6db --- /dev/null +++ b/python/paddle/trainer_config_helpers/tests/configs/protostr/test_multibox_loss_layer.protostr @@ -0,0 +1,79 @@ +type: "nn" +layers { + name: "input_loc" + type: "data" + size: 16 + active_type: "" + height: 16 + width: 1 +} +layers { + name: "input_conf" + type: "data" + size: 8 + active_type: "" + height: 1 + width: 8 +} +layers { + name: "priorbox" + type: "data" + size: 32 + active_type: "" + height: 4 + width: 8 +} +layers { + name: "label" + type: "data" + size: 24 + active_type: "" + height: 4 + width: 6 +} +layers { + name: "test_multibox_loss" + type: "multibox_loss" + size: 1 + active_type: "" + inputs { + input_layer_name: "priorbox" + multibox_loss_conf { + num_classes: 21 + overlap_threshold: 0.5 + neg_pos_ratio: 3.0 + neg_overlap: 0.5 + background_id: 0 + input_num: 1 + } + } + inputs { + input_layer_name: "label" + } + inputs { + input_layer_name: "input_loc" + } + inputs { + input_layer_name: "input_conf" + } +} +input_layer_names: "priorbox" +input_layer_names: "label" +input_layer_names: "input_loc" +input_layer_names: "input_conf" +output_layer_names: "test_multibox_loss" +sub_models { + name: "root" + layer_names: "input_loc" + layer_names: "input_conf" + layer_names: "priorbox" + layer_names: "label" + layer_names: "test_multibox_loss" + input_layer_names: "priorbox" + input_layer_names: "label" + input_layer_names: "input_loc" + input_layer_names: "input_conf" + output_layer_names: "test_multibox_loss" + is_recurrent_layer_group: false +} + -- GitLab