Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / tests / nnapi / specs / V1_2 / quantize.mod.py
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
17 import numpy as np
18
19 num_values = 300
20 values = list(np.linspace(-10, 10, num_values))
21
22 for 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.
45 p1 = Parameter("scores", "TENSOR_FLOAT32", "{1, 2}", [0.90, 0.10]) # scores
46 p2 = Parameter("roi", "TENSOR_FLOAT32", "{1, 8}", [1, 1, 10, 10, 0, 0, 10, 10]) # roi
47 o1 = Output("scoresOut", "TENSOR_FLOAT32", "{0}") # scores out
48 o2 = Output("classesOut", "TENSOR_INT32", "{0}") # classes out
49 tmp1 = Internal("roiOut", "TENSOR_FLOAT32", "{0, 4}") # roi out
50 tmp2 = Internal("batchSplitOut", "TENSOR_INT32", "{0}") # batch split out
51 model = 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.
54 layout = BoolScalar("layout", False) # NHWC
55 i1 = Input("in", "TENSOR_FLOAT32", "{1, 1, 1, 1}")
56 zero_sized = Internal("featureMap", "TENSOR_FLOAT32", "{0, 2, 2, 1}")
57 model = 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.
60 o3 = Output("out", "TENSOR_QUANT8_ASYMM", "{0, 2, 2, 1}, 0.1f, 128") # out
61 model = model.Operation("QUANTIZE", zero_sized).To(o3)
62
63 # Create test case with dummy values.
64 Example({
65     i1: [1],
66     o1: [0],
67     o2: [0],
68     o3: [0],
69 }).AddVariations("relaxed", "float16")