1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
7 #include <description_buffer.hpp>
8 #include "ie_built_in_impl.hpp"
9 #include <shape_infer/ie_reshaper.hpp>
10 #include <ie_layers.h>
16 namespace InferenceEngine {
17 namespace ShapeInfer {
20 *@brief Implementation of Shape inference for DetectionOutput layer
22 class TensorIteratorShapeProp : public BuiltInShapeInferImpl {
24 explicit TensorIteratorShapeProp(const std::string& type) : BuiltInShapeInferImpl(type) {}
26 void setOriginalLayer(const CNNLayer *layer) {
27 auto ti = dynamic_cast<const TensorIterator*>(layer);
29 THROW_IE_EXCEPTION << "Error during shape infer. Original layer is not TensorIterator.";
33 void inferShapesImpl(const std::vector<Blob::CPtr>& inBlobs,
34 const std::map<std::string, std::string>& params,
35 const std::map<std::string, Blob::Ptr>& blobs,
36 std::vector<SizeVector>& outShapes) override {
38 TensorIterator ti(lp);
41 ti.body = _original_ti->body;
42 ti.back_edges = _original_ti->back_edges;
43 ti.input_port_map = _original_ti->input_port_map;
44 ti.output_port_map = _original_ti->output_port_map;
45 validate(&ti, inBlobs, params, blobs);
47 // TODO: make util function to calculate num of iteration
48 int num_iteration = 1;
50 // Prepare input shapes for internal body
51 std::map<std::string, std::vector<size_t>> newInShapes;
52 for (auto &port_map : ti.input_port_map) {
53 int ext_port = port_map.from;
54 int int_port = port_map.to;
55 auto int_name = ti.body.inputs[int_port]->name;
57 auto shape = inShapes[ext_port];
58 if (port_map.axis != -1) {
59 int size = shape[port_map.axis];
60 int start = port_map.start < 0
61 ? port_map.start + size + 1
63 int end = port_map.end < 0
64 ? port_map.end + size + 1
67 num_iteration = std::abs(end - start) / std::abs(port_map.stride);
69 // port with iterating through. Change dimension with iteration
70 shape[port_map.axis] = port_map.part_size;
73 newInShapes[int_name] = shape;
77 _body_reshaper = std::make_shared<Reshaper>(_original_ti->body.inputs);
78 _body_reshaper->runNoApply(newInShapes);
80 outShapes.resize(ti.output_port_map.size());
81 for (auto &port_map : ti.output_port_map) {
82 int ext_port = port_map.from;
83 int int_port = port_map.to;
84 auto &int_out_data = ti.body.outputs[int_port];
85 auto shape = _body_reshaper->getResultShapeFor(int_out_data);
87 if (port_map.axis != -1) {
88 // port with iterating through. Change dimension with iteration
89 shape[port_map.axis] *= num_iteration;
92 outShapes[ext_port] = shape;
98 THROW_IE_EXCEPTION << "Request of apply reshape results while shape infer was not finished";
99 _body_reshaper->apply();
104 const TensorIterator* _original_ti;
105 std::shared_ptr<Reshaper> _body_reshaper;
108 } // namespace ShapeInfer
109 } // namespace InferenceEngine