Compare commits

..
5 Commits
Author SHA1 Message Date
Jake Miller 65bb9efa0d Fail more loudly when model file has an error
FileNotFound and invalid models will now throw errors
2019-05-15 11:21:10 -04:00
Dan Petro df3c0b968d Merge branch 'interrupt_evaluator' 2019-04-30 13:56:00 -07:00
Dan Petro 001061f639 Removed unused variable 2019-04-30 13:55:39 -07:00
Dan Petro 18d66608aa Linter exception for matplotlib 2019-04-30 10:41:04 -07:00
Jake Miller 14eece1b52 Evaluator can be stopped early and output results
Callback object contains intermediate results from evaluator function and
will output results if user sends a keyboard interrupt.
2019-04-30 12:41:00 -04:00
2 changed files with 41 additions and 6 deletions

No files matched your search

+40 -5
View File
@@ -1,9 +1,10 @@
import os
import random
import sys
# Prevent Tkinter Dependency
import matplotlib
matplotlib.use('agg')
matplotlib.use('agg') # noqa: E402
import matplotlib.pyplot as plt
import numpy as np
@@ -22,10 +23,23 @@ from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import classification_report, accuracy_score, hamming_loss
from augmentation import EyeballerAugmentation
from keras.callbacks import Callback
DATA_LABELS = ["custom404", "login", "homepage", "oldlooking"]
class PredictionManager(Callback):
def __init__(self):
super(PredictionManager, self).__init__()
self.predictions = []
def getPredictions(self):
return np.array(self.predictions)
def on_predict_batch_end(self, batch, logs=None):
self.predictions.append(logs['outputs'][0].tolist()[0])
class EyeballModel:
"""The primary model class of Eyeballer.
@@ -37,7 +51,7 @@ class EyeballModel:
image_width, image_height = 224, 224
input_shape = (image_width, image_height, 3)
def __init__(self, print_summary=False, weights_file="weights.h5", seed=None):
def __init__(self, weights_file, print_summary=False, seed=None):
"""Constructor for model class.
Keyword arguments:
@@ -82,10 +96,16 @@ class EyeballModel:
self.training_labels = self.training_labels.sample(frac=1)
if weights_file is not None and os.path.isfile(weights_file):
self.model.load_weights(weights_file)
try:
self.model.load_weights(weights_file)
except OSError as e:
print(f"ERROR: Unable to open weights file '{weights_file}'")
sys.exit(-1)
print("Loaded model from file.")
else:
print("No model loaded from file")
if weights_file is not None:
raise FileNotFoundError
print("WARN: No model loaded from file. Generating random model")
# Data augmentation
augmentor = Augmentor.Pipeline()
@@ -251,7 +271,22 @@ class EyeballModel:
else:
print("Using evaluation set...")
predictions = self.model.predict_generator(evaluation_generator, verbose=1, steps=len(evaluation_generator))
prediction_manager = PredictionManager()
try:
self.model.predict_generator(
evaluation_generator,
callbacks=[prediction_manager],
verbose=1,
steps=len(evaluation_generator))
except KeyboardInterrupt:
print('\r\nJob interrupted (Ctrl+C): Outputting completed results')
predictions = prediction_manager.getPredictions()
# Truncates evaluation generator if test was interrupted
evaluation_generator._data = evaluation_generator.data[:predictions.shape[0]]
self._save_prediction_histograms(predictions)
predictions = predictions > threshold
ground_truth = evaluation_generator.data
+1 -1
View File
@@ -1,9 +1,9 @@
Augmentor
click
keras
matplotlib
numpy
pandas
pillow
sklearn
tensorflow
git+https://github.com/keras-team/keras/