当前位置: 首页>>代码示例>>Python>>正文


Python neural_network.NeuralNetworkBuilder方法代码示例

本文整理汇总了Python中coremltools.models.neural_network.NeuralNetworkBuilder方法的典型用法代码示例。如果您正苦于以下问题:Python neural_network.NeuralNetworkBuilder方法的具体用法?Python neural_network.NeuralNetworkBuilder怎么用?Python neural_network.NeuralNetworkBuilder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在coremltools.models.neural_network的用法示例。


在下文中一共展示了neural_network.NeuralNetworkBuilder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: verify_AddLayerParams

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def verify_AddLayerParams(input_dim, alpha=2):
    dtype = 'float32'

    a_np1 = np.random.uniform(size=input_dim).astype(dtype)
    a_np2 = np.random.uniform(size=input_dim).astype(dtype)

    b_np = np.add(a_np1, a_np2) + alpha
    inputs = [('input1', datatypes.Array(*input_dim)),
              ('input2', datatypes.Array(*input_dim))]
    output = [('output', datatypes.Array(*b_np.shape))]
    builder = NeuralNetworkBuilder(inputs, output)
    builder.add_elementwise(name='Add',
                            alpha=alpha,
                            input_names=['input1', 'input2'],
                            output_name='output',
                            mode='ADD')
    model = cm.models.MLModel(builder.spec)
    for target, ctx in ctx_list():
        out = run_tvm_graph(model,
                           [a_np1, a_np2],
                           ['input1', 'input2'],
                           b_np.shape,
                           dtype)
        np.testing.assert_allclose(out, b_np, rtol=1e-5) 
开发者ID:mlperf,项目名称:training_results_v0.6,代码行数:26,代码来源:test_forward.py

示例2: verify_MultiplyLayerParams

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def verify_MultiplyLayerParams(input_dim, alpha):
    dtype = 'float32'

    a_np1 = np.random.uniform(size=input_dim).astype(dtype)
    a_np2 = np.random.uniform(size=input_dim).astype(dtype)

    b_np = np.multiply(a_np1, a_np2) * alpha
    inputs = [('input1', datatypes.Array(*input_dim)),
              ('input2', datatypes.Array(*input_dim))]
    output = [('output', datatypes.Array(*b_np.shape))]
    builder = NeuralNetworkBuilder(inputs, output)
    builder.add_elementwise(name='Mul',
                            alpha=alpha,
                            input_names=['input1', 'input2'],
                            output_name='output',
                            mode='MULTIPLY')
    model = cm.models.MLModel(builder.spec)
    for target, ctx in ctx_list():
        out = run_tvm_graph(model,
                           [a_np1, a_np2],
                           ['input1', 'input2'],
                           b_np.shape,
                           dtype)
        np.testing.assert_allclose(out, b_np, rtol=1e-5) 
开发者ID:mlperf,项目名称:training_results_v0.6,代码行数:26,代码来源:test_forward.py

示例3: verify_ConcatLayerParams

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def verify_ConcatLayerParams(input1_dim, input2_dim):
    dtype = 'float32'

    a_np1 = np.random.uniform(size=input1_dim).astype(dtype)
    a_np2 = np.random.uniform(size=input2_dim).astype(dtype)

    b_np = np.concatenate((a_np1, a_np2), axis=1)
    inputs = [('input1', datatypes.Array(*input1_dim)),
              ('input2', datatypes.Array(*input2_dim))]
    output = [('output', datatypes.Array(*b_np.shape))]
    builder = NeuralNetworkBuilder(inputs, output)
    builder.add_elementwise(name='Concate',
                            input_names=['input1', 'input2'],
                            output_name='output',
                            mode='CONCAT')
    model = cm.models.MLModel(builder.spec)
    for target, ctx in ctx_list():
        out = run_tvm_graph(model,
                           [a_np1, a_np2],
                           ['input1', 'input2'],
                           b_np.shape,
                           dtype)
        np.testing.assert_allclose(out, b_np, rtol=1e-5) 
开发者ID:mlperf,项目名称:training_results_v0.6,代码行数:25,代码来源:test_forward.py

示例4: verify_UpsampleLayerParams

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def verify_UpsampleLayerParams(input_dim, scale, mode):
    dtype = "float32"

    a_np = np.full(input_dim, 1, dtype=dtype)
    if mode == 'NN':
        b_np = topi.testing.upsampling_python(a_np, scale)
    else:
        new_h = input_dim[2] * scale
        new_w = input_dim[3] * scale
        b_np = topi.testing.bilinear_resize_python(a_np, (new_h, new_w), 'NCHW')

    input = [('input', datatypes.Array(*input_dim))]
    output = [('output', datatypes.Array(*b_np.shape))]
    builder = NeuralNetworkBuilder(input, output)
    builder.add_upsample(name='Upsample',
                         scaling_factor_h=scale,
                         scaling_factor_w=scale,
                         mode=mode,
                         input_name='input',
                         output_name='output')

    model = cm.models.MLModel(builder.spec)
    for target, ctx in ctx_list():
        out = run_tvm_graph(model, a_np, 'input', b_np.shape, dtype)
        np.testing.assert_allclose(out, b_np, rtol=1e-5) 
