Esempio n. 1
0
    def test_dead_layer_partial_branch(self):
        convergence_tolerance = 1e-8

        input_features = [("input", datatypes.Array(*(2,)))]
        output_features = [("out", None)]

        builder = neural_network.NeuralNetworkBuilder(
            input_features, output_features, disable_rank5_shape_mapping=True
        )
        # add condition to break from the loop, if convergence criterion is met
        builder.add_less_than("cond", ["input"], "cond", alpha=convergence_tolerance)
        branch_layer = builder.add_branch("branch_layer", "cond")
        builder_ifbranch = neural_network.NeuralNetworkBuilder(
            nn_spec=branch_layer.branch.ifBranch
        )
        builder_ifbranch.add_activation("relu1", "RELU", "input", "relu1_out")
        builder_ifbranch.add_activation("relu2_out", "RELU", "relu1_out", "relu2_out")
        builder_elsebranch = neural_network.NeuralNetworkBuilder(
            nn_spec=branch_layer.branch.elseBranch
        )
        builder_elsebranch.add_activation("linear1", "LINEAR", "input", "linear1_out")
        builder_elsebranch.add_activation(
            "linear_red_1", "LINEAR", "input", "linear_red1_out"
        )
        builder_elsebranch.add_activation(
            "linear_red_2", "LINEAR", "linear_red1_out", "linear_red2_out"
        )
        builder_elsebranch.add_activation(
            "linear2", "LINEAR", "linear1_out", "relu2_out"
        )
        builder.add_squeeze("out", "relu2_out", "out", squeeze_all=True)

        mlmodel = MLModel(builder.spec, compute_units=ComputeUnit.CPU_ONLY)

        if not _IS_MACOS:
            # Can not get predictions unless on macOS.
            return

        data = np.random.rand(2,)
        data_dict = {"input": data}
        before_pass_out = mlmodel.predict(data_dict)["out"]
        if DEBUG:
            print("\n mlmodel description before remove disconnected layers pass: \n")
            print_network_spec(builder.spec, style="coding")
        old_spec = copy.copy(builder.spec)
        remove_disconnected_layers(builder.spec)
        if DEBUG:
            print("\n mlmodel description after remove disconnected layers pass: \n")
            print_network_spec(builder.spec, style="coding")
        mlmodel = MLModel(builder.spec, compute_units=ComputeUnit.CPU_ONLY)
        after_pass_out = mlmodel.predict(data_dict)["out"]

        np.testing.assert_almost_equal(before_pass_out, after_pass_out, decimal=2)
        np.testing.assert_equal(
            len(old_spec.neuralNetwork.layers[1].branch.ifBranch.layers),
            len(builder.spec.neuralNetwork.layers[1].branch.ifBranch.layers),
        )
        np.testing.assert_equal(
            len(builder.spec.neuralNetwork.layers[1].branch.elseBranch.layers), 2
        )
Esempio n. 2
0
    def test_dead_layer_remove_branch(self):
        convergence_tolerance = 1e-8

        input_features = [('input', datatypes.Array(*(2,)))]
        output_features = [('out', None)]

        builder = neural_network.NeuralNetworkBuilder(input_features, output_features, disable_rank5_shape_mapping=True)
        # add condition to break from the loop, if convergence criterion is met
        builder.add_less_than('cond', ['input'], 'cond', alpha=convergence_tolerance)
        branch_layer = builder.add_branch('branch_layer', 'cond')
        builder_ifbranch = neural_network.NeuralNetworkBuilder(nn_spec=branch_layer.branch.ifBranch)
        builder_ifbranch.add_activation('relu1', 'RELU', 'input', 'relu1_out')
        builder_ifbranch.add_activation('relu2_out', 'RELU', 'relu1_out', 'relu2_out')
        builder_elsebranch = neural_network.NeuralNetworkBuilder(nn_spec=branch_layer.branch.elseBranch)
        builder_elsebranch.add_activation('linear1', 'LINEAR', 'input', 'linear1_out')
        builder_elsebranch.add_activation('linear2', 'LINEAR', 'linear1_out', 'relu2_out')
        builder.add_squeeze('out', 'input', 'out', squeeze_all=True)

        mlmodel = MLModel(builder.spec)
        data = np.random.rand(2,)
        data_dict = {'input': data}
        before_pass_out = mlmodel.predict(data_dict)['out']
        if DEBUG:
            print('\n mlmodel description before remove disconnected layers pass: \n')
            print_network_spec(builder.spec, style='coding')
        remove_disconnected_layers(builder.spec)
        if DEBUG:
            print('\n mlmodel description after remove disconnected layers pass: \n')
            print_network_spec(builder.spec, style='coding')
        mlmodel = MLModel(builder.spec)
        after_pass_out = mlmodel.predict(data_dict)['out']

        np.testing.assert_almost_equal(before_pass_out, after_pass_out, decimal=4)
        np.testing.assert_equal(len(builder.spec.neuralNetwork.layers), 1)
