-
Notifications
You must be signed in to change notification settings - Fork 7
/
svm_SGD_per100img.py
457 lines (407 loc) · 16.9 KB
/
svm_SGD_per100img.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import os
import joblib
import datetime
import numpy as np
from sklearn.linear_model import SGDClassifier
from GetData.get_color import get_color
from GetData.get_SURF import get_SURF
from GetData.get_ELA import get_ELA
inputpath_train = 'G:/SVM/Z_Train'
inputpath_test = 'G:/SVM/Z_Test'
savepath = 'D:/1puyao/engineer/0github/graduation-design/SVM/SVM-SGD/Model/'
# savepath = 'G:/SVM/Model/'
train_true = inputpath_train+'/True'
train_fake = inputpath_train+'/Fake'
test_true = inputpath_test+'/True'
test_fake = inputpath_test+'/Fake'
train_true_lists = os.listdir(train_true)
train_fake_lists = os.listdir(train_fake)
test_true_lists = os.listdir(test_true)
test_fake_lists = os.listdir(test_fake)
color_true_data_train = []
color_fake_data_train = []
SURF_true_data_train = []
SURF_fake_data_train = []
ELA_true_data_train = []
ELA_fake_data_train = []
color_true_data_test = []
color_fake_data_test = []
SURF_true_data_test = []
SURF_fake_data_test = []
ELA_true_data_test = []
ELA_fake_data_test = []
X = []
Y = []
accuracy_set = []
def train_color():
print('color特征SVM分类器训练开始 ……')
color_clf = SGDClassifier()
excel_num = 0
for train_true_list in train_true_lists:
if train_true_list.endswith('color.xlsx'):
inputpath = train_true + '/' + train_true_list
sheets, sheets_1dim = get_color(inputpath)
if excel_num % 10 == 0:
color_true_data_train = sheets_1dim
Y = [1]*len(sheets_1dim)
else:
for k in sheets_1dim:
color_true_data_train.append(k)
Y.append(1)
excel_num += 1
if excel_num % 10 == 0:
X = color_true_data_train
color_clf.partial_fit(X, Y, classes=np.array([0, 1]))
joblib.dump(color_clf, savepath + 'color_clf.pkl')
X = []
Y = []
excel_num = 0
for train_fake_list in train_fake_lists:
if train_fake_list.endswith('color.xlsx'):
inputpath = train_fake + '/' + train_fake_list
sheets, sheets_1dim = get_color(inputpath)
if excel_num % 10 == 0:
color_fake_data_train = sheets_1dim
Y = [0] * len(sheets_1dim)
else:
for k in sheets_1dim:
color_fake_data_train.append(k)
Y.append(0)
excel_num += 1
if excel_num % 10 == 0:
X = color_fake_data_train
color_clf.partial_fit(X, Y, classes=np.array([0, 1]))
joblib.dump(color_clf, savepath + 'color_clf.pkl')
X = []
Y = []
# joblib.dump(color_clf, savepath + 'color_clf.pkl')
excel_num = 0
X = []
Y = []
def test_color():
print('color特征SVM分类器测试开始 ……')
color_clf2 = joblib.load(savepath + 'color_clf.pkl')
excel_num = 0
global accuracy_set
for test_true_list in test_true_lists:
if test_true_list.endswith('color.xlsx'):
inputpath = test_true + '/' + test_true_list
sheets, sheets_1dim = get_color(inputpath)
if excel_num % 10 == 0:
color_true_data_test = sheets_1dim
Y = [1]*len(sheets_1dim)
else:
for k in sheets_1dim:
color_true_data_test.append(k)
Y.append(1)
excel_num += 1
if excel_num % 10 == 0:
X = color_true_data_test
Z = color_clf2.predict(X)
accuracy = color_clf2.score(X, Y)
accuracy_set.append(accuracy)
X = []
Y = []
Z = []
excel_num = 0
for test_fake_list in test_fake_lists:
if test_fake_list.endswith('color.xlsx'):
inputpath = test_fake + '/' + test_fake_list
sheets, sheets_1dim = get_color(inputpath)
if excel_num % 10 == 0:
color_fake_data_test = sheets_1dim
Y = [0] * len(sheets_1dim)
else:
for k in sheets_1dim:
color_fake_data_test.append(k)
Y.append(0)
excel_num += 1
if excel_num % 10 == 0:
X = color_fake_data_test
Z = color_clf2.predict(X)
accuracy = color_clf2.score(X, Y)
accuracy_set.append(accuracy)
X = []
Y = []
Z = []
print('color_clf average accuracy: {}'.format(
sum(accuracy_set)/len(accuracy_set)))
excel_num = 0
X = []
Y = []
Z = []
accuracy_set = []
def train_SURF():
print('SURF特征SVM分类器训练开始 ……')
SURF_clf = SGDClassifier()
excel_num = 0
for train_true_list in train_true_lists:
if train_true_list.endswith('SURF.xlsx'):
inputpath = train_true + '/' + train_true_list
sheets, sheets_1dim = get_SURF(inputpath)
if excel_num % 10 == 0:
SURF_true_data_train = sheets_1dim
Y = [1]*len(sheets_1dim)
else:
for k in sheets_1dim:
SURF_true_data_train.append(k)
Y.append(1)
excel_num += 1
if excel_num % 10 == 0:
X = SURF_true_data_train
SURF_clf.partial_fit(X, Y, classes=np.array([0, 1]))
joblib.dump(SURF_clf, savepath + 'SURF_clf.pkl')
X = []
Y = []
excel_num = 0
for train_fake_list in train_fake_lists:
if train_fake_list.endswith('SURF.xlsx'):
inputpath = train_fake + '/' + train_fake_list
sheets, sheets_1dim = get_SURF(inputpath)
if excel_num % 10 == 0:
SURF_fake_data_train = sheets_1dim
Y = [0] * len(sheets_1dim)
else:
for k in sheets_1dim:
SURF_fake_data_train.append(k)
Y.append(0)
excel_num += 1
if excel_num % 10 == 0:
X = SURF_fake_data_train
SURF_clf.partial_fit(X, Y, classes=np.array([0, 1]))
joblib.dump(SURF_clf, savepath + 'SURF_clf.pkl')
X = []
Y = []
# joblib.dump(SURF_clf, savepath + 'SURF_clf.pkl')
excel_num = 0
X = []
Y = []
def test_SURF():
print('SURF特征SVM分类器测试开始 ……')
SURF_clf2 = joblib.load(savepath + 'SURF_clf.pkl')
excel_num = 0
global accuracy_set
for test_true_list in test_true_lists:
if test_true_list.endswith('SURF.xlsx'):
inputpath = test_true + '/' + test_true_list
sheets, sheets_1dim = get_SURF(inputpath)
if excel_num % 10 == 0:
SURF_true_data_test = sheets_1dim
Y = [1]*len(sheets_1dim)
else:
for k in sheets_1dim:
SURF_true_data_test.append(k)
Y.append(1)
excel_num += 1
if excel_num % 10 == 0:
X = SURF_true_data_test
Z = SURF_clf2.predict(X)
accuracy = SURF_clf2.score(X, Y)
accuracy_set.append(accuracy)
X = []
Y = []
Z = []
excel_num = 0
for test_fake_list in test_fake_lists:
if test_fake_list.endswith('SURF.xlsx'):
inputpath = test_fake + '/' + test_fake_list
sheets, sheets_1dim = get_SURF(inputpath)
if excel_num % 10 == 0:
SURF_fake_data_test = sheets_1dim
Y = [0] * len(sheets_1dim)
else:
for k in sheets_1dim:
SURF_fake_data_test.append(k)
Y.append(0)
excel_num += 1
if excel_num % 10 == 0:
X = SURF_fake_data_test
Z = SURF_clf2.predict(X)
accuracy = SURF_clf2.score(X, Y)
accuracy_set.append(accuracy)
X = []
Y = []
Z = []
print('SURF_clf average accuracy: {}'.format(
sum(accuracy_set)/len(accuracy_set)))
excel_num = 0
X = []
Y = []
Z = []
accuracy_set = []
def train_ELA():
print('ELA特征SVM分类器训练开始 ……')
ELA_clf = SGDClassifier()
excel_num = 0
for train_true_list in train_true_lists:
if train_true_list.endswith('ELA.xlsx'):
inputpath = train_true + '/' + train_true_list
sheets, sheets_1dim = get_ELA(inputpath)
if excel_num % 10 == 0:
ELA_true_data_train = sheets_1dim
Y = [1]*len(sheets_1dim)
else:
for k in sheets_1dim:
ELA_true_data_train.append(k)
Y.append(1)
excel_num += 1
if excel_num % 10 == 0:
X = ELA_true_data_train
ELA_clf.partial_fit(X, Y, classes=np.array([0, 1]))
joblib.dump(ELA_clf, savepath + 'ELA_clf.pkl')
X = []
Y = []
excel_num = 0
for train_fake_list in train_fake_lists:
if train_fake_list.endswith('ELA.xlsx'):
inputpath = train_fake + '/' + train_fake_list
sheets, sheets_1dim = get_ELA(inputpath)
if excel_num % 10 == 0:
ELA_fake_data_train = sheets_1dim
Y = [0] * len(sheets_1dim)
else:
for k in sheets_1dim:
ELA_fake_data_train.append(k)
Y.append(0)
excel_num += 1
if excel_num % 10 == 0:
X = ELA_fake_data_train
ELA_clf.partial_fit(X, Y, classes=np.array([0, 1]))
joblib.dump(ELA_clf, savepath + 'ELA_clf.pkl')
X = []
Y = []
# joblib.dump(ELA_clf, savepath + 'ELA_clf.pkl')
excel_num = 0
X = []
Y = []
def test_ELA():
print('ELA特征SVM分类器测试开始 ……')
ELA_clf2 = joblib.load(savepath + 'ELA_clf.pkl')
excel_num = 0
global accuracy_set
for test_true_list in test_true_lists:
if test_true_list.endswith('ELA.xlsx'):
inputpath = test_true + '/' + test_true_list
sheets, sheets_1dim = get_ELA(inputpath)
if excel_num % 10 == 0:
ELA_true_data_test = sheets_1dim
Y = [1]*len(sheets_1dim)
else:
for k in sheets_1dim:
ELA_true_data_test.append(k)
Y.append(1)
excel_num += 1
if excel_num % 10 == 0:
X = ELA_true_data_test
Z = ELA_clf2.predict(X)
accuracy = ELA_clf2.score(X, Y)
accuracy_set.append(accuracy)
X = []
Y = []
Z = []
excel_num = 0
for test_fake_list in test_fake_lists:
if test_fake_list.endswith('ELA.xlsx'):
inputpath = test_fake + '/' + test_fake_list
sheets, sheets_1dim = get_ELA(inputpath)
if excel_num % 10 == 0:
ELA_fake_data_test = sheets_1dim
Y = [0] * len(sheets_1dim)
else:
for k in sheets_1dim:
ELA_fake_data_test.append(k)
Y.append(0)
excel_num += 1
if excel_num % 10 == 0:
X = ELA_fake_data_test
Z = ELA_clf2.predict(X)
accuracy = ELA_clf2.score(X, Y)
accuracy_set.append(accuracy)
X = []
Y = []
Z = []
print('ELA_clf average accuracy: {}'.format(
sum(accuracy_set)/len(accuracy_set)))
excel_num = 0
X = []
Y = []
Z = []
accuracy_set = []
def print_runtime(function, string):
start = datetime.datetime.now()
print('Start Time of {} : {}'.format(string, start))
function()
end = datetime.datetime.now()
print('End Time of {} : {}'.format(string, end))
print('Running Time of {} : {}\n'.format(string, (end - start)))
if __name__ == '__main__':
print_runtime(train_color, '训练color特征SVM分类器')
print_runtime(train_SURF, '训练SURF特征SVM分类器')
print_runtime(train_ELA, '训练ELA特征SVM分类器')
print_runtime(test_color, '测试color特征SVM分类器')
print_runtime(test_SURF, '测试SURF特征SVM分类器')
print_runtime(test_ELA, '测试ELA特征SVM分类器')
# celeba:3k+1k
# pggan:1.5k+1.5k+1k
# attack:1323+660
# original:1359+675
# ——————————————————————————————————————————————————————————————————————————————————————
# 第一次训练测试,数据是全部数据前一部分训练后一部分测试
# Start Time of 训练color特征SVM分类器 : 2020-05-09 12:13:54.245692
# color特征SVM分类器训练开始 ……
# End Time of 训练color特征SVM分类器 : 2020-05-09 19:58:45.445587
# Running Time of 训练color特征SVM分类器 : 7:44:51.199895
# Start Time of 训练SURF特征SVM分类器 : 2020-05-09 19:58:45.574289
# SURF特征SVM分类器训练开始 ……
# End Time of 训练SURF特征SVM分类器 : 2020-05-09 19:59:33.644728
# Running Time of 训练SURF特征SVM分类器 : 0:00:48.070439
# Start Time of 训练ELA特征SVM分类器 : 2020-05-09 19:59:33.646711
# ELA特征SVM分类器训练开始 ……
# End Time of 训练ELA特征SVM分类器 : 2020-05-09 22:07:43.086718
# Running Time of 训练ELA特征SVM分类器 : 2:08:09.440007
# Start Time of 测试color特征SVM分类器 : 2020-05-10 22:37:42.768791
# color特征SVM分类器测试开始 ……
# color_clf average accuracy: 0.5
# End Time of 测试color特征SVM分类器 : 2020-05-10 01:57:25.119221
# Running Time of 测试color特征SVM分类器 : 3:19:42.350430
# Start Time of 测试SURF特征SVM分类器 : 2020-05-10 01:57:25.285813
# SURF特征SVM分类器测试开始 ……
# SURF_clf average accuracy: 0.5
# End Time of 测试SURF特征SVM分类器 : 2020-05-10 01:57:44.946177
# Running Time of 测试SURF特征SVM分类器 : 0:00:19.660364
# Start Time of 测试ELA特征SVM分类器 : 2020-05-10 01:57:44.947172
# ELA特征SVM分类器测试开始 ……
# ELA_clf average accuracy: 0.5
# End Time of 测试ELA特征SVM分类器 : 2020-05-10 02:42:46.021191
# Running Time of 测试ELA特征SVM分类器 : 0:45:01.074019
# ——————————————————————————————————————————————————————————————————————————————————————
# 第二次训练测试,数据是每一部分数据前一部分训练后一部分测试
# Start Time of 训练color特征SVM分类器 : 2020-05-10 11:53:33.510095
# color特征SVM分类器训练开始 ……
# End Time of 训练color特征SVM分类器 : 2020-05-10 20:27:21.956960
# Running Time of 训练color特征SVM分类器 : 8:33:48.446865
# Start Time of 训练SURF特征SVM分类器 : 2020-05-10 20:27:21.972917
# SURF特征SVM分类器训练开始 ……
# End Time of 训练SURF特征SVM分类器 : 2020-05-10 20:28:25.578129
# Running Time of 训练SURF特征SVM分类器 : 0:01:03.605212
# Start Time of 训练ELA特征SVM分类器 : 2020-05-10 20:28:25.579094
# ELA特征SVM分类器训练开始 ……
# End Time of 训练ELA特征SVM分类器 : 2020-05-10 22:45:25.565611
# Running Time of 训练ELA特征SVM分类器 : 2:16:59.986517
# Start Time of 测试color特征SVM分类器 : 2020-05-10 22:45:25.571563
# color特征SVM分类器测试开始 ……
# color_clf average accuracy: 0.5166666666666667
# End Time of 测试color特征SVM分类器 : 2020-05-11 01:41:00.856418
# Running Time of 测试color特征SVM分类器 : 2:55:35.284855
# Start Time of 测试SURF特征SVM分类器 : 2020-05-11 01:41:00.858413
# SURF特征SVM分类器测试开始 ……
# SURF_clf average accuracy: 0.5166666666666667
# End Time of 测试SURF特征SVM分类器 : 2020-05-11 01:41:18.488249
# Running Time of 测试SURF特征SVM分类器 : 0:00:17.629836
# Start Time of 测试ELA特征SVM分类器 : 2020-05-11 01:41:18.489249
# ELA特征SVM分类器测试开始 ……
# ELA_clf average accuracy: 0.5166666666666667
# End Time of 测试ELA特征SVM分类器 : 2020-05-11 02:26:08.730687
# Running Time of 测试ELA特征SVM分类器 : 0:44:50.241438
# ——————————————————————————————————————————————————————————————————————————————————————
# 准确率三个总是一样,感觉是代码哪里有问题,暂时还没察觉到问题所在,希望有人能看出端倪,欢迎issue!!!