penyesuaian
This commit is contained in:
parent
9ece3d9aea
commit
62cce21166
|
|
@ -7,7 +7,7 @@ import joblib
|
||||||
from sklearn.svm import SVC
|
from sklearn.svm import SVC
|
||||||
from sklearn.preprocessing import StandardScaler
|
from sklearn.preprocessing import StandardScaler
|
||||||
from sklearn.pipeline import Pipeline
|
from sklearn.pipeline import Pipeline
|
||||||
from sklearn.model_selection import train_test_split, GridSearchCV
|
from sklearn.model_selection import train_test_split
|
||||||
from skimage.feature import local_binary_pattern
|
from skimage.feature import local_binary_pattern
|
||||||
|
|
||||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
||||||
|
|
@ -17,6 +17,9 @@ from utils.lbp_features import (
|
||||||
augmentasi_lbp, preprocess_face, FACE_SIZE, LBP_SCALES
|
augmentasi_lbp, preprocess_face, FACE_SIZE, LBP_SCALES
|
||||||
)
|
)
|
||||||
|
|
||||||
|
OPTIMAL_C = 20
|
||||||
|
OPTIMAL_GAMMA = 0.1
|
||||||
|
|
||||||
|
|
||||||
def load_images(dataset_path):
|
def load_images(dataset_path):
|
||||||
valid_ext = ('.jpg', '.jpeg', '.png')
|
valid_ext = ('.jpg', '.jpeg', '.png')
|
||||||
|
|
@ -111,26 +114,8 @@ def train_model(base_datasets_path, model_output_path, approved_user_ids=None):
|
||||||
X, y, test_size=0.2, random_state=42, stratify=y
|
X, y, test_size=0.2, random_state=42, stratify=y
|
||||||
)
|
)
|
||||||
|
|
||||||
param_grid = {
|
best_C = OPTIMAL_C
|
||||||
'svm__C': [0.1, 1, 10, 20],
|
best_gamma = OPTIMAL_GAMMA
|
||||||
'svm__gamma': ['scale', 'auto', 0.01, 0.001, 0.1]
|
|
||||||
}
|
|
||||||
|
|
||||||
pipe_search = Pipeline([
|
|
||||||
('scaler', StandardScaler()),
|
|
||||||
('svm', SVC(kernel='rbf', probability=False))
|
|
||||||
])
|
|
||||||
|
|
||||||
grid = GridSearchCV(
|
|
||||||
pipe_search, param_grid,
|
|
||||||
cv=5, scoring='accuracy',
|
|
||||||
refit=False, return_train_score=True, verbose=0
|
|
||||||
)
|
|
||||||
grid.fit(X_train, y_train)
|
|
||||||
|
|
||||||
best_C = grid.best_params_['svm__C']
|
|
||||||
best_gamma = grid.best_params_['svm__gamma']
|
|
||||||
cv_score = grid.best_score_
|
|
||||||
|
|
||||||
model_final = Pipeline([
|
model_final = Pipeline([
|
||||||
('scaler', StandardScaler()),
|
('scaler', StandardScaler()),
|
||||||
|
|
@ -158,8 +143,8 @@ def train_model(base_datasets_path, model_output_path, approved_user_ids=None):
|
||||||
"user_ids": list(label_map.keys()),
|
"user_ids": list(label_map.keys()),
|
||||||
"best_C": best_C,
|
"best_C": best_C,
|
||||||
"best_gamma": str(best_gamma),
|
"best_gamma": str(best_gamma),
|
||||||
"cv_score": round(cv_score, 4),
|
|
||||||
"test_accuracy": round(test_acc, 4),
|
"test_accuracy": round(test_acc, 4),
|
||||||
|
"source": "Ablasi Jalur 2 (GridSearchCV 5-Fold)",
|
||||||
}
|
}
|
||||||
with open(temp_labels_file, 'w') as f:
|
with open(temp_labels_file, 'w') as f:
|
||||||
json.dump(labels_data, f, indent=2)
|
json.dump(labels_data, f, indent=2)
|
||||||
|
|
@ -177,7 +162,6 @@ def train_model(base_datasets_path, model_output_path, approved_user_ids=None):
|
||||||
"feature_dimension": int(X.shape[1]),
|
"feature_dimension": int(X.shape[1]),
|
||||||
"best_C": best_C,
|
"best_C": best_C,
|
||||||
"best_gamma": str(best_gamma),
|
"best_gamma": str(best_gamma),
|
||||||
"cv_score": round(cv_score, 4),
|
|
||||||
"test_accuracy": round(test_acc, 4),
|
"test_accuracy": round(test_acc, 4),
|
||||||
"classes": list(model_final.named_steps['svm'].classes_),
|
"classes": list(model_final.named_steps['svm'].classes_),
|
||||||
"model_path": model_file,
|
"model_path": model_file,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue