本文整理匯總了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)
示例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)
示例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)
示例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)
示例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)
示例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))
示例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)
示例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)
示例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)
示例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()))
示例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
示例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))
示例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,
)
示例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])
示例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"
)