本文整理汇总了Python中coremltools.models.datatypes.Array方法的典型用法代码示例。如果您正苦于以下问题:Python datatypes.Array方法的具体用法?Python datatypes.Array怎么用?Python datatypes.Array使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类coremltools.models.datatypes
的用法示例。
在下文中一共展示了datatypes.Array方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: verify_AddLayerParams
# 需要导入模块: from coremltools.models import datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [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 datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [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 datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [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 datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [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 datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [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 datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [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 datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [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 datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [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 datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [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_undefined_shape_single_output
# 需要导入模块: from coremltools.models import datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [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))
示例11: test_multiarray_type_convert_to_float
# 需要导入模块: from coremltools.models import datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [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,
)
示例12: test_multiarray_to_image_input_util
# 需要导入模块: from coremltools.models import datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [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])
示例13: test_multiarray_to_image_input_util_HWC_format
# 需要导入模块: from coremltools.models import datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [as 别名]
def test_multiarray_to_image_input_util_HWC_format(self):
H, W, C = 1, 1, 3
input_features = [("data", datatypes.Array(H, W, C))]
output_features = [("out", datatypes.Array(H, W, C))]
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="NHWC",
)
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, (H, W, C))
np.testing.assert_almost_equal(y.flatten(), [35.0, 14.0, 47.5])
示例14: test_nn_classifier_util
# 需要导入模块: from coremltools.models import datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [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"
)
示例15: save_model
# 需要导入模块: from coremltools.models import datatypes [as 别名]
# 或者: from coremltools.models.datatypes import Array [as 别名]
def save_model(self, path: str):
"""
Serializes the model into path.
Args:
path (str): Target destination
"""
inputs = [('input', datatypes.Array(*self.input))]
outputs = [('output', datatypes.Array(*self.output))]
net_builder = NeuralNetworkBuilder(inputs, outputs)
input = 'input'
prev_device = next(next(self.nn.children()).parameters()).device
try:
for name, layer in self.nn.to('cpu').named_children():
input = layer.serialize(name, input, net_builder)
mlmodel = MLModel(net_builder.spec)
mlmodel.short_description = 'kraken recognition model'
mlmodel.user_defined_metadata['vgsl'] = '[' + ' '.join(self.named_spec) + ']'
if self.codec:
mlmodel.user_defined_metadata['codec'] = json.dumps(self.codec.c2l)
if self.user_metadata:
mlmodel.user_defined_metadata['kraken_meta'] = json.dumps(self.user_metadata)
mlmodel.save(path)
finally:
self.nn.to(prev_device)