penyesuaian

This commit is contained in:
jouel88 2026-05-03 08:26:12 +07:00
parent 9ece3d9aea
commit 62cce21166
1 changed files with 7 additions and 23 deletions

View File

@ -7,7 +7,7 @@ import joblib
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
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
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
)
OPTIMAL_C = 20
OPTIMAL_GAMMA = 0.1
def load_images(dataset_path):
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
)
param_grid = {
'svm__C': [0.1, 1, 10, 20],
'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_
best_C = OPTIMAL_C
best_gamma = OPTIMAL_GAMMA
model_final = Pipeline([
('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()),
"best_C": best_C,
"best_gamma": str(best_gamma),
"cv_score": round(cv_score, 4),
"test_accuracy": round(test_acc, 4),
"source": "Ablasi Jalur 2 (GridSearchCV 5-Fold)",
}
with open(temp_labels_file, 'w') as f:
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]),
"best_C": best_C,
"best_gamma": str(best_gamma),
"cv_score": round(cv_score, 4),
"test_accuracy": round(test_acc, 4),
"classes": list(model_final.named_steps['svm'].classes_),
"model_path": model_file,