def test_bias_converter(self):
     input_dim = (2, 1, 1)
     output_dim = (2, 1, 1)
     input = [('input', datatypes.Array(*input_dim))]
     output = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(input, output)
     bias = numpy.ndarray(shape=(2,))
     bias[:] = [1, 2]
     builder.add_bias(name='Bias', b=bias, input_name='input', output_name='output', shape_bias=[2])
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
 def test_reorganize_data_converter(self):
     block_size = 2
     input_dim = (3, 4 * block_size, 2 * block_size)
     output_dim = (3 * block_size * block_size, 4, 2)
     inputs = [('input', datatypes.Array(*input_dim))]
     outputs = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, outputs)
     builder.add_reorganize_data(name='Reorg', input_name='input', output_name='output', mode='SPACE_TO_DEPTH',
                                 block_size=2)
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #3
0
 def test_l2_normalize_converter(self):
     input_dim = (3, )
     output_dim = (3, )
     input = [('input', datatypes.Array(*input_dim))]
     output = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(input, output)
     builder.add_l2_normalize(name='L2',
                              input_name='input',
                              output_name='output')
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
 def test_load_constant_converter(self):
     value = numpy.ndarray(shape=(1, 1, 2))
     value[:] = [[[-95, 95]]]
     shape = value.shape
     input_dim = (1, 2, 3, 4)
     inputs = [('input', datatypes.Array(*input_dim))]
     outputs = [('const', datatypes.Array(*shape)), ('output', datatypes.Array(*input_dim))]
     builder = NeuralNetworkBuilder(inputs, outputs)
     builder.add_load_constant(name='LoadConstant', output_name='const', constant_value=value, shape=shape)
     builder.add_permute(name='Permute', input_name='input', output_name='output', dim=(0, 1, 2, 3))
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #5
0
 def test_split_converter(self):
     input_dim = (8, 1, 1)
     output_dim = (4, 1, 1)
     inputs = [('input', datatypes.Array(*input_dim))]
     outputs = [('output1', datatypes.Array(*output_dim)),
                ('output2', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, outputs)
     builder.add_split(name='Split',
                       input_name='input',
                       output_names=['output1', 'output2'])
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #6
0
def coreml2onnx(args):
    # Load your CoreML model
    coreml_model = coremltools.utils.load_spec(args.get("model"))

    # Convert the CoreML model into ONNX
    onnx_model = onnxmltools.convert_coreml(
        coreml_model,
        initial_types=args.get("initial_types"),
        target_opset=int(args.get("target_opset")))

    # Save as protobuf
    onnxmltools.utils.save_model(onnx_model, args.get("output_onnx_path"))
Пример #7
0
 def test_mean_variance_normalize_converter(self):
     input_dim = (3, )
     output_dim = (3, )
     input = [('input', datatypes.Array(*input_dim))]
     output = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(input, output)
     builder.add_mvn(name='MVN',
                     input_name='input',
                     output_name='output',
                     epsilon=0)
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #8
0
 def test_permute_converter(self):
     input_dim = (4, 1, 2, 3)
     output_dim = (4, 3, 1, 2)
     inputs = [('input', datatypes.Array(*input_dim))]
     outputs = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, outputs)
     builder.add_permute(name='Permute',
                         input_name='input',
                         output_name='output',
                         dim=(0, 2, 3, 1))
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #9
0
 def test_flatten_converter(self):
     input_dim = (1, 2, 3)
     output_dim = (6, 1, 1)
     inputs = [('input', datatypes.Array(*input_dim))]
     outputs = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, outputs)
     builder.add_flatten(name='Flatten',
                         input_name='input',
                         output_name='output',
                         mode=1)
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #10
0
 def test_activation_converter(self):
     input_dim = (3, )
     output_dim = (3, )
     input = [('input', datatypes.Array(*input_dim))]
     output = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(input, output)
     builder.add_activation(name='Activation',
                            non_linearity='RELU',
                            input_name='input',
                            output_name='output')
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #11
0
 def test_sequence_repeat_converter(self):
     input_dim = (3, 1, 1)
     output_dim = (9, 1, 1)
     inputs = [('input', datatypes.Array(*input_dim))]
     outputs = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, outputs)
     builder.add_sequence_repeat(name='Repeat',
                                 input_name='input',
                                 output_name='output',
                                 nrep=3)
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #12
0
 def test_reshape_converter(self):
     input_dim = (1, 1, 2)
     output_dim = (1, 2, 1)
     inputs = [('input', datatypes.Array(*input_dim))]
     outputs = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, outputs)
     builder.add_reshape(name='Reshape',
                         input_name='input',
                         output_name='output',
                         target_shape=output_dim,
                         mode=1)
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #13
0
 def test_reduce_converter(self):
     input_dim = (1, 2, 2)
     output_dim = (1, )
     inputs = [('input', datatypes.Array(*input_dim))]
     output = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, output)
     builder.add_reduce(name='Reduce',
                        input_name='input',
                        output_name='output',
                        axis='CHW',
                        mode='sum')
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #14
0
 def test_dot_product_converter(self):
     input_dim = (3, )
     output_dim = (1, )
     inputs = [('input1', datatypes.Array(*input_dim)),
               ('input2', datatypes.Array(*input_dim))]
     output = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, output)
     builder.add_elementwise(name='Dot',
                             input_names=['input1', 'input2'],
                             output_name='output',
                             mode='DOT')
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #15
0
 def test_multiply_converter(self):
     input_dim = (1, 2, 2)
     output_dim = (1, 2, 2)
     inputs = [('input1', datatypes.Array(*input_dim)),
               ('input2', datatypes.Array(*input_dim))]
     output = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, output)
     builder.add_elementwise(name='Mul',
                             input_names=['input1', 'input2'],
                             output_name='output',
                             mode='MULTIPLY')
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #16
0
 def test_upsample_converter(self):
     input_dim = (1, 1, 1)
     output_dim = (1, 2, 2)
     input = [('input', datatypes.Array(*input_dim))]
     output = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(input, output)
     builder.add_upsample(name='Upsample',
                          scaling_factor_h=2,
                          scaling_factor_w=2,
                          input_name='input',
                          output_name='output')
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #17
0
 def test_concat_converter(self):
     input_dim = (5, 1, 1)
     output_dim = (10, 1, 1)
     inputs = [('input1', datatypes.Array(*input_dim)),
               ('input2', datatypes.Array(*input_dim))]
     outputs = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, outputs)
     builder.add_elementwise(name='Concate',
                             input_names=['input1', 'input2'],
                             output_name='output',
                             mode='CONCAT')
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
 def test_scale_converter(self):
     input_dim = (3,)
     output_dim = (3,)
     input = [('input', datatypes.Array(*input_dim))]
     output = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(input, output)
     scale = numpy.ndarray(shape=(1,))
     scale[:] = 10
     bias = numpy.ndarray(shape=(1,))
     bias[:] = -100
     builder.add_scale(name='ImageScaler', W=scale, b=bias, has_bias=True, input_name='input', output_name='output')
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #19
0
 def test_bidirectional_lstm_converter(self):
     input_dim = (1, 8)
     output_dim = (1, 2)
     inputs = [('input', datatypes.Array(*input_dim))]
     outputs = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, outputs)
     W_h = [
         numpy.random.rand(2, 2),
         numpy.random.rand(2, 2),
         numpy.random.rand(2, 2),
         numpy.random.rand(2, 2)
     ]
     W_x = [
         numpy.random.rand(2, 8),
         numpy.random.rand(2, 8),
         numpy.random.rand(2, 8),
         numpy.random.rand(2, 8)
     ]
     b = [
         numpy.random.rand(2, 1),
         numpy.random.rand(2, 1),
         numpy.random.rand(2, 1),
         numpy.random.rand(2, 1)
     ]
     p = [
         numpy.zeros(shape=(2, 1)),
         numpy.zeros(shape=(2, 1)),
         numpy.zeros(shape=(2, 1))
     ]
     builder.add_bidirlstm(name='LSTM',
                           W_h=W_h,
                           W_x=W_x,
                           W_h_back=W_h,
                           b=b,
                           W_x_back=W_x,
                           b_back=b,
                           hidden_size=2,
                           input_size=8,
                           input_names=['input'],
                           output_names=['output'],
                           inner_activation='SIGMOID',
                           cell_state_update_activation='TANH',
                           output_activation='TANH',
                           peep=p,
                           peep_back=p,
                           output_all=False,
                           forget_bias=False,
                           coupled_input_forget_gate=False,
                           cell_clip_threshold=10000)
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #20
0
    def _test_one_to_one_operator_core_channels_last(self, keras_model, x):
        '''
        There are two test paths. One is Keras-->CoreML-->ONNX and the other one is Keras-->ONNX.

        Keras-->CoreML-->ONNX:

        Keras computation path:
            [N, C, H, W] ---> numpy transpose ---> [N, H, W, C] ---> keras convolution --->
            [N, H, W, C] ---> numpy transpose ---> [N, C, H, W]

        ONNX computation path:
            [N, C, H, W] ---> ONNX convolution ---> [N, C, H, W]

        The reason for having extra transpose's in the Keras path is that CoreMLTools doesn't not handle channels_last
        flag properly. Precisely, oreMLTools always converts Conv2D under channels_first mode.

        Keras-->ONNX

        Keras computation path:
            [N, C, H, W] ---> numpy transpose ---> [N, H, W, C] ---> keras convolution --->
            [N, H, W, C]

        ONNX computation path:
            [N, C, H, W] ---> numpy transpose ---> [N, H, W, C] ---> ONNX convolution ---> [N, H, W, C]

        '''
        # Verify Keras-to-CoreML-to-ONNX path
        try:
            coreml_model = coremltools.converters.keras.convert(keras_model)
        except (AttributeError, ImportError) as e:
            warnings.warn(
                "Unable to test due to an error in coremltools '{0}'.".format(
                    e))
            return
        onnx_model_p1 = onnxmltools.convert_coreml(coreml_model)
        onnx_model_p2 = onnxmltools.convert_keras(keras_model)

        if isinstance(x, list):
            x_t = [np.transpose(_, [0, 2, 3, 1]) for _ in x]
        else:
            x_t = np.transpose(x, [0, 2, 3, 1])
        y_reference = np.transpose(keras_model.predict(x_t), [0, 3, 1, 2])
        y_produced = evaluate_deep_model(onnx_model_p1, x)

        self.assertTrue(np.allclose(y_reference, y_produced))

        # Verify Keras-to-ONNX path
        y_reference = np.transpose(y_reference, [0, 2, 3, 1])
        y_produced = evaluate_deep_model(onnx_model_p2, x_t)

        self.assertTrue(np.allclose(y_reference, y_produced, atol=1e-6))
 def test_simple_recurrent_converter(self):
     input_dim = (1, 8)
     output_dim = (1, 2)
     inputs = [('input', datatypes.Array(*input_dim))]
     outputs = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, outputs)
     W_h = numpy.random.rand(2, 2)
     W_x = numpy.random.rand(2, 8)
     b = numpy.random.rand(2, 1)
     builder.add_simple_rnn(name='RNN', W_h=W_h, W_x=W_x, b=b, hidden_size=2, input_size=8,
                            input_names=['input', 'h_init'], output_names=['output', 'h'], activation='TANH',
                            output_all=False, reverse_input=False)
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
    def _test_one_to_one_operator_core(self, keras_model, x_keras, x_cntk):
        coreml_model = coremltools.converters.keras.convert(keras_model)
        onnx_model = onnxmltools.convert_coreml(coreml_model)

        temporary_onnx_model_file_name = onnx_model.graph.name + '_temp.onnx'
        onnxmltools.utils.save_model(onnx_model,
                                     temporary_onnx_model_file_name)
        cntk_model = cntk.Function.load(temporary_onnx_model_file_name,
                                        format=cntk.ModelFormat.ONNX)

        y_keras = keras_model.predict(x_keras)
        y_cntk = cntk_model.eval({cntk_model.arguments[0]: x_cntk})

        self.assertTrue(np.allclose(y_keras, y_cntk))
 def test_embedding_converter(self):
     input_dim = (1, 1, 1, 1)
     output_dim = (1, 2, 1, 1)
     input = [('input', datatypes.Array(*input_dim))]
     output = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(input, output)
     weights = numpy.zeros(shape=(2))
     weights[:] = [-1, 1]
     bias = numpy.zeros(shape=(2))
     bias[:] = [-100, 100]
     builder.add_embedding(name='Embed', input_dim=1, W=weights, b=bias, output_channels=2, has_bias=True,
                           input_name='input', output_name='output')
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
 def test_inner_product_converter(self):
     input_dim = (3,)
     output_dim = (2,)
     input = [('input', datatypes.Array(*input_dim))]
     output = [('output', datatypes.Array(*output_dim))]
     weights = numpy.zeros(shape=(3, 2))
     weights[:] = [[1, 2], [3, 4], [5, 6]]
     bias = numpy.zeros(shape=(2))
     bias[:] = [-100, 100]
     builder = NeuralNetworkBuilder(input, output)
     builder.add_inner_product(name='FC', W=weights, b=bias, input_channels=3, output_channels=2, has_bias=True,
                               input_name='input', output_name='output')
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
 def test_gru_converter(self):
     input_dim = (1, 8)
     output_dim = (1, 2)
     inputs = [('input', datatypes.Array(*input_dim))]
     outputs = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, outputs)
     W_h = [numpy.random.rand(2, 2), numpy.random.rand(2, 2), numpy.random.rand(2, 2)]
     W_x = [numpy.random.rand(2, 8), numpy.random.rand(2, 8), numpy.random.rand(2, 8)]
     b = [numpy.random.rand(2, 1), numpy.random.rand(2, 1), numpy.random.rand(2, 1)]
     builder.add_gru(name='GRU', W_h=W_h, W_x=W_x, b=b, hidden_size=2, input_size=8, input_names=['input'],
                     output_names=['output'], activation='TANH', inner_activation='SIGMOID_HARD', output_all=False,
                     reverse_input=False)
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
 def test_crop_converter(self):
     top_crop = 1
     bottom_crop = 1
     left_crop = 1
     right_crop = 1
     input_dim = (8, 6, 4)
     output_dim = (8, 6 - top_crop - bottom_crop, 4 - left_crop - right_crop)
     input = [('input', datatypes.Array(*input_dim))]
     output = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(input, output)
     builder.add_crop(name='Crop', left=left_crop, right=right_crop, top=top_crop, bottom=bottom_crop, offset=[0, 0],
                      input_names=['input'], output_name='output')
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #27
0
 def test_lrn_converter(self):
     input_dim = (3, )
     output_dim = (3, )
     input = [('input', datatypes.Array(*input_dim))]
     output = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(input, output)
     builder.add_lrn(name='LRN',
                     input_name='input',
                     output_name='output',
                     alpha=0.5,
                     beta=2,
                     k=1,
                     local_size=2)
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)
Пример #28
0
def convert_caffee2onnx(proto, caffemodel, output):
    '''
    It is not supported in Wondows,for the library 
    -- "coremltools" are only for linux and macos
    '''
    import coremltools
    import onnxmltools
    output_coreml_model = './model/model.mlmodel'

    output_onnx_model = 'model.onnx'
    coreml_model = coremltools.converters.caffe.convert((caffemodel, proto))
    coreml_model.save(output)
    coreml_model = coremltools.utils.load_spec(output)
    onnx_model = onnxmltools.convert_coreml(coreml_model)
    onnxmltools.utils.save_model(onnx_model, output)
    def _test_one_to_one_operator_core(self, keras_model, x):
        # Verify Keras-to-CoreML-to-ONNX path
        coreml_model = coremltools.converters.keras.convert(keras_model)
        onnx_model = onnxmltools.convert_coreml(coreml_model)

        y_reference = keras_model.predict(x)
        y_produced = _evaluate(onnx_model, x)

        self.assertTrue(np.allclose(y_reference, y_produced))

        # Verify Keras-to-ONNX path
        onnx_model = onnxmltools.convert_keras(keras_model)
        y_produced = _evaluate(onnx_model, x)

        self.assertTrue(np.allclose(y_reference, y_produced))
Пример #30
0
 def test_slice_converter(self):
     input_dim = (1, 4, 2)
     output_dim = (1, 2, 2)
     inputs = [('input', datatypes.Array(*input_dim))]
     outputs = [('output', datatypes.Array(*output_dim))]
     builder = NeuralNetworkBuilder(inputs, outputs)
     builder.add_slice(name='Slice',
                       input_name='input',
                       output_name='output',
                       axis='height',
                       start_index=0,
                       end_index=-1,
                       stride=1)
     model_onnx = convert_coreml(builder.spec)
     self.assertTrue(model_onnx is not None)