You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gesture_rec/ml_gestures/evaluate.py

48 lines
1.8 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import pandas as pd
import numpy as np
import joblib
import argparse
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
def evaluate(csv_path, model_path, test_size=0.2, random_state=42):
# Загрузка модели
data = joblib.load(model_path)
model = data['model']
class_names = data['class_names']
print(f"Loaded model with classes: {class_names}")
# Загрузка данных
df = pd.read_csv(csv_path)
X = df.iloc[:, 1:].values.astype(np.float32)
y_labels = df.iloc[:, 0].values
label_to_id = {label: i for i, label in enumerate(class_names)}
y = np.array([label_to_id[label] for label in y_labels])
# Разделение (должно совпадать с параметрами обучения)
from sklearn.model_selection import train_test_split
_, X_test, _, y_test, _, y_labels_test = train_test_split(
X, y, y_labels, test_size=test_size, random_state=random_state, stratify=y
)
print(f"Test size: {len(X_test)}")
y_pred = model.predict(X_test)
acc = accuracy_score(y_test, y_pred)
report = classification_report(y_test, y_pred, target_names=class_names)
cm = confusion_matrix(y_test, y_pred)
print(f"\nAccuracy: {acc:.4f}")
print("\nClassification Report:")
print(report)
print("Confusion Matrix:")
print(cm)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--csv', required=True, help='CSV с данными')
parser.add_argument('--model', required=True, help='Путь к модели')
parser.add_argument('--test_size', type=float, default=0.2)
parser.add_argument('--random_state', type=int, default=42)
args = parser.parse_args()
evaluate(args.csv, args.model, args.test_size, args.random_state)