forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference_test.py
executable file
·114 lines (89 loc) · 3.97 KB
/
inference_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Run `yarn test-python` in the package root directory.
# This test only supports running in Linux.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import json
import os.path
import tempfile
import shutil
import tensorflow as tf
import inference
class InferenceTest(tf.test.TestCase):
def testInference(self):
backends = ['cpu', 'wasm']
for backend in backends:
binary_path = os.path.join('../binaries', 'tfjs-inference-linux')
model_path = os.path.join('../test_data', 'model.json')
test_data_dir = os.path.join('../test_data')
tmp_dir = tempfile.mkdtemp()
inference.predict(binary_path, model_path, test_data_dir, tmp_dir, backend=backend)
with open(os.path.join(tmp_dir, 'data.json'), 'rt') as f:
ys_values = json.load(f)
# The output is a list of tensor data in the form of dict.
# Example output:
# [{"0":0.7567615509033203,"1":-0.18349379301071167,"2":0.7567615509033203,"3":-0.18349379301071167}]
ys_values = [list(y.values()) for y in ys_values]
with open(os.path.join(tmp_dir, 'shape.json'), 'rt') as f:
ys_shapes = json.load(f)
with open(os.path.join(tmp_dir, 'dtype.json'), 'rt') as f:
ys_dtypes = json.load(f)
self.assertAllClose(ys_values[0], [
0.7567615509033203, -0.18349379301071167, 0.7567615509033203,
-0.18349379301071167
])
self.assertAllEqual(ys_shapes[0], [2, 2])
self.assertEqual(ys_dtypes[0], 'float32')
# Cleanup tmp dir.
shutil.rmtree(tmp_dir)
# Todo(linazhao): Add a test model that outputs multiple tensors.
def testInferenceWithOutputNameFile(self):
binary_path = os.path.join('../binaries', 'tfjs-inference-linux')
model_path = os.path.join('../test_data', 'model.json')
test_data_dir = os.path.join('../test_data')
tmp_dir = tempfile.mkdtemp()
inference.predict(binary_path, model_path, test_data_dir, tmp_dir, tf_output_name_file='tf_output_name.json')
with open(os.path.join(tmp_dir, 'data.json'), 'rt') as f:
ys_values = json.load(f)
# The output is a list of tensor data in the form of dict.
# Example output:
# [{"0":0.7567615509033203,"1":-0.18349379301071167,"2":0.7567615509033203,"3":-0.18349379301071167}]
ys_values = [list(y.values()) for y in ys_values]
with open(os.path.join(tmp_dir, 'shape.json'), 'rt') as f:
ys_shapes = json.load(f)
with open(os.path.join(tmp_dir, 'dtype.json'), 'rt') as f:
ys_dtypes = json.load(f)
self.assertAllClose(ys_values[0], [
0.7567615509033203, -0.18349379301071167, 0.7567615509033203,
-0.18349379301071167
])
self.assertAllEqual(ys_shapes[0], [2, 2])
self.assertEqual(ys_dtypes[0], 'float32')
# Cleanup tmp dir.
shutil.rmtree(tmp_dir)
def testInferenceWithNonExistingOutputNameFile(self):
binary_path = os.path.join('../binaries', 'tfjs-inference-linux')
model_path = os.path.join('../test_data', 'model.json')
test_data_dir = os.path.join('../test_data')
tmp_dir = tempfile.mkdtemp()
inference.predict(binary_path, model_path, test_data_dir, tmp_dir, tf_output_name_file='non_exist.json')
with self.assertRaises(FileNotFoundError):
with open(os.path.join(tmp_dir, 'data.json'), 'rt') as f:
json.load(f)
# Cleanup tmp dir.
shutil.rmtree(tmp_dir)
if __name__ == '__main__':
tf.test.main()