Esempio n. 1
0
def main(args: List[str]) -> None:
    if len(args) != 2:
        print(f"usage: {os.path.basename(args[0])} IMAGE")
        exit(1)

    if not os.path.isfile(args[1]):
        print(f"{os.path.basename(args[0])}: '{args[1]}' isn't a file")
        exit(1)

    tfjs.api.enable_cuda()
    image = tf.keras.preprocessing.image.load_img(args[1])
    image = tf.keras.preprocessing.image.img_to_array(image)
    # normalise image data to [-1, 1]
    image /= 127.5
    image -= 1.
    # ensure image size matches model input shape
    if image.shape != (32, 32, 3):
        print(f"{os.path.basename(args[0])}: WARNING - image size "
              f"should be 32x32, not {image.shape[0]}x{image.shape[1]}")
        image = Resizing(height=32, width=32)(image)
    # reshape to fit model input (and convert to tensor if necessary)
    image = tf.reshape(image, [1, 32, 32, 3])
    # grab the model file and convert graph to function
    graph = util.get_sample_graph(util.get_path_to(util.DEPTHWISE_RELU_FILE))
    model = tfjs.api.graph_to_function_v2(graph)
    # run the model on the input image
    result = model(image)
    # show result
    label, confidence = _evaluate(result)
    print(f"Result: {label}, confidence={confidence}")
Esempio n. 2
0
 def test_graph_to_function_v2_given_graph(self):
     """graph_def_to_function_v2 should accept tf.Graph"""
     graph = testutils.get_sample_graph(testutils.SIMPLE_MODEL_FILE_NAME)
     estimate = api.graph_to_function_v2(graph)
     x_ = 12
     x = tf.constant([[x_]], dtype=tf.float32)
     y = as_scalar(estimate(x))
     self.assertAlmostEqual(y, x_ * 5, places=1)
 def test_optimize_graph(self):
     """optimize_graph should replace nodes if possible"""
     # generate optimisable test model
     input_graph = testutils.get_sample_graph()
     input_ops = [node.op for node in _op_nodes(input_graph)]
     # optimise the graph model
     output_graph = optimization.optimize_graph(input_graph)
     output_ops = [node.op for node in _op_nodes(output_graph)]
     # output should differ from input and be more efficient (smaller)
     self.assertNotEqual(input_ops, output_ops)
     self.assertLess(len(output_ops), len(input_ops))
Esempio n. 4
0
    def test_get_input_nodes(self):
        """Should return node info for inputs"""
        def _shape_of(node):
            shape = [d.size for d in node.attr['shape'].shape.dim]
            return [n if n > 0 else None for n in shape]

        graph = testutils.get_sample_graph()
        actual = util.get_input_nodes(graph)
        expected = testutils.get_inputs(graph.as_graph_def())
        self.assertEqual(len(actual), len(expected))
        for i, result in enumerate(actual):
            self.assertEqual(result.name, expected[i].name)
            self.assertEqual(result.shape, _shape_of(expected[i]))
            self.assertEqual(result.tensor, expected[i].name + ':0')
Esempio n. 5
0
    def test_build_signatures(self):
        """_build_signatures should apply given key and include inputs"""
        graph = testutils.get_sample_graph(
            testutils.get_path_to(testutils.MULTI_HEAD_FILE))
        default_key = tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY
        debug_key = 'debug_model'
        signature_map = {
            '': {api.SIGNATURE_OUTPUTS: ['Identity']},
            debug_key: {api.SIGNATURE_OUTPUTS: ['Identity', 'Identity_1']}}

        signature_def_map = api._build_signatures(graph, signature_map)
        self.assertIn(default_key, signature_def_map)
        self.assertIn(debug_key, signature_def_map)
        for signature_def in signature_def_map.values():
            self.assertTrue(is_valid_signature(signature_def))
Esempio n. 6
0
 def test_build_signatures_applies_defaults(self):
     """_build_signatures should return defaults given None or empty"""
     graph = testutils.get_sample_graph()
     signature_map = {None: {api.SIGNATURE_OUTPUTS: ['Identity']}}
     signature_def_map = api._build_signatures(graph, signature_map)
     default_key = tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY
     self.assertIn(default_key, signature_def_map)
     default_name = tf.saved_model.PREDICT_METHOD_NAME
     self.assertEqual(signature_def_map[default_key].method_name,
                      default_name)
     # empty method names map to default, too
     signature_map = {None: {api.SIGNATURE_OUTPUTS: ['Identity'],
                             api.SIGNATURE_METHOD: ''}}
     signature_def_map = api._build_signatures(graph, signature_map)
     self.assertEqual(signature_def_map[default_key].method_name,
                      default_name)
Esempio n. 7
0
 def test_infer_signature(self):
     """Should return valid SignatureDef from TF Graph"""
     graph = testutils.get_sample_graph()
     signature_def = util.infer_signature(graph)
     self.assertIsInstance(signature_def, util.SignatureDef)
     self.assertTrue(tf.compat.v1.saved_model.is_valid_signature(
         signature_def))
     # verify inputs
     self.assertEqual(len(signature_def.inputs), 1)
     key, value = list(signature_def.inputs.items())[0]
     self.assertEqual(key, 'x')
     self.assertEqual(value.name, 'x:0')
     self.assertEqual(value.dtype, tf.dtypes.float32)
     self.assertEqual(_shape_of(value), (-1, 128, 128, 3))
     # verify outputs
     self.assertEqual(len(signature_def.outputs), 1)
     key, value = list(signature_def.outputs.items())[0]
     self.assertEqual(key, 'Identity')
     self.assertEqual(value.name, 'Identity:0')
     self.assertEqual(value.dtype, tf.dtypes.float32)
     self.assertEqual(_shape_of(value), (-1, 10))
Esempio n. 8
0
 def test_build_signatures_verifies_outputs(self):
     """_build_signatures should not accept invalid output names"""
     graph = testutils.get_sample_graph()
     signature_map = {'': {api.SIGNATURE_OUTPUTS: ['Not_A_Tensor']}}
     self.assertRaises(ValueError,
                       lambda: api._build_signatures(graph, signature_map))