-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_saved_model_inference.py
45 lines (39 loc) · 1.67 KB
/
run_saved_model_inference.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
# Copyright 2021 The Kalray Authors. All Rights Reserved.
#
# Licensed under the MIT License;
# you may not use this file except in compliance with the License.
#
# 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.
#
import os
import time
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
def run_inference(model, image_dir):
for image_file in os.listdir(image_dir):
print(f'prediction of {image_file}')
print('---------------------------')
img_path = os.path.join(image_dir, image_file)
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0) # batch
x = preprocess_input(x)
s = time.time()
preds = model.predict(x)
t = time.time() - s
fps = 1 / t
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
for (_, class_predicted, prob) in decode_predictions(preds, top=3)[0]:
print('- class: {}({:.2f}%)'.format(class_predicted, 100 * prob))
print(f'time prediction {1e3 * t:.3f} ms - {fps:.2f} FPS')
print('---------------------------')
model_path = r'saved_model'
model = tf.keras.models.load_model(model_path)
run_inference(model, r'images')