Esempio n. 3
0
    def test_remove_three_transpose(self):

        # Three transpose layer which can be removed
        input_shape = (1, 10, 5)
        input_features = [('data', datatypes.Array(*input_shape))]
        output_features = [('out', None)]
        builder = neural_network.NeuralNetworkBuilder(
            input_features, output_features, disable_rank5_shape_mapping=True)
        transpose = [[2, 1, 0], [1, 0, 2], [2, 0, 1]]
        input_name = 'data'
        for i, axes in enumerate(transpose):
            name = 'transpose_' + str(i)
            output_name = name + '_out'
            builder.add_transpose(name=name,
                                  axes=axes,
                                  input_name=input_name,
                                  output_name=output_name)
            input_name = output_name

        builder.add_activation(name='relu',
                               non_linearity='RELU',
                               input_name=input_name,
                               output_name='out')

        self._test_builder(builder, input_shape, 1)
    def test_remove_thousands_random_transpose_layers_case_2(self):
        """
        Same test as the previous one, but add more layers and dimension.
        """
        from itertools import permutations
        import random

        random.seed(0)
        input_shape = (3, 10, 5, 2, 4)
        input_features = [("data", datatypes.Array(*input_shape))]
        output_features = [("out", None)]
        builder = neural_network.NeuralNetworkBuilder(
            input_features, output_features, disable_rank5_shape_mapping=True
        )

        num_layers = 5000
        dim = 5
        input_name = "data"
        for i in range(num_layers):
            axes = list(permutations(range(dim)))
            random.shuffle(axes)
            output_name = "layer_" + str(i) + "_output"
            name = "layer_" + str(i)
            builder.add_transpose(
                name=name, axes=axes[0], input_name=input_name, output_name=output_name
            )
            input_name = output_name
        builder.add_activation(
            name="relu", non_linearity="RELU", input_name=input_name, output_name="out"
        )
        self._test_builder(builder, input_shape, None)
Esempio n. 5
0
        def get_coreml_model_crop_resize(params):
            eval = True
            mlmodel = None
            batch, ch, n_roi = params["b_c_n"]
            H = params["H"]
            W = params["W"]
            try:
                input_features = [('data', datatypes.Array(ch,H,W))]
                input_features.append(('roi', datatypes.Array(4, 1, 1)))
                if batch != 1:
                    input_features.append(('box_ind', datatypes.Array(1, 1, 1)))
                output_features = [('output', None)]
                builder = neural_network.NeuralNetworkBuilder(input_features, output_features)

                if batch != 1:
                    builder.add_elementwise('concat', ['box_ind','roi'], 'roi_out', 'CONCAT')
                    input_names = ['data', 'roi_out']
                else:
                    input_names = ['data', 'roi']

                builder.add_crop_resize('resize', input_names, 'output',
                                        target_height=params["Hnew"], target_width=params["Wnew"],
                                        mode='ALIGN_ENDPOINTS_MODE',
                                        normalized_roi=True,
                                        box_indices_mode='CORNERS_HEIGHT_FIRST',
                                        spatial_scale=1.0)
                mlmodel = MLModel(builder.spec)
            except RuntimeError as e:
                print(e)
                eval = False

            return mlmodel, eval
Esempio n. 6
0
    def test_bias_constant(self):

        #create a tiny mlmodel
        input_dim = (1, 2, 2)
        input_features = [('data', datatypes.Array(*input_dim))]
        output_features = [('output', None)]

        builder = neural_network.NeuralNetworkBuilder(input_features,
                                                      output_features)

        builder.add_bias(name='bias',
                         b=45,
                         input_name='data',
                         output_name='output')

        #save the model
        model_dir = tempfile.mkdtemp()
        model_path = os.path.join(model_dir, 'test_layer.mlmodel')
        coremltools.utils.save_spec(builder.spec, model_path)

        #preprare input and get predictions
        coreml_model = coremltools.models.MLModel(model_path)
        if macos_version() >= (10, 13):
            x = np.reshape(np.arange(4, dtype=np.float32), (1, 2, 2))
            coreml_input = {'data': x}
            coreml_preds = coreml_model.predict(coreml_input)['output']

            #harcoded for this simple test case
            numpy_preds = x + 45
            self.assertTrue(self._compare_shapes(numpy_preds, coreml_preds))
            self.assertTrue(
                self._compare_predictions(numpy_preds, coreml_preds))

        if os.path.exists(model_dir):
            shutil.rmtree(model_dir)
Esempio n. 7
0
        def get_coreml_model_reorganize(X, params):
            eval = True
            mlmodel = None
            try:
                input_dim = X.shape[2:]
                input_features = [("data", datatypes.Array(*input_dim))]
                output_features = [("output", None)]
                builder = neural_network.NeuralNetworkBuilder(
                    input_features, output_features)
                builder.add_reorganize_data(
                    "reorg",
                    "data",
                    "output",
                    mode=params["mode"],
                    block_size=params["block_size"],
                )

                if cpu_only:
                    compute_unit = ComputeUnit.CPU_ONLY
                else:
                    compute_unit = ComputeUnit.ALL
                mlmodel = MLModel(builder.spec, compute_units=compute_unit)
            except RuntimeError as e:
                print(e)
                eval = False

            return mlmodel, eval
