Skip to content

Commit

Permalink
ExperimentalDetectron* shape infer old-style (openvinotoolkit#1962)
Browse files Browse the repository at this point in the history
* ExperimentalDetectron* shape infer old-style

* nGraph reshape tests
  • Loading branch information
Evgenya Stepyreva authored and Rom committed Aug 28, 2020
1 parent 6000589 commit 27904ee
Show file tree
Hide file tree
Showing 8 changed files with 701 additions and 2 deletions.
5 changes: 3 additions & 2 deletions inference-engine/src/inference_engine/generic_ie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ void ngraph::op::GenericIE::validate_and_infer_types() {
}
}

// WA: Proposal shape infer has to know number of outputs
if (type == "Proposal" && parameters.find("num_outputs") == parameters.end()) {
// WA: shape infer has to know number of outputs
if ((type == "Proposal" || type == "ExperimentalDetectronROIFeatureExtractor" || type == "ExperimentalDetectronDetectionOutput")
&& parameters.find("num_outputs") == parameters.end()) {
parameters["num_outputs"] = std::to_string(outputs.size());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "ie_deconv_shape_infer.hpp"
#include "ie_deformable_conv_shape_infer.hpp"
#include "ie_depth_to_space_shape_infer.hpp"
#include "ie_detectionoutput_onnx_shape_infer.hpp"
#include "ie_detection_output_shape_infer.hpp"
#include "ie_eltwise_shape_infer.hpp"
#include "ie_equal_shape_infer.hpp"
Expand All @@ -35,6 +36,8 @@
#include "ie_pool_shape_infer.hpp"
#include "ie_priorbox_clustered_shape_infer.hpp"
#include "ie_priorbox_shape_infer.hpp"
#include "ie_priorgridgenerator_onnx_shape_infer.hpp"
#include "ie_proposal_onnx_shape_infer.hpp"
#include "ie_proposal_shape_infer.hpp"
#include "ie_psroi_pooling_shape_infer.hpp"
#include "ie_quantize_shape_infer.hpp"
Expand All @@ -48,6 +51,7 @@
#include "ie_rnn_cell_shape_infer.hpp"
#include "ie_rnn_shape_infer.hpp"
#include "ie_roi_pooling_shape_infer.hpp"
#include "ie_roifeatureextractor_onnx_shape_infer.hpp"
#include "ie_scatter_shape_infer.hpp"
#include "ie_select_shape_infer.hpp"
#include "ie_shape_shape_infer.hpp"
Expand All @@ -65,6 +69,7 @@
#include "ie_tensor_iterator_shape_infer.hpp"
#include "ie_tile_shape_infer.hpp"
#include "ie_topk_shape_infer.hpp"
#include "ie_topkrois_onnx_shape_infer.hpp"
#include "ie_unique_shape_infer.hpp"
#include "ie_unsqueeze_shape_infer.hpp"
#include "ie_upsampling_shape_infer.hpp"
Expand Down Expand Up @@ -157,6 +162,11 @@ REG_SHAPE_INFER_FOR_TYPE(ReshapeShapeProp, Reshape);
REG_SHAPE_INFER_FOR_TYPE(DetectionOutputShapeProp, DetectionOutput);
REG_SHAPE_INFER_FOR_TYPE(PriorBoxClusteredShapeProp, PriorBoxClustered);
REG_SHAPE_INFER_FOR_TYPE(PriorBoxShapeProp, PriorBox);
REG_SHAPE_INFER_FOR_TYPE(ExperimentalDetectronDetectionOutputShapeProp, ExperimentalDetectronDetectionOutput);
REG_SHAPE_INFER_FOR_TYPE(ExperimentalDetectronPriorGridGeneratorShapeProp, ExperimentalDetectronPriorGridGenerator);
REG_SHAPE_INFER_FOR_TYPE(ExperimentalDetectronGenerateProposalsSingleImageShapeProp, ExperimentalDetectronGenerateProposalsSingleImage);
REG_SHAPE_INFER_FOR_TYPE(ExperimentalDetectronROIFeatureExtractorShapeProp, ExperimentalDetectronROIFeatureExtractor);
REG_SHAPE_INFER_FOR_TYPE(ExperimentalDetectronTopKROIsShapeProp, ExperimentalDetectronTopKROIs);
REG_SHAPE_INFER_FOR_TYPE(RoiPoolingShapeProp, ROIPooling);
REG_SHAPE_INFER_FOR_TYPE(PSRoiPoolingShapeProp, PSROIPooling);
REG_SHAPE_INFER_FOR_TYPE(UpsamplingShapeProp, Upsampling);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (C) 2018-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <map>
#include <memory>
#include <string>
#include <vector>

#include "ie_built_in_impl.hpp"

namespace InferenceEngine {
namespace ShapeInfer {

/**
*@brief Implementation of Shape inference for ExperimentalDetectronDetectionOutput layer
*/
class ExperimentalDetectronDetectionOutputShapeProp : public BuiltInShapeInferImpl {
protected:
const int ROIS = 0;
const int FEATMAPS = 1;

public:
explicit ExperimentalDetectronDetectionOutputShapeProp(const std::string& type): BuiltInShapeInferImpl(type) {}

void inferShapesImpl(const std::vector<Blob::CPtr>& inBlobs, const std::map<std::string, std::string>& params,
const std::map<std::string, Blob::Ptr>& blobs, std::vector<SizeVector>& outShapes) override {
LayerParams lp {};
CNNLayer cnnLayer(lp);
cnnLayer.params = params;
cnnLayer.type = _type;
validate(&cnnLayer, inBlobs, params, blobs);

auto rois_num = cnnLayer.GetParamAsUInt("max_detections_per_image");
outShapes.push_back({rois_num, 4});

auto num_outputs = cnnLayer.GetParamAsUInt("num_outputs");
if (num_outputs > 3) THROW_IE_EXCEPTION << "Incorrect value num_outputs: " << num_outputs;
if (num_outputs >= 2) {
outShapes.push_back({rois_num});
}
if (num_outputs == 3) {
outShapes.push_back({rois_num});
}
}
};

} // namespace ShapeInfer
} // namespace InferenceEngine
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (C) 2018-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <ie_layers.h>

#include <description_buffer.hpp>
#include <map>
#include <memory>
#include <string>
#include <vector>

#include "ie_built_in_impl.hpp"

namespace InferenceEngine {
namespace ShapeInfer {

/**
*@brief Implementation of Shape inference for ExperimentalDetectronPriorGridGenerator layer
*/
class ExperimentalDetectronPriorGridGeneratorShapeProp : public BuiltInShapeInferImpl {
protected:
const int PRIORS = 0;
const int FEATMAP = 1;
const int H = 2;
const int W = 3;

public:
explicit ExperimentalDetectronPriorGridGeneratorShapeProp(const std::string& type): BuiltInShapeInferImpl(type) {}

void inferShapesImpl(const std::vector<Blob::CPtr>& inBlobs, const std::map<std::string, std::string>& params,
const std::map<std::string, Blob::Ptr>& blobs, std::vector<SizeVector>& outShapes) override {
LayerParams lp {};
CNNLayer cnnLayer(lp);
cnnLayer.params = params;
cnnLayer.type = _type;
validate(&cnnLayer, inBlobs, params, blobs);

const auto& priors_shape = inShapes.at(PRIORS);
const auto priors_num = priors_shape.at(0);
const auto& featmap_shape = inShapes.at(FEATMAP);
const auto grid_height = featmap_shape.at(H);
const auto grid_width = featmap_shape.at(W);

const bool flatten = cnnLayer.GetParamAsBool("flatten", true);
if (flatten) {
outShapes.push_back({grid_height * grid_width * priors_num, 4});
} else {
outShapes.push_back({grid_height, grid_width, priors_num, 4});
}
}
};

} // namespace ShapeInfer
} // namespace InferenceEngine
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (C) 2018-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <map>
#include <memory>
#include <string>
#include <vector>

#include "ie_built_in_impl.hpp"

namespace InferenceEngine {
namespace ShapeInfer {

/**
*@brief Implementation of Shape inference for ExperimentalDetectronGenerateProposalsSingleImage layer
*/
class ExperimentalDetectronGenerateProposalsSingleImageShapeProp : public BuiltInShapeInferImpl {
public:
explicit ExperimentalDetectronGenerateProposalsSingleImageShapeProp(const std::string& type): BuiltInShapeInferImpl(type) {}

void inferShapesImpl(const std::vector<Blob::CPtr>& inBlobs, const std::map<std::string, std::string>& params,
const std::map<std::string, Blob::Ptr>& blobs, std::vector<SizeVector>& outShapes) override {
LayerParams lp {};
CNNLayer cnnLayer(lp);
cnnLayer.params = params;
cnnLayer.type = _type;
validate(&cnnLayer, inBlobs, params, blobs);

size_t post_nms_count = static_cast<size_t>(cnnLayer.GetParamAsInt("post_nms_count"));

outShapes.push_back({post_nms_count, 4});
outShapes.push_back({post_nms_count, });
}
};

} // namespace ShapeInfer
} // namespace InferenceEngine
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (C) 2018-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <map>
#include <memory>
#include <string>
#include <vector>

#include "ie_built_in_impl.hpp"

namespace InferenceEngine {
namespace ShapeInfer {

/**
*@brief Implementation of Shape inference for ExperimentalDetectronROIFeatureExtractor layer
*/
class ExperimentalDetectronROIFeatureExtractorShapeProp : public BuiltInShapeInferImpl {
protected:
const int ROIS = 0;
const int FEATMAPS = 1;

public:
explicit ExperimentalDetectronROIFeatureExtractorShapeProp(const std::string& type): BuiltInShapeInferImpl(type) {}

void inferShapesImpl(const std::vector<Blob::CPtr>& inBlobs, const std::map<std::string, std::string>& params,
const std::map<std::string, Blob::Ptr>& blobs, std::vector<SizeVector>& outShapes) override {
LayerParams lp {};
CNNLayer cnnLayer(lp);
cnnLayer.params = params;
cnnLayer.type = _type;
validate(&cnnLayer, inBlobs, params, blobs);

size_t rois_num = inShapes.at(ROIS).at(0);
size_t channels_num = inShapes.at(FEATMAPS).at(1);
size_t output_size = static_cast<size_t>(cnnLayer.GetParamAsInt("output_size"));
outShapes.push_back({rois_num, channels_num, output_size, output_size});

auto num_outputs = cnnLayer.GetParamAsUInt("num_outputs");
if (num_outputs > 2) THROW_IE_EXCEPTION << "Incorrect value num_outputs: " << num_outputs;
if (num_outputs == 2) {
outShapes.push_back({rois_num, 4});
}
}
};

} // namespace ShapeInfer
} // namespace InferenceEngine
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (C) 2018-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

#pragma once

#include <ie_layers.h>

#include <description_buffer.hpp>
#include <map>
#include <memory>
#include <string>
#include <vector>

#include "ie_built_in_impl.hpp"

namespace InferenceEngine {
namespace ShapeInfer {

/**
*@brief Implementation of Shape inference for ExperimentalDetectronTopKROIs layer
*/
class ExperimentalDetectronTopKROIsShapeProp : public BuiltInShapeInferImpl {
public:
explicit ExperimentalDetectronTopKROIsShapeProp(const std::string& type): BuiltInShapeInferImpl(type) {}

void inferShapesImpl(const std::vector<Blob::CPtr>& inBlobs, const std::map<std::string, std::string>& params,
const std::map<std::string, Blob::Ptr>& blobs, std::vector<SizeVector>& outShapes) override {
LayerParams lp {};
CNNLayer cnnLayer(lp);
cnnLayer.params = params;
cnnLayer.type = _type;
validate(&cnnLayer, inBlobs, params, blobs);

const bool max_rois = cnnLayer.GetParamAsInt("max_rois");
outShapes.push_back({max_rois, 4});
}
};

} // namespace ShapeInfer
} // namespace InferenceEngine
Loading

0 comments on commit 27904ee

Please sign in to comment.