def test_model(saved_files_path=None): with open(os.path.join(saved_files_path, 'configs.yaml'), 'r') as yamlfile: opts = yaml.safe_load(yamlfile) print(opts) model_opts = opts['model_opts'] data_opts = opts['data_opts'] net_opts = opts['net_opts'] tte = model_opts['time_to_event'] if isinstance(model_opts['time_to_event'], int) else \ model_opts['time_to_event'][1] data_opts['min_track_size'] = model_opts['obs_length'] + tte if model_opts['dataset'] == 'pie': imdb = PIE(data_path=os.environ.copy()['PIE_PATH']) imdb.get_data_stats() elif model_opts['dataset'] == 'jaad': imdb = JAAD(data_path=os.environ.copy()['JAAD_PATH']) else: raise ValueError("{} dataset is incorrect".format( model_opts['dataset'])) method_class = action_prediction(model_opts['model'])(**net_opts) #beh_seq_train = imdb.generate_data_trajectory_sequence('train', **data_opts) #saved_files_path = method_class.train(beh_seq_train, **train_opts, model_opts=model_opts) beh_seq_test = imdb.generate_data_trajectory_sequence('test', **data_opts) acc, auc, f1, precision, recall = method_class.test( beh_seq_test, saved_files_path)
regularizer_val=0.001, lstm_dropout=0.4, lstm_recurrent_dropout=0.2, convlstm_num_filters=64, convlstm_kernel_size=2) pretrained_model_path = 'data/pie/intention/context_loc_pretrained' data_path = './PIE_dataset' img_path = './PIE_dataset/images' set_id = 'set02' vid_id = 'video_0003' ped_id = '2_3_217' #['2_3_194', '2_3_195', '2_3_196', '2_3_198', '2_3_199', '2_3_200', '2_3_201', '2_3_202', '2_3_203', '2_3_204', '2_3_205', '2_3_206', '2_3_207', '2_3_208', '2_3_209', '2_3_211', '2_3_212', '2_3_213', '2_3_214', '2_3_215', '2_3_216', '2_3_217', '2_3_210', '2_3_197'] imdb = PIE(data_path=data_path) annt = imdb._get_annotations(set_id, vid_id) ped = annt['ped_annotations'] ped_frames = ped[ped_id]['frames'][:30] ped_frames = [i for i in range(63)] #bbox = ped['ped_id']['bbox'] save_path = './bag1_results' # 15 consecutive frames # load bbox bbox_sequences = ped[ped_id]['bbox'][:30] VERSION = 'v0' #Version v0, Total time: 31.7845561504364s, Per frame: 0.5045167642926413s #Version v1, Total time: 24.467329025268555s, Per frame: 0.38837030198838973s
def train_predict( dataset='pie', train_test=2, intent_model_path='data/pie/intention/context_loc_pretrained'): data_opts = { 'fstride': 1, 'sample_type': 'all', 'height_rng': [0, float('inf')], 'squarify_ratio': 0, 'data_split_type': 'default', # kfold, random, default 'seq_type': 'trajectory', 'min_track_size': 61, 'random_params': { 'ratios': None, 'val_data': True, 'regen_data': True }, 'kfold_params': { 'num_folds': 5, 'fold': 1 } } t = PIEPredict() pie_path = os.environ.copy()['PIE_PATH'] if dataset == 'pie': imdb = PIE(data_path=pie_path) traj_model_opts = { 'normalize_bbox': True, 'track_overlap': 0.5, 'observe_length': 15, 'predict_length': 45, 'enc_input_type': ['bbox'], 'dec_input_type': ['intention_prob', 'obd_speed'], 'prediction_type': ['bbox'] } speed_model_opts = { 'normalize_bbox': True, 'track_overlap': 0.5, 'observe_length': 15, 'predict_length': 45, 'enc_input_type': ['obd_speed'], 'dec_input_type': [], 'prediction_type': ['obd_speed'] } traj_model_path = 'data/pie/trajectory/loc_intent_speed_pretrained' speed_model_path = 'data/pie/speed/speed_pretrained' if train_test < 2: beh_seq_val = imdb.generate_data_trajectory_sequence( 'val', **data_opts) beh_seq_train = imdb.generate_data_trajectory_sequence( 'train', **data_opts) traj_model_path = t.train(beh_seq_train, beh_seq_val, **traj_model_opts) speed_model_path = t.train(beh_seq_train, beh_seq_val, **speed_model_opts) if train_test > 0: beh_seq_test = imdb.generate_data_trajectory_sequence( 'test', **data_opts) perf_final = t.test_final(beh_seq_test, traj_model_path=traj_model_path, speed_model_path=speed_model_path, intent_model_path=intent_model_path) t = PrettyTable(['MSE', 'C_MSE']) t.title = 'Trajectory prediction model (loc + PIE_intent + PIE_speed)' t.add_row([perf_final['mse-45'], perf_final['c-mse-45']]) print(t)
def train_intent(train_test=1): data_opts = { 'fstride': 1, 'sample_type': 'all', 'height_rng': [0, float('inf')], 'squarify_ratio': 0, 'data_split_type': 'default', # kfold, random, default 'seq_type': 'intention', # crossing , intention 'min_track_size': 0, # discard tracks that are shorter 'max_size_observe': 15, # number of observation frames 'max_size_predict': 5, # number of prediction frames 'seq_overlap_rate': 0.5, # how much consecutive sequences overlap 'balance': True, # balance the training and testing samples 'crop_type': 'context', # crop 2x size of bbox around the pedestrian 'crop_mode': 'pad_resize', # pad with 0s and resize to VGG input 'encoder_input_type': [], 'decoder_input_type': ['bbox'], 'output_type': ['intention_binary'] } t = PIEIntent(num_hidden_units=128, regularizer_val=0.001, lstm_dropout=0.4, lstm_recurrent_dropout=0.2, convlstm_num_filters=64, convlstm_kernel_size=2) saved_files_path = '' imdb = PIE(data_path=os.environ.copy()['PIE_PATH']) pretrained_model_path = 'data/pie/intention/context_loc_pretrained' if train_test < 2: # Train beh_seq_val = imdb.generate_data_trajectory_sequence( 'val', **data_opts) beh_seq_val = imdb.balance_samples_count(beh_seq_val, label_type='intention_binary') beh_seq_train = imdb.generate_data_trajectory_sequence( 'train', **data_opts) beh_seq_train = imdb.balance_samples_count( beh_seq_train, label_type='intention_binary') saved_files_path = t.train(data_train=beh_seq_train, data_val=beh_seq_val, epochs=400, loss=['binary_crossentropy'], metrics=['accuracy'], batch_size=128, optimizer_type='rmsprop', data_opts=data_opts) print(data_opts['seq_overlap_rate']) if train_test > 0: # Test if saved_files_path == '': saved_files_path = pretrained_model_path beh_seq_test = imdb.generate_data_trajectory_sequence( 'test', **data_opts) acc, f1 = t.test_chunk(beh_seq_test, data_opts, saved_files_path, False) t = PrettyTable(['Acc', 'F1']) t.title = 'Intention model (local_context + bbox)' t.add_row([acc, f1]) print(t) K.clear_session() tf.reset_default_graph() return saved_files_path
import os from sf_gru import SFGRU from pie_data import PIE data_opts = { 'fstride': 1, 'subset': 'default', 'data_split_type': 'random', # kfold, random, default 'seq_type': 'crossing', 'min_track_size': 75 } ## for obs length of 15 frames + 60 frames tte. This should be adjusted for different setup imdb = PIE(data_path=os.environ.copy() ['PIE_PATH']) # change with the path to the dataset model_opts = { 'obs_input_type': ['local_box', 'local_context', 'pose', 'box', 'speed'], 'enlarge_ratio': 1.5, 'pred_target_type': ['crossing'], 'obs_length': 15, # Determines min track size 'time_to_event': 60, # Determines min track size 'dataset': 'pie', 'normalize_boxes': True } method_class = SFGRU() beh_seq_train = imdb.generate_data_trajectory_sequence('train', **data_opts) saved_files_path = method_class.train(beh_seq_train, model_opts=model_opts) beh_seq_train = imdb.generate_data_trajectory_sequence('test', **data_opts) acc, auc, f1, precision, recall = method_class.test(beh_seq_train, saved_files_path)
def run(config_file=None): """ Run train and test on the dataset with parameters specified in configuration file. Args: config_file: path to configuration file in yaml format dataset: dataset to train and test the model on (pie, jaad_beh or jaad_all) """ print(config_file) # Read default Config file configs_default = 'config_files/configs_default.yaml' with open(configs_default, 'r') as f: configs = yaml.safe_load(f) with open(config_file, 'r') as f: model_configs = yaml.safe_load(f) # Update configs based on the model configs for k in ['model_opts', 'net_opts']: if k in model_configs: configs[k].update(model_configs[k]) # Calculate min track size tte = configs['model_opts']['time_to_event'] if isinstance(configs['model_opts']['time_to_event'], int) else \ configs['model_opts']['time_to_event'][1] configs['data_opts'][ 'min_track_size'] = configs['model_opts']['obs_length'] + tte # update model and training options from the config file for dataset_idx, dataset in enumerate( model_configs['exp_opts']['datasets']): configs['data_opts'][ 'sample_type'] = 'beh' if 'beh' in dataset else 'all' configs['model_opts']['overlap'] = 0.6 if 'pie' in dataset else 0.8 configs['model_opts']['dataset'] = dataset.split('_')[0] configs['train_opts']['batch_size'] = model_configs['exp_opts'][ 'batch_size'][dataset_idx] configs['train_opts']['lr'] = model_configs['exp_opts']['lr'][ dataset_idx] configs['train_opts']['epochs'] = model_configs['exp_opts']['epochs'][ dataset_idx] model_name = configs['model_opts']['model'] # Remove speed in case the dataset is jaad if 'RNN' in model_name and 'jaad' in dataset: configs['model_opts']['obs_input_type'] = configs['model_opts'][ 'obs_input_type'] for k, v in configs.items(): print(k, v) # set batch size if model_name in ['ConvLSTM']: configs['train_opts']['batch_size'] = 2 if model_name in ['C3D', 'I3D']: configs['train_opts']['batch_size'] = 16 if model_name in ['PCPA']: configs['train_opts']['batch_size'] = 8 if 'MultiRNN' in model_name: configs['train_opts']['batch_size'] = 8 if model_name in ['TwoStream']: configs['train_opts']['batch_size'] = 16 if configs['model_opts']['dataset'] == 'pie': imdb = PIE(data_path=os.environ.copy()['PIE_PATH']) elif configs['model_opts']['dataset'] == 'jaad': imdb = JAAD(data_path=os.environ.copy()['JAAD_PATH']) # get sequences beh_seq_train = imdb.generate_data_trajectory_sequence( 'train', **configs['data_opts']) beh_seq_val = None # Uncomment the line below to use validation set #beh_seq_val = imdb.generate_data_trajectory_sequence('val', **configs['data_opts']) beh_seq_test = imdb.generate_data_trajectory_sequence( 'test', **configs['data_opts']) # get the model method_class = action_prediction( configs['model_opts']['model'])(**configs['net_opts']) # train and save the model saved_files_path = method_class.train(beh_seq_train, beh_seq_val, **configs['train_opts'], model_opts=configs['model_opts']) # test and evaluate the model acc, auc, f1, precision, recall = method_class.test( beh_seq_test, saved_files_path) # save the results data = {} data['results'] = {} data['results']['acc'] = float(acc) data['results']['auc'] = float(auc) data['results']['f1'] = float(f1) data['results']['precision'] = float(precision) data['results']['recall'] = float(recall) write_to_yaml(yaml_path=os.path.join(saved_files_path, 'results.yaml'), data=data) data = configs write_to_yaml(yaml_path=os.path.join(saved_files_path, 'configs.yaml'), data=data) print('Model saved to {}'.format(saved_files_path))