Esempio n. 8
0
    def test_reshape_target_shape_4(self):

        #create a tiny mlmodel
        input_dim = (1, 2, 5)  #(C,H,W)
        input_features = [('data', datatypes.Array(*input_dim))]
        output_features = [('output', None)]

        builder = neural_network.NeuralNetworkBuilder(input_features,
                                                      output_features)

        builder.add_reshape(name='reshape',
                            input_name='data',
                            output_name='output',
                            target_shape=(1, 10, 1, 1),
                            mode=0)

        #save the model
        model_dir = tempfile.mkdtemp()
        model_path = os.path.join(model_dir, 'test_layer.mlmodel')
        coremltools.utils.save_spec(builder.spec, model_path)

        #preprare input and get predictions
        coreml_model = coremltools.models.MLModel(model_path)
        x = np.random.rand(*input_dim)
        coreml_input = {'data': x}
        coreml_preds = coreml_model.predict(coreml_input)['output']

        #harcoded for this simple test case
        numpy_preds = np.reshape(x, (1, 10, 1, 1))
        self.assertTrue(self._compare_shapes(numpy_preds, coreml_preds))
        self.assertTrue(self._compare_predictions(numpy_preds, coreml_preds))

        if os.path.exists(model_dir):
            shutil.rmtree(model_dir)
Esempio n. 9
0
    def test_LRN(self):

        #create a tiny mlmodel
        input_dim = (1, 3, 3)
        input_features = [('data', datatypes.Array(*input_dim))]
        output_features = [('output', datatypes.Array(*input_dim))]

        builder = neural_network.NeuralNetworkBuilder(input_features,
                                                      output_features)

        builder.add_lrn(name='lrn',
                        input_name='data',
                        output_name='output',
                        alpha=2,
                        beta=3,
                        local_size=1,
                        k=8)

        #save the model
        model_dir = tempfile.mkdtemp()
        model_path = os.path.join(model_dir, 'test_layer.mlmodel')
        coremltools.utils.save_spec(builder.spec, model_path)

        #preprare input and get predictions
        coreml_model = coremltools.models.MLModel(model_path)
        coreml_input = {'data': np.ones((1, 3, 3))}
        coreml_preds = coreml_model.predict(coreml_input)['output']

        #harcoded for this simple test case
        numpy_preds = 1e-3 * np.ones((1, 3, 3))
        self.assertTrue(self._compare_shapes(numpy_preds, coreml_preds))
        self.assertTrue(self._compare_predictions(numpy_preds, coreml_preds))

        if os.path.exists(model_dir):
            shutil.rmtree(model_dir)
Esempio n. 10
0
 def test_scale_matrix(self):
     
     #create a tiny mlmodel
     input_dim = (1,2,2)
     input_features = [('data', datatypes.Array(*input_dim))]
     output_features = [('output', None)]
             
     builder = neural_network.NeuralNetworkBuilder(input_features, output_features)
     
     W = np.reshape(np.arange(5,9), (1,2,2))
             
     builder.add_scale(name = 'scale', W = W, b = None, has_bias = False, input_name = 'data', output_name = 'output',
                         shape_scale = [1,2,2])
                     
     #save the model
     model_dir = tempfile.mkdtemp()
     model_path = os.path.join(model_dir, 'test_layer.mlmodel')                        
     coremltools.utils.save_spec(builder.spec, model_path)
     
     #preprare input and get predictions
     coreml_model = coremltools.models.MLModel(model_path)
     x = np.reshape(np.arange(4, dtype=np.float32), (1,2,2))
     coreml_input = {'data': x}
     coreml_preds = coreml_model.predict(coreml_input)['output']
     
     #harcoded for this simple test case
     numpy_preds = W * x
     self.assertTrue(self._compare_shapes(numpy_preds, coreml_preds))
     self.assertTrue(self._compare_predictions(numpy_preds, coreml_preds))
     
     if os.path.exists(model_dir):
         shutil.rmtree(model_dir)            
Esempio n. 11
0
 def test_MVN(self):
     
     #create a tiny mlmodel
     input_dim = (2,2,2)
     input_features = [('data', datatypes.Array(*input_dim))]
     output_features = [('output', datatypes.Array(*input_dim))]
             
     builder = neural_network.NeuralNetworkBuilder(input_features, 
             output_features)
             
     builder.add_mvn(name= 'mvn', input_name = 'data', output_name = 'output',
                     across_channels = False, normalize_variance = False)
                     
     #save the model
     model_dir = tempfile.mkdtemp()
     model_path = os.path.join(model_dir, 'test_layer.mlmodel')                        
     coremltools.utils.save_spec(builder.spec, model_path)
     
     #preprare input and get predictions
     coreml_model = coremltools.models.MLModel(model_path)
     coreml_input = {'data': np.reshape(np.arange(8, dtype=np.float32), (2,2,2))}
     coreml_preds = coreml_model.predict(coreml_input)['output']
     
     #harcoded for this simple test case
     numpy_preds = np.reshape(np.arange(8) - np.array([1.5,1.5,1.5,1.5,5.5,5.5,5.5,5.5]),(2,2,2))
     self.assertTrue(self._compare_shapes(numpy_preds, coreml_preds))
     self.assertTrue(self._compare_predictions(numpy_preds, coreml_preds))
     
     if os.path.exists(model_dir):
         shutil.rmtree(model_dir)  
Esempio n. 12
0
 def test_tiny_upsample_linear_mode(self):
             
     #create a tiny mlmodel
     input_dim = (1,1,3) #(C,H,W)
     input_features = [('data', datatypes.Array(*input_dim))]
     output_features = [('output', None)]
     
     builder = neural_network.NeuralNetworkBuilder(input_features, 
             output_features)
     builder.add_upsample(name= 'upsample', 
                          scaling_factor_h = 2, scaling_factor_w = 3, 
                          input_name= 'data', output_name= 'output', 
                          mode = 'BILINEAR')
     
     #save the model
     model_dir = tempfile.mkdtemp()
     model_path = os.path.join(model_dir, 'test_layer.mlmodel')                        
     coremltools.utils.save_spec(builder.spec, model_path)
     
     #preprare input and get predictions
     coreml_model = coremltools.models.MLModel(model_path)
     coreml_input = {'data': np.reshape(np.array([1.0,2.0,3.0]), (1,1,3))}
     coreml_preds = coreml_model.predict(coreml_input)['output']
     
     #harcoded for this simple test case
     numpy_preds = np.array([[1, 1.333, 1.666, 2, 2.333, 2.666, 3, 3, 3],\
             [1, 1.333, 1.6666, 2, 2.33333, 2.6666, 3, 3, 3]])
     #numpy_preds = np.array([[1, 1, 1, 2, 2, 2, 3, 3, 3],[1, 1, 1, 2, 2, 2, 3, 3, 3]])
     #Test
     self.assertTrue(self._compare_shapes(numpy_preds, coreml_preds))
     self.assertTrue(self._compare_predictions(numpy_preds, coreml_preds))
     
     if os.path.exists(model_dir):
         shutil.rmtree(model_dir)
Esempio n. 13
0
        def get_coreml_model_roi_align(params):
            eval = True
            mlmodel = None
            batch, ch, n_roi = params["b_c_n"]
            H = params["H"]
            W = params["W"]
            s_ratio = params["sampling_ratio"]
            try:
                input_features = [('data', datatypes.Array(ch,H,W))]
                if batch == 1:
                    input_features.append(('roi', datatypes.Array(4, 1, 1)))
                else:
                    input_features.append(('roi', datatypes.Array(5, 1, 1)))
                output_features = [('output', None)]
                builder = neural_network.NeuralNetworkBuilder(input_features, output_features)

                builder.add_crop_resize('resize', ['data', 'roi'], 'output_crop_resize',
                                        target_height=params["Hnew"]*s_ratio, target_width=params["Wnew"]*s_ratio,
                                        mode='ROI_ALIGN_MODE',
                                        normalized_roi=False,
                                        box_indices_mode='CORNERS_WIDTH_FIRST',
                                        spatial_scale=params["spatial_scale"])
                builder.add_pooling('pool', height=s_ratio, width=s_ratio,
                                    stride_height=s_ratio, stride_width=s_ratio,
                                    layer_type='AVERAGE',
                                    padding_type='VALID',
                                    input_name='output_crop_resize', output_name='output')
                mlmodel = MLModel(builder.spec)
            except RuntimeError as e:
                print(e)
                eval = False

            return mlmodel, eval
Esempio n. 14
0
        def get_coreml_model_resize_bilinear(X, params):
            eval = True
            mlmodel = None
            try:
                input_dim = X.shape[2:]
                input_features = [("data", datatypes.Array(*input_dim))]
                output_features = [("output", None)]
                builder = neural_network.NeuralNetworkBuilder(
                    input_features, output_features)
                if params["align_corners"]:
                    mode = "STRICT_ALIGN_ENDPOINTS_MODE"
                else:
                    mode = "UPSAMPLE_MODE"
                builder.add_resize_bilinear(
                    "resize",
                    "data",
                    "output",
                    target_height=params["Hnew"],
                    target_width=params["Wnew"],
                    mode=mode,
                )
                mlmodel = MLModel(builder.spec)
            except RuntimeError as e:
                print(e)
                eval = False

            return mlmodel, eval
Esempio n. 15
0
 def test_dead_layer_remove(self):
     input_features = [("data", datatypes.Array(*(3, 4)))]
     output_features = [("out", None)]
     builder = neural_network.NeuralNetworkBuilder(
         input_features, output_features, disable_rank5_shape_mapping=True
     )
     builder.add_activation("relu1", "RELU", "data", "relu1")
     builder.add_load_constant_nd(
         "const1", "c1", constant_value=np.ones((5,)), shape=(5,)
     )
     builder.add_load_constant_nd(
         "const2", "c2", constant_value=np.ones((5,)), shape=(5,)
     )
     builder.add_split_nd(
         "splitnd1", "const2", ["s1", "s2", "s3"], axis=0, num_splits=3
     )
     builder.add_squeeze("squeeze", "s1", "squeeze_out")
     builder.add_activation("relu4", "RELU", "s2", "relu4")
     builder.add_activation("relu5", "RELU", "relu4", "relu5")
     builder.add_load_constant_nd(
         "const3", "c3", constant_value=np.ones((5,)), shape=(5,)
     )
     builder.add_activation("relu2", "RELU", "relu1", "out")
     spec = builder.spec
     np.testing.assert_equal(9, len(spec.neuralNetwork.layers))
     remove_disconnected_layers(spec)
     np.testing.assert_equal(2, len(spec.neuralNetwork.layers))
