-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredictor.py
39 lines (26 loc) · 1.13 KB
/
predictor.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
# For Python 3.6 we use the base keras
# import keras
from tensorflow import keras
import numpy as np
from properties import MODEL, EXPECTED_LABEL, num_classes
class Predictor:
# Load the pre-trained model.
model = keras.models.load_model(MODEL)
print("Loaded model from disk")
@staticmethod
def predict(img):
explabel = (np.expand_dims(EXPECTED_LABEL, 0))
# Convert class vectors to binary class matrices
explabel = keras.utils.to_categorical(explabel, num_classes)
explabel = np.argmax(explabel.squeeze())
#Predictions vector
predictions = Predictor.model.predict(img)
prediction1, prediction2 = np.argsort(-predictions[0])[:2]
# Activation level corresponding to the expected class
confidence_expclass = predictions[0][explabel]
if prediction1 != EXPECTED_LABEL:
confidence_notclass = predictions[0][prediction1]
else:
confidence_notclass = predictions[0][prediction2]
confidence = confidence_expclass - confidence_notclass
return prediction1, confidence