def test_image_scaler_remover(self): # type: () -> None inputs = [("input", (1, 3, 50, 50))] outputs = [("out", (1, 3, 50, 50), TensorProto.FLOAT)] im_scaler = helper.make_node( "ImageScaler", inputs=["input"], outputs=["scaler_out"], bias=[10, -6, 20], scale=3.0, ) exp = helper.make_node("Exp", inputs=["scaler_out"], outputs=["out"]) onnx_model = _onnx_create_model([im_scaler, exp], inputs, outputs) graph = Graph.from_onnx(onnx_model.graph, onnx_ir_version=5) new_graph = graph.transformed([ImageScalerRemover()]) self.assertEqual(len(graph.nodes), 2) self.assertEqual(len(new_graph.nodes), 1) self.assertEqual(new_graph.nodes[0].inputs[0], "input") self.assertEqual(new_graph.nodes[0].outputs[0], "out") coreml_model = convert(onnx_model) spec = coreml_model.get_spec() self.assertEqual( spec.neuralNetwork.preprocessing[0].scaler.channelScale, 3.0) self.assertEqual(spec.neuralNetwork.preprocessing[0].scaler.blueBias, 20.0) self.assertEqual(spec.neuralNetwork.preprocessing[0].scaler.greenBias, -6.0) self.assertEqual(spec.neuralNetwork.preprocessing[0].scaler.redBias, 10.0)
def test_dropout_remover(self): # type: () -> None inputs = [("input", (1, 3, 50, 50))] outputs = [("out", (1, 5, 50, 50), TensorProto.FLOAT)] weight = numpy_helper.from_array(_random_array((5, 3, 1, 1)), name="weight") conv = helper.make_node( "Conv", inputs=["input", "weight"], outputs=["conv_output"], kernel_shape=(1, 1), strides=(1, 1), ) drop = helper.make_node( "Dropout", inputs=["conv_output"], outputs=["drop_output"], ) exp = helper.make_node("Exp", inputs=["drop_output"], outputs=["out"]) onnx_model = _onnx_create_model([conv, drop, exp], inputs, outputs) graph = Graph.from_onnx(onnx_model.graph, onnx_ir_version=5) new_graph = graph.transformed([DropoutRemover()]) self.assertEqual(len(graph.nodes), 3) self.assertEqual(len(new_graph.nodes), 2) self.assertEqual(new_graph.nodes[0].inputs[0], "input") self.assertEqual(new_graph.nodes[1].inputs[0], new_graph.nodes[0].outputs[0]) self.assertEqual(new_graph.nodes[1].outputs[0], "out")
def test_fuse_conv_without_bias(self): # type: () -> None kernel_shape = (3, 2) strides = (2, 3) pads = (4, 2, 4, 2) dilations = (1, 2) group = 1 weight = numpy_helper.from_array(_random_array((16, 3, 3, 2)), name="weight") input_shape = (1, 3, 224, 224) output_size = _conv_pool_output_size(input_shape, dilations, kernel_shape, pads, strides) output_shape = (1, int(weight.dims[0]), output_size[0], output_size[1]) inputs = [("input0", input_shape)] outputs = [("output0", output_shape, TensorProto.FLOAT)] conv = helper.make_node( "Conv", inputs=[inputs[0][0], "weight"], outputs=["conv_output"], dilations=dilations, group=group, kernel_shape=kernel_shape, pads=pads, strides=strides, ) b = _random_array((int(weight.dims[0]), )) bias = numpy_helper.from_array(b, name="bias") add = helper.make_node( "Add", inputs=[conv.output[0], "bias"], outputs=[outputs[0][0]], broadcast=1, axis=1, ) model = _onnx_create_model([conv, add], inputs, outputs, [weight, bias]) graph_ = Graph.from_onnx(model.graph, onnx_ir_version=5) fused_graph = graph_.transformed([ConvAddFuser()]) self.assertEqual(len(fused_graph.nodes), 1) node = fused_graph.nodes[0] self.assertEqual(len(node.inputs), 3) npt.assert_equal(node.input_tensors[node.inputs[2]], b) self.assertEqual(fused_graph.nodes[0].outputs[0], outputs[0][0])
def test_cast_op_remover(self): inputs = [("input", (1, 16, 224, 224))] outputs = [("output", (1, 1, 224, 224), TensorProto.FLOAT)] conv = helper.make_node( "Conv", name="Conv_1", inputs=["input", "weight"], outputs=["conv_output"], kernel_shape=(3, 3), strides=(1, 1), ) cast_1 = helper.make_node( "Cast", name="Cast_1", inputs=["conv_output"], outputs=["cast_1_output"], to=1 ) sigmoid = helper.make_node( "Sigmoid", name="Sigmoid_1", inputs=["cast_1_output"], outputs=["sigmoid_output"], ) cast_2 = helper.make_node( "Cast", name="Cast_2", inputs=["sigmoid_output"], outputs=["output"], to=1 ) value_info = [ ("input", (1, 16, 224, 224), TensorProto.FLOAT), ("conv_output", (1, 1, 224, 224), TensorProto.FLOAT), ("cast_1_output", (1, 1, 224, 224), TensorProto.FLOAT), ("sigmoid_output", (1, 1, 224, 224), TensorProto.FLOAT), ] onnx_model = _onnx_create_model([conv, cast_1, sigmoid, cast_2], inputs, outputs, value_info=value_info) graph = Graph.from_onnx(onnx_model.graph, onnx_ir_version=5) new_graph = graph.transformed([CastOpRemover()]) # The last Cast operation should not be removed self.assertEqual(len(graph.nodes), 4) self.assertEqual(len(new_graph.nodes), 3) self.assertEqual(new_graph.nodes[0].inputs[0], "input") self.assertEqual(new_graph.nodes[1].inputs[0], new_graph.nodes[0].outputs[0]) self.assertEqual(new_graph.nodes[2].outputs[0], "output")
def test_create_graph(self): # type: () -> None kernel_shape = (3, 2) strides = (2, 3) pads = (4, 2, 4, 2) dilations = (1, 2) group = 1 weight = numpy_helper.from_array(_random_array((16, 3, 3, 2)), name="weight") input_shape = (1, 3, 224, 224) output_size = _conv_pool_output_size(input_shape, dilations, kernel_shape, pads, strides) output_shape = (1, int(weight.dims[0]), output_size[0], output_size[1]) inputs = [("input0", input_shape)] outputs = [("output0", output_shape, TensorProto.FLOAT)] conv = helper.make_node( "Conv", inputs=[inputs[0][0], "weight"], outputs=["conv_output"], dilations=dilations, group=group, kernel_shape=kernel_shape, pads=pads, strides=strides, ) relu = helper.make_node("Relu", inputs=[conv.output[0]], outputs=[outputs[0][0]]) model = _onnx_create_model([conv, relu], inputs, outputs, [weight]) graph_ = Graph.from_onnx(model.graph, onnx_ir_version=5) self.assertTrue(len(graph_.inputs) == 1) self.assertEqual(graph_.inputs[0][2], input_shape) self.assertTrue(len(graph_.outputs) == 1) self.assertEqual(graph_.outputs[0][2], output_shape) self.assertTrue(len(graph_.nodes) == 2) self.assertEqual(len(graph_.nodes[0].parents), 0) self.assertEqual(len(graph_.nodes[1].parents), 1) self.assertEqual(len(graph_.nodes[0].children), 1) self.assertEqual(len(graph_.nodes[1].children), 0)