开发者ID:mlperf,项目名称:training_results_v0.6,代码行数:27,代码来源:test_forward.py

示例5: verify_average

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def verify_average(input_dim1, input_dim2, axis=0):
    dtype = 'float32'

    a_np1 = np.random.uniform(size=input_dim1).astype(dtype)
    a_np2 = np.random.uniform(size=input_dim2).astype(dtype)

    b_np = np.mean((a_np1, a_np2), axis=axis)

    inputs = [('input1', datatypes.Array(*input_dim1)),
              ('input2', datatypes.Array(*input_dim2))]
    output = [('output', datatypes.Array(*b_np.shape))]
    builder = NeuralNetworkBuilder(inputs, output)
    builder.add_elementwise(name='MEAN',
                            input_names=['input1', 'input2'],
                            output_name='output',
                            mode='AVE')
    model = cm.models.MLModel(builder.spec)
    for target, ctx in ctx_list():
        out = run_tvm_graph(model,
                           [a_np1, a_np2],
                           ['input1', 'input2'],
                           b_np.shape,
                           dtype)
        np.testing.assert_allclose(out, b_np, rtol=1e-5) 
开发者ID:mlperf,项目名称:training_results_v0.6,代码行数:26,代码来源:test_forward.py

示例6: test_load_constant_remove

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def test_load_constant_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_activation("relu2", "RELU", "relu1", "out")
        builder.add_load_constant_nd(
            "const2", "c2", constant_value=np.ones((5,)), shape=(5,)
        )
        builder.add_load_constant_nd(
            "const3", "c3", constant_value=np.ones((5,)), shape=(5,)
        )
        spec = builder.spec
        np.testing.assert_equal(5, len(spec.neuralNetwork.layers))
        remove_disconnected_layers(spec)
        np.testing.assert_equal(2, len(spec.neuralNetwork.layers)) 
开发者ID:apple,项目名称:coremltools,代码行数:23,代码来源:test_mlmodel_passes.py

示例7: test_output_edge_case

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def test_output_edge_case(self):

        # For now for safety purpose, the node which are output should't be merged
        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
        )
        builder.add_transpose(
            name="first_transpose",
            axes=[2, 0, 1],
            input_name="data",
            output_name="first_transpose_out",
        )
        builder.add_transpose(
            name="second_transpose",
            axes=[1, 2, 0],
            input_name="first_transpose_out",
            output_name="out",
        )

        self._test_builder(builder, input_shape, 2) 
开发者ID:apple,项目名称:coremltools,代码行数:25,代码来源:test_mlmodel_passes.py

示例8: test_remove_single_identity_transpose

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def test_remove_single_identity_transpose(self):

        # A single identity transpose (like 0,1,2) should also 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
        )
        builder.add_transpose(
            name="uselss_transpose",
            axes=[0, 1, 2],
            input_name="data",
            output_name="useless_transpose_out",
        )
        builder.add_activation(
            name="relu",
            non_linearity="RELU",
            input_name="useless_transpose_out",
            output_name="out",
        )

        self._test_builder(builder, input_shape, 1) 
开发者ID:apple,项目名称:coremltools,代码行数:25,代码来源:test_mlmodel_passes.py

示例9: test_remove_three_transpose

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
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) 
开发者ID:apple,项目名称:coremltools,代码行数:26,代码来源:test_mlmodel_passes.py

示例10: test_linear_quant_inner_product_3bit

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def test_linear_quant_inner_product_3bit(self):
        W = np.reshape(np.arange(6), (2, 3)).astype(np.uint8)
        input_features = [("data", datatypes.Array(3))]
        output_features = [("probs", None)]
        builder = NeuralNetworkBuilder(input_features, output_features)
        builder.add_inner_product(
            name="ip1",
            W=_convert_array_to_nbit_quantized_bytes(W.flatten(), 3).tobytes(),
            b=None,
            input_channels=3,
            output_channels=2,
            has_bias=False,
            input_name="data",
            output_name="probs",
            quantization_type="linear",
            nbits=3,
            quant_scale=[11.0, 2.0],
            quant_bias=[-2.0, 10.0],
        )
        mlmodel = MLModel(builder.spec)
        data = np.array([1.0, 3.0, 5.0])
        data_dict = {"data": data}
        probs = mlmodel.predict(data_dict)["probs"]
        expected_out = np.array([125, 170])
        self.assertTrue(np.allclose(probs.flatten(), expected_out.flatten())) 
开发者ID:apple,项目名称:coremltools,代码行数:27,代码来源:test_nn_builder.py

