1#
2# Copyright (C) 2019 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
17inp = Input("input", "TENSOR_QUANT8_ASYMM_SIGNED", "{3, 2, 3, 1}, 2.0, 0")
18inp_data = [
19    -127, -127, -127, -126, -126, -126, -125, -125, -125, -124, -124, -124,
20    -123, -123, -123, -122, -122, -122
21]
22begin = Input("begin", "TENSOR_INT32", "{4}")
23begin_data = [1, 0, 0, 0]
24size = Input("size", "TENSOR_INT32", "{4}")
25size_data = [2, 1, 3, 1]
26output = Output("output", "TENSOR_QUANT8_ASYMM_SIGNED", "{2, 1, 3, 1}, 2.0, 0")
27output_data = [-125, -125, -125, -123, -123, -123]
28
29model = Model().Operation("SLICE", inp, begin, size).To(output)
30Example(
31    {
32        inp: inp_data,
33        begin: begin_data,
34        size: size_data,
35        output: output_data,
36    },
37    model=model)
38
39# zero-sized input
40
41# Use BOX_WITH_NMS_LIMIT op to generate a zero-sized internal tensor for box cooridnates.
42p1 = Parameter("scores", "TENSOR_FLOAT32", "{1, 2}", [0.90, 0.10])  # scores
43p2 = Parameter("roi", "TENSOR_FLOAT32", "{1, 8}",
44               [1, 1, 10, 10, 0, 0, 10, 10])  # roi
45o1 = Output("scoresOut", "TENSOR_FLOAT32", "{0}")  # scores out
46o2 = Output("classesOut", "TENSOR_INT32", "{0}")  # classes out
47tmp1 = Internal("roiOut", "TENSOR_FLOAT32", "{0, 4}")  # roi out
48tmp2 = Internal("batchSplitOut", "TENSOR_INT32", "{0}")  # batch split out
49model = Model("zero_sized").Operation("BOX_WITH_NMS_LIMIT", p1, p2, [0], 0.3,
50                                      -1, 0, 0.4, 1.0,
51                                      0.3).To(o1, tmp1, o2, tmp2)
52
53# Use ROI_ALIGN op to convert into zero-sized feature map.
54layout = BoolScalar("layout", False)  # NHWC
55i1 = Input("in", "TENSOR_FLOAT32", "{1, 1, 1, 1}")
56zero_sized = Internal("featureMap", "TENSOR_FLOAT32", "{0, 2, 2, 1}")
57model = model.Operation("ROI_ALIGN", i1, tmp1, tmp2, 2, 2, 2.0, 2.0, 4, 4,
58                        layout).To(zero_sized)
59
60# SLICE op with numBatches = 0.
61o3 = Output("out", "TENSOR_FLOAT32", "{0, 1, 1, 1}")  # out
62model = model.Operation("SLICE", zero_sized, [0, 1, 1, 0],
63                        [-1, 1, -1, 1]).To(o3)
64
65quant8_signed = DataTypeConverter().Identify({
66    p1: ("TENSOR_QUANT8_ASYMM_SIGNED", 0.1, 0),
67    p2: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
68    o1: ("TENSOR_QUANT8_ASYMM_SIGNED", 0.1, 0),
69    tmp1: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
70    i1: ("TENSOR_QUANT8_ASYMM_SIGNED", 0.1, 0),
71    zero_sized: ("TENSOR_QUANT8_ASYMM_SIGNED", 0.1, 0),
72    o3: ("TENSOR_QUANT8_ASYMM_SIGNED", 0.1, 0)
73})
74
75Example({
76    i1: [1],
77    o1: [],
78    o2: [],
79    o3: [],
80}).AddVariations(quant8_signed, includeDefault=False)
81