def get(self): self.driver.get(self.dic_url[self.sid]) if self.sid == 'SWGS': try: self.driver.switch_to_alert() alert = WebDriverWait(self.driver, 1).until(EC.alert_is_present()) alert.accept() except NoAlertPresentException: pass finally: while True: try: WebDriverWait(self.driver, 0).until(EC.element_to_be_clickable((By.ID, 'bodyBlock'))) except: break try: WebDriverWait(self.driver, 60).until(EC.presence_of_element_located((By.ID, 'mdi01_subWindow0_iframe'))) finally: print('Page is ready!') self.driver.execute_script('''top.document.title = "(FOR AUTO TEST TOOL)"''') # Recorder 선언 self.recorder = Recorder(self.driver) self.recorder.addEventListener()
def do_client(idx, args): dataset = load_dataset('wmt14ende', splits=('test')) headers = {"Content-type": "application/json"} url = "http://127.0.0.1:9292/transformer/prediction" batch = [] sample = 0 f = open(args.output_file, "w") if args.profile: recorder = Recorder(args.infer_batch_size, args.model_name) recorder.tic() for sequence in dataset: sample += 1 batch.append(sequence[args.src_lang]) if len(batch) < args.infer_batch_size and sample != len(dataset): continue data = {"feed": [{"src_word": batch}], "fetch": ["finished_sequence"]} r = requests.post(url=url, headers=headers, data=json.dumps(data)) if r is not None: print("Status: ", r) if args.profile: recorder.toc(samples=len(batch)) else: for seq in r.json()["result"]["finished_sequence"]: f.write(seq[0] + "\n") batch = [] if args.profile: recorder.tic() f.close() if args.profile: recorder.report() return [[recorder.infer_time]]
def __init__(self, task, train_exp_dir, ite, logdir=None): self.task = task self.policy = LoadPolicy('../utils/models/{}/{}'.format(task, train_exp_dir), ite) self.env = CrossroadEnd2end(training_task=self.task, mode='testing') self.model = EnvironmentModel(self.task, mode='selecting') self.recorder = Recorder() self.episode_counter = -1 self.step_counter = -1 self.obs = None self.stg = MultiPathGenerator() self.step_timer = TimerStat() self.ss_timer = TimerStat() self.logdir = logdir if self.logdir is not None: config = dict(task=task, train_exp_dir=train_exp_dir, ite=ite) with open(self.logdir + '/config.json', 'w', encoding='utf-8') as f: json.dump(config, f, ensure_ascii=False, indent=4) self.fig = plt.figure(figsize=(8, 8)) plt.ion() self.hist_posi = [] self.old_index = 0 self.path_list = self.stg.generate_path(self.task) # ------------------build graph for tf.function in advance----------------------- for i in range(3): obs = self.env.reset() obs = tf.convert_to_tensor(obs[np.newaxis, :], dtype=tf.float32) self.is_safe(obs, i) obs = self.env.reset() obs_with_specific_shape = np.tile(obs, (3, 1)) self.policy.run_batch(obs_with_specific_shape) self.policy.obj_value_batch(obs_with_specific_shape) # ------------------build graph for tf.function in advance----------------------- self.reset()
def generate_recorder(self, logger2file, graph): """ create model/log/data dictionary and define writer to record training data. """ self.check_or_create(self.cp_dir, 'checkpoints') self.check_or_create(self.log_dir, 'logs(summaries)') self.check_or_create(self.excel_dir, 'excel') self.recorder = Recorder(log_dir=self.log_dir, excel_dir=self.excel_dir, logger2file=logger2file, graph=graph)
def start(): global recorder, logger, ConfigFile if recorder is None or recorder.is_stopped(): logger.log('Creating new PCRC recorder') try: recorder = Recorder(ConfigFile, TranslationFolder) except YggdrasilError as e: logger.error(e) return ret = recorder.start() logger.log('Recorder started, success = {}'.format(ret)) else: logger.warn('Recorder is running, ignore')
def run(self, category, im_list, output_fn="tmp.txt"): # Load checkpoint checkpoint_dir = "{}/{}/{}".format(self.main_dir, category, self.lr) checkpoint, _ = get_latest_checkpoint(checkpoint_dir) disc = Discriminator(lr=self.lr, checkpoint=checkpoint) output_dir = join(self.main_dir, "predictions") output_path = join(output_dir, output_fn) recorder = Recorder(output_path, restart=False) for im in im_list: if recorder.contains(im): print im, recorder.get(im), "Done." continue img, _ = self.datasource.get_image(im) gt, _ = self.datasource.get_ground_truth(im, one_hot=True) ap, _ = self.datasource.get_all_prob(im) pr_prob = disc.predict(img, ap, args.category) gt_prob = disc.predict(img, gt, args.category) print "{} {} {}".format(im, pr_prob, gt_prob) recorder.save(im, [pr_prob, gt_prob]) recorder.write() return output_path
def train( _net, _train_loader, _optimizer, _criterion, _device = 'cpu', _recorder: Recorder = None, _weight_quantization_error_collection = None, _input_quantization_error_collection = None, _weight_bit_allocation_collection = None, _input_bit_allocation_collection = None ): _net.train() _train_loss = 0 _correct = 0 _total = 0 for batch_idx, (inputs, targets) in enumerate(_train_loader): inputs, targets = inputs.to(_device), targets.to(_device) _optimizer.zero_grad() outputs = _net(inputs) losses = _criterion(outputs, targets) losses.backward() _optimizer.step() _train_loss += losses.data.item() _, predicted = torch.max(outputs.data, 1) _total += targets.size(0) _correct += predicted.eq(targets.data).cpu().sum().item() progress_bar( batch_idx, len(_train_loader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)' % (_train_loss / (batch_idx + 1), 100. * _correct / _total, _correct, _total) ) if _recorder is not None: _recorder.update(loss=losses.data.item(), acc=[_correct / _total], batch_size=inputs.size(0), is_train=True) if _weight_quantization_error_collection and _input_quantization_error_collection is not None: for name, layer in _net.quantized_layer_collections.items(): _weight_quantization_error = torch.abs(layer.quantized_weight - layer.pre_quantized_weight).mean().item() _input_quantization_error = torch.abs(layer.quantized_input - layer.pre_quantized_input).mean().item() _weight_quantization_error_collection[name].write('%.8e\n' % _weight_quantization_error) _input_quantization_error_collection[name].write('%.8e\n' % _input_quantization_error) if _weight_bit_allocation_collection and _input_bit_allocation_collection is not None: for name, layer in _net.quantized_layer_collections.items(): _weight_bit_allocation_collection[name].write('%.2f\n' % (torch.abs(layer.quantized_weight_bit).mean().item())) _input_bit_allocation_collection[name].write('%.2f\n' % (torch.abs(layer.quantized_input_bit).mean().item())) return _train_loss / (len(_train_loader)), _correct / _total
def __init__(self, task): self.task = task if self.task == 'left': self.policy = LoadPolicy('G:\\env_build\\utils\\models\\left', 100000) elif self.task == 'right': self.policy = LoadPolicy('G:\\env_build\\utils\\models\\right', 145000) elif self.task == 'straight': self.policy = LoadPolicy('G:\\env_build\\utils\\models\\straight', 95000) self.horizon = 25 self.num_future_data = 0 self.env = CrossroadEnd2end(training_task=self.task, num_future_data=self.num_future_data) self.model = EnvironmentModel(self.task) self.obs = self.env.reset() self.stg = StaticTrajectoryGenerator_origin(mode='static_traj') self.data2plot = [] self.mpc_cal_timer = TimerStat() self.adp_cal_timer = TimerStat() self.recorder = Recorder()
def __init__(self, task, logdir=None): self.task = task if self.task == 'left': self.policy = LoadPolicy('../utils/models/left', 100000) elif self.task == 'right': self.policy = LoadPolicy('../utils/models/right', 145000) elif self.task == 'straight': self.policy = LoadPolicy('../utils/models/straight', 95000) self.env = CrossroadEnd2end(training_task=self.task) self.model = EnvironmentModel(self.task, mode='selecting') self.recorder = Recorder() self.episode_counter = -1 self.step_counter = -1 self.obs = None self.stg = None self.step_timer = TimerStat() self.ss_timer = TimerStat() self.logdir = logdir self.fig = plt.figure(figsize=(8, 8)) plt.ion() self.hist_posi = [] self.reset()
def __init__(self, task, train_exp_dir, ite, logdir=None): self.task = task self.policy = LoadPolicy( '../utils/models/{}/{}'.format(task, train_exp_dir), ite) self.env = CrossroadEnd2end(training_task=self.task) self.model = EnvironmentModel(self.task, mode='selecting') self.recorder = Recorder() self.episode_counter = -1 self.step_counter = -1 self.obs = None self.stg = None self.step_timer = TimerStat() self.ss_timer = TimerStat() self.logdir = logdir if self.logdir is not None: config = dict(task=task, train_exp_dir=train_exp_dir, ite=ite) with open(self.logdir + '/config.json', 'w', encoding='utf-8') as f: json.dump(config, f, ensure_ascii=False, indent=4) self.fig = plt.figure(figsize=(8, 8)) plt.ion() self.hist_posi = [] self.reset()
def create_predictor(cls, args, config=None, profile=False, model_name=None): if config is None: config = inference.Config( os.path.join(args.inference_model_dir, "transformer.pdmodel"), os.path.join(args.inference_model_dir, "transformer.pdiparams")) if args.device == "gpu": config.enable_use_gpu(100, 0) elif args.device == "xpu": config.enable_xpu(100) else: # CPU config.disable_gpu() if args.use_mkl: config.enable_mkldnn() config.set_cpu_math_library_num_threads(args.threads) # Use ZeroCopy. config.switch_use_feed_fetch_ops(False) if profile: recorder = Recorder(config, args.infer_batch_size, model_name) else: recorder = None predictor = inference.create_predictor(config) input_handles = [ predictor.get_input_handle(name) for name in predictor.get_input_names() ] output_handles = [ predictor.get_input_handle(name) for name in predictor.get_output_names() ] return cls(predictor, input_handles, output_handles, recorder)
def setup_recorder(params): recorder = Recorder() # This is for early stopping, currectly I did not use it recorder.bad_counter = 0 # start from 0 recorder.estop = False recorder.lidx = -1 # local data index recorder.step = 0 # global step, start from 0 recorder.epoch = 1 # epoch number, start from 1 recorder.history_scores = [] recorder.valid_script_scores = [] # trying to load saved recorder record_path = os.path.join(params.output_dir, "record.json") record_path = os.path.abspath(record_path) if tf.gfile.Exists(record_path): recorder.load_from_json(record_path) params.add_hparam('recorder', recorder) return params
if use_cuda: net.cuda() # net = torch.nn.DataParallel(net, device_ids=range(torch.cuda.device_count())) cudnn.benchmark = True # --- # Begin Training # --- ascent_count = 0 min_train_loss = 1e9 criterion = nn.CrossEntropyLoss() max_training_epoch = args.max_epoch # Initialize recorder for general training recorder = Recorder(SummaryPath=save_root) recorder.write_arguments([args]) # Initialize recorder for threshold weight_quantization_error_recorder_collection = {} input_quantization_error_recorder_collection = {} weight_bit_allocation_collection = {} input_bit_allocation_collection = {} for name, layer in net.quantized_layer_collections.items(): if not os.path.exists('%s/%s' % (save_root, name)): os.makedirs('%s/%s' % (save_root, name)) weight_quantization_error_recorder_collection[name] = open('%s/%s/weight_quantization_error.txt' % (save_root, name), 'a+') input_quantization_error_recorder_collection[name] = open('%s/%s/input_quantization_error.txt' % (save_root, name), 'a+') weight_bit_allocation_collection[name] = open('%s/%s/weight_bit_allocation.txt' % (save_root, name), 'a+') input_bit_allocation_collection[name] = open('%s/%s/input_bit_allocation.txt' % (save_root, name), 'a+') for epoch in range(start_epoch, start_epoch + max_training_epoch):
import wave, audioop, time from aip import AipSpeech from utils.recorder import Recorder from utils import gmm_vad import threading UPLOAD_PATH = os.path.join(basedir, 'static/uploads/') VOICES_PATH = os.path.join(UPLOAD_PATH, 'voices/') TEMP_PATH = os.path.join(UPLOAD_PATH, 'temps/') ALGORITHM_PATH = os.path.join(UPLOAD_PATH, 'algorithms') MODELS_PATH = os.path.join(UPLOAD_PATH, 'models') sys.path.append(ALGORITHM_PATH) sys.path.append(MODELS_PATH) started_list = [] rec = Recorder() class TokenResource(Resource): # decorators = [auth.login_required] def get(self): token = g.user.generate_auth_token() return jsonify({'token': token.decode('ascii')}) class LoginResource(Resource): def post(self): username = request.json['username'] password = request.json['password'] result = {} if (self.verify_auth(username, password)):
from utils.recorder import Recorder from sorting.merge_sort import merge_sort from sorting.merge_sort_iterative import merge_sort_iter from sorting.quick_sort import quick_sort from sorting.quick_sort_3way import quick_sort_3way from sorting.selection_sort import selection_sort from sorting.insertion_sort import insert_sort_optimized from sorting.insertion_sort import insert_sort import numpy as np from time import time from threading import Thread if __name__ == '__main__': s = time() recorder = Recorder() start, end = 2, 6 for x in range(start, end): data = list(np.random.rand(np.power(10, x))) p1 = Thread(target=recorder.execute, args=(selection_sort, data)) p2 = Thread(target=recorder.execute, args=(insert_sort, data)) p3 = Thread(target=recorder.execute, args=(insert_sort_optimized, data)) p4 = Thread(target=recorder.execute, args=(merge_sort, data, [0, len(data) - 1])) p5 = Thread(target=recorder.execute, args=(merge_sort_iter, data)) p6 = Thread(target=recorder.execute, args=(quick_sort, data, [0, len(data) - 1]))
# Initial Recorder # #################### if args.exp_spec is not '': SummaryPath += ('-' + args.exp_spec) print('Save to %s' % SummaryPath) if os.path.exists(SummaryPath): print('Record exist, remove') input() shutil.rmtree(SummaryPath) os.makedirs(SummaryPath) else: os.makedirs(SummaryPath) recorder = Recorder(SummaryPath=SummaryPath, dataset_name=dataset_name) ################## # Begin Training # ################## meta_grad_dict = dict() for epoch in range(MAX_EPOCH): if recorder.stop: break print('\nEpoch: %d, lr: %e' % (epoch, optimizee.param_groups[0]['lr'])) net.train() end = time.time() recorder.reset_performance()
def plot_data(logdir, i): recorder = Recorder() recorder.load(logdir) recorder.plot_ith_episode_curves(i)
class Base: def __init__(self, *args, **kwargs): ''' inputs: a_dim: action spaces is_continuous: action type, refer to whether this control problem is continuous(True) or discrete(False) base_dir: the directory that store data, like model, logs, and other data ''' super().__init__() base_dir = kwargs.get('base_dir') tf_dtype = str(kwargs.get('tf_dtype')) tf.random.set_seed(int(kwargs.get('seed', 0))) self.device = get_device() self.logger2file = bool(kwargs.get('logger2file', False)) tf.keras.backend.set_floatx(tf_dtype) self.cp_dir, self.log_dir, self.excel_dir = [ os.path.join(base_dir, i) for i in ['model', 'log', 'excel'] ] self.global_step = tf.Variable( 0, name="global_step", trainable=False, dtype=tf.int64 ) # in TF 2.x must be tf.int64, because function set_step need args to be tf.int64. self.cast = self._cast(dtype=tf_dtype) def _cast(self, dtype='float32'): ''' TODO: Annotation ''' if dtype == 'float32': func = cast2float32 self._data_type = tf.float32 elif dtype == 'float64': self._data_type = tf.float64 func = cast2float64 else: raise Exception('Cast to this type has not been implemented.') def inner(*args, **kwargs): with tf.device(self.device): return func(*args, **kwargs) return inner def data_convert(self, data): ''' TODO: Annotation ''' return tf.convert_to_tensor(data, dtype=self._data_type) def get_init_train_step(self): """ get the initial training step. use for continue train from last training step. """ if os.path.exists(os.path.join(self.cp_dir, 'checkpoint')): return int(tf.train.latest_checkpoint(self.cp_dir).split('-')[-1]) else: return 0 def get_init_training_info(self): ''' TODO: Annotation ''' return self.recorder.get_training_info() def generate_recorder(self, kwargs): """ create model/log/data dictionary and define writer to record training data. """ self.check_or_create(self.cp_dir, 'checkpoints') self.check_or_create(self.log_dir, 'logs(summaries)') self.check_or_create(self.excel_dir, 'excel') self.recorder = Recorder(kwargs, cp_dir=self.cp_dir, log_dir=self.log_dir, excel_dir=self.excel_dir, logger2file=self.logger2file) def init_or_restore(self, base_dir): """ check whether chekpoint and model be within cp_dir, if in it, restore otherwise initialize randomly. """ cp_dir = os.path.join(base_dir, 'model') if os.path.exists(os.path.join(cp_dir, 'checkpoint')): try: self.recorder.checkpoint.restore( tf.train.latest_checkpoint( cp_dir)).expect_partial() # 从指定路径导入模型 except: self.recorder.logger.error( f'restore model from {cp_dir} FAILED.') raise Exception(f'restore model from {cp_dir} FAILED.') else: self.recorder.logger.info( f'restore model from {cp_dir} SUCCUESS.') else: self.recorder.checkpoint.restore( self.recorder.saver.latest_checkpoint).expect_partial( ) # 从本模型目录载入模型,断点续训 self.recorder.logger.info( f'restore model from {self.recorder.saver.latest_checkpoint} SUCCUESS.' ) self.recorder.logger.info('initialize model SUCCUESS.') def save_checkpoint(self, **kwargs): """ save the training model """ train_step = int(kwargs.get('train_step', 0)) self.recorder.saver.save(checkpoint_number=train_step) logger.info(f'Save checkpoint success. Training step: {train_step}') self.recorder.write_training_info(kwargs) def writer_summary(self, global_step, **kargs): """ record the data used to show in the tensorboard """ self.recorder.writer.set_as_default() tf.summary.experimental.set_step(global_step) for i in [{ 'tag': 'AGENT/' + key, 'value': kargs[key] } for key in kargs]: tf.summary.scalar(i['tag'], i['value']) self.recorder.writer.flush() def write_training_summaries(self, global_step, summaries: dict): ''' write tf summaries showing in tensorboard. ''' self.recorder.writer.set_as_default() tf.summary.experimental.set_step(global_step) for key, value in summaries.items(): tf.summary.scalar(key, value) self.recorder.writer.flush() def check_or_create(self, dicpath, name=''): """ check dictionary whether existing, if not then create it. """ if not os.path.exists(dicpath): os.makedirs(dicpath) logger.info(''.join([f'create {name} directionary :', dicpath])) def save_weights(self, path): """ save trained weights :return: None """ # self.net.save_weights(os.path.join(path, 'net.ckpt')) pass def load_weights(self, path): """ load trained weights :return: None """ # self.net.load_weights(os.path.join(path, 'net.ckpt')) pass def close(self): """ end training, and export the training model """ pass def get_global_step(self): """ get the current training step. """ return self.global_step def set_global_step(self, num): """ set the start training step. """ self.global_step.assign(num) def show_logo(self): raise NotImplementedError
def main(): if sys.platform.startswith('win'): # Add the _win_handler function to the windows console's handler function list win32api.SetConsoleCtrlHandler(_win_handler, True) if os.path.exists( os.path.join(config_file.config['config_file'], 'config.yaml')): config = sth.load_config(config_file.config['config_file']) else: config = config_file.config print(f'load config from config.') hyper_config = config['hyper parameters'] train_config = config['train config'] record_config = config['record config'] basic_dir = record_config['basic_dir'] last_name = record_config['project_name'] + '/' \ + record_config['remark'] \ + record_config['run_id'] cp_dir = record_config['checkpoint_basic_dir'] + last_name cp_file = cp_dir + '/rb' log_dir = record_config['log_basic_dir'] + last_name excel_dir = record_config['excel_basic_dir'] + last_name config_dir = record_config['config_basic_dir'] + last_name sth.check_or_create(basic_dir, 'basic') sth.check_or_create(cp_dir, 'checkpoints') sth.check_or_create(log_dir, 'logs(summaries)') sth.check_or_create(excel_dir, 'excel') sth.check_or_create(config_dir, 'config') logger = create_logger( name='logger', console_level=logging.INFO, console_format='%(levelname)s : %(message)s', logger2file=record_config['logger2file'], file_name=log_dir + '\log.txt', file_level=logging.WARNING, file_format= '%(lineno)d - %(asctime)s - %(module)s - %(funcName)s - %(levelname)s - %(message)s' ) if train_config['train']: sth.save_config(config_dir, config) if train_config['unity_mode']: env = UnityEnvironment() else: env = UnityEnvironment( file_name=train_config['unity_file'], no_graphics=True if train_config['train'] else False, base_port=train_config['port']) brain_name = env.external_brain_names[0] brain = env.brains[brain_name] # set the memory use proportion of GPU tf_config = tf.ConfigProto() tf_config.gpu_options.allow_growth = True # tf_config.gpu_options.per_process_gpu_memory_fraction = 0.5 tf.reset_default_graph() graph = tf.Graph() with graph.as_default() as g: with tf.Session(graph=g, config=tf_config) as sess: logger.info('Algorithm: {0}'.format( train_config['algorithm'].name)) if train_config['algorithm'] == config_file.algorithms.ppo_sep_ac: from ppo.ppo_base import PPO_SEP model = PPO_SEP(sess=sess, s_dim=brain.vector_observation_space_size, a_counts=brain.vector_action_space_size[0], hyper_config=hyper_config) logger.info('PPO_SEP initialize success.') elif train_config['algorithm'] == config_file.algorithms.ppo_com: from ppo.ppo_base import PPO_COM model = PPO_COM(sess=sess, s_dim=brain.vector_observation_space_size, a_counts=brain.vector_action_space_size[0], hyper_config=hyper_config) logger.info('PPO_COM initialize success.') elif train_config['algorithm'] == config_file.algorithms.sac: from sac.sac import SAC model = SAC(sess=sess, s_dim=brain.vector_observation_space_size, a_counts=brain.vector_action_space_size[0], hyper_config=hyper_config) logger.info('SAC initialize success.') elif train_config['algorithm'] == config_file.algorithms.sac_no_v: from sac.sac_no_v import SAC_NO_V model = SAC_NO_V(sess=sess, s_dim=brain.vector_observation_space_size, a_counts=brain.vector_action_space_size[0], hyper_config=hyper_config) logger.info('SAC_NO_V initialize success.') elif train_config['algorithm'] == config_file.algorithms.ddpg: from ddpg.ddpg import DDPG model = DDPG(sess=sess, s_dim=brain.vector_observation_space_size, a_counts=brain.vector_action_space_size[0], hyper_config=hyper_config) logger.info('DDPG initialize success.') elif train_config['algorithm'] == config_file.algorithms.td3: from td3.td3 import TD3 model = TD3(sess=sess, s_dim=brain.vector_observation_space_size, a_counts=brain.vector_action_space_size[0], hyper_config=hyper_config) logger.info('TD3 initialize success.') recorder = Recorder(log_dir, excel_dir, record_config, logger, max_to_keep=5, pad_step_number=True, graph=g) episode = init_or_restore(cp_dir, sess, recorder, cp_file) try: if train_config['train']: train_OnPolicy( sess=sess, env=env, brain_name=brain_name, begin_episode=episode, model=model, recorder=recorder, cp_file=cp_file, hyper_config=hyper_config, train_config=train_config) if not train_config[ 'use_replay_buffer'] else train_OffPolicy( sess=sess, env=env, brain_name=brain_name, begin_episode=episode, model=model, recorder=recorder, cp_file=cp_file, hyper_config=hyper_config, train_config=train_config) tf.train.write_graph(g, cp_dir, 'raw_graph_def.pb', as_text=False) export_model(cp_dir, g) else: inference(env, brain_name, model, train_config) except Exception as e: logger.error(e) finally: env.close() recorder.close() sys.exit()
def __init__(self, task_name, task_type = 'prune', optimizer_type = 'adam', save_root = None, SummaryPath = None, use_cuda = True, **kwargs): self.task_name = task_name self.task_type = task_type # prune, soft-quantize self.model_name, self.dataset_name = task_name.split('-') self.ratio = 'sample' if self.dataset_name in ['CIFARS'] else -1 ####### # Net # ####### if task_type == 'prune': if self.model_name == 'ResNet20': if self.dataset_name in ['CIFAR10', 'CIFARS']: self.net = resnet20_cifar() elif self.dataset_name == 'STL10': self.net = resnet20_stl() else: raise NotImplementedError elif self.model_name == 'ResNet32': if self.dataset_name in ['CIFAR10', 'CIFARS']: self.net = resnet32_cifar() elif self.dataset_name == 'STL10': self.net = resnet32_stl() else: raise NotImplementedError elif self.model_name == 'ResNet56': if self.dataset_name in ['CIFAR10', 'CIFARS']: self.net = resnet56_cifar() elif self.dataset_name == 'CIFAR100': self.net = resnet56_cifar(num_classes=100) elif self.dataset_name == 'STL10': self.net = resnet56_stl() else: raise NotImplementedError elif self.model_name == 'ResNet18': if self.dataset_name == 'ImageNet': self.net = resnet18() else: raise NotImplementedError elif self.model_name == 'vgg11': self.net = vgg11() if self.dataset_name == 'CIFAR10' else vgg11_stl10() else: print(self.model_name, self.dataset_name) raise NotImplementedError elif task_type == 'soft-quantize': if self.model_name == 'ResNet20': if self.dataset_name in ['CIFAR10', 'CIFARS']: self.net = soft_quantized_resnet20_cifar() elif self.dataset_name in ['STL10']: self.net = soft_quantized_resnet20_stl() else: raise NotImplementedError else: raise ('Task type not defined.') self.meta_opt_flag = True # True for enabling meta leraning ############## # Meta Prune # ############## self.mask_dict = dict() self.meta_grad_dict = dict() self.meta_hidden_state_dict = dict() ###################### # Meta Soft Quantize # ###################### self.quantized = 0 # Quantized type self.alpha_dict = dict() self.alpha_hidden_dict = dict() self.sq_rate = 0 self.s_rate = 0 self.q_rate = 0 ########## # Record # ########## self.dataset_type = 'large' if self.dataset_name in ['ImageNet'] else 'small' self.SummaryPath = SummaryPath self.save_root = save_root self.recorder = Recorder(self.SummaryPath, self.dataset_name, self.task_name) #################### # Load Pre-trained # #################### self.pretrain_path = '%s/%s-pretrain.pth' %(self.save_root, self.task_name) self.net.load_state_dict(torch.load(self.pretrain_path)) print('Load pre-trained model from %s' %self.pretrain_path) if use_cuda: self.net.cuda() # Optimizer for this task if optimizer_type in ['Adam', 'adam']: self.optimizer = Adam(self.net.parameters(), lr=1e-3) else: self.optimizer = SGD(self.net.parameters()) if self.dataset_name == 'ImageNet': try: self.train_loader = get_lmdb_imagenet('train', 128) self.test_loader = get_lmdb_imagenet('test', 100) except: self.train_loader = get_dataloader(self.dataset_name, 'train', 128) self.test_loader = get_dataloader(self.dataset_name, 'test', 100) else: self.train_loader = get_dataloader(self.dataset_name, 'train', 128, ratio=self.ratio) self.test_loader = get_dataloader(self.dataset_name, 'test', 128) self.iter_train_loader = yielder(self.train_loader)
class Base(object): _version_number_ = 2 def __init__(self, a_dim_or_list, action_type, base_dir): self.graph = tf.Graph() gpu_options = tf.GPUOptions(allow_growth=True) self.sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options), graph=self.graph) self.cp_dir, self.log_dir, self.excel_dir = [ os.path.join(base_dir, i) for i in ['model', 'log', 'excel'] ] self.action_type = action_type self.a_counts = int(np.array(a_dim_or_list).prod()) self.possible_output_nodes = [ 'action', 'version_number', 'is_continuous_control', 'action_output_shape', 'memory_size' ] with self.graph.as_default(): tf.set_random_seed(-1) # variables initialization consistent. tf.Variable(1 if action_type == 'continuous' else 0, name='is_continuous_control', trainable=False, dtype=tf.int32) # continuous 1 discrete 0 tf.Variable(self.a_counts, name="action_output_shape", trainable=False, dtype=tf.int32) tf.Variable(self._version_number_, name='version_number', trainable=False, dtype=tf.int32) tf.Variable(0, name="memory_size", trainable=False, dtype=tf.int32) self.episode = tf.Variable(tf.constant(0)) self.global_step = tf.Variable(0, name="global_step", trainable=False, dtype=tf.int64) def get_init_episode(self): """ get the initial training step. use for continue train from last training step. """ if os.path.exists(os.path.join(self.cp_dir, 'checkpoint')): return int(tf.train.latest_checkpoint(self.cp_dir).split('-')[-1]) else: return 0 def generate_recorder(self, logger2file, graph): """ create model/log/data dictionary and define writer to record training data. """ self.check_or_create(self.cp_dir, 'checkpoints') self.check_or_create(self.log_dir, 'logs(summaries)') self.check_or_create(self.excel_dir, 'excel') self.recorder = Recorder(log_dir=self.log_dir, excel_dir=self.excel_dir, logger2file=logger2file, graph=graph) def init_or_restore(self, base_dir): """ check whether chekpoint and model be within cp_dir, if in it, restore otherwise initialize randomly. """ cp_dir = os.path.join(base_dir, 'model') with self.graph.as_default(): if os.path.exists(os.path.join(cp_dir, 'checkpoint')): try: self.recorder.saver.restore( self.sess, tf.train.latest_checkpoint(cp_dir)) except: self.recorder.logger.error( 'restore model from checkpoint FAILED.') else: self.recorder.logger.info( 'restore model from checkpoint SUCCUESS.') else: self.sess.run(tf.global_variables_initializer()) self.recorder.logger.info('initialize model SUCCUESS.') def save_checkpoint(self, global_step): """ save the training model """ self.recorder.saver.save(self.sess, os.path.join(self.cp_dir, 'rb'), global_step=global_step, write_meta_graph=False) def writer_summary(self, global_step, **kargs): """ record the data used to show in the tensorboard """ self.recorder.writer_summary(x=global_step, ys=[{ 'tag': 'MAIN/' + key, 'value': kargs[key] } for key in kargs]) def _process_graph(self): """ Gets the list of the output nodes present in the graph for inference :return: list of node names """ all_nodes = [x.name for x in self.graph.as_graph_def().node] nodes = [x for x in all_nodes if x in self.possible_output_nodes] return nodes def export_model(self): """ Exports latest saved model to .nn format for Unity embedding. """ tf.train.write_graph(self.graph, self.cp_dir, 'raw_graph_def.pb', as_text=False) with self.graph.as_default(): target_nodes = ','.join(self._process_graph()) freeze_graph.freeze_graph( input_graph=os.path.join(self.cp_dir, 'raw_graph_def.pb'), input_binary=True, input_checkpoint=tf.train.latest_checkpoint(self.cp_dir), output_node_names=target_nodes, output_graph=(os.path.join(self.cp_dir, 'frozen_graph_def.pb')), # output_graph=(os.path.join(self.cp_dir,'model.bytes')), clear_devices=True, initializer_nodes='', input_saver='', restore_op_name='save/restore_all', filename_tensor_name='save/Const:0') tf2bc.convert(os.path.join(self.cp_dir, 'frozen_graph_def.pb'), os.path.join(self.cp_dir, 'model.nn')) def check_or_create(self, dicpath, name=''): """ check dictionary whether existing, if not then create it. """ if not os.path.exists(dicpath): os.makedirs(dicpath) print(f'create {name} directionary :', dicpath) def close(self): """ end training, and export the training model """ self.export_model() def get_global_step(self): """ get the current trianing step. """ return self.sess.run(self.global_step) def set_global_step(self, num): """ set the start training step. """ with self.graph.as_default(): self.global_step.load(num, self.sess)
def plot_data(epi_num, logdir): recorder = Recorder() recorder.load(logdir) recorder.plot_mpc_rl(epi_num)
eps=adam_epsilon) scheduler = get_linear_schedule_with_warmup( optimizer, num_warmup_steps=warmup_steps, num_training_steps=len(train_dataloader) * n_epoch) # ------- # Initialize Recorder # ------- SummaryPath = './Results/ALBERT-GLUE-%s/%s/%s/runs-%s-%d' % ( task_name.upper(), 'Prune' if args.prune else 'Quant', args.model_type, 'CR' if args.prune else 'bitW', int(100.0 * args.CR) if args.prune else args.bitW) if args.exp_spec is not None: SummaryPath += ('-' + args.exp_spec) recorder = Recorder(SummaryPath) if recorder is not None: recorder.write_arguments([args]) if args.first_eval: result = evaluate(task_name, model, eval_dataloader, model_type) # ['acc', 'f1', 'acc_f1'] print(result) # -------------- # Begin Training # -------------- for epoch_idx in range(n_epoch): print('Epoch: %d' % epoch_idx) model.train()
class HierarchicalMpc(object): def __init__(self, task): self.task = task if self.task == 'left': self.policy = LoadPolicy('G:\\env_build\\utils\\models\\left', 100000) elif self.task == 'right': self.policy = LoadPolicy('G:\\env_build\\utils\\models\\right', 145000) elif self.task == 'straight': self.policy = LoadPolicy('G:\\env_build\\utils\\models\\straight', 95000) self.horizon = 25 self.num_future_data = 0 self.env = CrossroadEnd2end(training_task=self.task, num_future_data=self.num_future_data) self.model = EnvironmentModel(self.task) self.obs = self.env.reset() self.stg = StaticTrajectoryGenerator_origin(mode='static_traj') self.data2plot = [] self.mpc_cal_timer = TimerStat() self.adp_cal_timer = TimerStat() self.recorder = Recorder() def reset(self): self.obs = self.env.reset() self.stg = StaticTrajectoryGenerator_origin(mode='static_traj') self.recorder.reset() self.recorder.save('.') self.data2plot = [] return self.obs def convert_vehs_to_abso(self, obs_rela): ego_infos, tracking_infos, veh_rela = obs_rela[:6], \ obs_rela[6:6 + 3 * (1 + self.num_future_data)],\ obs_rela[6 + 3 * (1 + self.num_future_data):] ego_vx, ego_vy, ego_r, ego_x, ego_y, ego_phi = ego_infos ego = np.array([ego_x, ego_y, 0, 0] * int(len(veh_rela) / 4), dtype=np.float32) vehs_abso = veh_rela + ego out = np.concatenate((ego_infos, tracking_infos, vehs_abso), axis=0) return out def step(self): traj_list, _ = self.stg.generate_traj(self.task, self.obs) ADP_traj_return_value, MPC_traj_return_value = [], [] action_total = [] state_total = [] with self.mpc_cal_timer: for ref_index, trajectory in enumerate(traj_list): mpc = ModelPredictiveControl(self.horizon, self.task, self.num_future_data, ref_index) state_all = np.array((list(self.obs[:6 + 3 * (1 + self.num_future_data)]) + [0, 0]) * self.horizon + list(self.obs[:6 + 3 * (1 + self.num_future_data)])).reshape((-1, 1)) state, control, state_all, g_all, cost = mpc.mpc_solver(list(self.convert_vehs_to_abso(self.obs)), state_all) state_total.append(state) if any(g_all < -1): print('optimization fail') mpc_action = np.array([0., -1.]) state_all = np.array((list(self.obs[:9]) + [0, 0]) * self.horizon + list(self.obs[:9])).reshape( (-1, 1)) else: state_all = np.array((list(self.obs[:9]) + [0, 0]) * self.horizon + list(self.obs[:9])).reshape( (-1, 1)) mpc_action = control[0] MPC_traj_return_value.append(-cost.squeeze().tolist()) action_total.append(mpc_action) MPC_traj_return_value = np.array(MPC_traj_return_value, dtype=np.float32) MPC_path_index = np.argmax(MPC_traj_return_value) MPC_action = action_total[MPC_path_index] with self.adp_cal_timer: for ref_index, trajectory in enumerate(traj_list): self.env.set_traj(trajectory) obs = self.env._get_obs()[np.newaxis, :] traj_value = self.policy.values(obs) ADP_traj_return_value.append(traj_value.numpy().squeeze().tolist()) ADP_traj_return_value = np.array(ADP_traj_return_value, dtype=np.float32)[:, 0] ADP_path_index = np.argmax(ADP_traj_return_value) if np.amax(ADP_traj_return_value) == np.amin(ADP_traj_return_value): ADP_path_index = MPC_path_index self.env.set_traj(traj_list[ADP_path_index]) self.obs_real = self.env._get_obs() ADP_action = self.policy.run(self.obs_real).numpy() self.recorder.record_compare(self.obs, ADP_action, MPC_action, self.adp_cal_timer.mean * 1000, self.mpc_cal_timer.mean * 1000, ADP_path_index, MPC_path_index, 'both') self.data2plot.append(dict(obs=self.obs, ADP_action=ADP_action, MPC_action=MPC_action, ADP_path_index=ADP_path_index, MPC_path_index=MPC_path_index, mpc_time=self.mpc_cal_timer.mean * 1000, )) self.obs, rew, done, _ = self.env.step(ADP_action) self.render(traj_list, ADP_traj_return_value, ADP_path_index, MPC_traj_return_value, MPC_path_index, method='ADP') state = state_total[MPC_path_index] plt.plot([state[i][3] for i in range(1, self.horizon - 1)], [state[i][4] for i in range(1, self.horizon - 1)], 'r*') plt.pause(0.001) return done def render(self, traj_list, ADP_traj_return_value, ADP_path_index, MPC_traj_return_value, MPC_path_index, method='ADP'): square_length = CROSSROAD_SIZE extension = 40 lane_width = LANE_WIDTH light_line_width = 3 dotted_line_style = '--' solid_line_style = '-' plt.cla() plt.title("Crossroad") ax = plt.axes(xlim=(-square_length / 2 - extension, square_length / 2 + extension), ylim=(-square_length / 2 - extension, square_length / 2 + extension)) plt.axis("equal") plt.axis('off') # ax.add_patch(plt.Rectangle((-square_length / 2, -square_length / 2), # square_length, square_length, edgecolor='black', facecolor='none')) ax.add_patch(plt.Rectangle((-square_length / 2 - extension, -square_length / 2 - extension), square_length + 2 * extension, square_length + 2 * extension, edgecolor='black', facecolor='none')) # ----------arrow-------------- plt.arrow(lane_width / 2, -square_length / 2 - 10, 0, 5, color='b') plt.arrow(lane_width / 2, -square_length / 2 - 10 + 5, -0.5, 0, color='b', head_width=1) plt.arrow(lane_width * 1.5, -square_length / 2 - 10, 0, 5, color='b', head_width=1) plt.arrow(lane_width * 2.5, -square_length / 2 - 10, 0, 5, color='b') plt.arrow(lane_width * 2.5, -square_length / 2 - 10 + 5, 0.5, 0, color='b', head_width=1) # ----------horizon-------------- plt.plot([-square_length / 2 - extension, -square_length / 2], [0, 0], color='black') plt.plot([square_length / 2 + extension, square_length / 2], [0, 0], color='black') # for i in range(1, LANE_NUMBER + 1): linestyle = dotted_line_style if i < LANE_NUMBER else solid_line_style plt.plot([-square_length / 2 - extension, -square_length / 2], [i * lane_width, i * lane_width], linestyle=linestyle, color='black') plt.plot([square_length / 2 + extension, square_length / 2], [i * lane_width, i * lane_width], linestyle=linestyle, color='black') plt.plot([-square_length / 2 - extension, -square_length / 2], [-i * lane_width, -i * lane_width], linestyle=linestyle, color='black') plt.plot([square_length / 2 + extension, square_length / 2], [-i * lane_width, -i * lane_width], linestyle=linestyle, color='black') # ----------vertical---------------- plt.plot([0, 0], [-square_length / 2 - extension, -square_length / 2], color='black') plt.plot([0, 0], [square_length / 2 + extension, square_length / 2], color='black') # for i in range(1, LANE_NUMBER + 1): linestyle = dotted_line_style if i < LANE_NUMBER else solid_line_style plt.plot([i * lane_width, i * lane_width], [-square_length / 2 - extension, -square_length / 2], linestyle=linestyle, color='black') plt.plot([i * lane_width, i * lane_width], [square_length / 2 + extension, square_length / 2], linestyle=linestyle, color='black') plt.plot([-i * lane_width, -i * lane_width], [-square_length / 2 - extension, -square_length / 2], linestyle=linestyle, color='black') plt.plot([-i * lane_width, -i * lane_width], [square_length / 2 + extension, square_length / 2], linestyle=linestyle, color='black') v_light = self.env.v_light if v_light == 0: v_color, h_color = 'green', 'red' elif v_light == 1: v_color, h_color = 'orange', 'red' elif v_light == 2: v_color, h_color = 'red', 'green' else: v_color, h_color = 'red', 'orange' plt.plot([0, (LANE_NUMBER - 1) * lane_width], [-square_length / 2, -square_length / 2], color=v_color, linewidth=light_line_width) plt.plot([(LANE_NUMBER - 1) * lane_width, LANE_NUMBER * lane_width], [-square_length / 2, -square_length / 2], color='green', linewidth=light_line_width) plt.plot([-LANE_NUMBER * lane_width, -(LANE_NUMBER - 1) * lane_width], [square_length / 2, square_length / 2], color='green', linewidth=light_line_width) plt.plot([-(LANE_NUMBER - 1) * lane_width, 0], [square_length / 2, square_length / 2], color=v_color, linewidth=light_line_width) plt.plot([-square_length / 2, -square_length / 2], [0, -(LANE_NUMBER - 1) * lane_width], color=h_color, linewidth=light_line_width) plt.plot([-square_length / 2, -square_length / 2], [-(LANE_NUMBER - 1) * lane_width, -LANE_NUMBER * lane_width], color='green', linewidth=light_line_width) plt.plot([square_length / 2, square_length / 2], [(LANE_NUMBER - 1) * lane_width, 0], color=h_color, linewidth=light_line_width) plt.plot([square_length / 2, square_length / 2], [LANE_NUMBER * lane_width, (LANE_NUMBER - 1) * lane_width], color='green', linewidth=light_line_width) # ----------Oblique-------------- plt.plot([LANE_NUMBER * lane_width, square_length / 2], [-square_length / 2, -LANE_NUMBER * lane_width], color='black') plt.plot([LANE_NUMBER * lane_width, square_length / 2], [square_length / 2, LANE_NUMBER * lane_width], color='black') plt.plot([-LANE_NUMBER * lane_width, -square_length / 2], [-square_length / 2, -LANE_NUMBER * lane_width], color='black') plt.plot([-LANE_NUMBER * lane_width, -square_length / 2], [square_length / 2, LANE_NUMBER * lane_width], color='black') def is_in_plot_area(x, y, tolerance=5): if -square_length / 2 - extension + tolerance < x < square_length / 2 + extension - tolerance and \ -square_length / 2 - extension + tolerance < y < square_length / 2 + extension - tolerance: return True else: return False def draw_rotate_rec(x, y, a, l, w, color, linestyle='-'): RU_x, RU_y, _ = rotate_coordination(l / 2, w / 2, 0, -a) RD_x, RD_y, _ = rotate_coordination(l / 2, -w / 2, 0, -a) LU_x, LU_y, _ = rotate_coordination(-l / 2, w / 2, 0, -a) LD_x, LD_y, _ = rotate_coordination(-l / 2, -w / 2, 0, -a) ax.plot([RU_x + x, RD_x + x], [RU_y + y, RD_y + y], color=color, linestyle=linestyle) ax.plot([RU_x + x, LU_x + x], [RU_y + y, LU_y + y], color=color, linestyle=linestyle) ax.plot([LD_x + x, RD_x + x], [LD_y + y, RD_y + y], color=color, linestyle=linestyle) ax.plot([LD_x + x, LU_x + x], [LD_y + y, LU_y + y], color=color, linestyle=linestyle) def plot_phi_line(x, y, phi, color): line_length = 5 x_forw, y_forw = x + line_length * cos(phi * pi / 180.), \ y + line_length * sin(phi * pi / 180.) plt.plot([x, x_forw], [y, y_forw], color=color, linewidth=0.5) # plot cars for veh in self.env.all_vehicles: veh_x = veh['x'] veh_y = veh['y'] veh_phi = veh['phi'] veh_l = veh['l'] veh_w = veh['w'] if is_in_plot_area(veh_x, veh_y): plot_phi_line(veh_x, veh_y, veh_phi, 'black') draw_rotate_rec(veh_x, veh_y, veh_phi, veh_l, veh_w, 'black') # plot_interested vehs # for mode, num in self.veh_mode_dict.items(): # for i in range(num): # veh = self.interested_vehs[mode][i] # veh_x = veh['x'] # veh_y = veh['y'] # veh_phi = veh['phi'] # veh_l = veh['l'] # veh_w = veh['w'] # task2color = {'left': 'b', 'straight': 'c', 'right': 'm'} # # if is_in_plot_area(veh_x, veh_y): # plot_phi_line(veh_x, veh_y, veh_phi, 'black') # task = MODE2TASK[mode] # color = task2color[task] # draw_rotate_rec(veh_x, veh_y, veh_phi, veh_l, veh_w, color, linestyle=':') ego_v_x = self.env.ego_dynamics['v_x'] ego_v_y = self.env.ego_dynamics['v_y'] ego_r = self.env.ego_dynamics['r'] ego_x = self.env.ego_dynamics['x'] ego_y = self.env.ego_dynamics['y'] ego_phi = self.env.ego_dynamics['phi'] ego_l = self.env.ego_dynamics['l'] ego_w = self.env.ego_dynamics['w'] ego_alpha_f = self.env.ego_dynamics['alpha_f'] ego_alpha_r = self.env.ego_dynamics['alpha_r'] alpha_f_bound = self.env.ego_dynamics['alpha_f_bound'] alpha_r_bound = self.env.ego_dynamics['alpha_r_bound'] r_bound = self.env.ego_dynamics['r_bound'] plot_phi_line(ego_x, ego_y, ego_phi, 'fuchsia') draw_rotate_rec(ego_x, ego_y, ego_phi, ego_l, ego_w, 'fuchsia') # plot future data tracking_info = self.obs[ self.env.ego_info_dim:self.env.ego_info_dim + self.env.per_tracking_info_dim * (self.env.num_future_data + 1)] future_path = tracking_info[self.env.per_tracking_info_dim:] for i in range(self.env.num_future_data): delta_x, delta_y, delta_phi = future_path[i * self.env.per_tracking_info_dim: (i + 1) * self.env.per_tracking_info_dim] path_x, path_y, path_phi = ego_x + delta_x, ego_y + delta_y, ego_phi - delta_phi plt.plot(path_x, path_y, 'g.') plot_phi_line(path_x, path_y, path_phi, 'g') delta_, _, _ = tracking_info[:3] indexs, points = self.env.ref_path.find_closest_point(np.array([ego_x], np.float32), np.array([ego_y], np.float32)) path_x, path_y, path_phi = points[0][0], points[1][0], points[2][0] # plt.plot(path_x, path_y, 'g.') delta_x, delta_y, delta_phi = ego_x - path_x, ego_y - path_y, ego_phi - path_phi # plot real time traj try: color = ['blue', 'coral', 'cyan'] for i, item in enumerate(traj_list): if method == 'ADP': if i == ADP_path_index: plt.plot(item.path[0], item.path[1], color=color[i]) else: plt.plot(item.path[0], item.path[1], color=color[i], alpha=0.3) indexs, points = item.find_closest_point(np.array([ego_x], np.float32), np.array([ego_y], np.float32)) path_x, path_y, path_phi = points[0][0], points[1][0], points[2][0] plt.plot(path_x, path_y, color=color[i]) elif method == 'MPC': if i == MPC_path_index: plt.plot(item.path[0], item.path[1], color=color[i]) else: plt.plot(item.path[0], item.path[1], color=color[i], alpha=0.3) indexs, points = item.find_closest_point(np.array([ego_x], np.float32), np.array([ego_y], np.float32)) path_x, path_y, path_phi = points[0][0], points[1][0], points[2][0] plt.plot(path_x, path_y, color=color[i]) except Exception: pass # plot ego dynamics text_x, text_y_start = -120, 60 ge = iter(range(0, 1000, 4)) plt.text(text_x, text_y_start - next(ge), 'ego_x: {:.2f}m'.format(ego_x)) plt.text(text_x, text_y_start - next(ge), 'ego_y: {:.2f}m'.format(ego_y)) plt.text(text_x, text_y_start - next(ge), 'path_x: {:.2f}m'.format(path_x)) plt.text(text_x, text_y_start - next(ge), 'path_y: {:.2f}m'.format(path_y)) plt.text(text_x, text_y_start - next(ge), 'delta_: {:.2f}m'.format(delta_)) plt.text(text_x, text_y_start - next(ge), 'delta_x: {:.2f}m'.format(delta_x)) plt.text(text_x, text_y_start - next(ge), 'delta_y: {:.2f}m'.format(delta_y)) plt.text(text_x, text_y_start - next(ge), r'ego_phi: ${:.2f}\degree$'.format(ego_phi)) plt.text(text_x, text_y_start - next(ge), r'path_phi: ${:.2f}\degree$'.format(path_phi)) plt.text(text_x, text_y_start - next(ge), r'delta_phi: ${:.2f}\degree$'.format(delta_phi)) plt.text(text_x, text_y_start - next(ge), 'v_x: {:.2f}m/s'.format(ego_v_x)) plt.text(text_x, text_y_start - next(ge), 'exp_v: {:.2f}m/s'.format(self.env.exp_v)) plt.text(text_x, text_y_start - next(ge), 'v_y: {:.2f}m/s'.format(ego_v_y)) plt.text(text_x, text_y_start - next(ge), 'yaw_rate: {:.2f}rad/s'.format(ego_r)) plt.text(text_x, text_y_start - next(ge), 'yaw_rate bound: [{:.2f}, {:.2f}]'.format(-r_bound, r_bound)) plt.text(text_x, text_y_start - next(ge), r'$\alpha_f$: {:.2f} rad'.format(ego_alpha_f)) plt.text(text_x, text_y_start - next(ge), r'$\alpha_f$ bound: [{:.2f}, {:.2f}] '.format(-alpha_f_bound, alpha_f_bound)) plt.text(text_x, text_y_start - next(ge), r'$\alpha_r$: {:.2f} rad'.format(ego_alpha_r)) plt.text(text_x, text_y_start - next(ge), r'$\alpha_r$ bound: [{:.2f}, {:.2f}] '.format(-alpha_r_bound, alpha_r_bound)) if self.env.action is not None: steer, a_x = self.env.action[0], self.env.action[1] plt.text(text_x, text_y_start - next(ge), r'steer: {:.2f}rad (${:.2f}\degree$)'.format(steer, steer * 180 / np.pi)) plt.text(text_x, text_y_start - next(ge), 'a_x: {:.2f}m/s^2'.format(a_x)) text_x, text_y_start = 70, 60 ge = iter(range(0, 1000, 4)) # done info plt.text(text_x, text_y_start - next(ge), 'done info: {}'.format(self.env.done_type)) # reward info if self.env.reward_info is not None: for key, val in self.env.reward_info.items(): plt.text(text_x, text_y_start - next(ge), '{}: {:.4f}'.format(key, val)) # indicator for Atrajectory selection text_x, text_y_start = 18, -70 ge = iter(range(0, 1000, 6)) plt.text(text_x+10, text_y_start - next(ge), 'ADP', fontsize=14, color='r', fontstyle='italic') if ADP_traj_return_value is not None: for i, value in enumerate(ADP_traj_return_value): if i == ADP_path_index: plt.text(text_x, text_y_start - next(ge), 'Path cost={:.4f}'.format(value), fontsize=14, color=color[i], fontstyle='italic') else: plt.text(text_x, text_y_start - next(ge), 'Path cost={:.4f}'.format(value), fontsize=12, color=color[i], fontstyle='italic') text_x, text_y_start = -36, -70 ge = iter(range(0, 1000, 6)) plt.text(text_x+10, text_y_start - next(ge), 'MPC', fontsize=14, color='r', fontstyle='italic') if MPC_traj_return_value is not None: for i, value in enumerate(MPC_traj_return_value): if i == MPC_path_index: plt.text(text_x, text_y_start - next(ge), 'Path cost={:.4f}'.format(value), fontsize=14, color=color[i], fontstyle='italic') else: plt.text(text_x, text_y_start - next(ge), 'Path cost={:.4f}'.format(value), fontsize=12, color=color[i], fontstyle='italic') plt.pause(0.001)
target_summary_path = '%s/runs-%s/CR%.2f' %(save_root, target_dataset_name, 100 * overall_CR) for SummaryPath in [source_summary_path, target_summary_path]: if args.exp_spec is not '': SummaryPath += ('-' + args.exp_spec) if os.path.exists(SummaryPath): print('Record exist, remove') input() shutil.rmtree(SummaryPath) os.makedirs(SummaryPath) else: os.makedirs(SummaryPath) source_recorder = Recorder(SummaryPath=source_summary_path, dataset_name=source_dataset_name) target_recorder = Recorder(SummaryPath=target_summary_path, dataset_name=source_dataset_name) alpha_change_point_file = open('%s/alpha_change_point.txt' %(target_summary_path), 'w+') ################## # Begin Training # ################## best_acc_list = [] # Best test acc in each training period under various alpha niter = 0 for ite, CR_ratio in enumerate(CR_list): alpha = alpha_list[ite] print('Adaptive iteration: %d, alpha: %.3f' % (ite, alpha)) print('Current CR: %s' % CR_ratio) print('niter: %d' %niter)
class WebBrowser(QObject): dic_url = {'SWGS': 'http://172.31.196.21:8060/websquare/websquare.html?w2xPath=/SWING/lib/xml/ZLIBSMAN90010.xml&coClCd=T&svrLocation=SWGS'} browser_log = [] requestList = [] eventList = [] browser_log_hst = [] HOME = os.path.expanduser("~") SAVE_PATH = os.path.join(HOME, 'Test_Tool') HOME_SAVE_PATH = os.path.join(HOME, '.test_tool') def __init__(self, sid, url='', websocket_server=None): QObject.__init__(self) self.sid = sid self.url = url self.driver = None self.recorder = None self.requestList = [] self.eventList = [] self.browser_log = [] self.browser_log_hst = [] self.chromedriver_version = '' self.recording = False self.web_iframe_list = [] # Load setting in the main thread self.settings = Settings(self.HOME_SAVE_PATH) self.settings.load() self.settings.save() self._loadSetting() self.config = Config() self.paths = [r"C:\Program Files\Google\Chrome\Application\chrome.exe", r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"] self.paths.extend(self.config.getlist("section_browser", "BROWSER_PATH")) self.browser_version = self.config.get('section_browser', 'BROWSER_VER') if self.sid in self.dic_url.keys(): print(self.dic_url[self.sid]) elif self.sid == 'URL': print(self.url) self.dic_url[self.sid] = self.url else: print('미존재 SID') self.websocket_server = websocket_server self.websocket_server.receivedSignal.connect(self.websocketSeverReceived) def _loadSetting(self): ''' 설정값으로 재설정 :return: None ''' self.getDriverVersion() def getDriverVersion(self): self.settings.load() self.chromedriver_version = self.settings.get("CHROMEDRIVER_VERSION", "") def getVersionViaCom(self): version_info = [] paths = self.paths try: parser = Dispatch("Scripting.FileSystemObject") except: if self.browser_version: result_verion= self.browser_version else: result_verion = '' return result_verion for file_name in paths: try: version = parser.GetFileVersion(file_name) version_info.append(version) except: pass try: result_verion = version_info[0].split('.')[0] except: result_verion = '' return result_verion def processBrowserLogEntry(self, entry): response = json.loads(entry['message'])['message'] return response def findEventLog(self, type, evets): requestList = [] xhr_events = [event for event in evets if 'type' in event['params'].keys() and event['params']['type'] == type] xhr_events = [event for event in xhr_events if event['params']['request']['method'] == 'POST' and 'postData' in event['params']['request'].keys()] #print('==== XHR List ====') for idx, xhr in enumerate(xhr_events): post_data = xhr['params']['request']['postData'] try: trx_code = json.loads(post_data)['HEAD']['Trx_Code'] except json.JSONDecodeError as e: print(e) print(post_data) request_id = xhr['params']['requestId'] #print('{index} - {trxCode} - {requestId}'.format(index=idx, trxCode=trx_code, requestId=request_id)) inputData = {} outputData = {} # request결과가 없는 경우가 있음 (ZNGMSATH10060_TR03) 결과가 없으면 제외 try: inputData = json.loads(self.driver.execute_cdp_cmd('Network.getRequestPostData', {'requestId': str(request_id)})['postData']) outputData = json.loads(self.driver.execute_cdp_cmd('Network.getResponseBody', {'requestId': str(request_id)})['body']) if 'dataInfo' not in list(outputData.keys()): outputData = makeDataInfo(outputData) requestDtl = {'trx_code': trx_code, 'request_id': request_id, 'input_data': inputData, 'output_data': outputData} requestList.append(requestDtl) except: pass return requestList def getInputDataset(self, events, idx): return json.loads(events[idx]['params']['request']['postData']) def popUp(self): version = self.getVersionViaCom() chromedriver_path = os.path.join(self.SAVE_PATH, 'chromedriver.exe') self.getDriverVersion() if not os.path.exists(chromedriver_path): print('chromedriver 파일 미존재 - 생성....') self.downloadChromedriver(version, chromedriver_path) elif version == self.chromedriver_version: print('동일한 Chrome / chromedriver 버전 - [{}]'.format(version)) else: print('Chrome [{chrome_version}] / chromedriver [{chromedriver_version}] 버전이 다른 경우'.format(chrome_version=version, chromedriver_version=self.chromedriver_version)) self.downloadChromedriver(version, chromedriver_path) self.web_iframe_list = [] chrome_options = Options() #chrome_options.add_argument("start-maximized") chrome_options.add_argument("disable-infobars") chrome_options.add_argument("--disable-extensions") chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-gpu") chrome_options.add_argument("window-size=1920x1080") # 가져올 크기를 결정 chrome_options.set_capability('unhandledPromptBehavior', 'accept') caps = DesiredCapabilities.CHROME caps['goog:loggingPrefs'] = {'performance': 'ALL'} if self.driver and self.getStatus(): pass else: # 드라이버 객체 생성 self.driver = webdriver.Chrome(chromedriver_path, chrome_options=chrome_options, desired_capabilities=caps) #self.driver.implicitly_wait(3) # 드라이버 초기화를 위해 3초 대기 self.get() def get(self): self.driver.get(self.dic_url[self.sid]) if self.sid == 'SWGS': try: self.driver.switch_to_alert() alert = WebDriverWait(self.driver, 1).until(EC.alert_is_present()) alert.accept() except NoAlertPresentException: pass finally: while True: try: WebDriverWait(self.driver, 0).until(EC.element_to_be_clickable((By.ID, 'bodyBlock'))) except: break try: WebDriverWait(self.driver, 60).until(EC.presence_of_element_located((By.ID, 'mdi01_subWindow0_iframe'))) finally: print('Page is ready!') self.driver.execute_script('''top.document.title = "(FOR AUTO TEST TOOL)"''') # Recorder 선언 self.recorder = Recorder(self.driver) self.recorder.addEventListener() #threading.Timer(3, self.getBrowserEvents).start() def getRequest(self): try: self.driver.switch_to_alert() except NoAlertPresentException: log = self.driver.get_log('performance') self.browser_log.extend(log) self.browser_log_hst.extend(log) events = [self.processBrowserLogEntry(entry) for entry in self.browser_log] # response_events = [event for event in events if 'Network.response' in event['method']] request_events = [event for event in events if 'Network.requestWillBeSent' in event['method']] self.requestList = self.findEventLog('XHR', request_events) return self.requestList def getRequestHst(self): try: self.driver.switch_to_alert() except NoAlertPresentException: log = self.driver.get_log('performance') self.browser_log.extend(log) self.browser_log_hst.extend(log) events = [self.processBrowserLogEntry(entry) for entry in self.browser_log_hst] # response_events = [event for event in events if 'Network.response' in event['method']] request_events = [event for event in events if 'Network.requestWillBeSent' in event['method']] self.requestHst = self.findEventLog('XHR', request_events) return self.requestHst def clear(self): self.browser_log_hst.extend(self.driver.get_log('performance')) self.browser_log = [] self.requestList = [] print('Request Clear Successful') def clearUiEventList(self): self.recorder.clearEventList() def recordUiEvent(self): if not self.recording: self.recorder.clearEventList() self.recorder.addEventListener() self.recording = not self.recording def websocketSeverReceived(self, data): ''' :websocket Server로부터 data를 받았을때 발생하는 이벤트 data: (str) 'add_iframe' ''' #print(data) if data == 'add_iframe': self.recorder.addEventListener() def getBrowserEvents(self): events = [] try: events = self.recorder.getEventList() #threading.Timer(3, self.getBrowserEvents).start() except JavascriptException: self.recorder.addEventListener() events = self.recorder.getEventList() #threading.Timer(3, self.getBrowserEvents).start() except WebDriverException: print('Browser 종료') finally: #print(events) self.recorder.addEventListener() return events # if events: # print(events) # self.eventList.extend(events) #self.recorder.clearEventList() # for event in events: # # One-time initialization # toaster = ToastNotifier() # # # Show notification whenever needed # toaster.show_toast(title="Command Record", # msg="Command:{command}\nTarget:{target}\nValue:{value}".format(command=event[1], target=event[3], value=event[5]), # threaded=True, # icon_path='auto_test_main.ico', # duration=1) # notification.notify( # title='Command Record', # ticker='A', # message="Command:{command}\nTarget:{target}\nValue:{value}".format(command=event[1], target=event[3], value=event[5]), # app_name="Auto Test Tool 0.0.4", # app_icon='auto_test_main.ico', # timeout=5, # seconds # ) def getDriver(self): return self.driver def getEventList(self): return self.eventList def getSessionId(self): if self.driver: return self.driver.session_id else: return False def setSessionId(self, session_id): self.driver.session_id = session_id def getStatus(self): try: self.driver.window_handles return True except: return False def setFocus(self): self.driver.switch_to_window(self.driver.current_window_handle) def saveChromedriverVersion(self, version): ''' chromedriver version 정보를 저장 ''' self.settings.load() self.settings["CHROMEDRIVER_VERSION"] = version self.settings.save() def chromedriverWebOn(self): ''' chromedriver site 접속 가능여부 체크 ''' url = "https://chromedriver.chromium.org/downloads" try: res = urllib.request.urlopen(url) if res.status == 200: return True else: return False except: return False def downloadChromedriver(self, version, chromedriver_path): ''' chromedriver가 미존재하거나 버전이 다른 경우 발생하는 이벤트 - 온라인 연결이 가능한 경우는 다운로드 - 온라인 연결이 불가능한 경우 임시 파일 중 가능한 버전으로 대체 :param version: (str) '81' :param chromedriver_path: (str) ''' if self.chromedriverWebOn(): print("Chromedriver Downloading...") download_url = self.findDriverVersion(version) download_path = os.path.join(self.SAVE_PATH, "chromedriver.zip") urllib.request.urlretrieve(download_url, download_path) print("Download Complete!") proc_name = "chromedriver.exe" # 현재 수행중인 chromedriver 존재여부 확인 for proc in psutil.process_iter(): try: if proc.name().lower() == proc_name.lower(): print('현재 수행중인 chromedriver kill [{}]'.format(proc.name())) proc.kill() except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): pass try: with zipfile.ZipFile(download_path) as zf: zf.extractall(path=self.SAVE_PATH) os.remove(download_path) except Exception as e: print(e) self.saveChromedriverVersion(version) else: print('Online 연결 불가능 - 오프라인 파일 생성') try: if version == '81': shutil.copy('chromedriver_tmp/chromedriver_81.exe', chromedriver_path) self.saveChromedriverVersion(version) elif version == '80': shutil.copy('chromedriver_tmp/chromedriver_80.exe', chromedriver_path) self.saveChromedriverVersion(version) elif version == '79': shutil.copy('chromedriver_tmp/chromedriver_79.exe', chromedriver_path) self.saveChromedriverVersion(version) elif version in ['65', '66', '67']: shutil.copy('chromedriver_tmp/chromedriver_65.exe', chromedriver_path) self.saveChromedriverVersion(version) else: print('Chrome Version Check : {} - 오프라인은 65, 66, 67, 79, 80, 81 Version만 지원가능'.format(version)) return False except: pass pass def findDriverVersion(self, find_version): ''' chromedriver를 다운로드 할 url를 찾아 string으로 Return chromedriver chrome 85 85 . . . . 71 71 70 70 --------------------- 2.46 71-73 2.45 70-72 2.44 69-71 2.43 69-71 2.42 68-70 2.41 67-69 2.40 66-68 2.39 66-68 2.38 65-67 2.37 64-66 2.36 63-65 2.35 62-64 2.34 61-63 2.33 60-62 --------------------- 2.28 57+ 2.25 54+ 2.24 53+ 2.22 51+ 2.19 44+ 2.15 42+ :param find_version: (str) '81' :return: (str) https://chromedriver.storage.googleapis.com/81.0.4044.138/chromedriver_win32.zip ''' download_url = '' base_url = "https://chromedriver.chromium.org/downloads" base_req = requests.get(base_url) base_soup = BeautifulSoup(base_req.content, "html.parser") atags = base_soup.select("a") for a in atags: m = re.compile("ChromeDriver (.*)") p = m.search(a.text) try: version = p.group(1) try: m = re.compile("(\d*)\..*") p = m.search(version) if int(find_version) == int(p.group(1)): download_url = "/".join( [ "https://chromedriver.storage.googleapis.com", version, "chromedriver_win32.zip", ] ) break except: pass except AttributeError: pass return download_url
class HierarchicalDecision(object): def __init__(self, task, train_exp_dir, ite, logdir=None): self.task = task self.policy = LoadPolicy('../utils/models/{}/{}'.format(task, train_exp_dir), ite) self.env = CrossroadEnd2end(training_task=self.task, mode='testing') self.model = EnvironmentModel(self.task, mode='selecting') self.recorder = Recorder() self.episode_counter = -1 self.step_counter = -1 self.obs = None self.stg = MultiPathGenerator() self.step_timer = TimerStat() self.ss_timer = TimerStat() self.logdir = logdir if self.logdir is not None: config = dict(task=task, train_exp_dir=train_exp_dir, ite=ite) with open(self.logdir + '/config.json', 'w', encoding='utf-8') as f: json.dump(config, f, ensure_ascii=False, indent=4) self.fig = plt.figure(figsize=(8, 8)) plt.ion() self.hist_posi = [] self.old_index = 0 self.path_list = self.stg.generate_path(self.task) # ------------------build graph for tf.function in advance----------------------- for i in range(3): obs = self.env.reset() obs = tf.convert_to_tensor(obs[np.newaxis, :], dtype=tf.float32) self.is_safe(obs, i) obs = self.env.reset() obs_with_specific_shape = np.tile(obs, (3, 1)) self.policy.run_batch(obs_with_specific_shape) self.policy.obj_value_batch(obs_with_specific_shape) # ------------------build graph for tf.function in advance----------------------- self.reset() def reset(self,): self.obs = self.env.reset() self.recorder.reset() self.old_index = 0 self.hist_posi = [] if self.logdir is not None: self.episode_counter += 1 os.makedirs(self.logdir + '/episode{}/figs'.format(self.episode_counter)) self.step_counter = -1 self.recorder.save(self.logdir) if self.episode_counter >= 1: select_and_rename_snapshots_of_an_episode(self.logdir, self.episode_counter-1, 12) self.recorder.plot_and_save_ith_episode_curves(self.episode_counter-1, self.logdir + '/episode{}/figs'.format(self.episode_counter-1), isshow=False) return self.obs # @tf.function # def is_safe(self, obs, path_index): # self.model.ref_path.set_path(path_index) # action = self.policy.run_batch(obs) # veh2veh4real = self.model.ss(obs, action, lam=0.1) # return False if veh2veh4real[0] > 0 else True @tf.function def is_safe(self, obs, path_index): self.model.add_traj(obs, path_index) punish = 0. for step in range(5): action = self.policy.run_batch(obs) obs, _, _, _, veh2veh4real, _ = self.model.rollout_out(action) punish += veh2veh4real[0] return False if punish > 0 else True def safe_shield(self, real_obs, path_index): action_safe_set = [[[0., -1.]]] real_obs = tf.convert_to_tensor(real_obs[np.newaxis, :], dtype=tf.float32) obs = real_obs if not self.is_safe(obs, path_index): print('SAFETY SHIELD STARTED!') return np.array(action_safe_set[0], dtype=np.float32).squeeze(0), True else: return self.policy.run_batch(real_obs).numpy()[0], False def step(self): self.step_counter += 1 with self.step_timer: obs_list = [] # select optimal path for path in self.path_list: self.env.set_traj(path) obs_list.append(self.env._get_obs()) all_obs = tf.stack(obs_list, axis=0) path_values = self.policy.obj_value_batch(all_obs).numpy() old_value = path_values[self.old_index] new_index, new_value = int(np.argmin(path_values)), min(path_values) # value is to approximate (- sum of reward) path_index = self.old_index if old_value - new_value < 0.1 else new_index self.old_index = path_index self.env.set_traj(self.path_list[path_index]) self.obs_real = obs_list[path_index] # obtain safe action with self.ss_timer: safe_action, is_ss = self.safe_shield(self.obs_real, path_index) print('ALL TIME:', self.step_timer.mean, 'ss', self.ss_timer.mean) self.render(self.path_list, path_values, path_index) self.recorder.record(self.obs_real, safe_action, self.step_timer.mean, path_index, path_values, self.ss_timer.mean, is_ss) self.obs, r, done, info = self.env.step(safe_action) return done def render(self, traj_list, path_values, path_index): square_length = CROSSROAD_SIZE extension = 40 lane_width = LANE_WIDTH light_line_width = 3 dotted_line_style = '--' solid_line_style = '-' plt.cla() ax = plt.axes([-0.05, -0.05, 1.1, 1.1]) for ax in self.fig.get_axes(): ax.axis('off') ax.axis("equal") # ----------arrow-------------- plt.arrow(lane_width / 2, -square_length / 2 - 10, 0, 5, color='b') plt.arrow(lane_width / 2, -square_length / 2 - 10 + 5, -0.5, 0, color='b', head_width=1) plt.arrow(lane_width * 1.5, -square_length / 2 - 10, 0, 4, color='b', head_width=1) plt.arrow(lane_width * 2.5, -square_length / 2 - 10, 0, 5, color='b') plt.arrow(lane_width * 2.5, -square_length / 2 - 10 + 5, 0.5, 0, color='b', head_width=1) # ----------horizon-------------- plt.plot([-square_length / 2 - extension, -square_length / 2], [0.3, 0.3], color='orange') plt.plot([-square_length / 2 - extension, -square_length / 2], [-0.3, -0.3], color='orange') plt.plot([square_length / 2 + extension, square_length / 2], [0.3, 0.3], color='orange') plt.plot([square_length / 2 + extension, square_length / 2], [-0.3, -0.3], color='orange') # for i in range(1, LANE_NUMBER + 1): linestyle = dotted_line_style if i < LANE_NUMBER else solid_line_style linewidth = 1 if i < LANE_NUMBER else 2 plt.plot([-square_length / 2 - extension, -square_length / 2], [i * lane_width, i * lane_width], linestyle=linestyle, color='black', linewidth=linewidth) plt.plot([square_length / 2 + extension, square_length / 2], [i * lane_width, i * lane_width], linestyle=linestyle, color='black', linewidth=linewidth) plt.plot([-square_length / 2 - extension, -square_length / 2], [-i * lane_width, -i * lane_width], linestyle=linestyle, color='black', linewidth=linewidth) plt.plot([square_length / 2 + extension, square_length / 2], [-i * lane_width, -i * lane_width], linestyle=linestyle, color='black', linewidth=linewidth) # ----------vertical---------------- plt.plot([0.3, 0.3], [-square_length / 2 - extension, -square_length / 2], color='orange') plt.plot([-0.3, -0.3], [-square_length / 2 - extension, -square_length / 2], color='orange') plt.plot([0.3, 0.3], [square_length / 2 + extension, square_length / 2], color='orange') plt.plot([-0.3, -0.3], [square_length / 2 + extension, square_length / 2], color='orange') # for i in range(1, LANE_NUMBER + 1): linestyle = dotted_line_style if i < LANE_NUMBER else solid_line_style linewidth = 1 if i < LANE_NUMBER else 2 plt.plot([i * lane_width, i * lane_width], [-square_length / 2 - extension, -square_length / 2], linestyle=linestyle, color='black', linewidth=linewidth) plt.plot([i * lane_width, i * lane_width], [square_length / 2 + extension, square_length / 2], linestyle=linestyle, color='black', linewidth=linewidth) plt.plot([-i * lane_width, -i * lane_width], [-square_length / 2 - extension, -square_length / 2], linestyle=linestyle, color='black', linewidth=linewidth) plt.plot([-i * lane_width, -i * lane_width], [square_length / 2 + extension, square_length / 2], linestyle=linestyle, color='black', linewidth=linewidth) v_light = self.env.v_light if v_light == 0: v_color, h_color = 'green', 'red' elif v_light == 1: v_color, h_color = 'orange', 'red' elif v_light == 2: v_color, h_color = 'red', 'green' else: v_color, h_color = 'red', 'orange' plt.plot([0, (LANE_NUMBER - 1) * lane_width], [-square_length / 2, -square_length / 2], color=v_color, linewidth=light_line_width) plt.plot([(LANE_NUMBER - 1) * lane_width, LANE_NUMBER * lane_width], [-square_length / 2, -square_length / 2], color='green', linewidth=light_line_width) plt.plot([-LANE_NUMBER * lane_width, -(LANE_NUMBER - 1) * lane_width], [square_length / 2, square_length / 2], color='green', linewidth=light_line_width) plt.plot([-(LANE_NUMBER - 1) * lane_width, 0], [square_length / 2, square_length / 2], color=v_color, linewidth=light_line_width) plt.plot([-square_length / 2, -square_length / 2], [0, -(LANE_NUMBER - 1) * lane_width], color=h_color, linewidth=light_line_width) plt.plot([-square_length / 2, -square_length / 2], [-(LANE_NUMBER - 1) * lane_width, -LANE_NUMBER * lane_width], color='green', linewidth=light_line_width) plt.plot([square_length / 2, square_length / 2], [(LANE_NUMBER - 1) * lane_width, 0], color=h_color, linewidth=light_line_width) plt.plot([square_length / 2, square_length / 2], [LANE_NUMBER * lane_width, (LANE_NUMBER - 1) * lane_width], color='green', linewidth=light_line_width) # ----------Oblique-------------- plt.plot([LANE_NUMBER * lane_width, square_length / 2], [-square_length / 2, -LANE_NUMBER * lane_width], color='black', linewidth=2) plt.plot([LANE_NUMBER * lane_width, square_length / 2], [square_length / 2, LANE_NUMBER * lane_width], color='black', linewidth=2) plt.plot([-LANE_NUMBER * lane_width, -square_length / 2], [-square_length / 2, -LANE_NUMBER * lane_width], color='black', linewidth=2) plt.plot([-LANE_NUMBER * lane_width, -square_length / 2], [square_length / 2, LANE_NUMBER * lane_width], color='black', linewidth=2) def is_in_plot_area(x, y, tolerance=5): if -square_length / 2 - extension + tolerance < x < square_length / 2 + extension - tolerance and \ -square_length / 2 - extension + tolerance < y < square_length / 2 + extension - tolerance: return True else: return False def draw_rotate_rec(x, y, a, l, w, c): bottom_left_x, bottom_left_y, _ = rotate_coordination(-l / 2, w / 2, 0, -a) ax.add_patch(plt.Rectangle((x + bottom_left_x, y + bottom_left_y), w, l, edgecolor=c, facecolor='white', angle=-(90 - a), zorder=50)) def plot_phi_line(x, y, phi, color): line_length = 3 x_forw, y_forw = x + line_length * cos(phi * pi / 180.), \ y + line_length * sin(phi * pi / 180.) plt.plot([x, x_forw], [y, y_forw], color=color, linewidth=0.5) # plot cars for veh in self.env.all_vehicles: veh_x = veh['x'] veh_y = veh['y'] veh_phi = veh['phi'] veh_l = veh['l'] veh_w = veh['w'] if is_in_plot_area(veh_x, veh_y): plot_phi_line(veh_x, veh_y, veh_phi, 'black') draw_rotate_rec(veh_x, veh_y, veh_phi, veh_l, veh_w, 'black') # plot_interested vehs # for mode, num in self.env.veh_mode_dict.items(): # for i in range(num): # veh = self.env.interested_vehs[mode][i] # veh_x = veh['x'] # veh_y = veh['y'] # veh_phi = veh['phi'] # veh_l = veh['l'] # veh_w = veh['w'] # task2color = {'left': 'b', 'straight': 'c', 'right': 'm'} # # if is_in_plot_area(veh_x, veh_y): # plot_phi_line(veh_x, veh_y, veh_phi, 'black') # task = MODE2TASK[mode] # color = task2color[task] # draw_rotate_rec(veh_x, veh_y, veh_phi, veh_l, veh_w, color) ego_v_x = self.env.ego_dynamics['v_x'] ego_v_y = self.env.ego_dynamics['v_y'] ego_r = self.env.ego_dynamics['r'] ego_x = self.env.ego_dynamics['x'] ego_y = self.env.ego_dynamics['y'] ego_phi = self.env.ego_dynamics['phi'] ego_l = self.env.ego_dynamics['l'] ego_w = self.env.ego_dynamics['w'] ego_alpha_f = self.env.ego_dynamics['alpha_f'] ego_alpha_r = self.env.ego_dynamics['alpha_r'] alpha_f_bound = self.env.ego_dynamics['alpha_f_bound'] alpha_r_bound = self.env.ego_dynamics['alpha_r_bound'] r_bound = self.env.ego_dynamics['r_bound'] plot_phi_line(ego_x, ego_y, ego_phi, 'fuchsia') draw_rotate_rec(ego_x, ego_y, ego_phi, ego_l, ego_w, 'fuchsia') self.hist_posi.append((ego_x, ego_y)) # plot history xs = [pos[0] for pos in self.hist_posi] ys = [pos[1] for pos in self.hist_posi] plt.scatter(np.array(xs), np.array(ys), color='fuchsia', alpha=0.1) # plot future data tracking_info = self.obs[ self.env.ego_info_dim:self.env.ego_info_dim + self.env.per_tracking_info_dim * (self.env.num_future_data + 1)] future_path = tracking_info[self.env.per_tracking_info_dim:] for i in range(self.env.num_future_data): delta_x, delta_y, delta_phi = future_path[i * self.env.per_tracking_info_dim: (i + 1) * self.env.per_tracking_info_dim] path_x, path_y, path_phi = ego_x + delta_x, ego_y + delta_y, ego_phi - delta_phi plt.plot(path_x, path_y, 'g.') plot_phi_line(path_x, path_y, path_phi, 'g') delta_, _, _ = tracking_info[:3] indexs, points = self.env.ref_path.find_closest_point(np.array([ego_x], np.float32), np.array([ego_y], np.float32)) path_x, path_y, path_phi = points[0][0], points[1][0], points[2][0] # plt.plot(path_x, path_y, 'g.') delta_x, delta_y, delta_phi = ego_x - path_x, ego_y - path_y, ego_phi - path_phi # plot real time traj try: color = ['blue', 'coral', 'darkcyan'] for i, item in enumerate(traj_list): if i == path_index: plt.plot(item.path[0], item.path[1], color=color[i]) else: plt.plot(item.path[0], item.path[1], color=color[i], alpha=0.3) indexs, points = item.find_closest_point(np.array([ego_x], np.float32), np.array([ego_y], np.float32)) path_x, path_y, path_phi = points[0][0], points[1][0], points[2][0] plt.plot(path_x, path_y, color=color[i]) except Exception: pass # text # text_x, text_y_start = -120, 60 # ge = iter(range(0, 1000, 4)) # plt.text(text_x, text_y_start - next(ge), 'ego_x: {:.2f}m'.format(ego_x)) # plt.text(text_x, text_y_start - next(ge), 'ego_y: {:.2f}m'.format(ego_y)) # plt.text(text_x, text_y_start - next(ge), 'path_x: {:.2f}m'.format(path_x)) # plt.text(text_x, text_y_start - next(ge), 'path_y: {:.2f}m'.format(path_y)) # plt.text(text_x, text_y_start - next(ge), 'delta_: {:.2f}m'.format(delta_)) # plt.text(text_x, text_y_start - next(ge), 'delta_x: {:.2f}m'.format(delta_x)) # plt.text(text_x, text_y_start - next(ge), 'delta_y: {:.2f}m'.format(delta_y)) # plt.text(text_x, text_y_start - next(ge), r'ego_phi: ${:.2f}\degree$'.format(ego_phi)) # plt.text(text_x, text_y_start - next(ge), r'path_phi: ${:.2f}\degree$'.format(path_phi)) # plt.text(text_x, text_y_start - next(ge), r'delta_phi: ${:.2f}\degree$'.format(delta_phi)) # plt.text(text_x, text_y_start - next(ge), 'v_x: {:.2f}m/s'.format(ego_v_x)) # plt.text(text_x, text_y_start - next(ge), 'exp_v: {:.2f}m/s'.format(self.env.exp_v)) # plt.text(text_x, text_y_start - next(ge), 'v_y: {:.2f}m/s'.format(ego_v_y)) # plt.text(text_x, text_y_start - next(ge), 'yaw_rate: {:.2f}rad/s'.format(ego_r)) # plt.text(text_x, text_y_start - next(ge), 'yaw_rate bound: [{:.2f}, {:.2f}]'.format(-r_bound, r_bound)) # # plt.text(text_x, text_y_start - next(ge), r'$\alpha_f$: {:.2f} rad'.format(ego_alpha_f)) # plt.text(text_x, text_y_start - next(ge), r'$\alpha_f$ bound: [{:.2f}, {:.2f}] '.format(-alpha_f_bound, # alpha_f_bound)) # plt.text(text_x, text_y_start - next(ge), r'$\alpha_r$: {:.2f} rad'.format(ego_alpha_r)) # plt.text(text_x, text_y_start - next(ge), r'$\alpha_r$ bound: [{:.2f}, {:.2f}] '.format(-alpha_r_bound, # alpha_r_bound)) # if self.env.action is not None: # steer, a_x = self.env.action[0], self.env.action[1] # plt.text(text_x, text_y_start - next(ge), # r'steer: {:.2f}rad (${:.2f}\degree$)'.format(steer, steer * 180 / np.pi)) # plt.text(text_x, text_y_start - next(ge), 'a_x: {:.2f}m/s^2'.format(a_x)) # # text_x, text_y_start = 70, 60 # ge = iter(range(0, 1000, 4)) # # # done info # plt.text(text_x, text_y_start - next(ge), 'done info: {}'.format(self.env.done_type)) # # # reward info # if self.env.reward_info is not None: # for key, val in self.env.reward_info.items(): # plt.text(text_x, text_y_start - next(ge), '{}: {:.4f}'.format(key, val)) # # # indicator for trajectory selection # text_x, text_y_start = -18, -70 # ge = iter(range(0, 1000, 6)) # if path_values is not None: # for i, value in enumerate(path_values): # if i == path_index: # plt.text(text_x, text_y_start - next(ge), 'Path reward={:.4f}'.format(value[0]), fontsize=14, # color=color[i], fontstyle='italic') # else: # plt.text(text_x, text_y_start - next(ge), 'Path reward={:.4f}'.format(value[0]), fontsize=12, # color=color[i], fontstyle='italic') plt.show() plt.pause(0.001) if self.logdir is not None: plt.savefig(self.logdir + '/episode{}'.format(self.episode_counter) + '/step{:03d}.png'.format(self.step_counter))
SummaryPath = '%s/runs-TTQ/TheshFactor-%.3f-Optimizer-%s' \ %(save_root, thresh_factor, optimizer_type) if args.exp_spec is not '': SummaryPath += ('-' + args.exp_spec) print('Save to %s' % SummaryPath) if os.path.exists(SummaryPath): print('Record exist, remove') input() shutil.rmtree(SummaryPath) os.makedirs(SummaryPath) else: os.makedirs(SummaryPath) recorder = Recorder(SummaryPath=SummaryPath, dataset_name=dataset_name) conv1_pos_file = open('%s/conv1_pos.txt' % SummaryPath, 'w+') conv1_neg_file = open('%s/conv1_neg.txt' % SummaryPath, 'w+') conv1_prune_file = open('%s/conv1_prune.txt' % SummaryPath, 'w+') for epoch in range(MAX_EPOCH): if recorder.stop: break net.train() end = time.time() recorder.reset_performance() for batch_idx, (inputs, targets) in enumerate(train_loader):
def plot_and_save_ith_episode_data(logdir, i): recorder = Recorder() recorder.load(logdir) save_dir = logdir + '/episode{}/figs'.format(i) os.makedirs(save_dir, exist_ok=True) recorder.plot_and_save_ith_episode_curves(i, save_dir, True)
class HierarchicalDecision(object): def __init__(self, task, logdir=None): self.task = task if self.task == 'left': self.policy = LoadPolicy('../utils/models/left', 100000) elif self.task == 'right': self.policy = LoadPolicy('../utils/models/right', 145000) elif self.task == 'straight': self.policy = LoadPolicy('../utils/models/straight', 95000) self.env = CrossroadEnd2end(training_task=self.task) self.model = EnvironmentModel(self.task, mode='selecting') self.recorder = Recorder() self.episode_counter = -1 self.step_counter = -1 self.obs = None self.stg = None self.step_timer = TimerStat() self.ss_timer = TimerStat() self.logdir = logdir self.fig = plt.figure(figsize=(8, 8)) plt.ion() self.hist_posi = [] self.reset() def reset(self, ): self.obs = self.env.reset() self.stg = MultiPathGenerator() self.recorder.reset() self.hist_posi = [] if self.logdir is not None: self.episode_counter += 1 os.makedirs(self.logdir + '/episode{}'.format(self.episode_counter)) self.step_counter = -1 self.recorder.save(self.logdir) return self.obs def safe_shield(self, real_obs, traj): action_bound = 1.0 action_safe_set = [[[0, -action_bound]]] real_obs = tf.convert_to_tensor(real_obs[np.newaxis, :]) obs = real_obs self.model.add_traj(obs, traj) total_punishment = 0.0 for step in range(3): action = self.policy.run(obs) _, _, _, _, veh2veh4real, _ = self.model.rollout_out(action) total_punishment += veh2veh4real if total_punishment != 0: print('original action will cause collision within three steps!!!') for safe_action in action_safe_set: obs = real_obs total_punishment = 0 # for step in range(1): # obs, veh2veh4real = self.model.safety_calculation(obs, safe_action) # total_punishment += veh2veh4real # if veh2veh4real != 0: # collide # break # if total_punishment == 0: # print('found the safe action', safe_action) # safe_action = np.array(safe_action) # break # else: # print('still collide') # safe_action = self.policy.run(real_obs).numpy().squeeze(0) return np.array(safe_action).squeeze(0) else: safe_action = self.policy.run(real_obs).numpy()[0] return safe_action def step(self): self.step_counter += 1 with self.step_timer: path_list = self.stg.generate_path(self.task) obs_list = [] # select optimal path for path in path_list: self.env.set_traj(path) obs_list.append(self.env._get_obs()) all_obs = tf.stack(obs_list, axis=0) path_values = self.policy.values(all_obs).numpy().squeeze() path_index = int(np.argmax(path_values[:, 0])) self.env.set_traj(path_list[path_index]) self.obs_real = obs_list[path_index] # obtain safe action # with self.ss_timer: # safe_action = self.safe_shield(self.obs_real, path_list[path_index]) safe_action = self.policy.run(self.obs_real).numpy() print('ALL TIME:', self.step_timer.mean) # deal with red light temporally # if self.env.v_light != 0 and -25 > self.env.ego_dynamics['y'] > -35 and self.env.training_task != 'right': # scaled_steer = 0. # if self.env.ego_dynamics['v_x'] == 0.0: # scaled_a_x = 0.33 # else: # scaled_a_x = np.random.uniform(-0.6, -0.4) # safe_action = np.array([scaled_steer, scaled_a_x], dtype=np.float32) self.render(path_list, path_values, path_index) self.recorder.record(self.obs_real, safe_action, self.step_timer.mean, path_index, path_values, self.ss_timer.mean) self.obs, r, done, info = self.env.step(safe_action) return done def render(self, traj_list, path_values, path_index): square_length = CROSSROAD_SIZE extension = 40 lane_width = LANE_WIDTH light_line_width = 3 dotted_line_style = '--' solid_line_style = '-' plt.cla() ax = plt.axes([-0.05, -0.05, 1.1, 1.1]) for ax in self.fig.get_axes(): ax.axis('off') ax.axis("equal") # ----------arrow-------------- plt.arrow(lane_width / 2, -square_length / 2 - 10, 0, 5, color='b') plt.arrow(lane_width / 2, -square_length / 2 - 10 + 5, -0.5, 0, color='b', head_width=1) plt.arrow(lane_width * 1.5, -square_length / 2 - 10, 0, 4, color='b', head_width=1) plt.arrow(lane_width * 2.5, -square_length / 2 - 10, 0, 5, color='b') plt.arrow(lane_width * 2.5, -square_length / 2 - 10 + 5, 0.5, 0, color='b', head_width=1) # ----------horizon-------------- plt.plot([-square_length / 2 - extension, -square_length / 2], [0.3, 0.3], color='orange') plt.plot([-square_length / 2 - extension, -square_length / 2], [-0.3, -0.3], color='orange') plt.plot([square_length / 2 + extension, square_length / 2], [0.3, 0.3], color='orange') plt.plot([square_length / 2 + extension, square_length / 2], [-0.3, -0.3], color='orange') # for i in range(1, LANE_NUMBER + 1): linestyle = dotted_line_style if i < LANE_NUMBER else solid_line_style linewidth = 1 if i < LANE_NUMBER else 2 plt.plot([-square_length / 2 - extension, -square_length / 2], [i * lane_width, i * lane_width], linestyle=linestyle, color='black', linewidth=linewidth) plt.plot([square_length / 2 + extension, square_length / 2], [i * lane_width, i * lane_width], linestyle=linestyle, color='black', linewidth=linewidth) plt.plot([-square_length / 2 - extension, -square_length / 2], [-i * lane_width, -i * lane_width], linestyle=linestyle, color='black', linewidth=linewidth) plt.plot([square_length / 2 + extension, square_length / 2], [-i * lane_width, -i * lane_width], linestyle=linestyle, color='black', linewidth=linewidth) # ----------vertical---------------- plt.plot([0.3, 0.3], [-square_length / 2 - extension, -square_length / 2], color='orange') plt.plot([-0.3, -0.3], [-square_length / 2 - extension, -square_length / 2], color='orange') plt.plot([0.3, 0.3], [square_length / 2 + extension, square_length / 2], color='orange') plt.plot([-0.3, -0.3], [square_length / 2 + extension, square_length / 2], color='orange') # for i in range(1, LANE_NUMBER + 1): linestyle = dotted_line_style if i < LANE_NUMBER else solid_line_style linewidth = 1 if i < LANE_NUMBER else 2 plt.plot([i * lane_width, i * lane_width], [-square_length / 2 - extension, -square_length / 2], linestyle=linestyle, color='black', linewidth=linewidth) plt.plot([i * lane_width, i * lane_width], [square_length / 2 + extension, square_length / 2], linestyle=linestyle, color='black', linewidth=linewidth) plt.plot([-i * lane_width, -i * lane_width], [-square_length / 2 - extension, -square_length / 2], linestyle=linestyle, color='black', linewidth=linewidth) plt.plot([-i * lane_width, -i * lane_width], [square_length / 2 + extension, square_length / 2], linestyle=linestyle, color='black', linewidth=linewidth) v_light = self.env.v_light if v_light == 0: v_color, h_color = 'green', 'red' elif v_light == 1: v_color, h_color = 'orange', 'red' elif v_light == 2: v_color, h_color = 'red', 'green' else: v_color, h_color = 'red', 'orange' plt.plot([0, (LANE_NUMBER - 1) * lane_width], [-square_length / 2, -square_length / 2], color=v_color, linewidth=light_line_width) plt.plot([(LANE_NUMBER - 1) * lane_width, LANE_NUMBER * lane_width], [-square_length / 2, -square_length / 2], color='green', linewidth=light_line_width) plt.plot([-LANE_NUMBER * lane_width, -(LANE_NUMBER - 1) * lane_width], [square_length / 2, square_length / 2], color='green', linewidth=light_line_width) plt.plot([-(LANE_NUMBER - 1) * lane_width, 0], [square_length / 2, square_length / 2], color=v_color, linewidth=light_line_width) plt.plot([-square_length / 2, -square_length / 2], [0, -(LANE_NUMBER - 1) * lane_width], color=h_color, linewidth=light_line_width) plt.plot([-square_length / 2, -square_length / 2], [-(LANE_NUMBER - 1) * lane_width, -LANE_NUMBER * lane_width], color='green', linewidth=light_line_width) plt.plot([square_length / 2, square_length / 2], [(LANE_NUMBER - 1) * lane_width, 0], color=h_color, linewidth=light_line_width) plt.plot([square_length / 2, square_length / 2], [LANE_NUMBER * lane_width, (LANE_NUMBER - 1) * lane_width], color='green', linewidth=light_line_width) # ----------Oblique-------------- plt.plot([LANE_NUMBER * lane_width, square_length / 2], [-square_length / 2, -LANE_NUMBER * lane_width], color='black', linewidth=2) plt.plot([LANE_NUMBER * lane_width, square_length / 2], [square_length / 2, LANE_NUMBER * lane_width], color='black', linewidth=2) plt.plot([-LANE_NUMBER * lane_width, -square_length / 2], [-square_length / 2, -LANE_NUMBER * lane_width], color='black', linewidth=2) plt.plot([-LANE_NUMBER * lane_width, -square_length / 2], [square_length / 2, LANE_NUMBER * lane_width], color='black', linewidth=2) def is_in_plot_area(x, y, tolerance=5): if -square_length / 2 - extension + tolerance < x < square_length / 2 + extension - tolerance and \ -square_length / 2 - extension + tolerance < y < square_length / 2 + extension - tolerance: return True else: return False def draw_rotate_rec(x, y, a, l, w, c): bottom_left_x, bottom_left_y, _ = rotate_coordination( -l / 2, w / 2, 0, -a) ax.add_patch( plt.Rectangle((x + bottom_left_x, y + bottom_left_y), w, l, edgecolor=c, facecolor='white', angle=-(90 - a), zorder=50)) def plot_phi_line(x, y, phi, color): line_length = 3 x_forw, y_forw = x + line_length * cos(phi * pi / 180.), \ y + line_length * sin(phi * pi / 180.) plt.plot([x, x_forw], [y, y_forw], color=color, linewidth=0.5) # plot cars for veh in self.env.all_vehicles: veh_x = veh['x'] veh_y = veh['y'] veh_phi = veh['phi'] veh_l = veh['l'] veh_w = veh['w'] if is_in_plot_area(veh_x, veh_y): plot_phi_line(veh_x, veh_y, veh_phi, 'black') draw_rotate_rec(veh_x, veh_y, veh_phi, veh_l, veh_w, 'black') # plot_interested vehs # for mode, num in self.veh_mode_dict.items(): # for i in range(num): # veh = self.interested_vehs[mode][i] # veh_x = veh['x'] # veh_y = veh['y'] # veh_phi = veh['phi'] # veh_l = veh['l'] # veh_w = veh['w'] # task2color = {'left': 'b', 'straight': 'c', 'right': 'm'} # # if is_in_plot_area(veh_x, veh_y): # plot_phi_line(veh_x, veh_y, veh_phi, 'black') # task = MODE2TASK[mode] # color = task2color[task] # draw_rotate_rec(veh_x, veh_y, veh_phi, veh_l, veh_w, color, linestyle=':') ego_v_x = self.env.ego_dynamics['v_x'] ego_v_y = self.env.ego_dynamics['v_y'] ego_r = self.env.ego_dynamics['r'] ego_x = self.env.ego_dynamics['x'] ego_y = self.env.ego_dynamics['y'] ego_phi = self.env.ego_dynamics['phi'] ego_l = self.env.ego_dynamics['l'] ego_w = self.env.ego_dynamics['w'] ego_alpha_f = self.env.ego_dynamics['alpha_f'] ego_alpha_r = self.env.ego_dynamics['alpha_r'] alpha_f_bound = self.env.ego_dynamics['alpha_f_bound'] alpha_r_bound = self.env.ego_dynamics['alpha_r_bound'] r_bound = self.env.ego_dynamics['r_bound'] plot_phi_line(ego_x, ego_y, ego_phi, 'fuchsia') draw_rotate_rec(ego_x, ego_y, ego_phi, ego_l, ego_w, 'fuchsia') self.hist_posi.append((ego_x, ego_y)) # plot history data for hist_x, hist_y in self.hist_posi: plt.scatter(hist_x, hist_y, color='fuchsia', alpha=0.1) # plot future data tracking_info = self.obs[self.env.ego_info_dim:self.env.ego_info_dim + self.env.per_tracking_info_dim * (self.env.num_future_data + 1)] future_path = tracking_info[self.env.per_tracking_info_dim:] for i in range(self.env.num_future_data): delta_x, delta_y, delta_phi = future_path[ i * self.env.per_tracking_info_dim:(i + 1) * self.env.per_tracking_info_dim] path_x, path_y, path_phi = ego_x + delta_x, ego_y + delta_y, ego_phi - delta_phi plt.plot(path_x, path_y, 'g.') plot_phi_line(path_x, path_y, path_phi, 'g') delta_, _, _ = tracking_info[:3] indexs, points = self.env.ref_path.find_closest_point( np.array([ego_x], np.float32), np.array([ego_y], np.float32)) path_x, path_y, path_phi = points[0][0], points[1][0], points[2][0] # plt.plot(path_x, path_y, 'g.') delta_x, delta_y, delta_phi = ego_x - path_x, ego_y - path_y, ego_phi - path_phi # plot real time traj try: color = ['blue', 'coral', 'darkcyan'] for i, item in enumerate(traj_list): if i == path_index: plt.plot(item.path[0], item.path[1], color=color[i]) else: plt.plot(item.path[0], item.path[1], color=color[i], alpha=0.3) indexs, points = item.find_closest_point( np.array([ego_x], np.float32), np.array([ego_y], np.float32)) path_x, path_y, path_phi = points[0][0], points[1][0], points[ 2][0] plt.plot(path_x, path_y, color=color[i]) except Exception: pass # text # text_x, text_y_start = -120, 60 # ge = iter(range(0, 1000, 4)) # plt.text(text_x, text_y_start - next(ge), 'ego_x: {:.2f}m'.format(ego_x)) # plt.text(text_x, text_y_start - next(ge), 'ego_y: {:.2f}m'.format(ego_y)) # plt.text(text_x, text_y_start - next(ge), 'path_x: {:.2f}m'.format(path_x)) # plt.text(text_x, text_y_start - next(ge), 'path_y: {:.2f}m'.format(path_y)) # plt.text(text_x, text_y_start - next(ge), 'delta_: {:.2f}m'.format(delta_)) # plt.text(text_x, text_y_start - next(ge), 'delta_x: {:.2f}m'.format(delta_x)) # plt.text(text_x, text_y_start - next(ge), 'delta_y: {:.2f}m'.format(delta_y)) # plt.text(text_x, text_y_start - next(ge), r'ego_phi: ${:.2f}\degree$'.format(ego_phi)) # plt.text(text_x, text_y_start - next(ge), r'path_phi: ${:.2f}\degree$'.format(path_phi)) # plt.text(text_x, text_y_start - next(ge), r'delta_phi: ${:.2f}\degree$'.format(delta_phi)) # plt.text(text_x, text_y_start - next(ge), 'v_x: {:.2f}m/s'.format(ego_v_x)) # plt.text(text_x, text_y_start - next(ge), 'exp_v: {:.2f}m/s'.format(self.env.exp_v)) # plt.text(text_x, text_y_start - next(ge), 'v_y: {:.2f}m/s'.format(ego_v_y)) # plt.text(text_x, text_y_start - next(ge), 'yaw_rate: {:.2f}rad/s'.format(ego_r)) # plt.text(text_x, text_y_start - next(ge), 'yaw_rate bound: [{:.2f}, {:.2f}]'.format(-r_bound, r_bound)) # # plt.text(text_x, text_y_start - next(ge), r'$\alpha_f$: {:.2f} rad'.format(ego_alpha_f)) # plt.text(text_x, text_y_start - next(ge), r'$\alpha_f$ bound: [{:.2f}, {:.2f}] '.format(-alpha_f_bound, # alpha_f_bound)) # plt.text(text_x, text_y_start - next(ge), r'$\alpha_r$: {:.2f} rad'.format(ego_alpha_r)) # plt.text(text_x, text_y_start - next(ge), r'$\alpha_r$ bound: [{:.2f}, {:.2f}] '.format(-alpha_r_bound, # alpha_r_bound)) # if self.env.action is not None: # steer, a_x = self.env.action[0], self.env.action[1] # plt.text(text_x, text_y_start - next(ge), # r'steer: {:.2f}rad (${:.2f}\degree$)'.format(steer, steer * 180 / np.pi)) # plt.text(text_x, text_y_start - next(ge), 'a_x: {:.2f}m/s^2'.format(a_x)) # # text_x, text_y_start = 70, 60 # ge = iter(range(0, 1000, 4)) # # # done info # plt.text(text_x, text_y_start - next(ge), 'done info: {}'.format(self.env.done_type)) # # # reward info # if self.env.reward_info is not None: # for key, val in self.env.reward_info.items(): # plt.text(text_x, text_y_start - next(ge), '{}: {:.4f}'.format(key, val)) # # # indicator for trajectory selection # text_x, text_y_start = -18, -70 # ge = iter(range(0, 1000, 6)) # if path_values is not None: # for i, value in enumerate(path_values): # if i == path_index: # plt.text(text_x, text_y_start - next(ge), 'Path reward={:.4f}'.format(value[0]), fontsize=14, # color=color[i], fontstyle='italic') # else: # plt.text(text_x, text_y_start - next(ge), 'Path reward={:.4f}'.format(value[0]), fontsize=12, # color=color[i], fontstyle='italic') plt.show() plt.pause(0.001) if self.logdir is not None: plt.savefig(self.logdir + '/episode{}'.format(self.episode_counter) + '/step{}.pdf'.format(self.step_counter))