Esempio n. 16
0
def get_coreml_predictions_slice(X, params):
    coreml_preds = []
    eval = True
    try:
        input_dim = X.shape
        output_dim = (1, 1, 1) #some random dimensions here: we are going to remove this information later
        input_features = [('data', datatypes.Array(*input_dim))]
        output_features = [('output', datatypes.Array(*output_dim))]
        builder = neural_network.NeuralNetworkBuilder(input_features, output_features)
        builder.add_slice('slice', 'data', 'output', start_index = params["start"], 
                            end_index = params["end"], stride = params["stride"], axis = params["axis"])                 
        #Remove output shape by deleting and adding an output
        del builder.spec.description.output[-1]                            
        output = builder.spec.description.output.add()
        output.name = 'output' 
        output.type.multiArrayType.dataType = coremltools.proto.FeatureTypes_pb2.ArrayFeatureType.ArrayDataType.Value('DOUBLE')
        #save the model                        
        model_dir = tempfile.mkdtemp()
        model_path = os.path.join(model_dir, 'test_layer.mlmodel')                        
        coremltools.utils.save_spec(builder.spec, model_path)
        #preprare input and get predictions
        coreml_model = coremltools.models.MLModel(model_path)
        coreml_input = {'data': X}
        coreml_preds = coreml_model.predict(coreml_input)['output']
        if os.path.exists(model_dir):
            shutil.rmtree(model_dir)
    except RuntimeError as e:
        print(e)
        eval = False        
        
    return coreml_preds, eval    
Esempio n. 17
0
    def test_lrn_model(tmpdir):

        input_dim = (1, 3, 3)
        input_features = [("data", datatypes.Array(*input_dim))]
        output_features = [("output", datatypes.Array(*input_dim))]

        builder = neural_network.NeuralNetworkBuilder(input_features, output_features)
        builder.add_lrn(
            name="lrn",
            input_name="data",
            output_name="output",
            alpha=2,
            beta=3,
            local_size=1,
            k=8,
        )

        input = {"data": np.ones((1, 3, 3))}
        expected = 1e-3 * np.ones((1, 3, 3))
        model_path = os.path.join(str(tmpdir), "lrn_model.mlmodel")
        coremltools.models.utils.save_spec(builder.spec, model_path)

        try:
            model = coremltools.models.MLModel(model_path)
            out = model.predict(input, useCPUOnly=True)
        except RuntimeError as e:
            print(e)
            assert str(e) == "Error compiling model: \"The file couldn’t be saved.\"."
        else:
            assert out['output'].shape == (1, 3, 3)
            np.testing.assert_allclose(expected, out['output'])
            print("Core ML output", out)
Esempio n. 18
0
 def test_conv_same_padding(self):
     
     #create a tiny mlmodel
     input_dim = (10,15,15)
     input_features = [('data', datatypes.Array(*input_dim))]
     output_features = [('output', None)]
             
     builder = neural_network.NeuralNetworkBuilder(input_features, output_features)
     
     W = np.random.rand(3,3,10,20)
             
     builder.add_convolution(name = 'conv', kernel_channels = 10, output_channels = 20, 
                     height = 3, width = 3, stride_height = 2, stride_width = 2, 
                     border_mode = 'same', groups = 1, 
                     W = W, b = None, has_bias = False, 
                     input_name = 'data', output_name = 'output',
                     same_padding_asymmetry_mode = 'TOP_LEFT_HEAVY')
                     
     #save the model
     model_dir = tempfile.mkdtemp()
     model_path = os.path.join(model_dir, 'test_layer.mlmodel')                        
     coremltools.utils.save_spec(builder.spec, model_path)
     
     #preprare input and get predictions
     coreml_model = coremltools.models.MLModel(model_path)
     x = np.random.rand(*input_dim)
     coreml_input = {'data': x}
     coreml_preds = coreml_model.predict(coreml_input)['output']
     
     #harcoded for this simple test case
     numpy_preds = np.random.rand(20,8,8)
     self.assertTrue(self._compare_shapes(numpy_preds, coreml_preds))
     
     if os.path.exists(model_dir):
         shutil.rmtree(model_dir)
Esempio n. 19
0
    def test_min(self):

        #create a tiny mlmodel
        input_dim = (1, 2, 2)
        input_features = [('data_0', datatypes.Array(*input_dim)),
                          ('data_1', datatypes.Array(*input_dim))]
        output_features = [('output', None)]

        builder = neural_network.NeuralNetworkBuilder(input_features,
                                                      output_features)

        builder.add_elementwise(name='min',
                                input_names=['data_0', 'data_1'],
                                output_name='output',
                                mode='MIN')

        #save the model
        model_dir = tempfile.mkdtemp()
        model_path = os.path.join(model_dir, 'test_layer.mlmodel')
        coremltools.utils.save_spec(builder.spec, model_path)

        #preprare input and get predictions
        coreml_model = coremltools.models.MLModel(model_path)
        x1 = np.reshape(np.arange(4, dtype=np.float32), (1, 2, 2))
        x2 = np.reshape(np.arange(2, 6, dtype=np.float32), (1, 2, 2))
        coreml_input = {'data_0': x1, 'data_1': x2}
        coreml_preds = coreml_model.predict(coreml_input)['output']

        #harcoded for this simple test case
        numpy_preds = np.minimum(x1, x2)
        self.assertTrue(self._compare_shapes(numpy_preds, coreml_preds))
        self.assertTrue(self._compare_predictions(numpy_preds, coreml_preds))

        if os.path.exists(model_dir):
            shutil.rmtree(model_dir)
