-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_model.py
56 lines (49 loc) · 1.72 KB
/
test_model.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
import matplotlib.pyplot as plt
import numpy as np
import sys
import pathlib
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
# configurations
MODEL_PATH = 'saved_model/my_model'
WEIGHTS_PATH = 'saved_weight/my_checkpoint'
EPOCHS = 10
IMG_HEIGHT = 180
IMG_WIDTH = 180
# create model
model = Sequential([
layers.experimental.preprocessing.Rescaling(1./255, input_shape=(IMG_HEIGHT, IMG_WIDTH, 3)),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(2)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# Restore the weights
model.load_weights(WEIGHTS_PATH)
# Restore entire model NOTE: you can remove model defination if if you want to use this option.
#model = keras.models.load_model(MODEL_PATH)
for img_path in sys.argv[1:]:
img = keras.preprocessing.image.load_img(
img_path, target_size=(IMG_HEIGHT, IMG_WIDTH)
)
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
class_names = ['correct_mask', 'no_mask']
print(predictions[0])
print(np.max(score), score)
print(
"This image most likely belongs to {} with a {:.2f} percent confidence."
.format(class_names[np.argmax(score)], 100 * np.max(score))
)