python : Updated code with better display, documentation and format_time

This commit is contained in:
saundersp
2024-04-28 00:25:13 +02:00
parent c7d21e1014
commit 718724b63b
11 changed files with 591 additions and 566 deletions

View File

@ -21,10 +21,10 @@ def build_features(width: int, height: int) -> np.ndarray:
"""Initialize the features base on the input shape.
Args:
shape (Tuple[int, int]): Shape of the image (Width, Height).
shape (Tuple[int, int]): Shape of the image (Width, Height)
Returns:
np.ndarray: The initialized features.
np.ndarray: The initialized features
"""
feats = []
empty = (0, 0, 0, 0)
@ -63,10 +63,10 @@ def init_weights(y_train: np.ndarray) -> np.ndarray:
"""Initialize the weights of the weak classifiers based on the training labels.
Args:
y_train (np.ndarray): Training labels.
y_train (np.ndarray): Training labels
Returns:
np.ndarray: The initialized weights.
np.ndarray: The initialized weights
"""
weights = np.empty_like(y_train, dtype = np.float64)
t = y_train.sum()
@ -79,12 +79,12 @@ def classify_weak_clf(x_feat_i: np.ndarray, threshold: int, polarity: int) -> np
"""Classify the integrated features based on polarity and threshold.
Args:
x_feat_i (np.ndarray): Integrated features.
threshold (int): Trained threshold.
polarity (int): Trained polarity.
x_feat_i (np.ndarray): Integrated features
threshold (int): Trained threshold
polarity (int): Trained polarity
Returns:
np.ndarray: Classified features.
np.ndarray: Classified features
"""
res = np.zeros_like(x_feat_i, dtype = np.int8)
res[polarity * x_feat_i < polarity * threshold] = 1
@ -95,10 +95,10 @@ def select_best(classifiers: np.ndarray, weights: np.ndarray, X_feat: np.ndarray
"""Select the best classifier given theirs predictions.
Args:
classifiers (np.ndarray): The weak classifiers.
weights (np.ndarray): Trained weights of each classifiers.
X_feat (np.ndarray): Integrated features.
y (np.ndarray): Features labels.
classifiers (np.ndarray): The weak classifiers
weights (np.ndarray): Trained weights of each classifiers
X_feat (np.ndarray): Integrated features
y (np.ndarray): Features labels
Returns:
Tuple[int, float, np.ndarray]: Index of the best classifier, the best error and the best accuracy
@ -116,13 +116,13 @@ def train_viola_jones(T: int, X_feat: np.ndarray, X_feat_argsort: np.ndarray, y:
"""Train the weak classifiers.
Args:
T (int): Number of weak classifiers.
X_feat (np.ndarray): Integrated features.
X_feat_argsort (np.ndarray): Sorted indexes of the integrated features.
y (np.ndarray): Features labels.
T (int): Number of weak classifiers
X_feat (np.ndarray): Integrated features
X_feat_argsort (np.ndarray): Sorted indexes of the integrated features
y (np.ndarray): Features labels
Returns:
Tuple[np.ndarray, np.ndarray]: List of trained alphas and the list of the final classifiers.
Tuple[np.ndarray, np.ndarray]: List of trained alphas and the list of the final classifiers
"""
weights = init_weights(y)
alphas, final_classifier = np.empty(T, dtype = np.float64), np.empty((T, 3), dtype = np.int32)
@ -144,12 +144,12 @@ def classify_viola_jones(alphas: np.ndarray, classifiers: np.ndarray, X_feat: np
"""Classify the trained classifiers on the given features.
Args:
alphas (np.ndarray): Trained alphas.
classifiers (np.ndarray): Trained classifiers.
X_feat (np.ndarray): Integrated features.
alphas (np.ndarray): Trained alphas
classifiers (np.ndarray): Trained classifiers
X_feat (np.ndarray): Integrated features
Returns:
np.ndarray: Classification results.
np.ndarray: Classification results
"""
total = np.zeros(X_feat.shape[1], dtype = np.float64)
@ -161,22 +161,22 @@ def classify_viola_jones(alphas: np.ndarray, classifiers: np.ndarray, X_feat: np
y_pred[total >= 0.5 * np.sum(alphas)] = 1
return y_pred
@njit
def get_best_anova_features(X: np.ndarray, y: np.ndarray) -> np.ndarray:
#SelectPercentile(f_classif, percentile = 10).fit(X, y).get_support(indices = True)
classes = [X.T[y == 0].astype(np.float64), X.T[y == 1].astype(np.float64)]
n_samples_per_class = np.asarray([classes[0].shape[0], classes[1].shape[0]])
n_samples = classes[0].shape[0] + classes[1].shape[0]
ss_alldata = (classes[0] ** 2).sum(axis = 0) + (classes[1] ** 2).sum(axis = 0)
sums_classes = [np.asarray(classes[0].sum(axis = 0)), np.asarray(classes[1].sum(axis = 0))]
sq_of_sums_alldata = (sums_classes[0] + sums_classes[1]) ** 2
sq_of_sums_args = [sums_classes[0] ** 2, sums_classes[1] ** 2]
ss_tot = ss_alldata - sq_of_sums_alldata / n_samples
sqd_sum_bw_n = sq_of_sums_args[0] / n_samples_per_class[0] + \
sq_of_sums_args[1] / n_samples_per_class[1] - sq_of_sums_alldata / n_samples
ss_wn = ss_tot - sqd_sum_bw_n
df_wn = n_samples - 2
msw = ss_wn / df_wn
f_values = sqd_sum_bw_n / msw
return np.sort(np.argsort(f_values)[::-1][: int(np.ceil(X.shape[0] / 10.0))])
#@njit
#def get_best_anova_features(X: np.ndarray, y: np.ndarray) -> np.ndarray:
# #SelectPercentile(f_classif, percentile = 10).fit(X, y).get_support(indices = True)
# classes = [X.T[y == 0].astype(np.float64), X.T[y == 1].astype(np.float64)]
# n_samples_per_class = np.asarray([classes[0].shape[0], classes[1].shape[0]])
# n_samples = classes[0].shape[0] + classes[1].shape[0]
# ss_all_data = (classes[0] ** 2).sum(axis = 0) + (classes[1] ** 2).sum(axis = 0)
# sums_classes = [np.asarray(classes[0].sum(axis = 0)), np.asarray(classes[1].sum(axis = 0))]
# sq_of_sums_all_data = (sums_classes[0] + sums_classes[1]) ** 2
# sq_of_sums_args = [sums_classes[0] ** 2, sums_classes[1] ** 2]
# ss_tot = ss_all_data - sq_of_sums_all_data / n_samples
#
# sqd_sum_bw_n = sq_of_sums_args[0] / n_samples_per_class[0] + \
# sq_of_sums_args[1] / n_samples_per_class[1] - sq_of_sums_all_data / n_samples
# ss_wn = ss_tot - sqd_sum_bw_n
# df_wn = n_samples - 2
# msw = ss_wn / df_wn
# f_values = sqd_sum_bw_n / msw
# return np.sort(np.argsort(f_values)[::-1][: int(np.ceil(X.shape[0] / 10.0))])