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
17layout = BoolScalar("layout", False) # NHWC
18
19# Operation 1, GENERATE_PROPOSALS
20scores = Input("scores", "TENSOR_FLOAT32", "{1, 1, 1, 1}")
21deltas = Input("deltas", "TENSOR_FLOAT32", "{1, 1, 1, 4}")
22anchors = Input("anchors", "TENSOR_FLOAT32", "{1, 4}")
23image = Input("imageInfo", "TENSOR_FLOAT32", "{1, 2}")
24scoresOut_1 = Output("scores", "TENSOR_FLOAT32", "{0}")
25roiOut_1 = Internal("roi", "TENSOR_FLOAT32", "{0, 4}")
26batchOut_1 = Internal("batches", "TENSOR_INT32", "{0}")
27model = Model("zero_sized").Operation("GENERATE_PROPOSALS", scores, deltas, anchors, image, 1.0, 1.0, -1, -1, 0.3, 10.0, layout).To(scoresOut_1, roiOut_1, batchOut_1)
28
29# Operation 2, ROI_ALIGN
30feature = Input("featureMap", "TENSOR_FLOAT32", "{1, 1, 1, 1}")
31featureOut_2 = Internal("scores", "TENSOR_FLOAT32", "{0, 2, 2, 1}")
32model = model.Operation("ROI_ALIGN", feature, roiOut_1, batchOut_1, 2, 2, 1.0, 1.0, 4, 4, layout).To(featureOut_2)
33
34# Operation 3, FULLY_CONNECTED
35weights_3 = Parameter("weights", "TENSOR_FLOAT32", "{8, 4}", [1] * 32)
36bias_3 = Parameter("bias", "TENSOR_FLOAT32", "{8}", [1] * 8)
37deltaOut_3 = Internal("delta", "TENSOR_FLOAT32", "{0, 8}")
38model = model.Operation("FULLY_CONNECTED", featureOut_2, weights_3, bias_3, 0).To(deltaOut_3)
39
40# Operation 4, FULLY_CONNECTED
41weights_4 = Parameter("weights", "TENSOR_FLOAT32", "{2, 4}", [1] * 8)
42bias_4 = Parameter("bias", "TENSOR_FLOAT32", "{2}", [1] * 2)
43scoresOut_4 = Internal("scores", "TENSOR_FLOAT32", "{0, 2}")
44model = model.Operation("FULLY_CONNECTED", featureOut_2, weights_4, bias_4, 0).To(scoresOut_4)
45
46# Operation 5, AXIS_ALIGNED_BBOX_TRANSFORM
47roiOut_5 = Internal("roi", "TENSOR_FLOAT32", "{0, 8}")
48model = model.Operation("AXIS_ALIGNED_BBOX_TRANSFORM", roiOut_1, deltaOut_3, batchOut_1, image).To(roiOut_5)
49
50# Operation 6, BOX_WITH_NMS_LIMIT
51scoresOut_6 = Output("scores", "TENSOR_FLOAT32", "{0}")
52roiOut_6 = Output("roi", "TENSOR_FLOAT32", "{0, 4}")
53classOut_6 = Output("classes", "TENSOR_INT32", "{0}")
54batchOut_6 = Output("batches", "TENSOR_INT32", "{0}")
55model = model.Operation("BOX_WITH_NMS_LIMIT", scoresOut_4, roiOut_5, batchOut_1, 0.1, -1, 0, 0.3, 1.0, 0.1).To(scoresOut_6, roiOut_6, classOut_6, batchOut_6)
56
57quant8 = DataTypeConverter().Identify({
58    scores: ("TENSOR_QUANT8_ASYMM", 0.1, 128),
59    deltas: ("TENSOR_QUANT8_ASYMM", 0.1, 128),
60    anchors: ("TENSOR_QUANT16_SYMM", 0.125, 0),
61    image: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
62    scoresOut_1: ("TENSOR_QUANT8_ASYMM", 0.1, 128),
63    roiOut_1: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
64    feature: ("TENSOR_QUANT8_ASYMM", 0.1, 128),
65    featureOut_2: ("TENSOR_QUANT8_ASYMM", 0.1, 128),
66    weights_3: ("TENSOR_QUANT8_ASYMM", 0.1, 128),
67    bias_3: ("TENSOR_INT32", 0.01, 0),
68    deltaOut_3: ("TENSOR_QUANT8_ASYMM", 0.1, 128),
69    weights_4: ("TENSOR_QUANT8_ASYMM", 0.1, 128),
70    bias_4: ("TENSOR_INT32", 0.01, 0),
71    scoresOut_4: ("TENSOR_QUANT8_ASYMM", 0.1, 128),
72    roiOut_5: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
73    scoresOut_6: ("TENSOR_QUANT8_ASYMM", 0.1, 128),
74    roiOut_6: ("TENSOR_QUANT16_ASYMM", 0.125, 0),
75})
76
77Example({
78
79    # Inputs that will lead to zero-sized output of GENERATE_PROPOSALS
80    scores: [0.5],
81    deltas: [0, 0, -10, -10],
82    anchors: [0, 0, 10, 10],
83    image: [32, 32],
84    feature: [1],
85
86    # Placeholder outputs
87    scoresOut_1: [],
88    scoresOut_6: [],
89    roiOut_6: [],
90    classOut_6: [],
91    batchOut_6: [],
92
93}).AddVariations("relaxed", "float16", quant8)
94