-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvm_model.py
179 lines (142 loc) · 4.15 KB
/
svm_model.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import numpy as np
import os
from utils import data
from sklearn.svm import SVC
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler
classifier = SVC(
kernel='rbf',
probability=True,
gamma='scale',
class_weight='balanced',
# degree=2
)
pipe = Pipeline([
('scaler', MinMaxScaler()),
('classification', classifier)
]
)
feature_names = data.include_features
feature_categories = [
'area_mean',
'area_variance',
'contour_count',
'distance_mean',
'distance_variance',
'har_mean',
'har_variance',
'largest_contour_area',
'largest_contour_circularity',
'largest_contour_convexity',
'largest_contour_eccentricity',
'largest_contour_har',
'largest_contour_saturation_mean',
'largest_contour_saturation_variance',
'largest_contour_value_mean',
'largest_contour_value_variance',
'percent',
'perimeter_mean',
'perimeter_variance',
'region_area',
'region_saturation_mean',
'region_saturation_variance',
'region_value_mean',
'region_value_variance'
]
color_categories = [
'black',
'white',
'gray',
'red',
'green',
'blue',
'violet',
'yellow',
'cyan',
'white_blue'
]
mask_opt = 'masked'
color_opt = 'full'
balance_opt = 'unbalanced'
parent_dir = 'model_data'
opt_str = "_".join([mask_opt, color_opt, balance_opt])
train_dir = "_".join(['train', 'numpy', opt_str])
train_path = os.path.join(parent_dir, train_dir)
test_dir = "_".join(['test', 'numpy', opt_str])
test_path = os.path.join(parent_dir, test_dir)
train_x = np.load(os.path.join(train_path, 'extra.npy'))
train_y = np.load(os.path.join(train_path, 'ohelabels.npy'))
y_ints = [y.argmax() for y in train_y]
val_x = np.load(os.path.join(test_path, 'extra.npy'))
val_y = np.load(os.path.join(test_path, 'ohelabels.npy'))
y_ints_val = [y.argmax() for y in val_y]
pipe.fit(
train_x,
y_ints
)
accuracy = pipe.score(val_x, y_ints_val)
print("Baseline all features:", accuracy)
for i, f in enumerate(feature_names):
i_train_x = train_x[:, i].reshape(train_x.shape[0], 1)
# train the SVM model with the ground truth
pipe.fit(
i_train_x,
y_ints
)
i_val_x = val_x[:, i].reshape(val_x.shape[0], 1)
accuracy = pipe.score(i_val_x, y_ints_val)
print("Single out", f, ":", accuracy)
for i, f in enumerate(feature_names):
i_train_x = np.column_stack((train_x[:, :i], train_x[:, i+1:]))
# train the SVM model with the ground truth
pipe.fit(
i_train_x,
y_ints
)
i_val_x = np.column_stack((val_x[:, :i], val_x[:, i+1:]))
accuracy = pipe.score(i_val_x, y_ints_val)
print("Excluding", f, ":", accuracy)
for cat_name in feature_categories:
feature_indices = []
other_indices = []
for i, f in enumerate(feature_names):
if f.startswith(cat_name):
feature_indices.append(i)
else:
other_indices.append(i)
# train the SVM model with the ground truth
pipe.fit(
train_x[:, feature_indices],
y_ints
)
accuracy = pipe.score(val_x[:, feature_indices], y_ints_val)
print("Only feature cat", cat_name, ":", accuracy)
# train the SVM model with the ground truth
pipe.fit(
train_x[:, other_indices],
y_ints
)
accuracy = pipe.score(val_x[:, other_indices], y_ints_val)
print("Excluding feature cat", cat_name, ":", accuracy)
for color_cat in color_categories:
feature_indices = []
other_indices = []
for i, f in enumerate(feature_names):
if f.endswith(color_cat + ')'):
feature_indices.append(i)
else:
other_indices.append(i)
# train the SVM model with the ground truth
pipe.fit(
train_x[:, feature_indices],
y_ints
)
accuracy = pipe.score(val_x[:, feature_indices], y_ints_val)
print("Only features with color", color_cat, ":", accuracy)
# train the SVM model with the ground truth
pipe.fit(
train_x[:, other_indices],
y_ints
)
accuracy = pipe.score(val_x[:, other_indices], y_ints_val)
print("Excluding features with color", color_cat, ":", accuracy)