-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpredict.py
51 lines (41 loc) · 1.86 KB
/
predict.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
import os
from argparse import ArgumentParser
import tensorflow as tf
from model import MobileNetV1
import numpy as np
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--test-file-path", type=str, required=True)
parser.add_argument("--model-path", default='./model/model.h5', type=str)
parser.add_argument("--image-size", default=224, type=int)
parser.add_argument("--image-channels", default=3, type=int)
parser.add_argument("--rho", help='modify img_size, should be (0:1]',default=1.0, type=float)
home_dir = os.getcwd()
args = parser.parse_args()
rho = args.rho
image_size = args.image_size
# FIXME
# Project Description
print('---------------------Welcome to ProtonX MobileNet-------------------')
print('Github: https://github.com/protonx-tf-03-projects/MobileNet')
print('Email: ${email}')
print('---------------------------------------------------------------------')
print('Testing MobileNet model with hyper-params:')
print('===========================')
for i, arg in enumerate(vars(args)):
print('{}.{}: {}'.format(i, arg, vars(args)[arg]))
print('===========================')
# Loading Model
mobilenet = tf.keras.models.load_model(args.model_path)
# Load test images from folder
image = tf.keras.preprocessing.image.load_img(args.test_file_path)
input_arr = tf.keras.preprocessing.image.img_to_array(image)
input_arr = np.array([input_arr]) # Convert single image to a batch.
x = tf.image.resize(
input_arr, [(int(image_size*rho)), (int(image_size*rho))]
)
x = x / 255
predictions = mobilenet.predict(x)
print('---------------------Prediction Result: -------------------')
print('Output Softmax: {}'.format(predictions))
print('This image belongs to class: {}'.format(np.argmax(predictions), axis=1))