-
Notifications
You must be signed in to change notification settings - Fork 5
/
run_experiments.py
55 lines (39 loc) · 2.44 KB
/
run_experiments.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
"""
Script for running framework.py under different adversarial conditions.
Set hyperparams in parameters.ini and script will create a duplicate parameters file and run framework.py using that
"""
# coding=utf-8
import os
from os import system
import shutil
from utils.utils import load_parameters, set_parameter
if __name__ == "__main__":
original_parameters_filepath = "parameters.ini"
original_parameters = load_parameters(original_parameters_filepath)
experiment_name = original_parameters['general']['experiment_suffix']
# Create duplicate parameters file in directory
new_params_directory = "experiment_parameters"
new_params_name = "parameters_{exp}.ini".format(exp=experiment_name)
if not os.path.exists(new_params_directory):
os.mkdir(new_params_directory)
new_params_filepath = os.path.join(new_params_directory, new_params_name)
shutil.copy(original_parameters_filepath, new_params_filepath)
train_methods = ['natural', 'rfgsm_k', 'dfgsm_k', 'bga_k', 'bca_k']
evasion_methods = ['natural', 'rfgsm_k', 'dfgsm_k', 'bga_k', 'bca_k']
saved_models_file = "trained_models"
for train_method in train_methods:
set_parameter(new_params_filepath, "general", "train_model_from_scratch", "True")
set_parameter(new_params_filepath, "general", "load_model_weights", "False")
set_parameter(new_params_filepath, "general", "training_method", train_method)
set_parameter(new_params_filepath, "general", "evasion_method", train_method)
system("source activate nn_mal;python framework.py {param_path}".format(param_path=new_params_filepath))
for train_method in train_methods:
model_filepath = os.path.join(saved_models_file, "[training:{train_meth}|evasion:{train_meth}]_{exp_name}-model.pt".format(
train_meth=train_method, exp_name=experiment_name))
set_parameter(new_params_filepath, "general", "training_method", train_method)
set_parameter(new_params_filepath, "general", "train_model_from_scratch", "False")
set_parameter(new_params_filepath, "general", "load_model_weights", "True")
set_parameter(new_params_filepath, "general", "model_weights_path", model_filepath)
for evasion_method in evasion_methods:
set_parameter(new_params_filepath, "general", "evasion_method", evasion_method)
system("source activate nn_mal;python framework.py {param_path}".format(param_path=new_params_filepath))