Esempio n. 20
0
 def test_padding_replication(self):
     
     #create a tiny mlmodel
     input_dim = (1,2,3)
     input_features = [('data', datatypes.Array(*input_dim))]
     output_features = [('output', None)]
             
     builder = neural_network.NeuralNetworkBuilder(input_features, output_features)
             
     builder.add_padding(name = 'pad', 
                         left = 1, top = 2, 
                         input_name = 'data',
                         output_name = 'output', padding_type = 'replication')
                     
     #save the model
     model_dir = tempfile.mkdtemp()
     model_path = os.path.join(model_dir, 'test_layer.mlmodel')                        
     coremltools.utils.save_spec(builder.spec, model_path)
     
     #preprare input and get predictions
     coreml_model = coremltools.models.MLModel(model_path)
     x = np.reshape(np.array([[1,2,3], [4,5,6]]), (1,2,3)).astype(np.float32)
     coreml_input = {'data': x}
     coreml_preds = coreml_model.predict(coreml_input)['output']
     
     #harcoded for this simple test case
     numpy_preds = np.reshape(np.array([[1,1,2,3], [1,1,2,3], [1,1,2,3], [4,4,5,6]]), (1,4,4)).astype(np.float32)
     self.assertTrue(self._compare_shapes(numpy_preds, coreml_preds))
     self.assertTrue(self._compare_predictions(numpy_preds, coreml_preds))
     
     if os.path.exists(model_dir):
         shutil.rmtree(model_dir)
Esempio n. 21
0
    def test_linear_activation_CPU(self):

        #create a tiny mlmodel
        input_dim = (10, 15, 15)
        input_features = [('data', datatypes.Array(*input_dim))]
        output_features = [('output', None)]

        builder = neural_network.NeuralNetworkBuilder(input_features,
                                                      output_features)

        builder.add_activation(name='activation',
                               non_linearity='LINEAR',
                               input_name='data',
                               output_name='output',
                               params=[34.0, 67.0])

        #save the model
        model_dir = tempfile.mkdtemp()
        model_path = os.path.join(model_dir, 'test_layer.mlmodel')
        coremltools.utils.save_spec(builder.spec, model_path)

        #preprare input and get predictions
        coreml_model = coremltools.models.MLModel(model_path)
        x = np.random.rand(*input_dim)
        coreml_input = {'data': x}
        coreml_preds = coreml_model.predict(coreml_input,
                                            useCPUOnly=True)['output']

        #harcoded for this simple test case
        numpy_preds = 34.0 * x + 67.0
        self.assertTrue(self._compare_shapes(numpy_preds, coreml_preds))
        self.assertTrue(self._compare_predictions(numpy_preds, coreml_preds))

        if os.path.exists(model_dir):
            shutil.rmtree(model_dir)
Esempio n. 22
0
        def get_coreml_model_depthwise(X, params, w):
            eval = True
            mlmodel = None
            try:
                input_dim = X.shape[2:]
                input_features = [('data', datatypes.Array(*input_dim))]
                output_features = [('output', None)]
                builder = neural_network.NeuralNetworkBuilder(input_features, output_features)
                # tranlate weights : (Kh, Kw, kernel_channels, output_channels) == (Kh, Kw, Cin/g, Cout) == (Kh, Kw, 1, channel_multiplier * Cin)
                w_e = np.reshape(w, (params["kernel_size"], params["kernel_size"], params["multiplier"] * params["C"], 1))
                w_e = np.transpose(w_e, [0, 1, 3, 2])
                if params["padding"] == 'SAME':
                    pad_mode = 'same'
                else:
                    pad_mode = 'valid'
                builder.add_convolution('conv',
                                        kernel_channels=1,
                                        output_channels=params["multiplier"] * params["C"],
                                        height=params["kernel_size"], width=params["kernel_size"],
                                        stride_height=params["stride"], stride_width=params["stride"],
                                        border_mode=pad_mode,
                                        groups=params["C"],
                                        W=w_e, b=None,
                                        has_bias=0, is_deconv=0, output_shape=None,
                                        input_name='data', output_name='output')

                mlmodel = MLModel(builder.spec)
            except RuntimeError as e:
                print(e)
                eval = False
            return mlmodel, eval
