def test_load_graph_model_with_fused_depthwise_prelu(self): """load_graph_model should split fused depthwise conv2d with prelu""" model_dir = testutils.get_path_to(testutils.DEPTHWISE_PRELU_PATH) graph = api.load_graph_model(model_dir) loaded_model = testutils.graph_to_model(graph) original_model_name = testutils.get_path_to( testutils.DEPTHWISE_PRELU_FILE) original_model = testutils.graph_to_model(original_model_name) # run both models and compare results x = _load_image(testutils.get_path_to('./data/human1.jpg')) y_from_loaded = _argmax(loaded_model(x)) y_from_original = _argmax(original_model(x)) # sanity check - should be reckognised as a human (class 1) self.assertEqual(y_from_original[0], 1) # same class self.assertEqual(y_from_loaded[0], y_from_original[0]) # same confidence self.assertAlmostEqual(y_from_loaded[1], y_from_original[1], 4)
def test_load_graph_model_with_simple_model(self): """load_graph_model should load simple model""" model_dir = testutils.get_path_to(testutils.SIMPLE_MODEL_PATH_NAME) graph = api.load_graph_model(model_dir) self.assertIsInstance(graph, tf.Graph) loaded_model = testutils.graph_to_model(graph) original_model_name = testutils.get_path_to( testutils.SIMPLE_MODEL_FILE_NAME) original_model = testutils.graph_to_model(original_model_name) # run both models and compare results x_ = 4 x = tf.constant([[x_]], dtype=tf.float32) y_from_loaded_model = as_scalar(loaded_model(x)) y_from_original_model = as_scalar(original_model(x)) # sanity check; fails if model is different from the one we expected: # we want a model that predicts y = 5*x self.assertAlmostEqual(y_from_original_model, x_*5, places=1) # actual test self.assertAlmostEqual(y_from_loaded_model, y_from_original_model, places=4)
def test_load_graph_model_with_prelu(self): """load_graph_model should convert prelu operations""" model_dir = testutils.get_path_to(testutils.PRELU_MODEL_PATH) graph = api.load_graph_model(model_dir) loaded_model = testutils.graph_to_model(graph) original_model_name = testutils.get_path_to(testutils.PRELU_MODEL_FILE) original_model = testutils.graph_to_model(original_model_name) # run both models and compare results cx, cy, cz, r = -0.12, 0.2, 0.1, 0.314158 px, py, pz = -0.4, 0.5, 0.4 x = tf.constant([[cx, cy, cz, r, px, py, pz]], dtype=tf.float32) y_from_loaded_model = as_scalar(loaded_model(x)) y_from_original_model = as_scalar(original_model(x)) # sanity check; fails if model is different from the one we expected: # we want a model that predicts whether a point (px,py,pz) is inside # a sphere at (cx,cy,cz) of radius r self.assertAlmostEqual(y_from_original_model, 1, places=1) # actual test self.assertAlmostEqual(y_from_loaded_model, y_from_original_model, places=4)
def test_rename_input_nodes(self): """rename_input_nodes should rename input nodes in-place""" model_file = testutils.get_path_to(testutils.SIMPLE_MODEL_FILE_NAME) graph_def = testutils.get_sample_graph_def(model_file) updated = util.rename_input_nodes(graph_def, {'x': 'scalar'}) # update should be in-place self.assertEqual(graph_def, updated) # inputs should be renamed self.assertEqual(util.get_input_nodes(updated)[0].name, 'scalar') # model should still work model = testutils.graph_to_model(updated) s = 18 scalar = tf.constant([[s]], dtype=tf.float32) result = model(scalar) value = result[0].numpy() # value = np.reshape(value, (1)) y = value[0] self.assertAlmostEqual(y, s*5, delta=0.1)
def test_rename_output_nodes_append_identity(self): """rename_output_nodes should work for outputs that aren't Identity""" model_file = testutils.get_path_to(testutils.SIMPLE_MODEL_FILE_NAME) graph_def = testutils.get_sample_graph_def(model_file) # some open-heart surgery on the model to remove the "Identity" output idx = [i for (i, n) in enumerate(graph_def.node) if n.op == 'Identity'] del graph_def.node[idx[0]] output = util.get_output_nodes(graph_def)[0].name updated = util.rename_output_nodes(graph_def, {output: 'estimate'}) # update should be in-place self.assertEqual(graph_def, updated) # outputs should be renamed self.assertEqual(util.get_output_nodes(updated)[0].name, 'estimate') # model should still work model = testutils.graph_to_model(updated) s = 18 scalar = tf.constant([[s]], dtype=tf.float32) result = model(scalar) value = result[0].numpy() # value = np.reshape(value, (1)) y = value[0] self.assertAlmostEqual(y, s*5, delta=0.1)