forked from ZJUICSR/AIcert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.py
2057 lines (1939 loc) · 105 KB
/
interface.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding:utf-8 -*-
__copyright__ = 'Copyright © 2021/11/01, ZJUICSR'
import os, json, copy, torch
import os.path as osp, cv2
from IOtool import IOtool
from torchvision import transforms
from function.formal_verify import *
from function.formal_verify.knowledge_consistency import Model_zoo as zoomodels
from PIL import Image
from model.model_net.lenet import Lenet
from function.attack import run_adversarial, run_backdoor
from function.fairness import run_dataset_debias, run_model_debias, run_image_model_debias, run_model_evaluate, run_image_model_evaluate
from function import concolic, env_test, coverage, deepsst, dataclean, framework_test, modelmeasure, modulardevelop, robust_gnn, adv_traind
from function.ex_methods import *
import matplotlib.pyplot as plt
from function.defense import *
from torchvision.models import vgg16
from torchvision.datasets import CIFAR10, mnist
from function.side import *
ROOT = osp.dirname(osp.abspath(__file__))
def run_model_debias_api(tid, stid, dataname, modelname, algorithmname, metrics = [], sensattrs = [], targetattr=None, staAttrList= [],
test_mode = True, model_path='', save_folder=''):
"""模型公平性提升
:params tid:主任务ID
:params stid:子任务id
:params dataname:数据集名称
:params modelname:模型名称
:params algorithmname:优化算法名称
"""
IOtool.change_subtask_state(tid, stid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, stid, time.time())
logging = IOtool.get_logger(stid)
if dataname in ["Compas", "Adult", "German"]:
res = run_model_debias(dataname, modelname, algorithmname, metrics, sensattrs, targetattr, staAttrList, logging=logging, model_path=model_path, save_folder=save_folder)
else:
res = run_image_model_debias(dataset_name=dataname,model_path=model_path,
model_name=modelname, algorithm_name=algorithmname,
metrics=metrics, test_mode=test_mode, logging=logging, save_folder=save_folder)
res["stop"] = 1
IOtool.write_json(res, osp.join(ROOT,"output", tid, stid+"_result.json"))
IOtool.change_subtask_state(tid, stid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_model_eva_api(tid, stid, dataname, model_path='', modelname='', metrics = [], senAttrList = [], tarAttrList = [], staAttrList= [], test_mode = True):
"""模型公平性提升
:params tid:主任务ID
:params stid:子任务id
:params dataname:数据集名称
:params modelname:模型名称
:params algorithmname:优化算法名称
"""
IOtool.change_subtask_state(tid, stid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, stid, time.time())
logging = IOtool.get_logger(stid)
if dataname in ["Compas", "Adult", "German"]:
res = run_model_evaluate(dataname, model_path, modelname, metrics, senAttrList, tarAttrList, staAttrList, logging=logging)
else:
res = run_image_model_evaluate(dataname, model_path, modelname, metrics, test_mode, logging=logging)
if "Consistency" in res.keys():
res["Consistency"] = float(res["Consistency"])
res["stop"] = 1
IOtool.write_json(res, osp.join(ROOT,"output", tid, stid+"_result.json"))
IOtool.change_subtask_state(tid, stid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_data_debias_api(tid, stid, dataname, datamethod, senAttrList, tarAttrList, staAttrList):
"""数据集公平性提升
:params tid:主任务ID
:params stid:子任务id
:params dataname:数据集名称
:params datamethod:优化算法名称
"""
IOtool.change_subtask_state(tid, stid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, stid, time.time())
logging = IOtool.get_logger(stid)
res = run_dataset_debias(dataname, datamethod, senAttrList, tarAttrList, staAttrList, logging=logging)
res["stop"] = 1
IOtool.write_json(res,osp.join(ROOT,"output", tid, stid+"_result.json"))
IOtool.change_subtask_state(tid, stid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_verify(tid, AAtid, param):
"""鲁棒性形式化验证
:params tid:主任务ID
:params AAtid:子任务id
:params param:参数
"""
IOtool.change_subtask_state(tid, AAtid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, AAtid, time.time())
logging = IOtool.get_logger(AAtid)
N = param['size']
device = 'cpu'
verify = vision_verify
if param['dataset'] == 'mnist':
mn_model = get_mnist_cnn_model()
test_data, n_class = get_mnist_data(number=N, batch_size=10)
if param['dataset'] == 'cifar10':
mn_model = get_cifar_resnet18() if param['model']!= 'densenet' else get_cifar_densenet_model()
test_data, n_class = get_cifar_data(number=N, batch_size=10)
if param['dataset'] == 'gtsrb':
mn_model = get_gtsrb_resnet18()
test_data, n_class = get_gtsrb_data(number=N, batch_size=10)
if param['dataset'] == 'mtfl':
mn_model = get_MTFL_resnet18()
test_data, n_class = get_MTFL_data(number=N, batch_size=10)
if param['dataset'] == 'sst2':
mn_model = get_lstm_demo_model() if param['model'] != 'transformer' else get_transformer_model()
test_data, _ = get_sst_data(ver_num=N)
n_class = 2
verify = language_verify
global LiRPA_LOGS
input_param = {'interface': 'Verification',
'node': "中间结果可视化",
'input_param': {'model': mn_model,
'dataset': test_data,
'n_class': n_class,
'up_eps': param['up_eps'],
'down_eps': param['down_eps'],
'steps': param['steps'],
'device': device,
"log_func":logging,
'output_path': osp.join('output',tid,AAtid,"formal_img"),
'task_id': f"{param['task_id']}"}}
global result
result = verify(input_param)
result["stop"] = 1
IOtool.write_json(result,osp.join(ROOT,"output", tid, AAtid+"_result.json"))
IOtool.change_subtask_state(tid, AAtid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_concolic(tid, AAtid, dataname, modelname, norm, times):
"""测试样本自动生成
:params tid:主任务ID
:params AAtid:子任务id
:params dataname:数据集名称
:params modelname:模型名称
:params norm:范数约束
"""
IOtool.change_subtask_state(tid, AAtid, 1)
IOtool.change_task_state(tid, 1)
logging = IOtool.get_logger(AAtid)
res = concolic.run_concolic(dataname.lower(), modelname.lower(), norm.lower(), int(times), osp.join(ROOT,"output", tid, AAtid), logging)
res["stop"]=1
IOtool.write_json(res,osp.join(ROOT,"output", tid, AAtid+"_result.json"))
IOtool.change_subtask_state(tid, AAtid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_dataclean(tid, AAtid, dataname, upload_flag, upload_path):
"""异常数据检测
:params tid:主任务ID
:params AAtid:子任务id
:params dataname:数据集名称
:params upload_flag:上传标志
:params upload_path:上传文件路径
:output res:需保存到子任务json中的返回结果/路径
"""
IOtool.change_subtask_state(tid, AAtid, 1)
IOtool.change_task_state(tid, 1)
logging = IOtool.get_logger(AAtid)
res = dataclean.run_dataclean(dataname, int(upload_flag), upload_path, osp.join(ROOT,"output", tid, AAtid), logging)
res["stop"] = 1
IOtool.write_json(res,osp.join(ROOT,"output", tid, AAtid+"_result.json"))
IOtool.change_subtask_state(tid, AAtid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_envtest(tid,AAtid,matchmethod,frameworkname,frameversion):
"""系统环境分析
:params tid:主任务ID
:params AAtid:子任务id
:params matchmethod:环境分析匹配机制
:params frameworkname:适配框架名称
:params frameversion:框架版本
:output res:需保存到子任务json中的返回结果/路径
"""
IOtool.change_subtask_state(tid, AAtid, 1)
IOtool.change_task_state(tid, 1)
logging = IOtool.get_logger(AAtid)
res = env_test.run_env_frame(matchmethod,frameworkname,frameversion, osp.join(ROOT,"output", tid, AAtid), logging)
# res = concolic.run_concolic(dataname, modelname, norm)
res["detection_result"]=IOtool.load_json(res["env_test"]["detection_result"])
res["stop"] = 1
IOtool.write_json(res,osp.join(ROOT,"output", tid, AAtid+"_result.json"))
IOtool.change_subtask_state(tid, AAtid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_coverage_neural(tid,AAtid,dataset,model, k, N):
"""单神经元测试准则
:params tid:主任务ID
:params AAtid:子任务id
:params dataset: 数据集名称
:params model: 模型名称
:params k: 激活阈值
:params N: 测试的图片数量
:output res:需保存到子任务json中的返回结果/路径
"""
IOtool.change_subtask_state(tid, AAtid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, AAtid, time.time())
logging = IOtool.get_logger(AAtid)
res = coverage.run_coverage_neural_func(dataset.lower(), model.lower(), float(k), int(N), osp.join(ROOT,"output", tid, AAtid), logging)
res["stop"] = 1
IOtool.write_json(res,osp.join(ROOT,"output", tid, AAtid+"_result.json"))
IOtool.change_subtask_state(tid, AAtid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_coverage_layer(tid,AAtid,dataset,model, k, N):
"""神经层测试准则
:params tid:主任务ID
:params AAtid:子任务id
:params dataset: 数据集名称
:params model: 模型名称
:params k: 激活阈值
:params N: 测试的图片数量
:output res:需保存到子任务json中的返回结果/路径
"""
IOtool.change_subtask_state(tid, AAtid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, AAtid, time.time())
logging = IOtool.get_logger(AAtid)
res = coverage.run_coverage_layer_func(dataset.lower(), model.lower(), float(k), int(N), osp.join(ROOT,"output", tid, AAtid), logging)
res["stop"] = 1
IOtool.write_json(res,osp.join(ROOT,"output", tid, AAtid+"_result.json"))
IOtool.change_subtask_state(tid, AAtid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_coverage_importance(tid, AAtid, dataset, model, n_imp, clus):
"""重要神经元覆盖测试准则
:params tid:主任务ID
:params AAtid:子任务id
:params dataset: 数据集名称
:params model: 模型名称
:params n_imp: 重要神经元数目
:params clus: 聚类数
:output res:需保存到子任务json中的返回结果/路径
"""
IOtool.change_subtask_state(tid, AAtid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, AAtid, time.time())
logging = IOtool.get_logger(AAtid)
res = coverage.run_coverage_importance_func(dataset.lower(), model.lower(), int(n_imp), int(clus), osp.join(ROOT,"output", tid, AAtid), logging)
res["stop"] = 1
IOtool.write_json(res,osp.join(ROOT,"output", tid, AAtid+"_result.json"))
IOtool.change_subtask_state(tid, AAtid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_deepsst(tid,AAtid,dataset,modelname,pertube,m_dir):
"""敏感神经元测试准则
:params tid:主任务ID
:params AAtid:子任务id
:params dataset: 数据集名称
:params modelname: 模型名称
:params pertube: 敏感神经元扰动比例
:params m_dir: 敏感度值文件位置
:output res:需保存到子任务json中的返回结果/路径
"""
IOtool.change_subtask_state(tid, AAtid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, AAtid, time.time())
logging = IOtool.get_logger(AAtid)
res = deepsst.run_deepsst(dataset.lower(), modelname, float(pertube), m_dir, osp.join(ROOT,"output", tid, AAtid), logging)
res["stop"] = 1
IOtool.write_json(res,osp.join(ROOT,"output", tid, AAtid+"_result.json"))
IOtool.change_subtask_state(tid, AAtid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_deeplogic(tid,AAtid,dataset,modelname):
"""逻辑神经元测试准则
:params tid:主任务ID
:params AAtid:子任务id
:params dataset: 数据集名称
:params modelname: 模型名称
:output res:需保存到子任务json中的返回结果/路径
"""
IOtool.change_subtask_state(tid, AAtid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, AAtid, time.time())
logging = IOtool.get_logger(AAtid)
from function import DeepLogic
res = DeepLogic.run_deeplogic(dataset.lower(), modelname.lower(), osp.join(ROOT,"output", tid, AAtid), logging)
res["stop"] = 1
IOtool.write_json(res,osp.join(ROOT,"output", tid, AAtid+"_result.json"))
IOtool.change_subtask_state(tid, AAtid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_frameworktest(tid,AAtid,modelname,framework):
"""开发框架安全结构度量
:params tid:主任务ID
:params AAtid:子任务id
:params framework: 开发框架名称
:params modelname: 模型名称
:output res:需保存到子任务json中的返回结果/路径
"""
IOtool.change_subtask_state(tid, AAtid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, AAtid, time.time())
logging = IOtool.get_logger(AAtid)
res = framework_test.run_framework_test_exec(modelname.lower(), framework, osp.join(ROOT,"output", tid, AAtid), logging)
res["stop"] = 1
IOtool.write_json(res,osp.join(ROOT,"output", tid, AAtid+"_result.json"))
IOtool.change_subtask_state(tid, AAtid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_modelmeasure(tid,AAtid,dataset, modelname, naturemethod, natureargs, advmethod, advargs, measuremethod):
"""模型安全度量
:params tid:主任务ID
:params AAtid:子任务id
:params dataset: 数据集名称
:params modelname: 模型名称
:params naturemethod: 自然样本生成方法
:params natureargs: 自然样本扰动强度
:params advmethod: 对抗样本生成方法
:params advargs: 对抗样本扰动强度
:params measuremethod: 安全度量维度
:output res:需保存到子任务json中的返回结果/路径
"""
IOtool.change_subtask_state(tid, AAtid, 1)
IOtool.change_task_state(tid, 1)
time.sleep(10)
IOtool.set_task_starttime(tid, AAtid, time.time())
logging = IOtool.get_logger(AAtid)
res = modelmeasure.run_modelmeasure(dataset.upper(), modelname.lower(), naturemethod, float(natureargs), advmethod.lower(), float(advargs), measuremethod, osp.join(ROOT,"output", tid, AAtid), logging)
res["stop"] = 1
IOtool.write_json(res,osp.join(ROOT,"output", tid, AAtid+"_result.json"))
IOtool.change_subtask_state(tid, AAtid, 2)
IOtool.change_task_success_v2(tid=tid)
def run_modulardevelop(tid,AAtid, dataset, modelname, tuner, init, epoch, iternum):
"""模型模块化开发
:params tid:主任务ID
:params AAtid:子任务id
:params dataset: 数据集名称
:params modelname: 模型名称
:params tuner: 搜索方法
:params* init: 初始化方法(仅DeepAlchemy方法需要)
:params epoch: 搜索轮数
:params iternum: 每次搜索迭代轮数
:output res:需保存到子任务json中的返回结果/路径
"""
IOtool.change_subtask_state(tid, AAtid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, AAtid, time.time())
devive = IOtool.get_device()
logging = IOtool.get_logger(AAtid)
res = modulardevelop.run_modulardevelop(dataset.lower(), modelname.lower(), tuner.lower(), init.lower(), epoch, iternum, devive, osp.join(ROOT,"output", tid, AAtid), logging)
res["stop"] = 1
IOtool.write_json(res,osp.join(ROOT,"output", tid, AAtid+"_result.json"))
IOtool.change_subtask_state(tid, AAtid, 2)
IOtool.change_task_success_v2(tid=tid)
from train_network import train_resnet_mnist, train_resnet_cifar10, eval_test, test_batch, robust_train
def run_adv_attack(tid, stid, dataname, model, methods, inputParam, sample_num=128):
"""对抗攻击评估
:params tid:主任务ID
:params stid:子任务id
:params dataname:数据集名称
:params model:模型名称
:params methods:list,对抗攻击方法
:params inputParam:输入参数
"""
# 开始执行标记任务状态
IOtool.change_subtask_state(tid, stid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, stid, time.time())
logging = IOtool.get_logger(stid)
inputParam['device'] = IOtool.get_device()
device = torch.device(inputParam['device'])
modelpath = osp.join("./model/ckpt", dataname.upper() + "_" + model.lower()+".pth")
if (not osp.exists(modelpath)):
logging.info("[模型获取]:服务器上模型不存在")
if dataname.upper() == "CIFAR10":
logging.info("[模型训练]:开始训练模型")
train_resnet_cifar10(model, modelpath, logging, device)
logging.info("[模型训练]:模型训练结束")
elif dataname.upper() == "MNIST":
logging.info("[模型训练]:开始训练模型")
train_resnet_mnist(model, modelpath, logging, device)
logging.info("[模型训练]:模型训练结束")
else:
logging.info(f"[模型训练]:不支持该数据集{dataname.upper()}")
result={}
result["stop"] = 1
IOtool.write_json(result,osp.join(ROOT,"output", tid, stid+"_result.json"))
IOtool.change_subtask_state(tid, stid, 3)
IOtool.change_task_success_v2(tid)
return 0
if not osp.exists(osp.join(ROOT,"output", tid, stid)):
os.mkdir(osp.join(ROOT,"output", tid, stid))
resultlist={}
all_num = 0
for method in methods:
logging.info("[执行对抗攻击]:正在执行{:s}对抗攻击".format(method))
attackparam = inputParam[method]
attackparam["save_path"] = osp.join(ROOT,"output", tid, stid)
if "norm" in attackparam.keys() and attackparam["norm"]=="np.inf":
attackparam["norm"]=np.inf
resultlist[method] ,resultlist[method]["pic"], resultlist[method]["path"], resultlist[method]["num"] = run_adversarial(model, modelpath, dataname, method, attackparam, device, sample_num)
logging.info("[执行对抗攻击中]:{:s}对抗攻击结束,攻击成功率为{}%".format(method,resultlist[method]["asr"]))
# 统计缓存攻击用例
# save_root = "dataset/adv_data"
# num_all = 0
# for dirpath,dirnames,filenames in os.walk(save_root):
# for filepath in filenames:
# if "adv_attack_" in filepath:
# datacatch = torch.load(osp.join(dirpath,filepath))
# num_all += len(datacatch['x'])
# del datacatch
# logging.info('**************************')
# logging.info(f'平台攻击用例总数:{num_all}')
# logging.info('**************************')
# logging.info("[执行对抗攻击]:对抗攻击执行完成,数据保存中")
# resultlist["num_all"] = num_all
resultlist["stop"] = 1
IOtool.write_json(resultlist,osp.join(ROOT,"output", tid, stid+"_result.json"))
IOtool.change_subtask_state(tid, stid, 2)
IOtool.change_task_success_v2(tid)
def run_backdoor_attack(tid, stid, dataname, model, methods, inputParam):
"""后门攻击评估
:params tid:主任务ID
:params stid:子任务id
:params dataname:数据集名称
:params model:模型名称
:params methods:list,后门攻击方法
:params inputParam:输入参数
"""
# 开始执行标记任务状态
IOtool.change_subtask_state(tid, stid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, stid, time.time())
logging = IOtool.get_logger(stid)
inputParam['device'] = IOtool.get_device()
if 'PoisoningAttackAdversarialEmbedding' in methods:
feature=True
modelpath = osp.join("./model/ckpt",dataname.upper() + "_" + model.lower()+"_1.pth")
else:
feature=False
modelpath = osp.join("./model/ckpt",dataname.upper() + "_" + model.lower()+".pth")
if (not osp.exists(modelpath)):
logging.info("[模型获取]:服务器上模型不存在")
if dataname.upper() == "CIFAR10":
logging.info("[模型训练]:开始训练模型")
train_resnet_cifar10(model, modelpath, logging, inputParam['device'], feature)
logging.info("[模型训练]:模型训练结束")
elif dataname.upper() == "MNIST":
logging.info("[模型训练]:开始训练模型")
train_resnet_mnist(model, modelpath, logging, inputParam['device'], feature)
logging.info("[模型训练]:模型训练结束")
else:
logging.info(f"[模型训练]:不支持该数据集{dataname.upper()}")
result={}
result["stop"] = 1
IOtool.write_json(result,osp.join(ROOT,"output", tid, stid+"_result.json"))
IOtool.change_subtask_state(tid, stid, 3)
IOtool.change_task_success_v2(tid)
return 0
res = {}
logging.info("[执行后门攻击]:开始后门攻击")
for method in methods:
logging.info("[执行后门攻击]:正在执行{:s}后门攻击".format(method))
res[method]={}
attackparam = inputParam[method]
save_path = osp.join(ROOT,"output", tid, stid)
if not osp.exists(save_path):
os.makedirs(save_path)
inputParam[method]["save_path"] = save_path
res[method]= run_backdoor(model, modelpath, dataname, method, pp_poison=inputParam[method]["pp_poison"],
save_num=inputParam[method]["save_num"],
target=inputParam[method]["target"],trigger=inputParam[method]["trigger"], device=inputParam["device"],
test_sample_num=inputParam[method]["test_sample_num"], train_sample_num=inputParam[method]["train_sample_num"],
nb_classes=10, method_param=inputParam[method], feature=feature)
logging.info("[执行后门攻击]:{:s}后门攻击运行结束,投毒率为{}时,攻击成功率为{}%".format(method, inputParam[method]["pp_poison"], res[method]["attack_success_rate"]*100))
res["stop"] = 1
IOtool.write_json(res, osp.join(ROOT,"output", tid, stid+"_result.json"))
IOtool.change_subtask_state(tid, stid, 2)
IOtool.change_task_success_v2(tid)
def run_dim_reduct(tid, stid, datasetparam, modelparam, vis_methods, adv_methods):
"""降维可视化
:params tid:主任务ID
:params stid:子任务id
:params datasetparam:数据集参数
:params modelparam:模型参数
:params vis_methods:list,降维方法
:params adv_methods:list,对抗攻击方法
:params device:GPU
"""
IOtool.change_subtask_state(tid, stid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, stid, time.time())
logging = IOtool.get_logger(stid)
device = IOtool.get_device()
params = {
"dataset": datasetparam,
"model": modelparam,
"out_path": osp.join(ROOT,"output", tid),
"device": torch.device(device),
"adv_methods":{"methods":adv_methods},
"root":ROOT
}
root = ROOT
dataset = datasetparam["name"]
model_name = modelparam["name"]
batchsize = get_batchsize(model_name,dataset)
nor_data = torch.load(osp.join(root, f"dataset/data/{dataset}_NOR.pt"))
nor_loader = get_loader(nor_data, batchsize=batchsize)
logging.info( "[数据集获取]:获取{:s}数据集正常样本已完成.".format(dataset))
model = modelparam["ckpt"]
logging.info( "[加载被解释模型]:准备加载被解释模型{:s}".format(model_name))
net = load_model_ex(model_name, dataset, device, root, reference_model=model, logging=logging)
net = net.eval().to(device)
logging.info( "[加载被解释模型]:被解释模型{:s}已加载完成".format(model_name))
adv_loader = {}
for adv_method in adv_methods:
adv_loader[adv_method] = get_adv_loader(net, nor_loader, adv_method, params, batchsize=batchsize, logging=logging)
logging.info( "[数据集获取]:获取{:s}对抗样本已完成".format(dataset))
save_path = osp.join(ROOT,"output", tid, stid)
if not osp.exists(save_path):
os.mkdir(save_path)
res = {}
for adv_method in adv_methods:
temp = dim_reduciton_visualize(vis_methods, nor_loader, adv_loader[adv_method], net, model_name, dataset, device, save_path)
res[adv_method] = temp
logging.info( "[数据分布降维解释]:{:s}对抗样本数据分布降维解释已完成".format(adv_method))
res["stop"] = 1
IOtool.write_json(res, osp.join(ROOT,"output", tid, stid+"_result.json"))
print("interfase modify sub task state:",tid, stid)
IOtool.change_subtask_state(tid, stid, 2)
IOtool.change_task_success_v2(tid)
def run_attrbution_analysis(tid, stid, datasetparam, modelparam, ex_methods, adv_methods, use_layer_explain):
"""对抗攻击归因解释
:params tid:主任务ID
:params stid:子任务id
:params datasetparam:数据集参数
:params modelparam:模型参数
:params ex_methods:list,攻击解释方法
:params adv_methods:list,对抗攻击方法
:params device:GPU
:params use_layer_explain: bool, 是否使用层间解释分析方法
"""
IOtool.change_subtask_state(tid, stid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, stid, time.time())
device = IOtool.get_device()
logging = IOtool.get_logger(stid)
params = {
"dataset": datasetparam,
"model": modelparam,
"out_path": osp.join(ROOT,"output", tid, stid),
"device": torch.device(device),
"ex_methods":{"methods":ex_methods},
"adv_methods":{"methods":adv_methods},
"root":ROOT,
"stid":stid
}
root = ROOT
result = {}
img_num = 20
dataset = datasetparam["name"]
model_name = modelparam["name"]
batchsize = get_batchsize(model_name, dataset)
nor_data = torch.load(osp.join(root, f"dataset/data/{dataset}_NOR.pt"))
nor_loader = get_loader(nor_data, batchsize=batchsize)
logging.info("[数据集获取]:获取{:s}数据集正常样本已完成.".format(dataset))
# ckpt参数 直接存储模型object,不存储模型路径;可以直接带入load_model_ex函数中,该函数会自动根据输入作相应处理
model = modelparam["ckpt"]
logging.info( "[加载被解释模型]:准备加载被解释模型{:s}".format(model_name))
net = load_model_ex(model_name, dataset, device, root, reference_model=model, logging=logging)
net = net.eval().to(device)
logging.info( "[加载被解释模型]:被解释模型{:s}已加载完成".format(model_name))
adv_loader = {}
for adv_method in adv_methods:
adv_loader[adv_method] = get_adv_loader(net, nor_loader, adv_method, params, batchsize=batchsize, logging=logging)
logging.info( "[数据集获取]:获取{:s}对抗样本已完成".format(dataset))
save_path = osp.join(ROOT,"output", tid, stid)
if not osp.exists(save_path):
os.mkdir(save_path)
logging.info( "[注意力分布图计算]:选择了{:s}解释算法".format(", ".join(ex_methods)))
ex_images = attribution_maps(net, nor_loader, adv_loader, ex_methods, params, img_num, logging)
result.update({"adv_ex":ex_images})
if use_layer_explain == True:
logging.info( "[已选择执行模型层间解释]:正在执行...")
layer_ex = layer_explain(net, model_name, nor_loader, adv_loader, dataset, params["out_path"], device, img_num, logging)
result.update({"layer_ex": layer_ex})
logging.info( "[已选择执行模型层间解释]:层间解释执行完成")
else:
logging.info( "[未选择执行模型层间解释]:将不执行模型层间解释分析方法")
result["stop"] = 1
IOtool.write_json(result, osp.join(ROOT,"output", tid, stid+"_result.json"))
print("interfase modify sub task state:",tid, stid)
IOtool.change_subtask_state(tid, stid, 2)
IOtool.change_task_success_v2(tid)
def run_lime(tid, stid, datasetparam, modelparam, adv_methods, device):
"""多模态解释
:params tid:主任务ID
:params stid:子任务id
:params datasetparam:数据集参数
:params modelparam:模型参数
:params adv_methods:list,对抗攻击方法
:params device:GPU
"""
IOtool.change_subtask_state(tid, stid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, stid, time.time())
device = IOtool.get_device()
logging = IOtool.get_logger(stid)
params = {
"dataset": datasetparam,
"model": modelparam,
"out_path": osp.join(ROOT,"output", tid),
"device": torch.device(device),
"adv_methods":{"methods":adv_methods},
# "ex_methods":ex_methods,
"root":ROOT,
"stid":stid
}
logging = IOtool.get_logger(stid)
root = ROOT
dataset = datasetparam["name"]
nor_data = torch.load(osp.join(root, f"dataset/data/{dataset}_NOR.pt"))
# nor_loader = get_loader(nor_data, batchsize=16)
nor_img_x = nor_data["x"][2]
label = nor_data['y'][2]
img = recreate_image(nor_img_x.squeeze())
logging.info("[数据集获取]:获取{:s}数据集正常样本已完成.".format(dataset))
model_name = modelparam["name"]
if modelparam["ckpt"] != "None":
model = torch.load(modelparam["ckpt"])
else:
modelparam["ckpt"] = None
model = modelparam["ckpt"]
logging.info("[加载被解释模型]:准备加载被解释模型{:s}".format(model_name))
net = load_model_ex(model_name, dataset, device, root, reference_model=model, logging=logging)
net = net.eval().to(device)
logging.info("[加载被解释模型]:被解释模型{:s}已加载完成".format(model_name))
adv_loader = {}
res = {}
for adv_method in adv_methods:
logging.info("[数据集获取]:获取{:s}对抗样本".format(adv_method))
adv_img_x = sample_untargeted_attack(dataset, adv_methods[0], net, nor_img_x, label, device, root)
logging.info("[数据集获取]:获取{:s}对抗样本已完成".format(adv_method))
save_path = params["out_path"]
result = lime_image_ex(img, net, model_name, dataset, device, root, save_path)
res[adv_method]=result
res["stop"] = 1
IOtool.write_json(res, osp.join(ROOT,"output", tid, stid+"_result.json"))
IOtool.change_subtask_state(tid, stid, 2)
IOtool.change_task_success_v2(tid)
def verify_img(tid, stid, net, dataset, eps, pic_path):
IOtool.change_subtask_state(tid, stid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, stid, time.time())
logging = IOtool.get_logger(stid)
b1=[]
b2=[]
print(net, dataset)
lb1,ub1,lb2,ub2,predicted,score_IBP,score_CROWN=auto_verify_img(net, dataset, eps, pic_path)
categories=[]
for i in range(len(lb1)):
b1.append([round(lb1[i],4),round(ub1[i],4)])
b2.append([round(lb2[i],4),round(ub2[i],4)])
categories.append(f'f_{i}')
resp={'boundary1':b1,'boundary2':b2,'categories':categories,'predicted':predicted,
'score_IBP':score_IBP,'score_CROWN':score_CROWN}
IOtool.write_json(resp, osp.join(ROOT,"output", tid, stid+"_result.json"))
IOtool.change_subtask_state(tid, stid, 2)
IOtool.change_task_success_v2(tid)
return resp
def submitAandB(tid, stid, a, b):
IOtool.change_subtask_state(tid, stid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, stid, time.time())
IOtool.change_subtask_state(tid, stid, 2)
IOtool.change_task_success_v2(tid)
return a + b
def reach(tid, stid, dataset, pic_path,label, target_label):
IOtool.change_subtask_state(tid, stid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, stid, time.time())
logging = IOtool.get_logger(stid)
save_path = osp.join("output", tid, stid)
logging.info(f"The reachability task starts ,dateset: {dataset}")
base=os.path.join(os.getcwd(),"model","ckpt")
base_path=os.path.join(base,'reach_checkpoints')
model = reachNet()
if dataset=='CIFAR10':
logging.info(f"Start to load CNN-3layer")
model.load_state_dict(torch.load(os.path.join(base_path,'cifar_torch_net.pth'), map_location=torch.device('cpu')))
else:
logging.info(f"Start to load CNN-3layer")
model.load_state_dict(torch.load(os.path.join(base_path,'mnist_torch_net.pth'), map_location=torch.device('cpu')))
logging.info(f"End of model loading")
model.eval()
transf = transforms.ToTensor()
logging.info(f"Start to load the uploaded image")
img = cv2.imread(pic_path)
image=transf(img)
image=torch.unsqueeze(image, 0)
label=torch.tensor(int(label))
target_label=torch.tensor(int(target_label))
attack_block = (1,1)
epsilon = 0.02
relaxation = 0.01
logging.info(f"reachability verification")
reach_model = ReachMethod(model, image, label, save_path,
attack_block=attack_block,
epsilon=epsilon,
relaxation=relaxation)
output_sets = reach_model.reach()
# num 越小越快
sims = reach_model.simulate(num=1000)
# Plot output reachable sets and simulations
dim0, dim1 = label.numpy(), target_label.numpy()
fig = plt.figure()
ax = fig.add_subplot(111)
logging.info(f"Output data process, reachable area drawing")
for item in output_sets:
out_vertices = item[0]
plot_polytope2d(out_vertices[:, [dim0, dim1]], ax, color='b', alpha=1.0, edgecolor='k', linewidth=0.0,zorder=2)
ax.plot(sims[:,dim0], sims[:,dim1],'k.',zorder=1)
minnum = min(sims[:,dim0]) if min(sims[:,dim0]) > min(sims[:,dim1]) else min(sims[:,dim1])
maxnum = max(sims[:,dim0]) if max(sims[:,dim0]) > max(sims[:,dim1]) else max(sims[:,dim1])
x = np.linspace(minnum, maxnum, 1000)
y = x
ax.plot(x, y, 'r', linewidth = 2)
ax.autoscale()
plt.tight_layout()
# plt.show()
pt_dir=os.path.dirname(pic_path)
outpath = osp.join(ROOT, "output", tid, stid, 'output.png')
plt.savefig(outpath)
plt.close()
logging.info(f"The reachable areas is drawn. The reachability verification is complete")
resp={"path":osp.join("/static", "output", tid, stid, 'output.png')}
resp['input']=f'static/img/tmp_imgs/{tid}/{stid}.png'
resp["stop"] = 1
IOtool.write_json(resp, osp.join(ROOT,"output", tid, stid+"_result.json"))
IOtool.change_subtask_state(tid, stid, 2)
IOtool.change_task_success_v2(tid)
return resp
def run_detect(tid, stid, defense_methods, adv_dataset, adv_model, adv_method, adv_nums, adv_file_path):
IOtool.change_subtask_state(tid, stid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, stid, time.time())
# device = IOtool.get_device()
logging = IOtool.get_logger(stid)
detect_rate_dict = {}
if "CARTL" in defense_methods:
# 调换顺序,将CARTL放在最后执行
defense_methods.remove("CARTL")
defense_methods.append("CARTL")
for defense_method in defense_methods:
logging.info("开始执行防御任务{:s}".format(defense_method))
detect_rate, no_defense_accuracy = detect(adv_dataset, adv_model, adv_method, adv_nums, defense_method, adv_file_path,logging)
detect_rate_dict[defense_method] = round(detect_rate, 4)
logging.info("{:s}防御算法执行结束,对抗鲁棒性为:{:.3f}".format(defense_method,round(detect_rate, 4)))
no_defense_accuracy_list = no_defense_accuracy.tolist() if isinstance(no_defense_accuracy, np.ndarray) else no_defense_accuracy
response_data = {
"detect_rates": detect_rate_dict,
"no_defense_accuracy": no_defense_accuracy_list
}
response_data["stop"] = 1
IOtool.write_json(response_data, osp.join(ROOT,"output", tid, stid+"_result.json"))
IOtool.change_subtask_state(tid, stid, 2)
IOtool.change_task_success_v2(tid)
return response_data
def detect(adv_dataset, adv_model, adv_method, adv_nums, defense_methods, adv_examples=None, logging=None):
from model.model_net.resnet import ResNet18
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
# device = IOtool.get_device()
if torch.cuda.is_available():
print("got GPU")
logging.info("加载模型{:s}".format(adv_model))
if adv_dataset == 'CIFAR10':
mean = [0.4914, 0.4822, 0.4465]
std = [0.2023, 0.1994, 0.2010]
if adv_model == 'ResNet18':
model = ResNet18()
checkpoint = torch.load(osp.join(ROOT,'model/model-cifar-resnet18/model-res-epoch85.pt'))
elif adv_model == 'VGG16':
model = vgg16()
model.classifier[6] = nn.Linear(4096, 10)
checkpoint = torch.load(osp.join(ROOT,'model/model-cifar-vgg16/model-vgg16-epoch85.pt'))
else:
raise Exception('CIFAR10 can only use ResNet18 and VGG16!')
model.load_state_dict(checkpoint)
model = model.to(device)
elif adv_dataset == 'MNIST':
mean = (0.1307,)
std = (0.3081,)
if adv_model == 'SmallCNN':
model = SmallCNN()
checkpoint = torch.load(osp.join(ROOT,'model/model-mnist-smallCNN/model-nn-epoch61.pt'))
elif adv_model == 'VGG16':
model = vgg16()
model.features[0] = nn.Conv2d(1, 64, kernel_size=3, padding=1)
model.classifier[6] = nn.Linear(4096, 10)
checkpoint = torch.load(osp.join(ROOT,'model/model-mnist-vgg16/model-vgg16-epoch32.pt'))
else:
raise Exception('MNIST can only use SmallCNN and VGG16!')
model.load_state_dict(checkpoint)
model = model.to(device).eval()
logging.info("{:s}模型加载结束".format(adv_model))
print(defense_methods)
if defense_methods not in ['Pixel Defend', 'Pixel Defend Enhanced'] and adv_method == 'BPDA':
raise Exception('BPDA can only use to attack Pixel Defend and Pixel Defend Enhanced!')
if defense_methods == 'JPEG':
detector = Jpeg(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Feature Squeeze':
detector = feature_squeeze(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Twis':
detector = Twis(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Rgioned-based':
detector = RegionBased(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Pixel Deflection':
detector = Pixel_Deflection(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Label Smoothing':
detector = Label_smoothing(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Spatial Smoothing':
detector = Spatial_smoothing(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Gaussian Data Augmentation':
detector = Gaussian_augmentation(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Total Variance Minimization':
detector = Total_var_min(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Pixel Defend':
detector = Pixel_defend(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Pixel Defend Enhanced':
detector = Pixel_defend_enhanced(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'InverseGAN':
detector = Inverse_gan(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'DefenseGAN':
detector = Defense_gan(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Madry':
detector = Madry(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'FastAT':
detector = FastAT(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'TRADES':
detector = Trades(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'FreeAT':
detector = FreeAT(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'MART':
detector = Mart(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'CARTL':
detector = Cartl(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Activation':
detector = Activation_defence(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Spectral Signature':
detector = Spectral_signature(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Provenance':
detector = Provenance_defense(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Neural Cleanse L1':
detector = Neural_cleanse_l1(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Neural Cleanse L2':
detector = Neural_cleanse_l2(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'Neural Cleanse Linf':
detector = Neural_cleanse_linf(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == 'SAGE':
detector = Sage(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
elif defense_methods == "STRIP":
detector = Strip(model, mean, std, adv_examples=adv_examples, adv_method=adv_method, adv_dataset=adv_dataset, adv_nums=adv_nums, device=device)
_, _, detect_rate, no_defense_accuracy = detector.detect()
return detect_rate, no_defense_accuracy
def run_side_api(trs_file, method, tid, stid):
IOtool.change_subtask_state(tid, stid, 1)
IOtool.change_task_state(tid, 1)
IOtool.set_task_starttime(tid, stid, time.time())
device = IOtool.get_device()
logging = IOtool.get_logger(stid)
logging.info("开始执行侧信道分析")
res={}
if method in ['cpa','dpa']:
logging.info("当前分析文件为{:s},分析方法为{:s},分析时间较久,约需2分钟".format(trs_file, method))
else:
logging.info("当前分析文件为{:s},分析方法为{:s}".format(trs_file, method))
number = trs_file.split(".trs")[0].split("_")[-1]
# outpath = osp.join(ROOT,"output", tid,stid + "_" + method+"_"+number+"_out.txt")
# trs_file_path = osp.join(ROOT,"dataset/Trs/samples",trs_file)
if method in ['cpa', 'dpa', 'hpa']:
trs_file_path = osp.join(ROOT, "dataset/Trs/samples", method, "elmotrace"+number, trs_file)
outpath = osp.join(ROOT,"output", tid,stid + "_" + method+"_"+number+"_out.txt")
elif method == "dpa":
trs_file_path = osp.join(ROOT, "dataset/Trs/samples", "cpa", "elmotrace"+number, trs_file)
outpath = osp.join(ROOT,"output", tid,stid + "_" + method+"_"+number+"_out.txt")
elif method == "spa":
trs_file_path = osp.join(ROOT, "dataset/Trs/samples", method, trs_file)
outpath = osp.join(ROOT,"output", tid, stid)
if not osp.exists(outpath):
os.makedirs(outpath)
elif method in ['ttest', "x2test"]:
trs_file_path = osp.join(ROOT, "dataset/Trs/samples", "cpa", "elmotrace"+number, trs_file)
outpath = osp.join(ROOT,"output", tid,stid + "_" + method+"_"+number+"_out.txt")
use_time = run_side(trs_file_path, method, outpath)
res[method] = {}
index = 128+ int(number)
# res[method].append([float(s) for s in line.split()])
if method in ["cpa","dpa","hpa","ttest", "x2test"]:
res[method]["Y"] = []
res[method]["X"] = []
count = 9 if method == 'hpa' else 100
cur = 0
for line in open(outpath, 'r'):
values = [float(s) for s in line.split()]
if method in ['x2test', 'ttest'] :
count == 127 if len(values) >127 else len(values)
Y = []
i = 0
while i < count:
Y.append(values[i])
i += 1
if cur == index:
res[method]["true"] = Y
if cur == index+1:
res[method]["false"] = Y
else:
res[method]["Y"].append(Y)
cur += 1
j = 1
while j < (count+1):
res[method]["X"].append(j)
j += 1
else:
# 其他方法结果处理
pass
if method in ["cpa","dpa"]:
logging.info("分析方法{:s}执行结束,运行100次耗时{:s}s".format(method,str(round(use_time,1))))
logging.info("系统平均耗时{:s}s".format(method,str(round(use_time/100,1))))
res[method]["runTime"] = use_time/100
else:
logging.info("分析方法{:s}执行结束,耗时{:s}s".format(method,str(round(use_time,1))))
res[method]["runTime"] = use_time
logging.info("侧信道分析执行结束!")
res["stop"] = 1
IOtool.write_json(res, osp.join(ROOT,"output", tid, stid+"_result.json"))
IOtool.change_subtask_state(tid, stid, 2)
IOtool.change_task_success_v2(tid)