-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
2078 lines (1535 loc) · 100 KB
/
main.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
import copy
import json
import logging
import os
import re
import shutil
import subprocess
import sys
import time
import warnings
from decimal import Decimal
import joblib
from mpi4py import MPI
from tqdm import tqdm
import utils.filesystem.getpaths as gp
from utils.workerops.paramfactory import (attack_factory, attack_train_factory,
clean_factory, manip_factory,
train_factory)
# Deactivate warnings from Python unless requested at command line
if not sys.warnoptions:
warnings.simplefilter("ignore")
# Global values to be shared across all nodes
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
ROOT_PATH = os.getcwd()
CONFIG_FILE = ".config.json"
TIME_FMT = "%d-%m-%Y:%I:%M:%S-%p"
TIME = time.localtime(); TIME = time.strftime(TIME_FMT, TIME)
PYTHON_PATH = subprocess.getoutput("which python")
if rank == 0:
# Imports only necessary for manager node
import argparse
from colorama import Fore, Style, init
from utils.appinfo.licenseinfo import licenseinfo
from utils.appinfo.versioninfo import versioninfo
from utils.managerops import xml2dict as x2d
from utils.managerops.compress import Compression
from utils.managerops.unwrap import unwrap_attack, unwrap_train
from utils.workeradmin import greenlight as gl
from utils.workeradmin import skip
from utils.workerops import scattershot as sst
# Initialize colorama and define lambda functions
init(autoreset=True)
print_good = lambda x: print(Fore.GREEN + x)
print_dim_good = lambda x: print(Fore.GREEN + Style.DIM + x)
print_info = lambda x: print(Fore.BLUE + x)
print_dim_info = lambda x: print(Fore.BLUE + Style.DIM + x)
print_bad = lambda x: print(Fore.RED + x)
print_status = lambda x: print(Fore.YELLOW + x)
# Initialize argument parser
fin = open("assets/description.txt"); desc = fin.read(); fin.close()
fin = open("assets/epilog.txt"); epilog = fin.read(); fin.close()
parser = argparse.ArgumentParser(prog="jespipe",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=desc,
epilog=epilog)
parser.add_argument("--license", action="store_true", default=False, help="Print Jespipe licensing info.")
parser.add_argument("-s", "--silent", action="store_true", default=False, help="Silence all output from Jespipe.")
parser.add_argument("-np", "--noprogress", action="store_true", default=False, help="Activate or deactivate progress bars (default: False).")
parser.add_argument("-V", "--version", action="store_true", default=False, help="Print Jespipe version info.")
parser.add_argument("xml_control_file", nargs="?", default=None)
args = parser.parse_args()
if args.version:
versioninfo("Jespipe-v0.0.1", "2021", "LICENSE", "https://github.com/NucciTheBoss/jespipe",
"Jason C. Nucciarone", "Eric Inae", "Sheila Alemany", ascii_banner="assets/ascii_banner.txt")
exit()
if args.license:
licenseinfo("Jespipe: A Plugin-Based, Open MPI Framework for Adversarial Machine Learning Analysis", "2021",
"Jason C. Nucciarone", "Eric Inae", "Sheila Alemany")
exit()
stdout_bak = sys.stdout; stderr_bak = sys.stderr
if args.silent:
# Write stdout and stderr to /dev/null on host system
dev_null = open("/dev/null", "wt")
sys.stdout = dev_null; sys.stderr = dev_null
# Check that there is at least more than one node in the
# MPI.COMM_WORLD if we are not just print version or licensing info.
print_dim_info("Checking if there are enough nodes available in the MPI.COMM_WORLD.")
if size <= 1:
raise RuntimeError(Fore.RED + "Not enough nodes in the MPI.COMM_WORLD. Detect only {} node(s). Jespipe needs =< 2 nodes to run properly.".format(size))
else:
print_dim_good("Detected {} nodes. Number of Manager nodes: {}. Number of Worker nodes: {}.".format(size, 1, size-1))
# Check if we are working in the same directory as main.py.
# If not, throw error as that will impact the pipelines ability to run.
print_info("Checking if running out of same directory as {}".format(sys.argv[0]))
cwd_contents = [f for f in os.listdir(".") if os.path.isfile(f)]
if sys.argv[0] not in cwd_contents:
gl.killmsg(comm, size, True)
raise OSError(Fore.RED + "Not in same directory as {}. Please change current working directory to where {} is located.".format(sys.argv[0], sys.argv[0]))
# PREPROCESSING: neccesary preprocessing before beginning the execution of the pipeline
print_status("Launching preprocessing stage.")
# Read in config to get default configurations file
print_info("Loading configuration file {}.".format(CONFIG_FILE))
try:
fin = open(CONFIG_FILE, "rt"); config = fin.read(); fin.close()
config = json.loads(config)
except:
gl.killmsg(comm, size, True)
raise OSError(Fore.RED + "Cannot find or read {}. Please veify that {} exists and is readable.".format(CONFIG_FILE, CONFIG_FILE))
# Read in marco XML file
print_info("Checking if macro XML file passed at command line.")
if args.xml_control_file is None:
gl.killmsg(comm, size, True)
raise ValueError(Fore.RED + "No macro XML file specified before launching pipeline.")
print_info("Loading macro XML file {}.".format(args.xml_control_file))
# Perform checks to verify that XML file is in good format
if re.search("\Wxml", args.xml_control_file):
pass
else:
gl.killmsg(comm, size, True)
raise ValueError(Fore.RED + "Specified macro file {} not in XML format.".format(args.xml_control_file))
# Convert macro XML file to dictionary to begin staging for the pipeline
print_info("Parsing macro file {} into control dictionaries.".format(args.xml_control_file))
try:
job_control = x2d.xml2dict(args.xml_control_file, config)
except:
gl.killmsg(comm, size, True)
raise RuntimeError(Fore.RED + "A fatal was encountered while parsing the XML file.")
# Split job control dictionary into its three parts: train, attack, cleanup
train_control = job_control["train"] if "train" in job_control else None
attack_control = job_control["attack"] if "attack" in job_control else None
clean_control = job_control["clean"] if "clean" in job_control else None
print_info("Creating data directories.")
# Create directory for nodes to log their status if not exist
os.makedirs("data/.logs", exist_ok=True)
# Create directory for processes to write temporary files to
os.makedirs("data/.tmp", exist_ok=True)
# Begin execution the stages for the pipeline. Inform workers they are ready to start!
gl.killmsg(comm, size, False)
print_good("Preprocessing stage complete!")
# TRAIN: launch training stage of the pipeline
if train_control is not None:
print_status("Launching training stage.")
# Broadcast out to workers that we are now operating on the training stage
skip.skip_train(comm, size, False)
print_info("Unwrapping train control dictionary.")
train_macro_list = unwrap_train(train_control)
print_info("Converting relative file paths to absolute paths.")
print_info("Checking that file paths to dataset(s) and plugin(s) are valid.")
# Loop through train_macro_list:
# - Convert relative paths to absolute paths
# - Verify path to dataset and plugin exists
# - Create directory for each dataset
for i in range(0, len(train_macro_list)):
# Convert to list in order to change elements
macro = list(train_macro_list[i])
# Check if path to dataset is absolute
if os.path.isabs(macro[1]) is False:
macro[1] = os.path.abspath(macro[1])
# Check if dataset exists
if os.path.isfile(macro[1]) is False:
gl.killmsg(comm, size, True)
raise FileNotFoundError(Fore.RED + "The dataset {} is not found. Please verify that you are using the correct file path.".format(macro[1]))
# Check if path to model plugin is absolute
if os.path.isabs(macro[5]) is False:
macro[5] = os.path.abspath(macro[5])
# Check if model plugin exists
if os.path.isfile(macro[5]) is False:
gl.killmsg(comm, size, True)
raise FileNotFoundError(Fore.RED + "The plugin {} is not found. Please verify that you are using the correct file path.".format(macro[5]))
# Loop through manipulations to check if data manipulation plugins exist
for manip in macro[6]:
for manip_tag in manip[1]:
if os.path.isabs(manip_tag[1]["plugin"]) is False:
manip_tag[1]["plugin"] = os.path.abspath(manip_tag[1]["plugin"])
if os.path.isfile(manip_tag[1]["plugin"]) is False:
gl.killmsg(comm, size, True)
raise FileNotFoundError(Fore.RED + "The plugin {} is not found. Please verify that you are using the correct file path.".format(manip_tag[1]["plugin"]))
# Convert back to tuple
train_macro_list[i] = tuple(macro)
# Create data/$dataset_name/models <- where trained models are stored
os.makedirs("data/" + macro[0] + "/models", exist_ok=True)
# Create directives for the worker nodes
print_info("Generating directive list for worker nodes.")
train_directive_list = sst.generate_train(train_macro_list)
sliced_directive_list = sst.slice(train_directive_list, size)
# Broadcast that everything is good to go for the training stage
gl.killmsg(comm, size, False)
print_info("Sending tasks to workers.")
# Send greenlight to workers and then follow up with tasks
node_rank = sst.delegate(comm, size, sliced_directive_list)
print_info("Blocking until all workers complete training tasks.")
print_dim_info("Warning: This procedure may take a few minutes to a couple hours to complete depending " +
"on the complexity of your data, architecture of your model(s), number of models to train, etc.")
# Block until manager hears back from all workers
node_status = list()
for node in tqdm(node_rank, desc="Model training task completion progress", disable=args.noprogress):
node_status.append(comm.recv(source=node, tag=node))
print_good("Training stage complete!")
else:
print_status("Skipping training stage.")
# Broadcast out to workers that manager is skipping the training stage
skip.skip_train(comm, size, True)
# ATTACK: launch attack stage of the pipeline
if attack_control is not None:
print_status("Launching attack stage.")
# Broadcast out to workers that we are now operating on the attack stage
skip.skip_attack(comm, size, False)
attack_macro_list = unwrap_attack(attack_control)
# Loop through attack_macro_list:
# - Convert relative paths to absolute paths
# - Verify that trained models exist (use autodetection)
# - Verify that mentioned dataset and plugins exist
print_info("Converting relative file paths to absolute paths.")
print_info("Checking that file paths to dataset(s) and plugin(s) are valid.")
for i in range(0, len(attack_macro_list)):
# Convert to list in order to change elements
macro = list(attack_macro_list[i])
# Convert dataset path to absolute path
if os.path.isabs(macro[1]) is False:
macro[1] = os.path.abspath(macro[1])
# Check that dataset exists
if os.path.isfile(macro[1]) is False:
gl.killmsg(comm, size, True)
raise FileNotFoundError(Fore.RED + "Specified dataset {} is not found. Please verify that you are using the correct file path.".format(macro[1]))
# Convert plugin and model plugin to absolute paths and check that they exist
for attack in macro[2]:
if attack[1] is not None:
attack_params = [k for k in attack[1]]
for param in attack_params:
if os.path.isabs(attack[1][param]["plugin"]) is False:
attack[1][param]["plugin"] = os.path.abspath(attack[1][param]["plugin"])
if os.path.isfile(attack[1][param]["plugin"]) is False:
gl.killmsg(comm, size, True)
raise FileNotFoundError(Fore.RED + "The plugin {} is not found. Please verify that you are using the correct file path.".format(attack[1][param]["plugin"]))
if os.path.isabs(attack[1][param]["model_plugin"]) is False:
attack[1][param]["model_plugin"] = os.path.abspath(attack[1][param]["model_plugin"])
if os.path.isfile(attack[1][param]["model_plugin"]) is False:
gl.killmsg(comm, size, True)
raise FileNotFoundError(Fore.RED + "The model plugin {} is not found. Please verify that you are using the correct file path.".format(attack[1][param]["model_plugin"]))
# Check if models exist
if os.path.exists("data/" + macro[0] + "/models") is False:
gl.killmsg(comm, size, True)
raise FileNotFoundError(Fore.RED + "Model(s) not found. Please verify that models are stored in data/{}/models.".format(macro[0]))
# If models do exist, autodetect the .h5 files and add to macro list
print_info("Auto-detecting models for dataset {}.".format(macro[0]))
model_list = gp.getmodels(ROOT_PATH + "/data/" + macro[0] + "/models", format=".h5")
# Loop through the model list and pull the model names
model_names = list()
for model in model_list:
tmp = model.split("/models/"); tmp = tmp[-1].split("/")
model_name = tmp[0]
model_names.append(model_name)
model_list = list(zip(model_list, model_names))
macro.append(model_list)
attack_macro_list[i] = tuple(macro)
# Once all checks are good, create directory for storing adversarial examples
os.makedirs("data/" + macro[0] + "/adver_examples", exist_ok=True)
# Create directives for the worker nodes
print_info("Generating adversarial generation directive list for worker nodes.")
attack_directive_list = sst.generate_attack(attack_macro_list)
# Loop through directive list and generate more directives based on the change step
adver_example_directive_list = list()
for directive in attack_directive_list:
max_change = Decimal(str(directive[6]["max_change"]))
min_change = Decimal(str(directive[6]["min_change"]))
change_step = Decimal(str(directive[6]["change_step"]))
# Construct perturbation steps list using max_change, min_change, and change_step
tmp_list = list()
while min_change <= max_change:
tmp_list.append(min_change); min_change += change_step
# Convert decimal values back to float values
change_values = [float(i) for i in tmp_list]
# Expand directive list using change values
for change in change_values:
tmp_direct = copy.deepcopy(directive); tmp_direct = list(tmp_direct)
del tmp_direct[6]["max_change"]; del tmp_direct[6]["min_change"]; del tmp_direct[6]["change_step"]
tmp_direct[6].update({"change": change})
adver_example_directive_list.append(tuple(tmp_direct))
sliced_directive_list = sst.slice(adver_example_directive_list, size)
# Broadcast that everything is good to go to the worker nodes
gl.killmsg(comm, size, False)
print_info("Sending adversarial example generation tasks to workers.")
# Follow greenlight up with task list
node_rank = sst.delegate(comm, size, sliced_directive_list)
print_info("Blocking until all workers complete adversarial example generation tasks.")
print_dim_info("Warning: This procedure may take a few minutes to a couple hours to complete depending " +
"on the complexity of your attack, batch size of your attack, number of attacks, etc.")
# Block until manager hears back from all workers
node_status = list()
for node in tqdm(node_rank, desc="Adversarial example generation task completion progress", disable=args.noprogress):
node_status.append(comm.recv(source=node, tag=node))
print_info("Generating model evaluation directive list for worker nodes.")
sliced_directive_list = sst.slice(attack_directive_list, size)
print_info("Sending model evaluation directive list to worker nodes.")
node_rank = sst.delegate(comm, size, sliced_directive_list)
print_info("Blocking until all workers complete model evaluation tasks.")
print_dim_info("Warning: This procedure may take a few minutes to a couple hours to complete depending " +
"on the number of models, size of adversarial examples, number of adversarial examples, etc.")
# Block until manager hears back from all workers
node_status = list()
for node in tqdm(node_rank, desc="Model evaluation task completion progress", disable=args.noprogress):
node_status.append(comm.recv(source=node, tag=node))
print_good("Attack stage complete!")
else:
print_status("Skipping attack stage.")
# Broadcast out to workers that manager is skipping the attack stage
skip.skip_attack(comm, size, True)
# CLEAN: launch cleaning stage of the pipeline
if clean_control is not None:
print_status("Launching cleaning stage.")
# Broadcast out to workers that we are now operating on the cleaning stage
skip.skip_clean(comm, size, False)
if clean_control["plot"] is not None:
print_info("Checking that file paths to plugin(s) are valid.")
# Loop through plot keys and convert relative paths to absolute paths
for key in clean_control["plot"]:
if os.path.isabs(clean_control["plot"][key]["plugin"]) is False:
clean_control["plot"][key]["plugin"] = os.path.abspath(clean_control["plot"][key]["plugin"])
# Check if path to the plugin is valid
if os.path.isfile(clean_control["plot"][key]["plugin"]) is False:
raise FileNotFoundError(Fore.RED + "The plugin {} is not found. Please verify that you are using the correct file path.".format(
clean_control["plot"][key]["plugin"]))
# Create plot directory to save plots
print_info("Creating directory to save plots.")
os.makedirs("data/plots", exist_ok=True)
print_info("Generating directive list for worker nodes.")
# Generate and slice directive list that will be sent out to the workers
clean_directive_list = sst.generate_clean(clean_control["plot"], ROOT_PATH + "/data/plots", ROOT_PATH + "/data")
sliced_directive_list = sst.slice(clean_directive_list, size)
# Send greenlight to workers
gl.killmsg(comm, size, False)
print_info("Sending tasks to workers.")
# Delegate tasks out to the available workers in the COMM_WORLD
node_rank = sst.delegate(comm, size, sliced_directive_list)
print_info("Blocking until all workers complete plotting tasks.")
print_dim_info("Warning: This procedure may take some time to complete depending on how many plots are being generated, " +
"complexity of the data being anaylzed, format of the plot, etc.")
# Block until hearing back from all the worker nodes
node_status = list()
for node in tqdm(node_rank, desc="Data plotting task completion progress", disable=args.noprogress):
node_status.append(comm.recv(source=node, tag=node))
else:
gl.killmsg(comm, size, True)
if clean_control["clean_tmp"] == 1:
print_info("Deleting data/.tmp directory.")
shutil.rmtree("data/.tmp", ignore_errors=True)
if clean_control["compress"] is not None:
for key in clean_control["compress"]:
print_info("Renaming data directory to {} and compressing into format {}.".format(key, clean_control["compress"][key]["format"]))
# Create compressor that will be used to shrink the data directory
shutil.move("data", key)
compressor = Compression(key, key)
# Create archive based on user-specified compression algorithm
if clean_control["compress"][key]["format"] == "gzip":
compressor.togzip(verbose=not args.noprogress)
shutil.rmtree(key, ignore_errors=True)
if os.path.exists(clean_control["compress"][key]["path"]):
shutil.move("{}.tar.gz".format(key), "{}/{}.tar.gz".format(clean_control["compress"][key]["path"], key))
elif clean_control["compress"][key]["format"] == "bz2":
compressor.tobzip(verbose=not args.noprogress)
shutil.rmtree(key, ignore_errors=True)
if os.path.exists(clean_control["compress"][key]["path"]):
shutil.move("{}.tar.bz2".format(key), "{}/{}.tar.bz2".format(clean_control["compress"][key]["path"], key))
elif clean_control["compress"][key]["format"] == "zip":
compressor.tozip(verbose=not args.noprogress)
shutil.rmtree(key, ignore_errors=True)
if os.path.exists(clean_control["compress"][key]["path"]):
shutil.move("{}.zip".format(key), "{}/{}.zip".format(clean_control["compress"][key]["path"], key))
elif clean_control["compress"][key]["format"] == "xz":
compressor.toxz(verbose=not args.noprogress)
shutil.rmtree(key, ignore_errors=True)
if os.path.exists(clean_control["compress"][key]["path"]):
shutil.move("{}.tar.xz".format(key), "{}/{}.tar.xz".format(clean_control["compress"][key]["path"], key))
elif clean_control["compress"][key]["format"] == "tar":
compressor.totar(verbose=not args.noprogress)
shutil.rmtree(key, ignore_errors=True)
if os.path.exists(clean_control["compress"][key]["path"]):
shutil.move("{}.tar".format(key), "{}/{}.tar".format(clean_control["compress"][key]["path"], key))
else:
# Catch all for if user passes invalid compression algorithm
compressor.togzip(verbose=not args.noprogress)
shutil.rmtree(key, ignore_errors=True)
if os.path.exists(clean_control["compress"][key]["path"]):
shutil.move("{}.tar.gz".format(key), "{}/{}.tar.gz".format(clean_control["compress"][key]["path"], key))
print_good("Cleaning stage complete!")
else:
print_status("Skipping cleaning stage.")
# Broadcast out to workers that manager is skipping the cleaning stage
skip.skip_clean(comm, size, True)
print_good("Jespipe has completed!")
if args.silent:
# Reset stdout and stderr back to their original values
sys.stdout = stdout_bak; sys.stderr = stderr_bak
elif rank == 1:
greenlight = comm.recv(source=0, tag=1)
if greenlight != 1:
exit(127)
# After getting greenlight, create logger for node
os.makedirs("data/.logs/worker-1", exist_ok=True)
logger = logging.getLogger("worker-1-logger")
f_handler = logging.FileHandler("data/.logs/worker-1/{}.log".format(TIME))
logger.addHandler(f_handler)
logger.warning("INFO: Received greenlight message {} from manager node. Begin execution.".format(greenlight))
# TRAINING STAGE
skip_stage_training = comm.recv(source=0, tag=1)
if skip_stage_training != 1:
logger.warning("INFO: Waiting for greenlight to start training stage.")
training_greenlight = comm.recv(source=0, tag=1)
if training_greenlight != 1:
logger.warning("ERROR: Received greenlight message {} for training stage. Aborting execution.".format(training_greenlight))
exit(127)
logger.warning("INFO: Received greenlight {}. Beginning execution of model training stage.".format(training_greenlight))
# Receive task from manager
task_list = comm.recv(source=0, tag=1)
logger.warning("INFO: Received task list {} from manager.".format(task_list))
# Check if task list sent is empty. If so, return message to the manager
if task_list != []:
# Loop through each of the tasks and perform necessary data manipulations
for task in task_list:
logger.warning("INFO: Beginning training of model {} using directive list {}.".format(task[2], task))
# Check if task[6], task[7], task[8], or task[9] is None
# If so, skip execution and tell user they need to mention something.
if task[6] is None or task[7] is None or task[8] is None or task[9] is None:
logger.warning("ERROR: Skipping model {} because no manipulation was specified " +
"Please use the tag <vanilla tag='default1' /> or something similiar " +
"in your control file.")
pass
else:
manip_save_path = ROOT_PATH + "/data/" + task[0] + "/maniped_data"
if os.path.exists(manip_save_path) is False:
os.makedirs(manip_save_path, exist_ok=True)
logger.warning("INFO: Using {} on dataset {} with parameters {}.".format(task[6], task[0], task[9]))
# Perform data manipulation using manipulation plugin
param_dict = manip_factory(task[1], task[7], task[9], manip_save_path, ROOT_PATH + "/data/.tmp", ROOT_PATH)
maniped_pickle = subprocess.getoutput("{} {} {} {}".format(PYTHON_PATH, task[8], "train", param_dict))
# Created special directory for each individual manipulation
save_path = ROOT_PATH + "/data/" + task[0] + "/models/" + task[7]
if os.path.exists(save_path):
shutil.rmtree(save_path, ignore_errors=True)
os.makedirs(save_path, exist_ok=True)
# Create dictionary that will be passed to the training plugin
param_dict = train_factory(task[0], task[1], task[2], joblib.load(maniped_pickle), task[4], task[9], save_path, task[6], task[7], ROOT_PATH)
# Spawn plugin execution and block until the training section of the plugin has completed
logger.warning("INFO: Training model...")
file_output = "data/.logs/worker-1/{}-{}-{}-{}.log".format(TIME, task[2], task[6], task[7])
logger.warning("INFO: Saving output of {} for model {} to logfile {}.".format(task[5], task[2], file_output))
# Open a file that the training plugin can use for stdout and stderr
fout = open(file_output, "wt")
try:
subprocess.run([PYTHON_PATH, task[5], "train", param_dict], stdout=fout, stderr=fout)
except subprocess.SubprocessError:
logger.warning("ERROR: Build for model {} failed. Please review logfile {} for error diagnostics.".format(task[2], file_output))
# Close the file the plugin is using to log stdout and stderr
fout.close()
comm.send(1, dest=0, tag=1)
else:
logger.warning("WARNING: Received empty task list. Returning status 1 to manager.")
comm.send(1, dest=0, tag=1)
else:
logger.warning("WARNING: Skipping training stage of pipeline.")
# ATTACK STAGE
skip_stage_attack = comm.recv(source=0, tag=1)
if skip_stage_attack != 1:
logger.warning("INFO: Waiting for greenlight to start attack stage.")
attack_greenlight = comm.recv(source=0, tag=1)
if attack_greenlight != 1:
logger.warning("ERROR: Received greenlight message {} for attack stage. Aborting execution.".format(attack_greenlight))
exit(127)
logger.warning("INFO: Received greenlight {}. Beginning execution of model attack stage.".format(attack_greenlight))
# Receive task from manager
task_list = comm.recv(source=0, tag=1)
logger.warning("INFO: Received task list {} from manager.".format(task_list))
if task_list != []:
# Generate adversarial examples
for task in task_list:
logger.warning("INFO: Beginning adversarial attack on model {} with attack {}".format(task[7], task[2]))
# Get model name
model_name = task[7].split("/"); model_name = model_name[-1].split("."); model_name = model_name[0]
# Get change value
change = task[6]["change"]
logger.warning("INFO: Generating adversial example with minimum change set to {}.".format(change))
# Open file that the attack plugin can use as a log file
file_output = "data/.logs/worker-1/{}-attack-{}-{}-{}-{}.log".format(TIME, task[2], task[3], model_name, change)
logger.warning("INFO: Saving output of {} for attack {} to logfile {}.".format(task[3], task[2], file_output))
fout = open(file_output, "wt")
test_features = gp.gettestfeat(task[9], feature_file="test_features.pkl")
attack_param = attack_factory(task[3], task[7], task[8], joblib.load(test_features), task[6],
ROOT_PATH + "/data/" + task[0] + "/adver_examples", ROOT_PATH)
try:
subprocess.run([PYTHON_PATH, task[4], "attack", attack_param], stdout=fout, stderr=fout)
except subprocess.SubprocessError:
logger.warning("ERROR: Attack on model {} failed. Please review logfile {} for error diagnostics.".format(model_name, file_output))
# Close the attack plugin log file
fout.close()
comm.send(1, dest=0, tag=1)
else:
logger.warning("WARNING: Received empty task list. Returning status 1 to manager.")
comm.send(1, dest=0, tag=1)
# Receive second task list from manager
task_list = comm.recv(source=0, tag=1)
logger.warning("INFO: Received task list {} from manager.".format(task_list))
if task_list != []:
# Evaluate model using adversarial examples
for task in task_list:
logger.warning("INFO: Beginning evaluation of model {} using adversarial examples.".format(task[7]))
# Get model name
model_name = task[7].split("/"); model_name = model_name[-1].split("."); model_name = model_name[0]
# Open file that the training plugin can use as a log file during evaluation
file_output = "data/.logs/worker-1/{}-eval-{}-{}-{}.log".format(TIME, task[2], task[3], model_name)
logger.warning("INFO: Saving output of {} evaluation logfile {}.".format(task[7], file_output))
fout = open(file_output, "wt")
adver_examples = gp.getfiles(ROOT_PATH + "/data/" + task[0] + "/adver_examples/" + task[3] + "/" + task[8])
test_labels = gp.gettestlabel(task[9], label_file="test_labels.pkl")
train_attack_param = attack_train_factory(adver_examples, task[3], joblib.load(test_labels),
task[9] + "/stat", task[7], ROOT_PATH)
try:
subprocess.run([PYTHON_PATH, task[5], "attack", train_attack_param], stdout=fout, stderr=fout)
except subprocess.SubprocessError:
logger.warning("ERROR: Evaluation for model {} failed. Please review logfile {} for error diagnostics.".format(model_name, file_output))
# Close the training plugin log file
fout.close()
comm.send(1, dest=0, tag=1)
else:
logger.warning("WARNING: Received empty task list. Returning status 1 to manager.")
comm.send(1, dest=0, tag=1)
else:
logger.warning("WARNING: Skipping attack stage of pipeline.")
# CLEANING STAGE
skip_clean_stage = comm.recv(source=0, tag=1)
if skip_clean_stage != 1:
logger.warning("INFO: Waiting for greenlight to start cleaning stage.")
cleaning_greenlight = comm.recv(source=0, tag=1)
if cleaning_greenlight != 1:
# 0 message means worker is not needed any more
logger.warning("WARNING: Received greenlight message {} for cleaning stage. Aborting execution.".format(cleaning_greenlight))
exit(0)
logger.warning("INFO: Received greenlight {}. Beginning execution of cleaning stage.".format(cleaning_greenlight))
# Receive task from manager
task_list = comm.recv(source=0, tag=1)
logger.warning("INFO: Received task list {} from manager.".format(task_list))
if task_list != []:
logger.warning("INFO: Beginning cleaning stage plotting.")
for task in task_list:
logger.warning("INFO: Generating plot {}.".format(task[2]))
file_output = "data/.logs/worker-1/{}-plot-{}.log".format(TIME, task[2])
logger.warning("INFO: Saving output of plotting plugin to logfile {}.".format(file_output))
fout = open(file_output, "wt")
clean_param = clean_factory(task[1], task[2], task[3], ROOT_PATH)
try:
subprocess.run([PYTHON_PATH, task[0], "clean", clean_param], stdout=fout, stderr=fout)
except subprocess.SubprocessError:
logger.warning("ERROR: Plotting failed. Please review logfile {} for error diagnostics.".format(file_output))
fout.close()
comm.send(1, dest=0, tag=1)
else:
logger.warning("WARNING: Received empty task list. Returning status 1 to manager.")
comm.send(1, dest=0, tag=1)
else:
logger.warning("WARNING: Skipping clean stage of pipeline.")
elif rank == 2:
greenlight = comm.recv(source=0, tag=2)
if greenlight != 1:
exit(127)
# After getting greenlight, create logger for node
os.makedirs("data/.logs/worker-2", exist_ok=True)
logger = logging.getLogger("worker-2-logger")
f_handler = logging.FileHandler("data/.logs/worker-2/{}.log".format(TIME))
logger.addHandler(f_handler)
logger.warning("INFO: Received greenlight message {} from manager node. Begin execution.".format(greenlight))
# TRAINING STAGE
skip_stage_training = comm.recv(source=0, tag=2)
if skip_stage_training != 1:
logger.warning("INFO: Waiting for greenlight to start training stage.")
training_greenlight = comm.recv(source=0, tag=2)
if training_greenlight != 1:
logger.warning("ERROR: Received greenlight message {} for training stage. Aborting execution.".format(training_greenlight))
exit(127)
logger.warning("INFO: Received greenlight {}. Beginning execution of model training stage.".format(training_greenlight))
# Receive task from manager
task_list = comm.recv(source=0, tag=2)
logger.warning("INFO: Received task list {} from manager.".format(task_list))
if task_list != []:
for task in task_list:
logger.warning("INFO: Beginning training of model {} using directive list {}.".format(task[2], task))
# Check if task[6], task[7], task[8], or task[9] is None
# If so, skip execution and tell user they need to mention something.
if task[6] is None or task[7] is None or task[8] is None or task[9] is None:
logger.warning("ERROR: Skipping model {} because no manipulation was specified " +
"Please use the tag <vanilla tag='default1' /> or something similiar " +
"in your control file.")
pass
else:
manip_save_path = ROOT_PATH + "/data/" + task[0] + "/maniped_data"
if os.path.exists(manip_save_path) is False:
os.makedirs(manip_save_path, exist_ok=True)
logger.warning("INFO: Using {} on dataset {} with parameters {}.".format(task[6], task[0], task[9]))
# Perform data manipulation using manipulation plugin
param_dict = manip_factory(task[1], task[7], task[9], manip_save_path, ROOT_PATH + "/data/.tmp", ROOT_PATH)
maniped_pickle = subprocess.getoutput("{} {} {} {}".format(PYTHON_PATH, task[8], "train", param_dict))
# Created special directory for each individual manipulation
save_path = ROOT_PATH + "/data/" + task[0] + "/models/" + task[7]
if os.path.exists(save_path):
shutil.rmtree(save_path, ignore_errors=True)
os.makedirs(save_path, exist_ok=True)
# Create dictionary that will be passed to the training plugin
param_dict = train_factory(task[0], task[1], task[2], joblib.load(maniped_pickle), task[4], task[8], save_path, task[6], task[7], ROOT_PATH)
# Spawn plugin execution and block until the training section of the plugin has completed
logger.warning("INFO: Training model...")
file_output = "data/.logs/worker-2/{}-{}-{}-{}.log".format(TIME, task[2], task[6], task[7])
logger.warning("INFO: Saving output of {} for model {} to logfile {}.".format(task[5], task[2], file_output))
# Open a file that the training plugin can use for stdout and stderr
fout = open(file_output, "wt")
try:
subprocess.run([PYTHON_PATH, task[5], "train", param_dict], stdout=fout, stderr=fout)
except subprocess.SubprocessError:
logger.warning("ERROR: Build for model {} failed. Please review logfile {} for error diagnostics.".format(task[2], file_output))
# Close the file the training plugin is using to log stdout and stderr
fout.close()
comm.send(1, dest=0, tag=2)
else:
logger.warning("WARNING: Received empty task list. Returning status 1 to manager.")
comm.send(1, dest=0, tag=2)
else:
logger.warning("WARNING: Skipping training stage of pipeline.")
# ATTACK STAGE
skip_stage_attack = comm.recv(source=0, tag=2)
if skip_stage_attack != 1:
logger.warning("INFO: Waiting for greenlight to start attack stage.")
attack_greenlight = comm.recv(source=0, tag=2)
if attack_greenlight != 1:
logger.warning("ERROR: Received greenlight message {} for attack stage. Aborting execution.".format(attack_greenlight))
exit(127)
logger.warning("INFO: Received greenlight {}. Beginning execution of model attack stage.".format(attack_greenlight))
# Receive task from manager
task_list = comm.recv(source=0, tag=2)
logger.warning("INFO: Received task list {} from manager.".format(task_list))
if task_list != []:
# Generate adversarial examples
for task in task_list:
logger.warning("INFO: Beginning adversarial attack on model {} with attack {}".format(task[7], task[2]))
# Get model name
model_name = task[7].split("/"); model_name = model_name[-1].split("."); model_name = model_name[0]
# Get change value
change = task[6]["change"]
logger.warning("INFO: Generating adversial example with minimum change set to {}.".format(change))
# Open file that the attack plugin can use as a log file
file_output = "data/.logs/worker-2/{}-attack-{}-{}-{}-{}.log".format(TIME, task[2], task[3], model_name, change)
logger.warning("INFO: Saving output of {} for attack {} to logfile {}.".format(task[3], task[2], file_output))
fout = open(file_output, "wt")
test_features = gp.gettestfeat(task[9], feature_file="test_features.pkl")
attack_param = attack_factory(task[3], task[7], task[8], joblib.load(test_features), task[6],
ROOT_PATH + "/data/" + task[0] + "/adver_examples", ROOT_PATH)
try:
subprocess.run([PYTHON_PATH, task[4], "attack", attack_param], stdout=fout, stderr=fout)
except subprocess.SubprocessError:
logger.warning("ERROR: Attack on model {} failed. Please review logfile {} for error diagnostics.".format(model_name, file_output))
# Close the attack plugin log file
fout.close()
comm.send(1, dest=0, tag=2)
else:
logger.warning("WARNING: Received empty task list. Returning status 1 to manager.")
comm.send(1, dest=0, tag=2)
# Receive second task list from manager
task_list = comm.recv(source=0, tag=2)
logger.warning("INFO: Received task list {} from manager.".format(task_list))
if task_list != []:
# Evaluate model using adversarial examples
for task in task_list:
logger.warning("INFO: Beginning evaluation of model {} using adversarial examples.".format(task[7]))
# Get model name
model_name = task[7].split("/"); model_name = model_name[-1].split("."); model_name = model_name[0]
# Open file that the training plugin can use as a log file during evaluation
file_output = "data/.logs/worker-2/{}-eval-{}-{}-{}.log".format(TIME, task[2], task[3], model_name)
logger.warning("INFO: Saving output of {} evaluation logfile {}.".format(task[7], file_output))
fout = open(file_output, "wt")
adver_examples = gp.getfiles(ROOT_PATH + "/data/" + task[0] + "/adver_examples/" + task[3] + "/" + task[8])
test_labels = gp.gettestlabel(task[9], label_file="test_labels.pkl")
train_attack_param = attack_train_factory(adver_examples, task[3], joblib.load(test_labels),
task[9] + "/stat", task[7], ROOT_PATH)
try:
subprocess.run([PYTHON_PATH, task[5], "attack", train_attack_param], stdout=fout, stderr=fout)
except subprocess.SubprocessError:
logger.warning("ERROR: Evaluation for model {} failed. Please review logfile {} for error diagnostics.".format(model_name, file_output))
# Close the training plugin log file
fout.close()
comm.send(1, dest=0, tag=2)
else:
logger.warning("WARNING: Received empty task list. Returning status 1 to manager.")
comm.send(1, dest=0, tag=2)
else:
logger.warning("WARNING: Skipping attack stage of pipeline.")
# CLEANING STAGE
skip_clean_stage = comm.recv(source=0, tag=2)
if skip_clean_stage != 1:
logger.warning("INFO: Waiting for greenlight to start cleaning stage.")
cleaning_greenlight = comm.recv(source=0, tag=2)
if cleaning_greenlight != 1:
# 0 message means worker is not needed any more
logger.warning("WARNING: Received greenlight message {} for cleaning stage. Aborting execution.".format(cleaning_greenlight))
exit(0)
logger.warning("INFO: Received greenlight {}. Beginning execution of cleaning stage.".format(cleaning_greenlight))
# Receive task from manager
task_list = comm.recv(source=0, tag=2)
logger.warning("INFO: Received task list {} from manager.".format(task_list))
if task_list != []:
logger.warning("INFO: Beginning cleaning stage plotting.")
for task in task_list:
logger.warning("INFO: Generating plot {}.".format(task[2]))
file_output = "data/.logs/worker-2/{}-plot-{}.log".format(TIME, task[2])
logger.warning("INFO: Saving output of plotting plugin to logfile {}.".format(file_output))
fout = open(file_output, "wt")
clean_param = clean_factory(task[1], task[2], task[3], ROOT_PATH)
try:
subprocess.run([PYTHON_PATH, task[0], "clean", clean_param], stdout=fout, stderr=fout)
except subprocess.SubprocessError:
logger.warning("ERROR: Plotting failed. Please review logfile {} for error diagnostics.".format(file_output))
fout.close()
comm.send(1, dest=0, tag=2)
else:
logger.warning("WARNING: Received empty task list. Returning status 1 to manager.")
comm.send(1, dest=0, tag=2)
else:
logger.warning("WARNING: Skipping cleaning stage of pipeline.")
elif rank == 3:
greenlight = comm.recv(source=0, tag=3)
if greenlight != 1:
exit(127)
# After getting greenlight, create logger for node
os.makedirs("data/.logs/worker-3", exist_ok=True)
logger = logging.getLogger("worker-3-logger")
f_handler = logging.FileHandler("data/.logs/worker-3/{}.log".format(TIME), mode="w")
logger.addHandler(f_handler)
logger.warning("INFO: Received greenlight message {} from manager node. Begin execution.".format(greenlight))
# TRAINING STAGE
skip_stage_training = comm.recv(source=0, tag=3)
if skip_stage_training != 1:
logger.warning("INFO: Waiting for greenlight to start training stage.")
training_greenlight = comm.recv(source=0, tag=3)
if training_greenlight != 1:
logger.warning("ERROR: Received greenlight message {} for training stage. Aborting execution.".format(training_greenlight))
exit(127)
logger.warning("INFO: Received greenlight {}. Beginning execution of model training stage.".format(training_greenlight))
# Receive task from manager
task_list = comm.recv(source=0, tag=3)
logger.warning("INFO: Received task list {} from manager.".format(task_list))
if task_list != []:
for task in task_list:
logger.warning("INFO: Beginning training of model {} using directive list {}.".format(task[2], task))
# Check if task[6], task[7], task[8], or task[9] is None
# If so, skip execution and tell user they need to mention something.
if task[6] is None or task[7] is None or task[8] is None or task[9] is None:
logger.warning("ERROR: Skipping model {} because no manipulation was specified " +
"Please use the tag <vanilla tag='default1' /> or something similiar " +
"in your control file.")
pass
else:
manip_save_path = ROOT_PATH + "/data/" + task[0] + "/maniped_data"
if os.path.exists(manip_save_path) is False:
os.makedirs(manip_save_path, exist_ok=True)
logger.warning("INFO: Using {} on dataset {} with parameters {}.".format(task[6], task[0], task[9]))
# Perform data manipulation using manipulation plugin
param_dict = manip_factory(task[1], task[7], task[9], manip_save_path, ROOT_PATH + "/data/.tmp", ROOT_PATH)
maniped_pickle = subprocess.getoutput("{} {} {} {}".format(PYTHON_PATH, task[8], "train", param_dict))