-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.py
48 lines (43 loc) · 1.54 KB
/
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
from keras.applications.mobilenetv2 import MobileNetV2
from keras.preprocessing import image
from keras.applications.vgg19 import preprocess_input
from keras.models import Model
from keras.models import load_model
import numpy as np
# other imports
import json
import datetime
import time
import glob
import os
# load the user configs
with open('conf.json') as f:
config = json.load(f)
# config variables
test_path = config["test_path"]
weights = config["weights"]
print ("Loading model...")
model = load_model(weights)
print ("Testing images...")
folders = [name for name in os.listdir(test_path) if os.path.isdir(os.path.join(test_path, name))]
for folder in folders:
#print("- class: {}".format(folder))
success = 0
average_confidence = 0
files = glob.glob(test_path + "/" + folder + "/*.jpg")
for file in files:
img = image.load_img(file, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
y_prob = model.predict(x)
y_class = y_prob.argmax(axis=-1)
y_class = y_class[0]
y_confidence = int(y_prob[0][y_class] * 100)
#print("predicted label: {} (prob = {})".format(y_class, y_confidence))
if y_class == int(folder):
success += 1
average_confidence += y_confidence
success = int(100*success/len(files))
average_confidence = int(average_confidence / len(files))
print("class '{}': success rate = {}% with {}% avg confidence".format(folder, success, average_confidence))