def test_dpf(task='nav01', data_path='../data/100s', model_path='../models/tmp'): # load test data test_data = load_data(data_path=data_path, filename=task + '_test') noisy_test_data = noisyfy_data(test_data) test_batch_iterator = make_batch_iterator(noisy_test_data, seq_len=50) # reset tensorflow graph tf.reset_default_graph() # instantiate method hyperparams = get_default_hyperparams() method = DPF(**hyperparams['global']) with tf.Session() as session: # load method and apply to new data method.load(session, model_path) for i in range(10): test_batch = next(test_batch_iterator) test_batch_input = remove_state(test_batch, provide_initial_state=False) result = method.predict(session, test_batch_input, **hyperparams['test'])
def plot_models(): task = 'nav01' data_path = '../data/100s' test_data = load_data(data_path=data_path, filename=task + '_test') noisy_test_data = noisyfy_data(reduce_data(test_data, 10)) num_examples = 10 # same seqlen and batchsize needed here! # test_batch_iterator = make_batch_iterator(noisy_test_data, seq_len=50, batch_size=50) test_batch_iterator = make_batch_iterator(noisy_test_data, seq_len=50, batch_size=num_examples) batch = next(test_batch_iterator) # for i in range(num_examples): # plot_observation(batch, i=0, t=i) predictions = dict() for variant, file_name in { 'ind_e2e': '2017-12-23_03:32:47_compute-0-9_nav01_pf_ind_e2e_1000', # 'ind_e2e': '2017-12-22_18:30:30_compute-0-1_nav02_pf_ind_e2e_1000', # 'lstm': '2017-12-24_13:25:53_compute-0-1_nav01_lstm_1000', # 'lstm': '2017-12-22_18:29:21_compute-1-2_nav02_lstm_1000', # 'ind': '2017-12-23_00:48:08_compute-0-74_nav01_pf_ind_500', # 'e2e': '2017-12-22_18:29:49_compute-0-15_nav01_pf_e2e_500', }.items(): with open('../log/lc/' + file_name, 'rb') as f: log = pickle.load(f) hyper_params = log['hyper_params'][0] model_path = '../models/' + log['exp_params'][0]['model_path'].split( '/models/')[-1] # ['exp_params']['model_path] # reset tensorflow graph tf.reset_default_graph() # instantiate method if 'lstm' in variant: method = RNN(**hyper_params['global']) else: method = DPF(**hyper_params['global']) with tf.Session() as session: # load method and apply to new data statistics = method.load(session, model_path) # print('predicting now') # predictions[variant] = method.predict(session, batch, num_particles=1000, return_particles=False) # print('prediction done') # plot_measurement_model(session, method, statistics, batch, task, num_examples, variant) # plot_proposer(session, method, statistics, batch, task, num_examples, variant) # plot_motion_model(session, method, statistics, batch, task, 10, 50, variant) plot_particle_filter(session, method, statistics, batch, task, num_examples, 1000, variant) print(predictions.keys()) # plot_prediction(predictions['ind_e2e'], predictions['lstm'], statistics, batch, task, num_examples, variant) plt.pause(10000.0)
def get_train_data_and_eval_iterator(data, exp_params): # noisify for k in ['train', 'test']: data[k] = noisify_data_condition(data[k], exp_params['noise_condition']) # form batches eval_batch_iterators = { k: make_batch_iterator(data[k], batch_size=exp_params['eval_batch_size'], seq_len=exp_params['eval_seq_len']) for k in ['test'] } return data['train'], eval_batch_iterators
def get_train_data_and_eval_iterator(data, exp_params): # noisify training data according to sampled noise condition data['train'] = noisify_data_condition(data['train'], exp_params['noise_condition']) # create eval batch iterators for every noise condition eval_batch_iterators = dict() for condition in noise_conds: key = 'test' + '_' + condition data[key] = noisify_data_condition(data['test'], condition) eval_batch_iterators[key] = make_batch_iterator( data[key], batch_size=exp_params['eval_batch_size'], seq_len=exp_params['eval_seq_len']) return data['train'], eval_batch_iterators
def plot_statistics(): task = 'nav02' data_path = '../data/100s' test_data = load_data(data_path=data_path, filename=task + '_test') noisy_test_data = noisyfy_data(test_data) # noisy_test_data = noisyfy_data(test_data) batch_size = 32 test_batch_iterator = make_batch_iterator(noisy_test_data, seq_len=2, batch_size=batch_size) filenames = { 'ind_e2e': '2017-12-22_18:30:30_compute-0-1_nav02_pf_ind_e2e_1000', 'ind': '2017-12-23_06:56:07_compute-0-26_nav02_pf_ind_1000', 'e2e': '2017-12-24_00:51:18_compute-1-0_nav02_pf_e2e_1000', } for variant in ['ind', 'e2e', 'ind_e2e']: file_name = filenames[variant] with open('../log/lc/' + file_name, 'rb') as f: log = pickle.load(f) hyper_params = log['hyper_params'][0] model_path = '../models/' + log['exp_params'][0]['model_path'].split( '/models/')[-1] # ['exp_params']['model_path] # reset tensorflow graph tf.reset_default_graph() # instantiate method method = DPF(**hyper_params['global']) with tf.Session() as session: # load method and apply to new data statistics = method.load(session, model_path) plot_measurement_statistics(session, method, statistics, test_batch_iterator, batch_size, variant) plot_motion_statistics(session, method, statistics, test_batch_iterator, task, variant) plt.pause(10000.0)
def fit(self, sess, data, model_path, train_individually, train_e2e, split_ratio, seq_len, batch_size, epoch_length, num_epochs, patience, learning_rate, dropout_keep_ratio, num_particles, particle_std, plot_task=None, plot=False): self.particle_std = particle_std # preprocess data data = split_data(data, ratio=split_ratio) epoch_lengths = {'train': epoch_length, 'val': epoch_length * 2} batch_iterators = { 'train': make_batch_iterator(data['train'], seq_len=seq_len, batch_size=batch_size), 'val': make_repeating_batch_iterator(data['val'], epoch_lengths['val'], batch_size=batch_size, seq_len=seq_len), 'train_ex': make_batch_iterator(data['train'], batch_size=batch_size, seq_len=seq_len), 'val_ex': make_batch_iterator(data['val'], batch_size=batch_size, seq_len=seq_len), 'train1': make_batch_iterator(data['train'], batch_size=batch_size, seq_len=2), 'val1': make_repeating_batch_iterator(data['val'], epoch_lengths['val'], batch_size=batch_size, seq_len=2), } # compute some statistics of the training data means, stds, state_step_sizes, state_mins, state_maxs = compute_staticstics( data['train']) # build the tensorflow graph by connecting all modules in the particles filter particles, particle_probs, encodings, particle_list, particle_probs_list = self.connect_modules( means, stds, state_mins, state_maxs, state_step_sizes) # define losses and train stages for different ways of training (e.g. training individual models and e2e training) losses, train_stages = self.compile_training_stages( sess, batch_iterators, particle_list, particle_probs_list, encodings, means, stds, state_step_sizes, state_mins, state_maxs, learning_rate, plot_task) # initialize variables init = tf.global_variables_initializer() sess.run(init) # save statistics and prepare saving variables if not os.path.exists(model_path): os.makedirs(model_path) np.savez(os.path.join(model_path, 'statistics'), means=means, stds=stds, state_step_sizes=state_step_sizes, state_mins=state_mins, state_maxs=state_maxs) saver = tf.train.Saver() save_path = os.path.join(model_path, 'best_validation') # define the training curriculum curriculum = [] if train_individually: if self.learn_odom: curriculum += ['train_odom'] curriculum += ['train_motion_sampling'] curriculum += ['train_measurement_model'] if self.use_proposer: curriculum += ['train_particle_proposer'] if train_e2e: curriculum += ['train_e2e'] # split data for early stopping data_keys = ['train'] if split_ratio < 1.0: data_keys.append('val') # define log dict log = { c: { dk: { lk: { 'mean': [], 'se': [] } for lk in train_stages[c]['monitor_losses'] } for dk in data_keys } for c in curriculum } # go through curriculum for c in curriculum: stage = train_stages[c] best_val_loss = np.inf best_epoch = 0 epoch = 0 while epoch < num_epochs and epoch - best_epoch < patience: # training for dk in data_keys: # don't train in the first epoch, just evaluate the initial parameters if dk == 'train' and epoch == 0: continue # set up loss lists which will be filled during the epoch loss_lists = {lk: [] for lk in stage['monitor_losses']} for e in range(epoch_lengths[dk]): # t0 = time.time() # pick a batch from the right iterator batch = next( batch_iterators[stage['batch_iterator_names'][dk]]) # define the inputs and train/run the model input_dict = { **{ self.placeholders[key]: batch[key] for key in 'osa' }, **{ self.placeholders['num_particles']: num_particles }, } if dk == 'train': input_dict[self.placeholders[ 'keep_prob']] = dropout_keep_ratio monitor_losses = { l: losses[l] for l in stage['monitor_losses'] } if dk == 'train': s_losses, _ = sess.run( [monitor_losses, stage['train_op']], input_dict) else: s_losses = sess.run(monitor_losses, input_dict) for lk in stage['monitor_losses']: loss_lists[lk].append(s_losses[lk]) # after each epoch, compute and log statistics for lk in stage['monitor_losses']: log[c][dk][lk]['mean'].append(np.mean(loss_lists[lk])) log[c][dk][lk]['se'].append( np.std(loss_lists[lk], ddof=1) / np.sqrt(len(loss_lists[lk]))) # check whether the current model is better than all previous models if 'val' in data_keys: current_val_loss = log[c]['val'][ stage['validation_loss']]['mean'][-1] if current_val_loss < best_val_loss: best_val_loss = current_val_loss best_epoch = epoch # save current model saver.save(sess, save_path) txt = 'epoch {:>3} >> '.format(epoch) else: txt = 'epoch {:>3} == '.format(epoch) else: best_epoch = epoch saver.save(sess, save_path) txt = 'epoch {:>3} >> '.format(epoch) # after going through all data sets, do a print out of the current result for lk in stage['monitor_losses']: txt += '{}: '.format(lk) for dk in data_keys: if len(log[c][dk][lk]['mean']) > 0: txt += '{:.2f}+-{:.2f}/'.format( log[c][dk][lk]['mean'][-1], log[c][dk][lk]['se'][-1]) txt = txt[:-1] + ' -- ' print(txt) # t1 = time.time() # time_deltas.append(t1 - t0) if plot: stage['plot'](epoch) epoch += 1 # after running out of patience, restore the model with lowest validation loss saver.restore(sess, save_path) return log