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
17import numpy as np
18
19num_values = 300
20values = list(np.linspace(-10, 10, num_values))
21
22for input_type in ["TENSOR_FLOAT32", "TENSOR_FLOAT16"]:
23  for scale, offset in [(1.0, 0),
24                        (1.0, 1),
25                        (0.01, 120),
26                        (10.0, 120)]:
27    input0 = Input("input0", input_type, "{%d}" % num_values)
28    output0 = Output("output0", input_type, "{%d}" % num_values)
29
30    model = Model().Operation("QUANTIZE", input0).To(output0)
31
32    quantizeOutput = DataTypeConverter().Identify({
33        output0: ["TENSOR_QUANT8_ASYMM", scale, offset],
34    })
35
36    Example({
37        input0: values,
38        output0: values,
39    }).AddVariations(quantizeOutput, includeDefault=False)
40
41
42# Zero-sized input
43
44# Use BOX_WITH_NMS_LIMIT op to generate a zero-sized internal tensor for box cooridnates.
45p1 = Parameter("scores", "TENSOR_FLOAT32", "{1, 2}", [0.90, 0.10]) # scores
46p2 = Parameter("roi", "TENSOR_FLOAT32", "{1, 8}", [1, 1, 10, 10, 0, 0, 10, 10]) # roi
47o1 = Output("scoresOut", "TENSOR_FLOAT32", "{0}") # scores out
48o2 = Output("classesOut", "TENSOR_INT32", "{0}") # classes out
49tmp1 = Internal("roiOut", "TENSOR_FLOAT32", "{0, 4}") # roi out
50tmp2 = Internal("batchSplitOut", "TENSOR_INT32", "{0}") # batch split out
51model = 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)
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, layout).To(zero_sized)
58
59# QUANTIZE op with numBatches = 0.
60o3 = Output("out", "TENSOR_QUANT8_ASYMM", "{0, 2, 2, 1}, 0.1f, 128") # out
61model = model.Operation("QUANTIZE", zero_sized).To(o3)
62
63Example({
64    i1: [1],
65    o1: [],
66    o2: [],
67    o3: [],
68}).AddVariations("relaxed", "float16")
69