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_ALIGN_1, outputShape = [2, 2], spatialScale = [0.5, 0.5], samplingRatio = [4, 4]
20i1 = Input("in", "TENSOR_FLOAT32", "{1, 4, 4, 1}")
21roi1 = Input("roi", "TENSOR_FLOAT32", "{4, 4}")
22o1 = Output("out", "TENSOR_FLOAT32", "{4, 2, 2, 1}")
23Model().Operation("ROI_ALIGN", i1, roi1, [0, 0, 0, 0], 2, 2, 2.0, 2.0, 4, 4, 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.0625, 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, 8, 8,
42        2, 0, 4, 8,
43        0, 2, 8, 4
44    ],
45    o1: [
46        0.375, 5.125, -0.375, 2.875,
47        -0.5, -0.3125, 3.1875, 1.125,
48         0.25, 4.25, 4.875, 0.625,
49        -0.1875, 1.125, 0.9375, -2.625
50    ]
51}).AddNchw(i1, o1, layout).AddVariations("relaxed", quant8, "float16")
52
53
54# TEST 2: ROI_ALIGN_2, outputShape = [2, 3], spatialScale = [0.25, 0.25], samplingRatio = [4, 4]
55i2 = Input("in", "TENSOR_FLOAT32", "{4, 4, 8, 2}")
56roi2 = Input("roi", "TENSOR_FLOAT32", "{4, 4}")
57o2 = Output("out", "TENSOR_FLOAT32", "{4, 2, 3, 2}")
58Model().Operation("ROI_ALIGN", i2, roi2, [0, 0, 3, 3], 2, 3, 4.0, 4.0, 4, 4, layout).To(o2)
59
60quant8 = DataTypeConverter().Identify({
61    i2: ("TENSOR_QUANT8_ASYMM", 0.04, 0),
62    roi2: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
63    o2: ("TENSOR_QUANT8_ASYMM", 0.03125, 10)
64})
65
66# Instantiate an example
67Example({
68    i2: [
69        8.84, 8.88, 7.41, 5.60, 9.95, 4.37, 0.10, 7.64, 6.50, 9.47,
70        7.55, 3.00, 0.89, 3.01, 6.30, 4.40, 1.64, 6.74, 6.16, 8.60,
71        5.85, 3.17, 7.12, 6.79, 5.77, 6.62, 5.13, 8.44, 5.08, 7.12,
72        2.84, 1.19, 8.37, 0.90, 7.86, 9.69, 1.97, 1.31, 4.42, 9.89,
73        0.18, 9.00, 9.30, 0.44, 5.05, 6.47, 1.09, 9.50, 1.30, 2.18,
74        2.05, 7.74, 7.66, 0.65, 4.18, 7.14, 5.35, 7.90, 1.04, 1.47,
75        9.01, 0.95, 4.07, 0.65,
76        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
77        0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00,
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,
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, 0.00, 0.00, 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,
90        5.47, 2.64, 0.86, 4.86, 2.38, 2.45, 8.77, 0.06, 3.60, 9.28,
91        5.84, 8.97, 6.89, 1.43, 3.90, 5.91, 7.40, 9.25, 3.12, 4.92,
92        1.87, 3.22, 9.50, 6.73, 2.07, 7.30, 3.07, 4.97, 0.24, 8.91,
93        1.09, 0.27, 7.29, 6.94, 2.31, 6.88, 4.33, 1.37, 0.86, 0.46,
94        6.07, 3.81, 0.86, 6.99, 4.36, 1.92, 8.19, 3.57, 7.90, 6.78,
95        4.64, 6.82, 6.18, 9.63, 2.63, 2.33, 1.36, 2.70, 9.99, 9.85,
96        8.06, 4.80, 7.80, 5.43
97    ],
98    roi2: [
99        4, 4, 28, 12,
100        4, 4, 32, 16,
101        7, 1, 29, 15,   # test rounding
102        1, 7,  9, 11    # test roi with shape smaller than output
103    ],
104    o2: [
105        5.150000, 5.491250, 4.733750, 7.100000, 4.827500,
106        5.843750, 4.721250, 4.797500, 3.750000, 6.592500,
107        5.452500, 3.362500,
108        4.899396, 5.861696, 4.941504, 5.979741, 3.182904,
109        6.111551, 5.141833, 4.631891, 3.903325, 4.627793,
110        5.537240, 1.356019,
111        4.845915, 3.618338, 3.301958, 6.250566, 2.930461,
112        4.269676, 3.642174, 4.201423, 5.008657, 5.735293,
113        7.426004, 4.819665,
114        4.518229, 6.887344, 2.952656, 5.565781, 3.952786,
115        2.552812, 5.191667, 6.854167, 3.920000, 6.512500,
116        4.886250, 5.497708
117    ]
118}).AddNchw(i2, o2, layout).AddVariations("relaxed", quant8, "float16")
119
120
121# TEST 3: ROI_ALIGN_3, outputShape = [2, 3], spatialScale = [0.25, 0.25], samplingRatio = [0, 0]
122i3 = Input("in", "TENSOR_FLOAT32", "{2, 4, 8, 2}")
123roi3 = Input("roi", "TENSOR_FLOAT32", "{4, 4}")
124o3 = Output("out", "TENSOR_FLOAT32", "{4, 2, 3, 2}")
125Model().Operation("ROI_ALIGN", i3, roi3, [0, 0, 1, 1], 2, 3, 4.0, 4.0, 0, 0, layout).To(o3)
126
127quant8 = DataTypeConverter().Identify({
128    i3: ("TENSOR_QUANT8_ASYMM", 0.04, 0),
129    roi3: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
130    o3: ("TENSOR_QUANT8_ASYMM", 0.03125, 10)
131})
132
133# Instantiate an example
134Example({
135    i3: [
136        8.84, 8.88, 7.41, 5.60, 9.95, 4.37, 0.10, 7.64, 6.50, 9.47,
137        7.55, 3.00, 0.89, 3.01, 6.30, 4.40, 1.64, 6.74, 6.16, 8.60,
138        5.85, 3.17, 7.12, 6.79, 5.77, 6.62, 5.13, 8.44, 5.08, 7.12,
139        2.84, 1.19, 8.37, 0.90, 7.86, 9.69, 1.97, 1.31, 4.42, 9.89,
140        0.18, 9.00, 9.30, 0.44, 5.05, 6.47, 1.09, 9.50, 1.30, 2.18,
141        2.05, 7.74, 7.66, 0.65, 4.18, 7.14, 5.35, 7.90, 1.04, 1.47,
142        9.01, 0.95, 4.07, 0.65,
143        5.47, 2.64, 0.86, 4.86, 2.38, 2.45, 8.77, 0.06, 3.60, 9.28,
144        5.84, 8.97, 6.89, 1.43, 3.90, 5.91, 7.40, 9.25, 3.12, 4.92,
145        1.87, 3.22, 9.50, 6.73, 2.07, 7.30, 3.07, 4.97, 0.24, 8.91,
146        1.09, 0.27, 7.29, 6.94, 2.31, 6.88, 4.33, 1.37, 0.86, 0.46,
147        6.07, 3.81, 0.86, 6.99, 4.36, 1.92, 8.19, 3.57, 7.90, 6.78,
148        4.64, 6.82, 6.18, 9.63, 2.63, 2.33, 1.36, 2.70, 9.99, 9.85,
149        8.06, 4.80, 7.80, 5.43
150    ],
151    roi3: [
152        4, 4, 28, 12,
153        4, 4, 32, 16,
154        7, 1, 29, 15,   # test rounding
155        1, 7,  9, 11    # test roi with shape smaller than output
156    ],
157    o3: [
158        5.150000, 5.491250, 4.733750, 7.100000, 4.827500,
159        5.843750, 4.721250, 4.797500, 3.750000, 6.592500,
160        5.452500, 3.362500,
161        4.869884, 5.908148, 4.941701, 5.955718, 3.113403,
162        6.341898, 5.156389, 4.604016, 3.881782, 4.616123,
163        5.690694, 1.237153,
164        5.028047, 3.560944, 3.157656, 6.395469, 2.896243,
165        4.336576, 3.563021, 4.057767, 5.053437, 6.028906,
166        7.396966, 4.668906,
167        4.385000, 6.905000, 2.815000, 5.502500, 4.161667,
168        1.829167, 5.191667, 6.854167, 3.920000, 6.512500,
169        5.106667, 5.612500
170    ]
171}).AddNchw(i3, o3, layout).AddVariations("relaxed", quant8, "float16")
172
173
174# TEST 4: ROI_ALIGN_4, outputShape = [2, 2], spatialScale = [0.5, 1.0], samplingRatio = [0, 4]
175i4 = Input("in", "TENSOR_FLOAT32", "{4, 4, 4, 1}")
176roi4 = Input("roi", "TENSOR_FLOAT32", "{5, 4}")
177o4 = Output("out", "TENSOR_FLOAT32", "{5, 2, 2, 1}")
178Model().Operation("ROI_ALIGN", i4, roi4, [2, 2, 2, 2, 2],  2, 2, 2.0, 1.0, 0, 4, layout).To(o4)
179
180quant8 = DataTypeConverter().Identify({
181    i4: ("TENSOR_QUANT8_ASYMM", 0.25, 128),
182    roi4: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
183    o4: ("TENSOR_QUANT8_ASYMM", 0.0625, 128)
184})
185
186# Instantiate an example
187Example({
188    i4: [
189        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
190        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
191        -10, -1,  4, -5,
192         -8, -2,  9,  1,
193          7, -2,  3, -7,
194         -2, 10, -3,  5,
195        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
196    ],
197    roi4: [
198        1, 2, 2, 4,
199        0, 0, 4, 8,
200        1, 0, 2, 8,
201        0, 2, 4, 4,
202        0, 0, 0, 0
203    ],
204    o4: [
205        0.375, 5.125, -0.375, 2.875,
206        -0.5, -0.3125, 3.1875, 1.125,
207         0.25, 4.25, 4.875, 0.625,
208        -0.1875, 1.125, 0.9375, -2.625,
209        -7.4375, -3.3125, -6.8125, -3.4375
210    ]
211}).AddNchw(i4, o4, layout).AddVariations("relaxed", quant8, "float16")
212
213
214# TEST 5: ROI_ALIGN_zero_sized
215
216# Use BOX_WITH_NMS_LIMIT op to generate a zero-sized internal tensor for box cooridnates.
217p1 = Parameter("scores", "TENSOR_FLOAT32", "{1, 2}", [0.90, 0.10]) # scores
218p2 = Parameter("roi", "TENSOR_FLOAT32", "{1, 8}", [1, 1, 10, 10, 0, 0, 10, 10]) # roi
219o1 = Output("scoresOut", "TENSOR_FLOAT32", "{0}") # scores out
220o2 = Output("classesOut", "TENSOR_INT32", "{0}") # classes out
221tmp1 = Internal("roiOut", "TENSOR_FLOAT32", "{0, 4}") # roi out
222tmp2 = Internal("batchSplitOut", "TENSOR_INT32", "{0}") # batch split out
223model = Model("zero_sized").Operation("BOX_WITH_NMS_LIMIT", p1, p2, [0], 0.3,  -1, 0, 0.4, 1.0, 0.3).To(o1, tmp1, o2, tmp2)
224
225# ROI_ALIGN op with numRois = 0.
226i1 = Input("in", "TENSOR_FLOAT32", "{1, 1, 1, 1}")
227zero_sized = Output("featureMap", "TENSOR_FLOAT32", "{0, 2, 2, 1}")
228model = model.Operation("ROI_ALIGN", i1, tmp1, tmp2, 2, 2, 2.0, 2.0, 4, 4, layout).To(zero_sized)
229
230quant8 = DataTypeConverter().Identify({
231    p1: ("TENSOR_QUANT8_ASYMM", 0.1, 128),
232    p2: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
233    o1: ("TENSOR_QUANT8_ASYMM", 0.1, 128),
234    tmp1: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
235    i1: ("TENSOR_QUANT8_ASYMM", 0.1, 128),
236    zero_sized: ("TENSOR_QUANT8_ASYMM", 0.1, 128)
237})
238
239Example({
240    i1: [0],
241    o1: [],
242    o2: [],
243    zero_sized: [],
244}).AddNchw(i1, zero_sized, layout).AddVariations("relaxed", quant8, "float16")
245
246
247# TEST 6: ROI_ALIGN_6, hanging issue
248i4 = Input("in", "TENSOR_FLOAT32", "{1, 512, 8, 1}")
249roi4 = Input("roi", "TENSOR_FLOAT32", "{1, 4}")
250o4 = Output("out", "TENSOR_FLOAT32", "{1, 128, 4, 1}")
251Model().Operation("ROI_ALIGN", i4, roi4, [0], 128, 4, 1.0, 64.0, 10, 10, layout).To(o4)
252
253quant8 = DataTypeConverter().Identify({
254    i4: ("TENSOR_QUANT8_ASYMM", 0.25, 128),
255    roi4: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
256    o4: ("TENSOR_QUANT8_ASYMM", 0.0625, 128)
257})
258
259# Instantiate an example
260Example({
261    i4: [0] * (512 * 8),
262    roi4: [450, 500, 466, 508],
263    o4: [0] * (128 * 4)
264}).AddNchw(i4, o4, layout).AddVariations("relaxed", quant8, "float16")
265