-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutant_predictor.py
40 lines (31 loc) · 1.24 KB
/
mutant_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
from tensorflow import keras
import numpy as np
import glob
from properties import MUT_MODELS, num_classes
#TODO: remove duplicate predictor
class Predictor:
# Load the pre-trained model.
PREDICTORS = [keras.models.load_model(p) for p in glob.glob(MUT_MODELS + '/*.h5')]
# model = keras.models.load_model(MODEL)
print("Loaded model from disk")
@staticmethod
def predict(predictor_index, img, label):
model = Predictor.PREDICTORS[predictor_index]
# Predictions vector
predictions = model.predict(img)
predictions1 = list()
confidences = list()
for i in range(len(predictions)):
preds = predictions[i]
explabel = label[i]
prediction1, prediction2 = np.argsort(-preds)[:2]
# Activation level corresponding to the expected class
confidence_expclass = preds[explabel]
if prediction1 != explabel:
confidence_notclass = preds[prediction1]
else:
confidence_notclass = preds[prediction2]
confidence = confidence_expclass - confidence_notclass
predictions1.append(prediction1)
confidences.append(confidence)
return predictions1, confidences