From d93b6be3cc13d20644b694e0d1efb46c139af70b Mon Sep 17 00:00:00 2001 From: Liubov Batanina Date: Tue, 16 Jun 2020 20:09:25 +0300 Subject: [PATCH] Changed StridedSlice to VariadicSplit in Region layer --- modules/dnn/src/layers/region_layer.cpp | 48 +++++++++---------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/modules/dnn/src/layers/region_layer.cpp b/modules/dnn/src/layers/region_layer.cpp index d229369abbcf..c0ba4b2ccfdb 100644 --- a/modules/dnn/src/layers/region_layer.cpp +++ b/modules/dnn/src/layers/region_layer.cpp @@ -412,12 +412,12 @@ class RegionLayerImpl CV_FINAL : public RegionLayer auto scale_x_y_node = std::make_shared(ngraph::element::f32, ngraph::Shape{1}, &scale_x_y); auto shift_node = std::make_shared(ngraph::element::f32, ngraph::Shape{1}, std::vector{0.5}); + auto axis = ngraph::op::Constant::create(ngraph::element::i64, ngraph::Shape{}, {0}); + auto splits = ngraph::op::Constant::create(ngraph::element::i64, ngraph::Shape{5}, {1, 1, 1, 1, rows - 4}); + auto split = std::make_shared(input2d, axis, splits); std::shared_ptr box_x; { - auto lower_bounds = std::make_shared(ngraph::element::i64, ngraph::Shape{2}, std::vector{0, 0}); - auto upper_bounds = std::make_shared(ngraph::element::i64, ngraph::Shape{2}, std::vector{1, cols}); - box_x = std::make_shared(input2d, lower_bounds, upper_bounds, strides, std::vector{}, std::vector{}); - box_x = std::make_shared(box_x); + box_x = std::make_shared(split->output(0)); box_x = std::make_shared(box_x, shift_node, ngraph::op::AutoBroadcastType::NUMPY); box_x = std::make_shared(box_x, scale_x_y_node, ngraph::op::AutoBroadcastType::NUMPY); box_x = std::make_shared(box_x, shift_node, ngraph::op::AutoBroadcastType::NUMPY); @@ -443,10 +443,7 @@ class RegionLayerImpl CV_FINAL : public RegionLayer std::shared_ptr box_y; { - auto lower_bounds = std::make_shared(ngraph::element::i64, ngraph::Shape{2}, std::vector{1, 0}); - auto upper_bounds = std::make_shared(ngraph::element::i64, ngraph::Shape{2}, std::vector{2, cols}); - box_y = std::make_shared(input2d, lower_bounds, upper_bounds, strides, std::vector{}, std::vector{}); - box_y = std::make_shared(box_y); + box_y = std::make_shared(split->output(1)); box_y = std::make_shared(box_y, shift_node, ngraph::op::AutoBroadcastType::NUMPY); box_y = std::make_shared(box_y, scale_x_y_node, ngraph::op::AutoBroadcastType::NUMPY); box_y = std::make_shared(box_y, shift_node, ngraph::op::AutoBroadcastType::NUMPY); @@ -499,45 +496,32 @@ class RegionLayerImpl CV_FINAL : public RegionLayer std::copy(bias_h.begin(), bias_h.begin() + h * anchors, bias_h.begin() + i * h * anchors); } - auto lower_bounds = std::make_shared(ngraph::element::i64, ngraph::Shape{2}, std::vector{2, 0}); - auto upper_bounds = std::make_shared(ngraph::element::i64, ngraph::Shape{2}, std::vector{3, cols}); - box_w = std::make_shared(input2d, lower_bounds, upper_bounds, strides, std::vector{}, std::vector{}); - box_w = std::make_shared(box_w); + box_w = std::make_shared(split->output(2)); box_w = std::make_shared(box_w, shape_3d, true); auto anchor_w_node = std::make_shared(ngraph::element::f32, box_broad_shape, bias_w.data()); box_w = std::make_shared(box_w, anchor_w_node, ngraph::op::AutoBroadcastType::NUMPY); - lower_bounds = std::make_shared(ngraph::element::i64, ngraph::Shape{2}, std::vector{3, 0}); - upper_bounds = std::make_shared(ngraph::element::i64, ngraph::Shape{2}, std::vector{4, cols}); - box_h = std::make_shared(input2d, lower_bounds, upper_bounds, strides, std::vector{}, std::vector{}); - box_h = std::make_shared(box_h); + box_h = std::make_shared(split->output(3)); box_h = std::make_shared(box_h, shape_3d, true); auto anchor_h_node = std::make_shared(ngraph::element::f32, box_broad_shape, bias_h.data()); box_h = std::make_shared(box_h, anchor_h_node, ngraph::op::AutoBroadcastType::NUMPY); } + auto region_splits = ngraph::op::Constant::create(ngraph::element::i64, ngraph::Shape{3}, {4, 1, rows - 5}); + auto region_split = std::make_shared(region, axis, region_splits); + std::shared_ptr scale; { - auto lower_bounds = std::make_shared(ngraph::element::i64, ngraph::Shape{2}, std::vector{4, 0}); - auto upper_bounds = std::make_shared(ngraph::element::i64, ngraph::Shape{2}, std::vector{5, cols}); - scale = std::make_shared(region, lower_bounds, upper_bounds, strides, std::vector{}, std::vector{}); - - if (classfix == -1) - { - auto thresh_node = std::make_shared(ngraph::element::f32, ngraph::Shape{1}, std::vector{0.5}); - auto mask = std::make_shared(scale, thresh_node); - auto zero_node = std::make_shared(ngraph::element::f32, mask->get_shape(), std::vector(b * cols, 0)); - scale = std::make_shared(mask, scale, zero_node); - } + float thr = classfix == -1 ? 0.5 : 0; + auto thresh_node = std::make_shared(ngraph::element::f32, ngraph::Shape{1}, std::vector{thr}); + auto mask = std::make_shared(region_split->output(1), thresh_node); + auto zero_node = std::make_shared(ngraph::element::f32, mask->get_shape(), std::vector(cols, 0)); + scale = std::make_shared(mask, zero_node, region_split->output(1)); } std::shared_ptr probs; { - auto lower_bounds = std::make_shared(ngraph::element::i64, ngraph::Shape{2}, std::vector{5, 0}); - auto upper_bounds = std::make_shared(ngraph::element::i64, ngraph::Shape{2}, std::vector{rows, cols}); - auto classes = std::make_shared(region, lower_bounds, upper_bounds, strides, std::vector{}, std::vector{}); - probs = std::make_shared(classes, scale, ngraph::op::AutoBroadcastType::NUMPY); - + probs = std::make_shared(region_split->output(2), scale, ngraph::op::AutoBroadcastType::NUMPY); auto thresh_node = std::make_shared(ngraph::element::f32, ngraph::Shape{1}, &thresh); auto mask = std::make_shared(probs, thresh_node); auto zero_node = std::make_shared(ngraph::element::f32, mask->get_shape(), std::vector((rows - 5) * cols, 0));