-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_params.py
66 lines (51 loc) · 2.31 KB
/
find_params.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
import os
import cv2
import numpy as np
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from concurrent.futures import ThreadPoolExecutor
from sklearn.model_selection import GridSearchCV
def process_image(img_path, image_size=(96, 96)):
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
if img is not None:
img = cv2.resize(img, image_size)
return img.flatten()
return None
def load_images_from_folder_parallel(folder_path, image_size=(96, 96), max_images_per_class=100):
features = []
labels = []
class_names = sorted(os.listdir(folder_path))
for label, class_name in enumerate(class_names):
class_folder = os.path.join(folder_path, class_name)
if os.path.isdir(class_folder):
print(f"Učitavanje slika iz klase: {class_name}")
image_paths = [os.path.join(class_folder, fname) for fname in os.listdir(class_folder)]
image_paths = image_paths[:max_images_per_class]
with ThreadPoolExecutor() as executor:
results = list(executor.map(lambda p: process_image(p, image_size), image_paths))
for res in results:
if res is not None:
features.append(res)
labels.append(label)
print(f"Ukupan broj slika: {len(features)}")
return np.array(features), np.array(labels), class_names
train_folder = "./dataset2split/train"
test_folder = "./dataset2split/test"
print("Učitavanje dataseta za treniranje")
features_train, labels_train, class_names = load_images_from_folder_parallel(train_folder)
print("Učitavanje dataseta za testiranje")
features_test, labels_test, _ = load_images_from_folder_parallel(test_folder)
print(f"Broj slika za treniranje: {len(features_train)}, Broj slika za testiranje: {len(features_test)}")
print(f"Klase: {class_names}")
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
param_grid = {
'C': [0.1, 1, 10, 50, 100],
'gamma': [1, 0.1, 0.01, 0.001, 0.0001],
'kernel': ['rbf']
}
grid = GridSearchCV(SVC(), param_grid, refit=True, verbose=3, cv=5)
grid.fit(X_train, y_train)
print("Najbolji parametri: ", grid.best_params_)
print("Najbolji estimator: ", grid.best_estimator_)