Esempio n. 23
0
def get_coreml_predictions_unary(x, mode, alpha=1.0):

    #create a tiny mlmodel
    input_dim = x.shape
    input_features = [('data', datatypes.Array(*input_dim))]
    output_features = [('output', datatypes.Array(*input_dim))]

    builder = neural_network.NeuralNetworkBuilder(input_features,
                                                  output_features)

    builder.add_unary(name='unary',
                      input_name='data',
                      output_name='output',
                      mode=mode,
                      alpha=alpha)

    #save the model
    model_dir = tempfile.mkdtemp()
    model_path = os.path.join(model_dir, 'test_layer.mlmodel')
    coremltools.utils.save_spec(builder.spec, model_path)

    #preprare input and get predictions
    coreml_model = coremltools.models.MLModel(model_path)
    if macos_version() >= (10, 13):
        coreml_input = {'data': x}
        coreml_preds = coreml_model.predict(coreml_input)['output']
    else:
        coreml_preds = None

    if os.path.exists(model_dir):
        shutil.rmtree(model_dir)

    return coreml_preds
Esempio n. 24
0
 def test_dead_layer_remove(self):
     input_features = [('data', datatypes.Array(*(3, 4)))]
     output_features = [('out', None)]
     builder = neural_network.NeuralNetworkBuilder(
         input_features, output_features, disable_rank5_shape_mapping=True)
     builder.add_activation('relu1', 'RELU', 'data', 'relu1')
     builder.add_load_constant_nd('const1',
                                  'c1',
                                  constant_value=np.ones((5, )),
                                  shape=(5, ))
     builder.add_load_constant_nd('const2',
                                  'c2',
                                  constant_value=np.ones((5, )),
                                  shape=(5, ))
     builder.add_split_nd('splitnd1',
                          'const2', ['s1', 's2', 's3'],
                          axis=0,
                          num_splits=3)
     builder.add_squeeze('squeeze', 's1', 'squeeze_out')
     builder.add_activation('relu4', 'RELU', 's2', 'relu4')
     builder.add_activation('relu5', 'RELU', 'relu4', 'relu5')
     builder.add_load_constant_nd('const3',
                                  'c3',
                                  constant_value=np.ones((5, )),
                                  shape=(5, ))
     builder.add_activation('relu2', 'RELU', 'relu1', 'out')
     spec = builder.spec
     np.testing.assert_equal(9, len(spec.neuralNetwork.layers))
     remove_disconnected_layers(spec)
     np.testing.assert_equal(2, len(spec.neuralNetwork.layers))
Esempio n. 25
0
    def test_branch_structure(self):
        """
                INPUT
                 |
                 v
                [t_0]
                 |
                 v
                [t_1]
                 |
                 v
                [t_3] --.
                 |      |
                 v      v
                [t_4]  RELU_1
                 |
                 v
                [t_5]
                 |
                 v
                RELU_2
        t_0, t_1, t_3 can be merged.
        t_4, t_5 can be merged.
        The output shuld be
                INPUT
                 |
                 .------.
                 |      |
                 v      v
               RELU_2  RELU_1

        """
        input_shape = (1, 10, 5)
        input_features = [("data", datatypes.Array(1, 10, 5))]
        output_features = [("out", None)]
        builder = neural_network.NeuralNetworkBuilder(
            input_features, output_features, disable_rank5_shape_mapping=True)
        transpose = [[2, 1, 0], [2, 1, 0], [0, 1, 2], [2, 0, 1], [1, 2, 0]]
        input_name = "data"
        for i, axes in enumerate(transpose):
            name = "transpose_" + str(i)
            output_name = name + "_out"
            builder.add_transpose(name=name,
                                  axes=axes,
                                  input_name=input_name,
                                  output_name=output_name)
            input_name = output_name

        builder.add_activation(name="relu",
                               non_linearity="RELU",
                               input_name=input_name,
                               output_name="out")
        builder.add_activation(
            name="dumpy",
            non_linearity="RELU",
            input_name="transpose_2_out",
            output_name="dumpy",
        )
        self._test_builder(builder, input_shape, 2)
Esempio n. 26
0
    def test_embeddingND_quantize(self):
        input_features = [("data", datatypes.Array(10, 1))]
        output_features = [("output", None)]
        builder = neural_network.NeuralNetworkBuilder(
            input_features, output_features, disable_rank5_shape_mapping=True)

        builder.add_embedding_nd(
            name="embedding_nd",
            input_name="data",
            output_name="output",
            vocab_size=300,
            embedding_size=20,
            W=np.random.rand(20, 300),
        )

        spec = builder.spec
        model_fp32 = coremltools.models.MLModel(spec)
        self.assertEqual(
            len(spec.neuralNetwork.layers[0].embeddingND.weights.floatValue),
            6000)

        # quantize to FP16
        model_fp16 = quantization_utils.quantize_weights(model_fp32, nbits=16)
        spec_fp16 = model_fp16.get_spec()
        self.assertEqual(
            len(spec_fp16.neuralNetwork.layers[0].embeddingND.weights.
                floatValue), 0)
        self.assertEqual(
            len(spec_fp16.neuralNetwork.layers[0].embeddingND.weights.
                float16Value),
            2 * 6000,
        )

        # quantize to uint8
        model_uint8 = quantization_utils.quantize_weights(model_fp32, nbits=8)
        spec_uint8 = model_uint8.get_spec()
        self.assertEqual(
            len(spec_uint8.neuralNetwork.layers[0].embeddingND.weights.
                floatValue), 0)
        self.assertEqual(
            len(spec_uint8.neuralNetwork.layers[0].embeddingND.weights.
                float16Value), 0)
        self.assertEqual(
            len(spec_uint8.neuralNetwork.layers[0].embeddingND.weights.rawValue
                ), 6000)

        # quantize to uint5
        model_uint5 = quantization_utils.quantize_weights(model_fp32, nbits=5)
        spec_uint5 = model_uint5.get_spec()
        self.assertEqual(
            len(spec_uint5.neuralNetwork.layers[0].embeddingND.weights.
                floatValue), 0)
        self.assertEqual(
            len(spec_uint5.neuralNetwork.layers[0].embeddingND.weights.
                float16Value), 0)
        self.assertEqual(
            len(spec_uint5.neuralNetwork.layers[0].embeddingND.weights.rawValue
                ), 3750)  # 3750 = 5*6000/8
