Skip to content

Instantly share code, notes, and snippets.

@albertogiunta
Created October 22, 2018 09:00
Show Gist options
  • Save albertogiunta/3391581ca0beb2f1be0740b1879daefd to your computer and use it in GitHub Desktop.
Save albertogiunta/3391581ca0beb2f1be0740b1879daefd to your computer and use it in GitHub Desktop.
def get_bpcer(self, feature_vectors, classes):
# apcer - attack presentation classification error rate: proportion of morphed presentations incorrectly classified as bona fide
# bpcer - bonafide presentation classification error rate: proportion of bona fide presentations incorrectly classified as morphed face attacks
apcer = [0.001, 0.003, 0.005, 0.01, 0.02, 0.03, 0.05, 0.1]
# apcer = [0.05, 0.1]
bpcer = []
X_test = feature_vectors
y_test = classes
self._load_classifier()
# prediction probability for every test element for class 1
y_score = self.clf.predict_proba(X_test)[:, 1]
# equivalent to:
# decision function scores for every test element
# y_score = self.clf.decision_function(X_test)
# precision and recall values at different threshold values
precisions, _, thresholds = precision_recall_curve(y_test, y_score)
# total number of morphed images in test group
num_morphed = len([el for el in y_test if el == -1])
plt.plot(precisions)
plt.show()
plt.plot(thresholds)
plt.show()
for apcer_rate in apcer:
# number of accepted false positives at this apcer_rate
num_false_positives = int(num_morphed * apcer_rate)
# precision wanted at this level of apcer_rate.
# precision formula was used: tp / (tp + fp)
# where tp = num_morphed, fp = num_false_positives
current_precision = num_morphed / (num_morphed + num_false_positives)
# the precision closest to the one we've found in the precision array
# needed in order to find the corresponding threshold
approx_precision = self.find_nearest(precisions, current_precision)
# minimum threshold corresponding to the wanted morphed fp precision
min_threshold = thresholds[precisions.tolist().index(approx_precision)]
# array of predictions adjusted on the newly found minimum threshold
y_pred_adjusted = [1 if y >= min_threshold else -1 for y in y_scores]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment