-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
80 lines (68 loc) · 2.39 KB
/
test.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
67
68
69
70
71
72
73
74
75
76
77
78
from utils import classification, feature_extraction, feature_preprocessing, dimensionality_reduction, regression
import os
import glob
from sklearn.model_selection import StratifiedKFold
import numpy as np
dir_path = os.path.dirname(os.path.realpath(__file__))
dataset = 'matsc_dataset1'
l = os.listdir(dir_path + '/' + dataset)
# Make datasets
haralick = []
VGG = []
inception = []
Y = []
cnt = 0
for i in range(len(l)):
l1 = l[i]
if l1[0] == '.':
continue
names = glob.glob(dir_path + '/' + dataset + '/' + l1 + '/*.jpg')
if cnt == 0:
haralick = feature_extraction.haralick_features(names)
VGG = feature_extraction.VGG(names)
inception = feature_extraction.inception(names)
else:
haralick = np.vstack((haralick, feature_extraction.haralick_features(names)))
VGG = np.vstack((VGG, feature_extraction.VGG(names)))
inception = np.vstack((inception, feature_extraction.inception(names)))
Y += [cnt] * len(names)
cnt += 1
Y = np.asarray(Y)
# Divide into training and testing data
skf = StratifiedKFold(n_splits=5)
a = []
f = []
p = []
r = []
rr = []
m = []
for train_index, test_index in skf.split(np.zeros((len(Y), 1)), Y):
train_labels = Y[train_index]
test_labels = Y[test_index]
train = haralick[train_index] # Do the same for VGG / inception
test = haralick[test_index]
# Standardization
train, test, _ = feature_preprocessing.preprocessing(train, test)
# PCA
train, test = dimensionality_reduction.principal_components(train, test)
# Classification
acc, f1, prec, rec, conf, probs, pred = classification.random_forests(train, test, train_labels, test_labels) # Try others the same way
a.append(acc)
f.append(f1)
p.append(prec)
r.append(rec)
# Regression
reg_train_labels = np.random.rand(len(train_labels), 1) # random outputs for regression, enter own outputs
reg_test_labels = np.random.rand(len(test_labels), 1) # random outputs for regression, enter own outputs
r2, mse = regression.linear_regression(train, test, reg_train_labels, reg_test_labels)
rr.append(r2)
m.append(mse)
# Output classification metrics
print("Accuracy: " + str(np.mean(a)) + '\n')
print("F1 - score: " + str(np.mean(f)) + '\n')
print("Precision: " + str(np.mean(p)) + '\n')
print("Recall: " + str(np.mean(r)) + '\n')
# Save confusion matrix, probabilities and predictions in csv files
# Output regression metrics
print("R2-score " + str(np.mean(rr)) + '\n')
print("Mean square error " + str(np.mean(m)) + '\n')