Esempio n. 27
0
    def test_branch_structure(self):
        '''
                INPUT
                 |
                 v
                [t_0]
                 |
                 v
                [t_1]
                 |
                 v
                [t_3] --.
                 |      |
                 v      v
                [t_4]  RELU_1
                 |
                 v
                [t_5]
                 |
                 v
                RELU_2
        t_0, t_1, t_3 can be merged.
        t_4, t_5 can be merged.
        The output shuld be
                INPUT
                 |
                 .------.
                 |      |
                 v      v
               RELU_2  RELU_1

        '''
        input_shape = (1, 10, 5)
        input_features = [('data', datatypes.Array(1, 10, 5))]
        output_features = [('out', None)]
        builder = neural_network.NeuralNetworkBuilder(
            input_features, output_features, disable_rank5_shape_mapping=True)
        transpose = [[2, 1, 0], [2, 1, 0], [0, 1, 2], [2, 0, 1], [1, 2, 0]]
        input_name = 'data'
        for i, axes in enumerate(transpose):
            name = 'transpose_' + str(i)
            output_name = name + '_out'
            builder.add_transpose(name=name,
                                  axes=axes,
                                  input_name=input_name,
                                  output_name=output_name)
            input_name = output_name

        builder.add_activation(name='relu',
                               non_linearity='RELU',
                               input_name=input_name,
                               output_name='out')
        builder.add_activation(name='dumpy',
                               non_linearity='RELU',
                               input_name='transpose_2_out',
                               output_name='dumpy')
        self._test_builder(builder, input_shape, 2)
Esempio n. 28
0
    def test_conv_crop_bn_relu_to_conv_bn_relu_crop(self):
        input_features = [('data', datatypes.Array(1, 10, 10))]
        output_features = [('out', None)]
        builder = neural_network.NeuralNetworkBuilder(input_features,
                                                      output_features)
        W = np.ones((2, 10, 1, 10), dtype=np.float32)
        builder.add_convolution(name='conv',
                                kernel_channels=1,
                                output_channels=2,
                                height=2,
                                width=2,
                                stride_height=1,
                                stride_width=1,
                                border_mode='valid',
                                groups=1,
                                W=W,
                                b=None,
                                has_bias=False,
                                input_name='data',
                                output_name='conv_out')
        builder.add_crop(name='crop',
                         left=1,
                         right=1,
                         top=1,
                         bottom=1,
                         offset=0,
                         input_names=['conv_out'],
                         output_name='crop_out')
        builder.add_batchnorm(name='bn',
                              channels=2,
                              gamma=np.ones(2, ).astype(np.float32),
                              beta=np.ones(2, ).astype(np.float32),
                              mean=np.ones(2, ).astype(np.float32),
                              variance=np.ones(2, ).astype(np.float32),
                              input_name='crop_out',
                              output_name='bn_out')
        builder.add_activation(name='relu',
                               non_linearity='RELU',
                               input_name='bn_out',
                               output_name='out')
        # Conv -> Crop -> BN -> ReLU
        spec = builder.spec.neuralNetwork
        np.testing.assert_equal('crop', spec.layers[1].WhichOneof('layer'))
        np.testing.assert_equal('batchnorm',
                                spec.layers[2].WhichOneof('layer'))
        np.testing.assert_equal('activation',
                                spec.layers[3].WhichOneof('layer'))

        # transform the pattern
        transform_conv_crop(builder.spec)
        # Conv -> BN -> ReLU -> Crop
        np.testing.assert_equal('batchnorm',
                                spec.layers[1].WhichOneof('layer'))
        np.testing.assert_equal('activation',
                                spec.layers[2].WhichOneof('layer'))
        np.testing.assert_equal('crop', spec.layers[3].WhichOneof('layer'))
Esempio n. 29
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)
Esempio n. 30
0
 def initialize(self):
     np.random.seed(1988)
     self.Cout, self.Cin = 16, 32
     self.W = np.random.rand(self.Cout, self.Cin) * 20.0 - 10.0
     self.b = np.random.rand(self.Cout) * 20.0 - 10.0
     self.input_shape = (5, self.Cin)
     input_features = [("data", datatypes.Array(*self.input_shape))]
     output_features = [("output", None)]
     self.builder = neural_network.NeuralNetworkBuilder(
         input_features, output_features, disable_rank5_shape_mapping=True)
     self.selector = MatrixMultiplyLayerSelector()