def setup_all(args): experiment, exp_id = setup_exp(args) print(exp_id) env = setup_env(args) del env experiment.log_parameters(args.__dict__) data = setup_tr_val_test(args) model = setup_model(args) model_dir = setup_dir(basename=".models", args=args, exp_id=exp_id) print("model save_dir: %s" % (str(model_dir))) ims_dir = setup_dir(basename=".images", args=args, exp_id=exp_id) return data, model, experiment, model_dir, ims_dir
from psychopy import visual, gui import pathlib import utils import sys #get experiment and monitor infos expInfo, monInfo = utils.setup_exp() #setup monitor mon = utils.setup_mon(monInfo) #get stimulation parameter from file stim_params_path = './params_stim.txt' params_stim = utils.get_stimulation_params(stim_params_path) #start experiment section = int(expInfo[3]) while section <= 10: # setup window win = visual.Window(size=mon.getSizePix(), fullscr=True, color='grey', monitor=mon, screen=0) win.recordFrameIntervals = True win.refreshThreshold = 1 / 60 + 0.004 # create folder to store data data_path = "./data/%s/%s/%s/section_%s" % (expInfo[0], expInfo[1], expInfo[2], section)
# json file with experiment config CONFIG_FILE = './config_attack/bpda_eot_attack.json' ############### # ## SETUP ## # ############### # load experiment config with open(CONFIG_FILE) as file: config = json.load(file) # directory for experiment results exp_dir = config['exp_dir'] + '_' + datetime.datetime.now().strftime('%d-%m-%Y_%I-%M-%S_%p') + '_/' # setup folders, save code, set seed and get device setup_exp(exp_dir, config['seed'], ['log'], ['bpda_eot_attack.py', 'nets.py', 'utils.py', CONFIG_FILE]) print('Loading data and nets.') # data loader data, num_classes = import_data(config['data_type'], False, False) attack_loader = DataLoader(data, batch_size=config['batch_size'], shuffle=config['subset_shuffle'], num_workers=0) # get clf and ebm networks and load saved weights clf = WideResNet(num_classes=num_classes).cuda() clf.load_state_dict(t.load(config['clf_weight_path'], map_location=lambda storage, loc: storage.cuda())) clf.eval() if config['langevin_steps'] > 0: ebm = EBM().cuda() ebm.load_state_dict(t.load(config['ebm_weight_path'], map_location=lambda storage, loc: storage.cuda())) ebm.eval() # cross-entropy loss function to generate attack gradients
import copy import torch from functools import partial from torch import nn from torch.optim import Adam, RMSprop import numpy as np from pathlib import Path import time from data.setup import setup_data import os from utils import setup_args, setup_dir, setup_exp if __name__ == "__main__": args = setup_args() data = setup_data(args) experiment = setup_exp(args) model = setup_model(args) if args.task == "embed": from training.embed_trainer import EmbedTrainer trainer = EmbedTrainer(model, args, experiment) elif args.task == "infer": from training.inference_trainer import InferenceTrainer trainer = InferenceTrainer(model, args, experiment) elif args.task == "predict": from training.prediction_trainer import PredictionTrainer trainer = PredictionTrainer(model, args, experiment) elif args.task == "control":
visited[origin_express] = origin_express # change route_args current_position->origin_express embedding embeddinged_route_args = env.get_embeddinged_route_args( origin_express) step_mix_s = np.append(embeddinged_route_args, now_traffic) return path # 如果选择的点有环路,则环境会返回来一个reward,reward的值,应该很小,表示不想出现环路 # 2:如果即将加入的originExpress的所有邻居,已经都在path中,则需要在path中删除后三个,重新设置 originExpress # env setup # setup_exp() folder = setup_run() env = ONOSEnv(folder) # training # s_dim = len(env.initial_route_args) - 1 + REPESENTATTION_SIZE + len( env.env_loads.flatten()) a_dim = REPESENTATTION_SIZE a_bound = 1 MAX_PATH_STEPS = env.active_nodes MAX_EP_STEPS = 1000 ddpg = DDPG(a_dim, s_dim, a_bound) # ddpg.train(routeTuple) # 路径元祖,是一个list:【(vector1,vector2)(vector1,vector2)】,vector是Embedding之后的表示 t1 = time.time() for i in range(MAX_EPISODES):
# json file with experiment config CONFIG_FILE = './config_train_clf/cifar10_nat.json' ############### # ## SETUP ## # ############### # load experiment config with open(CONFIG_FILE) as file: config = json.load(file) # directory for experiment results exp_dir = config['exp_dir'] + '_' + datetime.datetime.now().strftime('%d-%m-%Y_%I-%M-%S_%p') + '_/' # setup folders, save code, set seed and get device setup_exp(exp_dir, config['seed'], ['checkpoints'], ['train_clf.py', 'nets.py', 'utils.py', CONFIG_FILE]) print('Processing data...') # import train and test datasets and set up data loaders train_data, num_classes = import_data(config['data_type'], True, True) train_data_loader = DataLoader(train_data, config['batch_size'], shuffle=True, num_workers=config['num_workers']) test_data = import_data(config['data_type'], False, False)[0] test_data_loader = DataLoader(test_data, config['batch_size'], shuffle=False, num_workers=config['num_workers']) print('Setting up network and optimizer...') # network structure and weight init clf = WideResNet(num_classes=num_classes).cuda() clf.apply(conv_init) # initialize optim assert len(config['lr_list']) == len(config['lr_schedule']), 'lr_list and lr_schedule must have the same length' optim = t.optim.SGD(clf.parameters(), config['lr_list'][0], config['momentum'], weight_decay=config['weight_decay'])
# json file with experiment config CONFIG_FILE = './config_train_ebm/cifar10_ebm.json' ####################### # ## INITIAL SETUP ## # ####################### # load experiment config with open(CONFIG_FILE) as file: config = json.load(file) # directory for experiment results exp_dir = config['exp_dir'] + '_' + datetime.datetime.now().strftime( '%d-%m-%Y_%I-%M-%S_%p') + '_/' # setup folders, save code, set seed and get device setup_exp(exp_dir, config['seed'], ['checkpoints', 'shortrun', 'longrun', 'plots'], ['train_ebm.py', 'nets.py', 'utils.py', CONFIG_FILE]) print('Setting up network and optimizer...') # set up network ebm = EBM().cuda() # set up adam optimizer for first learning phase optim = t.optim.Adam(ebm.parameters(), lr=config['lr_adam']) print('Processing data...') # set up training data data = import_data(config['data_type'], True, False)[0] q = next(iter(DataLoader(data, len(data), num_workers=0)))[0].cuda() if config['shortrun_init'] == 'persistent': # initialize persistent images from noise (one persistent image for each data image)