Esempio n. 1
0
    def test_pad_conv_fusion(self):

        Cin = 3
        Cout = 5
        K = 9
        Hin = 32
        Win = 18
        Xin = np.random.rand(Cin, Hin, Win)
        # Test for several combinations of (pad,stride)
        params = [(5, 2), (4, 3), (6, 3), (5, 1), (5, 2), (6, 2), (3, 2),
                  (1, 1), (2, 3)]
        for param in params:
            pad, stride = param
            input_features = [('data', datatypes.Array(*(Cin, Hin, Win)))]
            output_features = [('output', None)]
            builder = neural_network.NeuralNetworkBuilder(
                input_features, output_features)
            builder.add_padding(name='pad',
                                left=pad,
                                right=pad,
                                top=pad,
                                bottom=pad,
                                input_name='data',
                                output_name='pad_out')
            builder.add_convolution(name='conv',
                                    kernel_channels=Cin,
                                    output_channels=Cout,
                                    height=K,
                                    width=K,
                                    stride_height=stride,
                                    stride_width=stride,
                                    border_mode='valid',
                                    groups=1,
                                    W=np.random.rand(K, K, Cin, Cout),
                                    b=None,
                                    has_bias=False,
                                    input_name='pad_out',
                                    output_name='output')

            #get unoptimized model
            original_spec = builder.spec
            model = coremltools.models.utils._get_model(original_spec)
            #get optimized model
            spec_copy = copy.deepcopy(original_spec)
            tfcoreml.optimize_nn_spec(spec_copy)
            model_opt = coremltools.models.utils._get_model(spec_copy)

            n_layers_original_model = len(
                model.get_spec().neuralNetwork.layers)
            n_layers_opt_model = len(model_opt.get_spec().neuralNetwork.layers)
            self.assertEqual(n_layers_original_model, 2)
            self.assertEqual(n_layers_opt_model, 1)

            original_model_out = model.predict({'data': Xin})['output']
            opt_model_out = model_opt.predict({'data': Xin})['output']
            self._compare_outputs(opt_model_out, original_model_out)
input_tensor = "tower_0/images"
output_tensors = ["tower_0/cls_output"]

centernet_model = tfcoreml.convert(
    tf_model_path='./model/shufflenet.pb',
    mlmodel_path='./model/shufflenet.mlmodel',
    #image_input_names=input_tensor,
    output_feature_names=output_tensors,
    input_name_shape_dict={
        'tower_0/images': [1, cfg.MODEL.hin, cfg.MODEL.win, 3]
    },  # map from input tensor name (placeholder op in the graph) to shape
    minimum_ios_deployment_target='13',
    is_bgr=False)

spec = centernet_model.get_spec()
tfcoreml.optimize_nn_spec(spec)

#
#####clean the name of the model
print(spec.description)
spec.description.input[0].name = "image"
spec.description.input[0].shortDescription = "Input image"
spec.description.output[0].name = "category"
spec.description.output[0].shortDescription = "Predicted category"
#
# # #
# # #
# ##rename the tensor name
for i in range(len(spec.neuralNetwork.layers)):

    try: