This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathcustom_layers_test.py
197 lines (163 loc) · 7.58 KB
/
custom_layers_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import onnx
import unittest
from tests._test_utils import _onnx_create_model
from onnx import helper, numpy_helper, ModelProto, TensorProto
from onnx_coreml import convert
from coremltools.proto import NeuralNetwork_pb2 #type: ignore
def _make_model_acos_exp_topk(): # type: (...) -> ModelProto
'''
make a very simple model for testing: input->clip->exp->topk->2 outputs
'''
inputs = [('input0', (10,), TensorProto.FLOAT), ('K', (1,), TensorProto.INT64)]
outputs = [('output_values', (3,), TensorProto.FLOAT), ('output_indices', (3,), TensorProto.INT64)]
acos = helper.make_node("Acos",
inputs=[inputs[0][0]],
outputs=['acos_out'])
exp = helper.make_node("Exp",
inputs=[acos.output[0]],
outputs=['exp_out'])
topk = helper.make_node("TopK",
inputs=[exp.output[0], inputs[1][0]],
outputs=[outputs[0][0], outputs[1][0]],
axis=0)
return _onnx_create_model([acos, exp, topk], inputs, outputs)
def _make_model_flatten_axis3(): # type: (...) -> ModelProto
'''
make a simple model: 4-D input -> flatten (axis=3)-> output
'''
inputs = [('input', (1,3,10,20), TensorProto.FLOAT)]
outputs = [('output', (30,20), TensorProto.FLOAT)]
flatten = helper.make_node("Flatten",
inputs=[inputs[0][0]],
outputs=[outputs[0][0]],
axis=3)
return _onnx_create_model([flatten], inputs, outputs)
class CustomLayerTest(unittest.TestCase):
def test_unsupported_ops(self): # type: () -> None
onnx_model = _make_model_acos_exp_topk()
coreml_model = convert(onnx_model, add_custom_layers=True)
spec = coreml_model.get_spec()
layers = spec.neuralNetwork.layers
self.assertIsNotNone(layers[0].custom)
self.assertIsNotNone(layers[2].custom)
self.assertEqual('Acos', layers[0].custom.className)
self.assertEqual('TopK', layers[2].custom.className)
def test_unsupported_ops_provide_functions(self): # type: () -> None
def convert_acos(builder, node, graph, err):
params = NeuralNetwork_pb2.CustomLayerParams()
params.className = node.op_type
params.description = "Custom layer that corresponds to the ONNX op {}".format(node.op_type, )
builder.add_custom(
name=node.name,
input_names=node.inputs,
output_names=node.outputs,
custom_proto_spec=params
)
def convert_topk(builder, node, graph, err):
params = NeuralNetwork_pb2.CustomLayerParams()
params.className = node.op_type
params.description = "Custom layer that corresponds to the ONNX op {}".format(node.op_type, )
params.parameters["axis"].intValue = node.attrs.get('axis', -1)
builder.add_custom(
name=node.name,
input_names=node.inputs,
output_names=node.outputs,
custom_proto_spec=params
)
onnx_model = _make_model_acos_exp_topk()
coreml_model = convert(model=onnx_model,
add_custom_layers=True,
custom_conversion_functions={'Acos':convert_acos, 'TopK':convert_topk})
spec = coreml_model.get_spec()
layers = spec.neuralNetwork.layers
self.assertIsNotNone(layers[0].custom)
self.assertIsNotNone(layers[2].custom)
self.assertEqual('Acos', layers[0].custom.className)
self.assertEqual('TopK', layers[2].custom.className)
self.assertEqual(0, layers[2].custom.parameters['axis'].intValue)
def test_node_name_type_custom_functions(self): # type: () -> None
def convert_acos(builder, node, graph, err):
params = NeuralNetwork_pb2.CustomLayerParams()
params.className = node.op_type
params.description = "Custom layer that corresponds to the ONNX op {}".format(node.op_type, )
builder.add_custom(
name=node.name,
input_names=node.inputs,
output_names=node.outputs,
custom_proto_spec=params
)
def convert_topk_generic(builder, node, graph, err):
params = NeuralNetwork_pb2.CustomLayerParams()
params.className = node.op_type
params.description = "Custom layer that corresponds to the ONNX op {}".format(node.op_type, )
params.parameters["axis"].intValue = node.attrs.get('axis', -1)
params.parameters["k"].intValue = node.attrs['k']
builder.add_custom(
name=node.name,
input_names=node.inputs,
output_names=node.outputs,
custom_proto_spec=params
)
def convert_topk_node_specific(builder, node, graph, err):
params = NeuralNetwork_pb2.CustomLayerParams()
params.className = node.op_type
params.description = "Custom layer that corresponds to the ONNX op {}".format(node.op_type, )
params.parameters["axis"].intValue = node.attrs.get('axis', -1)
builder.add_custom(
name=node.name,
input_names=node.inputs,
output_names=node.outputs,
custom_proto_spec=params
)
onnx_model = _make_model_acos_exp_topk()
onnx.save_model(onnx_model, 'acos.onnx')
coreml_model = convert(model=onnx_model,
add_custom_layers=True,
custom_conversion_functions={'Acos':convert_acos, 'TopK':convert_topk_generic,
'output_values_output_indices':convert_topk_node_specific})
spec = coreml_model.get_spec()
layers = spec.neuralNetwork.layers
self.assertIsNotNone(layers[0].custom)
self.assertIsNotNone(layers[2].custom)
self.assertEqual('Acos', layers[0].custom.className)
self.assertEqual('TopK', layers[2].custom.className)
self.assertEqual(0, layers[2].custom.parameters['axis'].intValue)
def test_unsupported_op_attribute(self): # type: () -> None
onnx_model = _make_model_flatten_axis3()
coreml_model = convert(onnx_model, add_custom_layers=True)
spec = coreml_model.get_spec()
layers = spec.neuralNetwork.layers
self.assertIsNotNone(layers[0].custom)
self.assertEqual('Flatten', layers[0].custom.className)
def test_unsupported_op_attribute_provide_functions(self): # type: () -> None
def convert_flatten(builder, node, graph, err):
params = NeuralNetwork_pb2.CustomLayerParams()
params.className = node.op_type
params.description = "Custom layer that corresponds to the ONNX op {}".format(node.op_type, )
params.parameters["axis"].intValue = node.attrs['axis']
builder.add_custom(
name=node.name,
input_names=node.inputs,
output_names=node.outputs,
custom_proto_spec=params
)
def test_conversion(onnx_model, add_custom_layers=False):
coreml_model = convert(onnx_model, add_custom_layers=add_custom_layers,
custom_conversion_functions={'Flatten': convert_flatten})
spec = coreml_model.get_spec()
layers = spec.neuralNetwork.layers
self.assertIsNotNone(layers[0].custom)
self.assertEqual('Flatten', layers[0].custom.className)
self.assertEqual(3, layers[0].custom.parameters['axis'].intValue)
onnx_model = _make_model_flatten_axis3()
# Test with add_custom_layers True
convert(onnx_model, add_custom_layers=True,
custom_conversion_functions={'Flatten': convert_flatten})
# Test with add_custom_layers False
convert(onnx_model, add_custom_layers=False,
custom_conversion_functions={'Flatten': convert_flatten})
if __name__ == '__main__':
unittest.main()