-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVM_classification.py
219 lines (185 loc) · 6.48 KB
/
SVM_classification.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
########## 2023 Fall PAML Project ##############
######### LinearSVM + KernelSVM ################
######### Young ############################
import numpy as np
from sklearn.pipeline import make_pipeline
from keras.datasets import cifar10
from sklearn.svm import LinearSVC
from sklearn.preprocessing import StandardScaler
import cv2
from skimage.feature import hog
from tqdm import tqdm
from sklearn.decomposition import PCA
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
import time
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from keras.models import Model
from keras.layers import Input, Dense
import argparse
parser = argparse.ArgumentParser(
description="choose HOG + PCA or Autoencoder to extract features"
)
parser.add_argument(
"--processing", type=str, default="hog", help="choose HOG+PCA or not"
)
parser.add_argument(
"--SVM", type=str, default="linear", help="choose linear SVM or kernel SVM"
)
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print(f"Training data shape: {X_train.shape}")
print(f"Training labels shape: {y_train.shape}")
print(f"Test data shape: {X_test.shape}")
print(f"Test labels shape: {y_test.shape}")
###正则系数提供不同的取值
acc_train_svm_linear, acc_test_svm_linear, c1 = (
[],
[],
[0.0001, 0.001, 0.01, 0.1, 1, 10],
)
## 提取HOG特征
def hog_process(imgs):
hog_imgs = []
for x in tqdm(imgs):
gray = cv2.cvtColor(x, cv2.COLOR_RGB2GRAY) / 255
fd = hog(gray, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2))
hog_imgs.append(fd)
return np.array(hog_imgs)
### 定义一个自编码器
def AutoEncoder(traingset, testset):
input_img = Input(shape=(3072,))
encoded = Dense(256, activation="relu")(input_img)
decoded = Dense(3072, activation="relu")(encoded)
autoencoder = Model(input_img, decoded)
encoder = Model(input_img, encoded)
autoencoder.compile(optimizer="adam", loss="binary_crossentropy")
# 训练自编码器
autoencoder.fit(
traingset,
traingset,
epochs=5,
batch_size=256,
shuffle=False,
validation_data=(testset, testset),
)
# 使用自编码器对数据进行降维
x_train_encoded = encoder.predict(traingset)
x_test_encoded = encoder.predict(testset)
return x_train_encoded, x_test_encoded
##在不同的正则系数C条件下,进行SVM分类
def testC(tc, Trainset, Trainlabel, Testset, Testlabel, c1, chooseSVM, processing):
for c in c1:
start = time.perf_counter()
svm_classifier(
Trainset, Trainlabel, Testset, Testlabel, c, chooseSVM, processing
)
end = time.perf_counter()
timecost = end - start
tc.append(timecost)
avgtc = np.mean(tc)
print(f"average time cost is {avgtc}")
### SVM 分类器
def svm_classifier(Trainset, Trainlabel, Testset, Testlabel, c, chooseSVM, processing):
clf = make_pipeline(StandardScaler())
if chooseSVM == "linear" and processing == "hog":
clf = make_pipeline(
StandardScaler(),
PCA(0.8),
LinearSVC(dual="auto", random_state=0, tol=1e-5, C=c, max_iter=1500),
)
if chooseSVM == "kernel" and processing == "hog":
clf = make_pipeline(
StandardScaler(),
PCA(0.8),
SVC(C=c, max_iter=1500, kernel="rbf"),
)
if chooseSVM == "kernel" and processing == "autoencoder":
clf = SVC(C=c, max_iter=1500, kernel="rbf")
if chooseSVM == "linear" and processing == "autoencoder":
clf = LinearSVC(
dual="auto", random_state=0, tol=1e-5, C=c, max_iter=1500
) ##使用SVC会比较慢,故这里用linearSVC,而不是SVC(kernel='linear')
# 训练集
clf.fit(Trainset, Trainlabel)
y_pred_svm_train = clf.predict(Trainset)
acc_train = accuracy_score(Trainlabel, y_pred_svm_train)
acc_train_svm_linear.append(acc_train)
print("Train Accuracy = {0:f}".format(acc_train))
# 验证集
y_pred_svm_test = clf.predict(Testset)
acc_test = accuracy_score(Testlabel, y_pred_svm_test)
acc_test_svm_linear.append(acc_test)
print("Test Accuracy = {0:f}".format(acc_test))
## 仅当 c == 10时,画出confusion matrix
if c == 10:
cm = confusion_matrix(Testlabel, y_pred_svm_test)
disp = ConfusionMatrixDisplay(
cm,
display_labels=[
"airplane",
"car",
"bird",
"cat",
"deer",
"dog",
"frog",
"horse",
"ship",
"truck",
],
)
disp.plot()
plt.xticks(rotation=45, ha="right")
# plt.show()
if __name__ == "__main__":
args = parser.parse_args()
yTrain = np.squeeze(y_train)
yTest = np.squeeze(y_test)
tc = []
oneofSVM = args.SVM
if args.processing == "hog":
## 提取图片HOG特征
xTrain = hog_process(X_train)
xTest = hog_process(X_test)
xTrain = xTrain.astype(np.float32)
xTest = xTest.astype(np.float32)
xTrain_encoder = np.reshape(xTrain, (xTrain.shape[0], -1))
XTest_encoder = np.reshape(xTest, (xTest.shape[0], -1))
testC(
tc,
xTrain_encoder,
yTrain,
XTest_encoder,
yTest,
c1,
args.SVM,
args.processing,
)
if args.processing == "autoencoder":
xTrain = X_train.astype(np.float32)
xTest = X_test.astype(np.float32)
scaler = StandardScaler()
xTrain = scaler.fit_transform(np.reshape(xTrain, (xTrain.shape[0], -1)))
xTest = scaler.transform(np.reshape(xTest, (xTest.shape[0], -1)))
xTrain_encoder, xTest_encoder = AutoEncoder(xTrain, xTest)
testC(
tc,
xTrain_encoder,
yTrain,
xTest_encoder,
yTest,
c1,
args.SVM,
args.processing,
)
### 画出 训练误差和验证误差随正则系数C的变化关系
plt.figure()
plt.plot(c1, acc_train_svm_linear, ".-", color="red", label="Train Accuracy")
plt.plot(c1, acc_test_svm_linear, ".-", color="orange", label="Test Accuracy")
plt.xlabel("c")
plt.ylabel("Accuracy")
plt.title("Plot of accuracy vs c for training and test data")
plt.grid()
plt.legend(["Training Accuracy", "Test Accuracy"])
plt.show()