Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / clDNN / tests / test_cases / upsampling_gpu_test.cpp
1 /*
2 // Copyright (c) 2016 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
18 #include <gtest/gtest.h>
19 #include "api/CPP/memory.hpp"
20 #include <api/CPP/input_layout.hpp>
21 #include "api/CPP/upsampling.hpp"
22 #include <api/CPP/topology.hpp>
23 #include <api/CPP/network.hpp>
24 #include <api/CPP/engine.hpp>
25 #include "test_utils/test_utils.h"
26 #include <api/CPP/reorder.hpp>
27 #include <api/CPP/data.hpp>
28
29 using namespace cldnn;
30 using namespace tests;
31
32 TEST(upsampling_gpu, basic_in2x3x2x2_nearest) {
33     //  Input  : 2x2x3x2
34     //  Output : 2x2x6x4
35     //  Sample Type: Nearest
36
37     //  Input:
38     //  f0: b0:  1    2  -10   b1:   0    0     -11
39     //  f0: b0:  3    4  -14   b1:   0.5 -0.5   -15  
40     //  f1: b0:  5    6  -12   b1:   1.5  5.2   -13     
41     //  f1: b0:  7    8  -16   b1:   12   9     -17
42     //
43
44     const auto& engine = get_test_engine();
45
46     auto input = memory::allocate(engine, { data_types::f32, format::bfyx, { 2, 2, 3, 2 } });
47
48     uint32_t scale = 2;
49
50     topology topology;
51     topology.add(input_layout("input", input.get_layout()));
52     topology.add(upsampling("upsampling", "input", scale, 0, upsampling_sample_type::nearest));
53
54     set_values(input, {
55         1.f, 2.f, -10.f,
56         3.f, 4.f, -14.f,
57         5.f, 6.f, -12.f,
58         7.f, 8.f, -16.f,
59         0.f, 0.f, -11.f,
60         0.5f, -0.5f, -15.f,
61         1.5f, 5.2f, -13.f,
62         12.f, 9.f, -17.f,
63     });
64
65     network network(engine, topology);
66
67     network.set_input_data("input", input);
68
69     auto outputs = network.execute();
70
71     auto output = outputs.at("upsampling").get_memory();
72     auto output_ptr = output.pointer<float>();
73
74     float answers[96] = {
75         1.f, 1.f, 2.f,   2.f,   -10.f,  -10.f,
76         1.f, 1.f, 2.f,   2.f,   -10.f,  -10.f,
77         3.f, 3.f, 4.f,   4.f,   -14.f,  -14.f,
78         3.f, 3.f, 4.f,   4.f,   -14.f,  -14.f,
79         5.f, 5.f, 6.f,   6.f,   -12.f,  -12.f,
80         5.f, 5.f, 6.f,   6.f,   -12.f,  -12.f,
81         7.f, 7.f, 8.f,   8.f,   -16.f,  -16.f,
82         7.f, 7.f, 8.f,   8.f,   -16.f,  -16.f,
83         0.f, 0.f, 0.f,   0.f,   -11.f,  -11.f,
84         0.f, 0.f, 0.f,   0.f,   -11.f,  -11.f,
85         0.5f,0.5f, -0.5f, -0.5f, -15.f,  -15.f,
86         0.5f,0.5f, -0.5f, -0.5f, -15.f,  -15.f,
87         1.5f,1.5f, 5.2f,  5.2f,  -13.f,  -13.f,
88         1.5f,1.5f, 5.2f,  5.2f,  -13.f,  -13.f,
89         12.f,12.f, 9.f,   9.f,  -17.f,  -17.f,
90         12.f,12.f, 9.f,   9.f,  -17.f,  -17.f,
91     };
92
93     for (int i = 0; i < 2; ++i) { //B
94         for (int j = 0; j < 2; ++j) { //F
95             for (int k = 0; k < 4; ++k) { //Y
96                 for (int l = 0; l < 6; ++l) { //X
97                     auto linear_id = l + k * 6 + j * 4 * 6 + i * 2 * 4 * 6;
98                     EXPECT_TRUE(are_equal(answers[linear_id], output_ptr[linear_id]));
99                 }
100             }
101         }
102     }
103 }
104
105 TEST(upsampling_gpu, basic_in2x3x2x2_bilinear) {
106     //  Input  : 1x1x2x2
107     //  Output : 1x1x4x4
108     //  Sample Type: Nearest
109
110     //  Input:
111     //  f0: b0:  1    2
112     //  f0: b0:  3    4
113     //
114
115     const auto& engine = get_test_engine();
116
117     auto input = memory::allocate(engine, { data_types::f32, format::bfyx,{ 1, 1, 2, 2 } });
118
119     uint32_t scale = 2;
120
121     topology topology;
122     topology.add(input_layout("input", input.get_layout()));
123     topology.add(upsampling("upsampling", "input", scale, 1, upsampling_sample_type::bilinear));
124
125     set_values(input, {
126         1.f, 2.f,
127         3.f, 4.f,
128     });
129
130     network network(engine, topology);
131
132     network.set_input_data("input", input);
133
134     auto outputs = network.execute();
135
136     auto output = outputs.at("upsampling").get_memory();
137     auto output_ptr = output.pointer<float>();
138
139     float answers[16] = {
140         0.5625f, 0.9375f, 1.3125f, 1.125f,
141         1.125f, 1.75f, 2.25f, 1.875f,
142         1.875f, 2.75f, 3.25f, 2.625f,
143         1.6875f, 2.4375f, 2.8125f, 2.25f,
144     };
145
146     for (int k = 0; k < 4; ++k) { //Y
147         for (int l = 0; l < 4; ++l) { //X
148             auto linear_id = l + k * 4;
149             EXPECT_NEAR(answers[linear_id], output_ptr[linear_id], 1e-05F);
150         }
151     }
152 }