type_prop_layers.cpp 7.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//*****************************************************************************
// Copyright 2017-2019 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************

#include "gtest/gtest.h"

#include "ngraph/ngraph.hpp"
20
#include "ngraph/op/experimental/layers/ctc_greedy_decoder.hpp"
21 22
#include "ngraph/op/experimental/layers/detection_output.hpp"
#include "ngraph/op/experimental/layers/interpolate.hpp"
23 24 25
#include "ngraph/op/experimental/layers/prior_box.hpp"
#include "ngraph/op/experimental/layers/prior_box_clustered.hpp"
#include "ngraph/op/experimental/layers/proposal.hpp"
26
#include "ngraph/op/experimental/layers/psroi_pooling.hpp"
27 28
#include "ngraph/op/experimental/layers/region_yolo.hpp"
#include "ngraph/op/experimental/layers/reorg_yolo.hpp"
29
#include "ngraph/op/experimental/layers/roi_pooling.hpp"
30 31 32 33 34

#include <memory>
using namespace std;
using namespace ngraph;

35 36 37 38 39 40 41 42
TEST(type_prop_layers, ctc_greedy_decoder)
{
    auto input = make_shared<op::Parameter>(element::f32, Shape{88, 2, 48, 1});
    auto seq_len = make_shared<op::Parameter>(element::f32, Shape{88, 2});
    auto op = make_shared<op::CTCGreedyDecoder>(input, seq_len, false);
    ASSERT_EQ(op->get_shape(), (Shape{2, 88, 1, 1}));
}

43 44 45 46 47 48 49 50
TEST(type_prop_layers, detection_output)
{
    auto box_logits = make_shared<op::Parameter>(element::f32, Shape{4, 1, 5, 5});
    auto class_preds = make_shared<op::Parameter>(element::f32, Shape{2, 1, 4, 5});
    auto proposals = make_shared<op::Parameter>(element::f32, Shape{2, 1, 4, 5});
    auto aux_class_preds = make_shared<op::Parameter>(element::f32, Shape{2, 1, 4, 5});
    auto aux_box_preds = make_shared<op::Parameter>(element::f32, Shape{2, 1, 4, 5});
    op::DetectionOutputAttrs attrs;
51
    attrs.keep_top_k = {200};
52 53 54 55 56 57 58 59
    auto op = make_shared<op::DetectionOutput>(
        box_logits, class_preds, proposals, aux_class_preds, aux_box_preds, attrs);
    ASSERT_EQ(op->get_shape(), (Shape{1, 1, 800, 7}));
}

TEST(type_prop_layers, interpolate)
{
    auto image = make_shared<op::Parameter>(element::f32, Shape{2, 2, 33, 65});
60 61
    auto dyn_output_shape = make_shared<op::Parameter>(element::i64, Shape{2});
    auto output_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {15, 30});
62 63

    op::InterpolateAttrs attrs;
64 65 66 67 68 69 70
    attrs.axes = {2, 3};
    auto op = make_shared<op::Interpolate>(image, output_shape, attrs);
    ASSERT_EQ(op->get_shape(), (Shape{2, 2, 15, 30}));

    EXPECT_TRUE(make_shared<op::Interpolate>(image, dyn_output_shape, attrs)
                    ->get_output_partial_shape(0)
                    .same_scheme(PartialShape{2, 2, Dimension::dynamic(), Dimension::dynamic()}));
71 72
}

73 74
TEST(type_prop_layers, prior_box1)
{
75
    op::PriorBoxAttrs attrs;
76 77
    attrs.min_size = {2.0f, 3.0f};
    attrs.aspect_ratio = {1.0f, 2.0f, 0.5f};
78

79 80
    auto layer_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {32, 32});
    auto image_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {300, 300});
81
    auto pb = make_shared<op::PriorBox>(layer_shape, image_shape, attrs);
82 83 84 85 86
    ASSERT_EQ(pb->get_shape(), (Shape{2, 16384}));
}

TEST(type_prop_layers, prior_box2)
{
87
    op::PriorBoxAttrs attrs;
88 89
    attrs.min_size = {2.0f, 3.0f};
    attrs.aspect_ratio = {1.0f, 2.0f, 0.5f};
90 91
    attrs.flip = true;

92 93
    auto layer_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {32, 32});
    auto image_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {300, 300});
94
    auto pb = make_shared<op::PriorBox>(layer_shape, image_shape, attrs);
95 96 97 98 99
    ASSERT_EQ(pb->get_shape(), (Shape{2, 28672}));
}

TEST(type_prop_layers, prior_box3)
{
100
    op::PriorBoxAttrs attrs;
101 102 103
    attrs.min_size = {256.0f};
    attrs.max_size = {315.0f};
    attrs.aspect_ratio = {2.0f};
104
    attrs.flip = true;
105
    attrs.scale_all_sizes = true;
106

107 108
    auto layer_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {1, 1});
    auto image_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {300, 300});
109
    auto pb = make_shared<op::PriorBox>(layer_shape, image_shape, attrs);
110 111 112 113 114
    ASSERT_EQ(pb->get_shape(), (Shape{2, 16}));
}

TEST(type_prop_layers, prior_box_clustered)
{
115 116 117 118 119
    op::PriorBoxClusteredAttrs attrs;
    attrs.num_priors = 3;
    attrs.widths = {4.0f, 2.0f, 3.2f};
    attrs.heights = {1.0f, 2.0f, 1.1f};

120 121
    auto layer_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {19, 19});
    auto image_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {300, 300});
122
    auto pbc = make_shared<op::PriorBoxClustered>(layer_shape, image_shape, attrs);
123 124 125 126 127 128
    // Output shape - 4 * 19 * 19 * 3 (num_priors)
    ASSERT_EQ(pbc->get_shape(), (Shape{2, 4332}));
}

TEST(type_prop_layers, proposal)
{
129 130 131 132 133
    op::ProposalAttrs attrs;
    attrs.base_size = 1;
    attrs.pre_nms_topn = 20;
    attrs.post_nms_topn = 200;

134 135 136
    auto class_probs = make_shared<op::Parameter>(element::f32, Shape{1, 12, 34, 62});
    auto class_logits = make_shared<op::Parameter>(element::f32, Shape{1, 24, 34, 62});
    auto image_shape = op::Constant::create<int64_t>(element::i64, Shape{2}, {1, 6});
137
    auto op = make_shared<op::Proposal>(class_probs, class_logits, image_shape, attrs);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    ASSERT_EQ(op->get_shape(), (Shape{200, 5}));
}

TEST(type_prop_layers, region_yolo1)
{
    auto inputs = make_shared<op::Parameter>(element::f32, Shape{1, 125, 13, 13});
    auto op = make_shared<op::RegionYolo>(inputs, 0, 0, 0, true, std::vector<int64_t>{}, 0, 1);
    ASSERT_EQ(op->get_shape(), (Shape{1 * 125, 13, 13}));
}

TEST(type_prop_layers, region_yolo2)
{
    auto inputs = make_shared<op::Parameter>(element::f32, Shape{1, 125, 13, 13});
    auto op = make_shared<op::RegionYolo>(inputs, 0, 0, 0, true, std::vector<int64_t>{}, 0, 2);
    ASSERT_EQ(op->get_shape(), (Shape{1 * 125 * 13, 13}));
}

TEST(type_prop_layers, region_yolo3)
{
    auto inputs = make_shared<op::Parameter>(element::f32, Shape{1, 125, 13, 13});
    auto op =
        make_shared<op::RegionYolo>(inputs, 4, 80, 1, false, std::vector<int64_t>{6, 7, 8}, 0, -1);
    ASSERT_EQ(op->get_shape(), (Shape{1, (80 + 4 + 1) * 3, 13, 13}));
}

TEST(type_prop_layers, reorg_yolo)
{
    auto inputs = make_shared<op::Parameter>(element::f32, Shape{2, 24, 34, 62});
    auto op = make_shared<op::ReorgYolo>(inputs, Strides{2});
    ASSERT_EQ(op->get_shape(), (Shape{2, 96, 17, 31}));
}
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

TEST(type_prop_layers, psroi_pooling)
{
    auto inputs = make_shared<op::Parameter>(element::f32, Shape{1, 3, 4, 5});
    auto coords = make_shared<op::Parameter>(element::f32, Shape{150, 5});
    auto op = make_shared<op::PSROIPooling>(inputs, coords, 2, 6, 0.0625, Shape{}, "Avg");
    ASSERT_EQ(op->get_shape(), (Shape{150, 2, 6, 6}));
}

TEST(type_prop_layers, roi_pooling)
{
    auto inputs = make_shared<op::Parameter>(element::f32, Shape{2, 3, 4, 5});
    auto coords = make_shared<op::Parameter>(element::f32, Shape{150, 5});
    auto op = make_shared<op::ROIPooling>(inputs, coords, Shape{6, 6}, 0.0625, "Max");
    ASSERT_EQ(op->get_shape(), (Shape{150, 3, 6, 6}));
}