File size: 1,803 Bytes
0fa5aba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import pickle
from tensorflow import keras
# from sklearn.metrics import accuracy_score, f1_score, classification_report, confusion_matrix
from RF.grappaBroskiSkibiddiLewandoski import HobaTitoMambo
from Metrics import classification_report2, confusion_matrix2, accuracy_score2

#Download Data
(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()

X_train = X_train / 255.0
X_test = X_test / 255.0


from skimage.feature import hog

def extract_hog_features(X):
    features = []

    for img in X:
        img_2d = img.reshape(28, 28)

        hog_features = hog(
            img_2d,
            orientations=9,
            pixels_per_cell=(4, 4),
            cells_per_block=(2, 2),
            block_norm='L2-Hys',
            feature_vector=True
        )

        features.append(hog_features)

    return np.array(features)


X_train_HOG = extract_hog_features(X_train)
X_test_HOG = extract_hog_features(X_test)

print("Initializing HobaTitoMambo Random Forest...")
rf = HobaTitoMambo(
        n_trees=10, 
        max_depth=15,
        min_samples_split=20,
        min_sample_leafs=5,
        max_features="sqrt",
        criterion="gini", 
        random_state=42
    )

print("Training model (this may take a few minutes)...")
rf.fit(X_train_HOG, y_train)

print("Predicting on test set...")
predictions = rf.predict(X_test_HOG)

conf_matrix = confusion_matrix2(y_test, predictions)

print("\n--- Evaluation Results ---")
print("\nConfusion Matrix:")
print(conf_matrix)
    
print("\nDetailed Classification Report:")
print(classification_report2(y_test, predictions))

model_filename = 'rf_hog_model.pkl'
print(f"\nSaving model to {model_filename}...")
with open(model_filename, 'wb') as file:
    pickle.dump(rf, file)
print("Model saved successfully!")