-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier_vs_feature_experiment.py
225 lines (189 loc) · 10.5 KB
/
classifier_vs_feature_experiment.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import argparse
import logging
import numpy as np
from sklearn.metrics import balanced_accuracy_score
from sklearn.pipeline import Pipeline
from cuml.svm import LinearSVC
from src.model_training import SVC
from cuml.preprocessing import StandardScaler
from sklearn.preprocessing import StandardScaler as SKStandardScaler
#from cuml.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestClassifier
from cuml.linear_model import LogisticRegression
from sklearn.utils import compute_class_weight
from src.model_training.mlp_classifier import MLP as MLP
from src.model_training.sequentialnn_classifier import SequentialNN
# This code was mainly used to store the tuned classifiers for each feature type
def get_tuned_classifiers(feature, class_weights, input_dim):
"""
Returns a dictionary of tuned classifiers for a given feature.
Classifiers were tuned by running grid search for each classifier and each feature (with StandardScaler).
"""
if feature == 'pdm' or feature == 'nonrigid_face_shape':
return {
'SVC': SVC(C=1, probability=True, kernel='rbf', class_weight='balanced'),
#'LinearSVC': LinearSVC(C=0.1, probability=True, class_weight='balanced'),
'RandomForest': RandomForestClassifier(n_estimators=400, max_depth=20,
min_samples_split=2, criterion='gini', class_weight='balanced'),
'LogisticRegression': LogisticRegression(C=10, class_weight='balanced'),
'MLP': MLP(hidden_size=256, batch_size=32, class_weight=class_weights, learning_rate=0.01, num_epochs=30,
num_classes=8, input_size=input_dim),
'NN': SequentialNN(batch_size=128, num_epochs=30, class_weight=class_weights, input_dim=input_dim)
}
elif feature == 'facs':
return {
'SVC': SVC(C=1, probability=True, kernel='rbf', class_weight='balanced'),
#'LinearSVC': LinearSVC(C=0.1, probability=True, class_weight='balanced'),
'RandomForest': RandomForestClassifier(n_estimators=400, max_depth=15,
min_samples_split=2, criterion='gini', class_weight='balanced'),
'LogisticRegression': LogisticRegression(C=0.1, class_weight='balanced'),
'MLP': MLP(hidden_size=256, batch_size=32, class_weight=class_weights, learning_rate=0.01, num_epochs=30,
num_classes=8, input_size=input_dim),
'NN': SequentialNN(batch_size=128, num_epochs=10, class_weight=class_weights, input_dim=input_dim)
}
elif feature == 'landmarks_3d':
return {
#'SVC': SVC(C=10, probability=True, kernel='rbf', class_weight='balanced'),
'SVC': SVC(C=1, probability=True, kernel='rbf', class_weight='balanced'),
#'LinearSVC': LinearSVC(C=0.1, probability=True, class_weight='balanced'),
'RandomForest': RandomForestClassifier(n_estimators=200, max_depth=15,
min_samples_split=2, criterion='gini', class_weight='balanced'),
'LogisticRegression': LogisticRegression(C=10, class_weight='balanced'),
'MLP': MLP(hidden_size=128, batch_size=64, class_weight=class_weights, learning_rate=0.01, num_epochs=30,
num_classes=8, input_size=input_dim),
'NN': SequentialNN(batch_size=128, num_epochs=30, class_weight=class_weights, input_dim=input_dim)
}
elif feature == 'embedded':
return {
'SVC': SVC(C=1, probability=True, kernel='rbf', class_weight='balanced'),
#'LinearSVC': LinearSVC(C=0.1, probability=True, class_weight='balanced'),
'RandomForest': RandomForestClassifier(n_estimators=300, max_depth=15,
min_samples_split=2, criterion='gini', class_weight='balanced'),
'LogisticRegression': LogisticRegression(C=1, class_weight='balanced'),
'MLP': MLP(hidden_size=256, batch_size=64, class_weight=class_weights, learning_rate=0.01, num_epochs=30,
num_classes=8, input_size=input_dim),
'NN': SequentialNN(batch_size=128, num_epochs=20, class_weight=class_weights, input_dim=input_dim)
}
elif feature == 'embeddings':
return {
'SVC': SVC(C=0.1, probability=True, kernel='rbf', class_weight='balanced'),
# 'LinearSVC': LinearSVC(C=0.1, probability=True, class_weight='balanced'),
'RandomForest': RandomForestClassifier(n_estimators=300, max_depth=15,
min_samples_split=2, criterion='gini', class_weight='balanced'),
'LogisticRegression': LogisticRegression(C=1, class_weight='balanced'),
'MLP': MLP(hidden_size=256, batch_size=128, class_weight=class_weights, learning_rate=0.01, num_epochs=20,
num_classes=8, input_size=input_dim),
'NN': SequentialNN(batch_size=128, num_epochs=20, class_weight=class_weights, input_dim=input_dim)
}
elif feature == 'hog':
return {
'SVC': SVC(C=1, probability=True, kernel='rbf', class_weight='balanced'),
#'LinearSVC': LinearSVC(C=0.1, probability=True, class_weight='balanced'),
'RandomForest': RandomForestClassifier(n_estimators=300, max_depth=15,
min_samples_split=2, criterion='gini', class_weight='balanced'),
'LogisticRegression': LogisticRegression(C=1, class_weight='balanced'),
'MLP': MLP(hidden_size=256, batch_size=64, class_weight=class_weights, learning_rate=0.01, num_epochs=20,
num_classes=8, input_size=input_dim),
'NN': SequentialNN(batch_size=128, num_epochs=20, class_weight=class_weights, input_dim=input_dim)
}
else:
raise ValueError(f"Feature {feature} not supported.")
feature_paths = {
'hog': {
'train': 'pca_train_hog_features.npy',
'val': 'pca_val_hog_features.npy',
'test': 'pca_test_hog_features.npy'
},
'landmarks_3d': {
'train': 'train_spatial_features.npy',
'val': 'val_spatial_features.npy',
'test': 'test_spatial_features.npy'
},
'pdm': {
'train': 'train_pdm_features.npy',
'val': 'val_pdm_features.npy',
'test': 'test_pdm_features.npy'
},
'facs': {
'train': 'train_facs_features.npy',
'val': 'val_facs_features.npy',
'test': 'test_facs_features.npy'
},
'embedded': {
'train': 'train_embedded_features.npy',
'val': 'val_embedded_features.npy',
'test': 'test_embedded_features.npy'
}
}
# Stacking
def evaluate_stacking(probabilities, y_val):
"""
Perform score fusion with stacking classifier
"""
# Use probabilities as input to the stacking classifier
X_stack = np.concatenate([probabilities[model] for model in probabilities], axis=1)
stacking_pipeline = Pipeline([('log_reg', LogisticRegression(C=1, class_weight='balanced'))])
stacking_pipeline.fit(X_stack, y_val)
stacking_accuracy = stacking_pipeline.score(X_stack, y_val)
logger.info(f"Accuracy of stacking classifier (Validation Set): {stacking_accuracy}")
balanced_accuracy = balanced_accuracy_score(y_val, stacking_pipeline.predict(X_stack))
logger.info(f"Balanced Accuracy of stacking classifier (Validation Set): {balanced_accuracy}")
# Return the stacking pipeline
return stacking_pipeline
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Training same feature on different classifiers or different features on same classifier')
parser.add_argument('--experiment-dir', type=str, help='Directory to checkpoint file',
default='/local/scratch/ptanner/cf_experiments')
parser.add_argument('--feature', type=str, help='Feature to use for training', default='pdm')
args = parser.parse_args()
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
handlers=[
logging.FileHandler(f'{args.experiment_dir}/experiment.log'),
logging.StreamHandler()
])
logger.info("Starting Experiment")
feature = args.feature
probabilities_val = {}
probabilities_test = {}
X_train_path = feature_paths[feature]['train']
X_val_path = feature_paths[feature]['val']
X_test_path = feature_paths[feature]['test']
y_train = np.load('y_train.npy')
y_val = np.load('y_val.npy')
y_test = np.load('y_test.npy')
class_weights = compute_class_weight('balanced', classes=np.unique(y_train), y=y_train)
class_weights = {i: class_weights[i] for i in range(len(class_weights))}
input_shape = np.load(X_test_path).shape[1]
num_classes = len(np.unique(y_train))
pipelines = {}
for name, classifier in get_tuned_classifiers(feature, class_weights, input_shape).items():
if classifier == 'SVC':
pipelines[name] = Pipeline([
('scaler', SKStandardScaler()),
('classifier', classifier)
])
elif classifier is not None:
pipelines[name] = Pipeline([
('scaler', StandardScaler()),
('classifier', classifier)
])
for name, pipeline in pipelines.items():
logger.info(f"Training with {name}")
pipeline.fit(np.load(X_train_path).astype(np.float32), y_train)
probabilities_val[name] = pipeline.predict_proba(np.load(X_val_path).astype(np.float32))
probabilities_test[name] = pipeline.predict_proba(np.load(X_test_path).astype(np.float32))
# Delete the pipeline to save memory
pipelines[name] = None
bal_acc_val = balanced_accuracy_score(y_val, np.argmax(probabilities_val[name], axis=1))
bal_acc_test = balanced_accuracy_score(y_test, np.argmax(probabilities_test[name], axis=1))
logger.info(f"Balanced Accuracy on Validation: {bal_acc_val}")
logger.info(f"Balanced Accuracy on Test: {bal_acc_test}")
stacking_pipeline = evaluate_stacking(probabilities_val, y_val)
# Evaluate the stacking classifier on the test set
X_test_stack = np.concatenate([probabilities_test[model] for model in probabilities_test], axis=1)
test_accuracy = stacking_pipeline.score(X_test_stack, y_test)
logger.info(f"Accuracy of stacking classifier (Test Set): {test_accuracy}")
logger.info("Experiment Finished")