Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_l2_normalize_converter(self):
input_dim = (3,)
output_dim = (3,)
input = [('input', datatypes.Array(*input_dim))]
output = [('output', datatypes.Array(*output_dim))]
builder = NeuralNetworkBuilder(input, output)
builder.add_l2_normalize(name='L2', input_name='input', output_name='output')
model_onnx = convert_coreml(builder.spec)
self.assertTrue(model_onnx is not None)
def _test_one_to_one_operator_coreml(self, keras_model, x):
# Verify Keras-to-CoreML-to-ONNX path
coreml_model = None
try:
coreml_model = coremltools.converters.keras.convert(keras_model)
except (AttributeError, ImportError) as e:
warnings.warn("Unable to test due to an error in coremltools '{0}'".format(e))
onnx_model = None if coreml_model is None else onnxmltools.convert_coreml(coreml_model)
self.assertTrue(onnx_model or coreml_model is None)
if self._no_available_inference_engine():
return
y_reference = keras_model.predict(x)
if onnx_model is None:
return
y_produced = evaluate_deep_model(onnx_model, x)
self.assertTrue(np.allclose(y_reference, y_produced))
# Verify Keras-to-ONNX path
onnx_model = onnxmltools.convert_keras(keras_model)
y_produced = evaluate_deep_model(onnx_model, x)
def test_multiply_converter(self):
input_dim = (1, 2, 2)
output_dim = (1, 2, 2)
inputs = [('input1', datatypes.Array(*input_dim)), ('input2', datatypes.Array(*input_dim))]
output = [('output', datatypes.Array(*output_dim))]
builder = NeuralNetworkBuilder(inputs, output)
builder.add_elementwise(name='Mul', input_names=['input1', 'input2'], output_name='output', mode='MULTIPLY')
model_onnx = convert_coreml(builder.spec)
self.assertTrue(model_onnx is not None)
def test_flatten(self):
N, C, H, W, D = 2, 3, 1, 2, 2
x = create_tensor(N, C, H, W)
keras_model = Sequential()
keras_model.add(Flatten(input_shape=(H, W, C)))
keras_model.add(Dense(D))
keras_model.compile(optimizer='adagrad', loss='mse')
try:
coreml_model = coremltools.converters.keras.convert(keras_model)
except (ImportError, AttributeError):
warnings.warn("Issue in coremltools.")
return
onnx_model = onnxmltools.convert_coreml(coreml_model)
self.assertIsNotNone(onnx_model)
if not self._no_available_inference_engine():
y_reference = keras_model.predict(np.transpose(x, [0, 2, 3, 1]))
y_produced = evaluate_deep_model(onnx_model, x).reshape(N, D)
self.assertTrue(np.allclose(y_reference, y_produced))
def test_softmax_converter(self):
input_dim = (3,)
output_dim = (3,)
input = [('input', datatypes.Array(*input_dim))]
output = [('output', datatypes.Array(*output_dim))]
builder = NeuralNetworkBuilder(input, output)
builder.add_softmax(name='Softmax', input_name='input', output_name='output')
model_onnx = convert_coreml(builder.spec)
self.assertTrue(model_onnx is not None)
def test_min_converter(self):
input_dim = (1, 2, 2)
output_dim = (1, 2, 2)
inputs = [('input1', datatypes.Array(*input_dim)), ('input2', datatypes.Array(*input_dim))]
output = [('output', datatypes.Array(*output_dim))]
builder = NeuralNetworkBuilder(inputs, output)
builder.add_elementwise(name='Min', input_names=['input1', 'input2'], output_name='output', mode='MIN')
model_onnx = convert_coreml(builder.spec)
self.assertTrue(model_onnx is not None)
def test_dense(self):
N, C, D = 2, 3, 2
x = create_tensor(N, C)
input = Input(shape=(C,))
result = Dense(D)(input)
keras_model = Model(inputs=input, outputs=result)
keras_model.compile(optimizer='adagrad', loss='mse')
try:
coreml_model = coremltools.converters.keras.convert(keras_model)
except (AttributeError, ImportError) as e:
warnings.warn("Unable to test due to an error in coremltools '{0}'.".format(e))
return
onnx_model = onnxmltools.convert_coreml(coreml_model)
if not self._no_available_inference_engine():
y_reference = keras_model.predict(x)
y_produced = evaluate_deep_model(onnx_model, x).reshape(N, D)
self.assertTrue(np.allclose(y_reference, y_produced))
def test_pooling_converter(self):
input_dim = (1, 1, 4, 2)
output_dim = (1, 1, 4, 2)
input = [('input', datatypes.Array(*input_dim))]
output = [('output', datatypes.Array(*output_dim))]
builder = NeuralNetworkBuilder(input, output)
builder.add_pooling(name='Pool', height=2, width=2, stride_height=1, stride_width=1, layer_type='MAX',
padding_type='SAME', input_name='input', output_name='output')
model_onnx = convert_coreml(builder.spec)
self.assertTrue(model_onnx is not None)
def test_max_converter(self):
input_dim = (1, 2, 2)
output_dim = (1, 2, 2)
inputs = [('input1', datatypes.Array(*input_dim)), ('input2', datatypes.Array(*input_dim))]
output = [('output', datatypes.Array(*output_dim))]
builder = NeuralNetworkBuilder(inputs, output)
builder.add_elementwise(name='Max', input_names=['input1', 'input2'], output_name='output', mode='MAX')
model_onnx = convert_coreml(builder.spec)
self.assertTrue(model_onnx is not None)
def test_bidirectional_lstm_converter(self):
input_dim = (1, 8)
output_dim = (1, 2)
inputs = [('input', datatypes.Array(*input_dim))]
outputs = [('output', datatypes.Array(*output_dim))]
builder = NeuralNetworkBuilder(inputs, outputs)
W_h = [numpy.random.rand(2, 2), numpy.random.rand(2, 2), numpy.random.rand(2, 2), numpy.random.rand(2, 2)]
W_x = [numpy.random.rand(2, 8), numpy.random.rand(2, 8), numpy.random.rand(2, 8), numpy.random.rand(2, 8)]
b = [numpy.random.rand(2, 1), numpy.random.rand(2, 1), numpy.random.rand(2, 1), numpy.random.rand(2, 1)]
p = [numpy.zeros(shape=(2, 1)), numpy.zeros(shape=(2, 1)), numpy.zeros(shape=(2, 1))]
builder.add_bidirlstm(name='LSTM', W_h=W_h, W_x=W_x, W_h_back=W_h, b=b, W_x_back=W_x, b_back=b, hidden_size=2,
input_size=8, input_names=['input'], output_names=['output'], inner_activation='SIGMOID',
cell_state_update_activation='TANH', output_activation='TANH', peep=p, peep_back=p,
output_all=False, forget_bias=False, coupled_input_forget_gate=False,
cell_clip_threshold=10000)
model_onnx = convert_coreml(builder.spec)
self.assertTrue(model_onnx is not None)