-
Notifications
You must be signed in to change notification settings - Fork 0
/
predictor.py
63 lines (42 loc) · 1.95 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# For Python 3.6 we use the base keras
import tensorflow as tf
import numpy as np
# local
from config import MODEL, EXPECTED_LABEL, NUM_CLASSES
class Predictor:
@staticmethod
def predict(img, model):
explabel = (np.expand_dims(EXPECTED_LABEL, 0))
# Convert class vectors to binary class matrices
explabel = tf.keras.utils.to_categorical(explabel, NUM_CLASSES)
explabel = np.argmax(explabel.squeeze())
#Predictions vector
predictions = 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
def predict_batch(imgs, model):
explabel = (np.expand_dims(EXPECTED_LABEL, 0))
# Convert class vectors to binary class matrices
explabel = tf.keras.utils.to_categorical(explabel, NUM_CLASSES)
explabel = np.argmax(explabel.squeeze())
# Predictions vector
batch_preds = model.predict(imgs)
pred_conf = []
for predictions in batch_preds:
prediction1, prediction2 = np.argsort(-predictions)[:2]
# Activation level corresponding to the expected class
confidence_expclass = predictions[explabel]
if prediction1 != EXPECTED_LABEL:
confidence_notclass = predictions[prediction1]
else:
confidence_notclass = predictions[prediction2]
confidence = confidence_expclass - confidence_notclass
pred_conf.append(( prediction1, confidence))
return pred_conf