示例11: _build_nn_with_one_ip_layer

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def _build_nn_with_one_ip_layer(self):
        input_features = [("data", datatypes.Array(3))]
        output_features = [("out", None)]
        builder = NeuralNetworkBuilder(
            input_features, output_features, disable_rank5_shape_mapping=True
        )
        w = np.random.uniform(-0.5, 0.5, (3, 3))
        builder.add_inner_product(
            name="ip1",
            W=w,
            b=None,
            input_channels=3,
            output_channels=3,
            has_bias=False,
            input_name="input",
            output_name="hidden",
        )
        return builder 
开发者ID:apple,项目名称:coremltools,代码行数:20,代码来源:test_nn_builder.py

示例12: test_undefined_shape_single_output

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def test_undefined_shape_single_output(self):
        W = np.ones((3, 3))
        input_features = [("data", datatypes.Array(3))]
        output_features = [("probs", None)]
        builder = NeuralNetworkBuilder(input_features, output_features)
        builder.add_inner_product(
            name="ip1",
            W=W,
            b=None,
            input_channels=3,
            output_channels=3,
            has_bias=False,
            input_name="data",
            output_name="probs",
        )
        mlmodel = MLModel(builder.spec)
        data = np.ones((3,))
        data_dict = {"data": data}
        probs = mlmodel.predict(data_dict)["probs"]
        self.assertTrue(np.allclose(probs, np.ones(3) * 3)) 
开发者ID:apple,项目名称:coremltools,代码行数:22,代码来源:test_nn_builder.py

示例13: test_multiarray_type_convert_to_float

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def test_multiarray_type_convert_to_float(self):
        input_features = [("data", datatypes.Array(2))]
        output_features = [("out", datatypes.Array(2))]
        builder = NeuralNetworkBuilder(input_features, output_features)
        builder.add_ceil("ceil", "data", "out")
        spec = builder.spec
        self.assertEqual(
            spec.description.input[0].type.multiArrayType.dataType,
            Model_pb2.ArrayFeatureType.DOUBLE,
        )
        self.assertEqual(
            spec.description.output[0].type.multiArrayType.dataType,
            Model_pb2.ArrayFeatureType.DOUBLE,
        )
        convert_double_to_float_multiarray_type(spec)
        self.assertEqual(
            spec.description.input[0].type.multiArrayType.dataType,
            Model_pb2.ArrayFeatureType.FLOAT32,
        )
        self.assertEqual(
            spec.description.output[0].type.multiArrayType.dataType,
            Model_pb2.ArrayFeatureType.FLOAT32,
        ) 
开发者ID:apple,项目名称:coremltools,代码行数:25,代码来源:test_model.py

示例14: test_multiarray_to_image_input_util

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def test_multiarray_to_image_input_util(self):
        H, W, C = 1, 1, 3
        input_features = [("data", datatypes.Array(C, H, W))]
        output_features = [("out", datatypes.Array(C, H, W))]
        builder = NeuralNetworkBuilder(
            input_features, output_features, disable_rank5_shape_mapping=True
        )
        builder.add_activation("linear", "LINEAR", "data", "out")
        spec = builder.spec
        mlmodel = MLModel(spec)
        mlmodel = make_image_input(
            mlmodel,
            "data",
            red_bias=-5,
            green_bias=-6,
            blue_bias=-2.5,
            scale=10.0,
            image_format="NCHW",
        )
        x = np.array([4, 2, 5], dtype=np.uint8)
        x = np.reshape(x, (H, W, C))
        pil_img = PIL.Image.fromarray(x)
        y = mlmodel.predict({"data": pil_img}, useCPUOnly=True)["out"]
        self.assertEqual(y.shape, (C, H, W))
        np.testing.assert_almost_equal(y.flatten(), [35.0, 14.0, 47.5]) 
开发者ID:apple,项目名称:coremltools,代码行数:27,代码来源:test_model.py

示例15: test_nn_classifier_util

# 需要导入模块: from coremltools.models import neural_network [as 别名]
# 或者: from coremltools.models.neural_network import NeuralNetworkBuilder [as 别名]
def test_nn_classifier_util(self):
        input_features = [("data", datatypes.Array(3,))]
        output_features = [("out", datatypes.Array(3,))]
        builder = NeuralNetworkBuilder(
            input_features, output_features, disable_rank5_shape_mapping=True
        )
        builder.add_activation("linear", "LINEAR", "data", "out")
        spec = builder.spec
        mlmodel = MLModel(spec)
        mlmodel = make_nn_classifier(
            mlmodel,
            class_labels=["a", "b", "c"],
            predicted_feature_name="out_confidence",
            predicted_probabilities_output="out",
        )
        out_dict = mlmodel.predict({"data": np.array([4.0, 5.5, 6.0])}, useCPUOnly=True)
        self.assertEqual(out_dict["out_confidence"], "c")
        self.assertEqual(
            mlmodel.get_spec().WhichOneof("Type"), "neuralNetworkClassifier"
        ) 
开发者ID:apple,项目名称:coremltools,代码行数:22,代码来源:test_model.py


注:本文中的coremltools.models.neural_network.NeuralNetworkBuilder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。