1#
2# Copyright (C) 2018 The Android Open Source Project
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
17layout = BoolScalar("layout", False) # NHWC
18
19# TEST 1: ROI_POOLING_1, outputShape = [2, 2], spatialScale = [0.5, 0.5]
20i1 = Input("in", "TENSOR_FLOAT32", "{1, 4, 4, 1}")
21roi1 = Input("roi", "TENSOR_FLOAT32", "{5, 4}")
22o1 = Output("out", "TENSOR_FLOAT32", "{5, 2, 2, 1}")
23Model().Operation("ROI_POOLING", i1, roi1, [0, 0, 0, 0, 0], 2, 2, 2.0, 2.0, layout).To(o1)
24
25quant8 = DataTypeConverter().Identify({
26    i1: ("TENSOR_QUANT8_ASYMM", 0.25, 128),
27    roi1: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
28    o1: ("TENSOR_QUANT8_ASYMM", 0.25, 128)
29})
30
31# Instantiate an example
32Example({
33    i1: [
34        -10, -1,  4, -5,
35        -8, -2,  9,  1,
36         7, -2,  3, -7,
37        -2, 10, -3,  5
38    ],
39    roi1: [
40        2, 2, 4, 4,
41        0, 0, 6, 6,
42        2, 0, 4, 6,
43        0, 2, 6, 4,
44        8, 8, 8, 8  # empty region
45    ],
46    o1: [
47        -2, 9, -2, 3,
48        -1, 9, 10, 5,
49        -1, 9, 10, 3,
50        -2, 9,  7, 3,
51         0, 0,  0, 0
52    ]
53}).AddNchw(i1, o1, layout).AddVariations("relaxed", quant8, "float16")
54
55
56# TEST 2: ROI_POOLING_2, outputShape = [2, 3], spatialScale = 0.25
57i2 = Input("in", "TENSOR_FLOAT32", "{4, 4, 8, 2}")
58roi2 = Input("roi", "TENSOR_FLOAT32", "{4, 4}")
59o2 = Output("out", "TENSOR_FLOAT32", "{4, 2, 3, 2}")
60Model().Operation("ROI_POOLING", i2, roi2, [0, 0, 3, 3], 2, 3, 4.0, 4.0, layout).To(o2)
61
62quant8 = DataTypeConverter().Identify({
63    i2: ("TENSOR_QUANT8_ASYMM", 0.04, 0),
64    roi2: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
65    o2: ("TENSOR_QUANT8_ASYMM", 0.04, 0)
66})
67
68# Instantiate an example
69Example({
70    i2: [
71        8.84, 8.88, 7.41, 5.60, 9.95, 4.37, 0.10, 7.64, 6.50, 9.47,
72        7.55, 3.00, 0.89, 3.01, 6.30, 4.40, 1.64, 6.74, 6.16, 8.60,
73        5.85, 3.17, 7.12, 6.79, 5.77, 6.62, 5.13, 8.44, 5.08, 7.12,
74        2.84, 1.19, 8.37, 0.90, 7.86, 9.69, 1.97, 1.31, 4.42, 9.89,
75        0.18, 9.00, 9.30, 0.44, 5.05, 6.47, 1.09, 9.50, 1.30, 2.18,
76        2.05, 7.74, 7.66, 0.65, 4.18, 7.14, 5.35, 7.90, 1.04, 1.47,
77        9.01, 0.95, 4.07, 0.65,
78        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
79        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
80        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
81        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
82        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
83        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
84        0.00, 0.00, 0.00, 0.00,
85        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
86        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
87        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
88        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
89        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
90        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
91        0.00, 0.00, 0.00, 0.00,
92        5.47, 2.64, 0.86, 4.86, 2.38, 2.45, 8.77, 0.06, 3.60, 9.28,
93        5.84, 8.97, 6.89, 1.43, 3.90, 5.91, 7.40, 9.25, 3.12, 4.92,
94        1.87, 3.22, 9.50, 6.73, 2.07, 7.30, 3.07, 4.97, 0.24, 8.91,
95        1.09, 0.27, 7.29, 6.94, 2.31, 6.88, 4.33, 1.37, 0.86, 0.46,
96        6.07, 3.81, 0.86, 6.99, 4.36, 1.92, 8.19, 3.57, 7.90, 6.78,
97        4.64, 6.82, 6.18, 9.63, 2.63, 2.33, 1.36, 2.70, 9.99, 9.85,
98        8.06, 4.80, 7.80, 5.43
99    ],
100    roi2: [
101        4, 4, 24, 8,
102        4, 4, 28, 12,
103        7, 1, 25, 11,   # test rounding
104        1, 7,  5, 11    # test roi with shape smaller than output
105    ],
106    o2: [
107        6.16, 8.60, 7.12, 6.79, 5.13, 8.44, 7.86, 9.69, 4.42, 9.89, 9.30, 6.47,
108        7.86, 9.89, 9.30, 9.89, 9.30, 9.50, 7.86, 9.89, 9.30, 9.89, 9.30, 9.50,
109        9.50, 6.73, 9.50, 9.28, 6.89, 8.97, 6.18, 9.63, 9.99, 9.85, 9.99, 9.85,
110        7.29, 6.94, 7.29, 6.94, 2.31, 6.88, 7.90, 6.78, 7.90, 6.82, 4.64, 6.82
111    ]
112}).AddNchw(i2, o2, layout).AddVariations("relaxed", quant8, "float16")
113
114
115# TEST 3: ROI_POOLING_3, outputShape = [2, 2], spatialScale = [0.5, 1]
116i3 = Input("in", "TENSOR_FLOAT32", "{4, 4, 4, 1}")
117roi3 = Input("roi", "TENSOR_FLOAT32", "{5, 4}")
118o3 = Output("out", "TENSOR_FLOAT32", "{5, 2, 2, 1}")
119Model().Operation("ROI_POOLING", i3, roi3, [2, 2, 2, 2, 2], 2, 2, 2.0, 1.0, layout).To(o3)
120
121quant8 = DataTypeConverter().Identify({
122    i3: ("TENSOR_QUANT8_ASYMM", 0.25, 128),
123    roi3: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
124    o3: ("TENSOR_QUANT8_ASYMM", 0.25, 128)
125})
126
127# Instantiate an example
128Example({
129    i3: [
130        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
131        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
132        -10, -1,  4, -5,
133        -8, -2,  9,  1,
134         7, -2,  3, -7,
135        -2, 10, -3,  5,
136        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
137    ],
138    roi3: [
139        1, 2, 2, 4,
140        0, 0, 3, 6,
141        1, 0, 2, 6,
142        0, 2, 3, 4,
143        0, 0, 0, 0
144    ],
145    o3: [
146        -2, 9, -2, 3,
147        -1, 9, 10, 5,
148        -1, 9, 10, 3,
149        -2, 9,  7, 3,
150        -10, -10, -10, -10
151    ]
152}).AddNchw(i3, o3, layout).AddVariations("relaxed", quant8, "float16")
153