class NNPushFwdModel(object): def __init__(self, sess, network_params): self._sess = sess self._params = network_params self._device = self._params['device'] self._tf_sumry_wrtr = None self._optimiser = network_params['optimiser'] self._data_configured = False if network_params['write_summary']: if 'summary_dir' in network_params: summary_dir = network_params['summary_dir'] else: summary_dir = None self._tf_sumry_wrtr = TfSummaryWriter(tf_session=sess,summary_dir=summary_dir) cuda_path = '/usr/local/cuda/extras/CUPTI/lib64' curr_ld_path = os.environ["LD_LIBRARY_PATH"] if not cuda_path in curr_ld_path.split(os.pathsep): print "Enviroment variable LD_LIBRARY_PATH does not contain %s"%cuda_path print "Please add it, else the program will crash!" raw_input("Press Ctrl+C") # os.environ["LD_LIBRARY_PATH"] = curr_ld_path + ':'+cuda_path with tf.device(self._device): self._net_ops = tf_model(dim_input=network_params['dim_input'], dim_output=network_params['dim_output'], loss_type='quadratic', cnn_params=network_params['cnn_params'], fc_params=network_params['fc_params'], optimiser_params=network_params['optimiser'], tf_sumry_wrtr=self._tf_sumry_wrtr) self._init_op = tf.initialize_all_variables() self._saver = tf.train.Saver() def init_model(self): with tf.device(self._device): self._sess.run(self._init_op) if self._params['load_saved_model']: self.load_model() def configure_data(self, data_x, data_y, batch_creator): self._data_x = data_x self._data_y = data_y self._batch_creator = batch_creator self._data_configured = True def get_model_path(self): if 'model_dir' in self._params: model_dir = self._params['model_dir'] else: model_path = './fwd/' if not os.path.exists(model_dir): os.makedirs(model_dir) if 'model_name' in self._params: model_name = self._params['model_name'] else: model_name = 'fwd_model.ckpt' return model_dir+model_name def load_model(self): load_tf_check_point(session=self._sess, filename=self.get_model_path()) def save_model(self): save_path = self._saver.save(self._sess, self.get_model_path()) print("Model saved in file: %s" % save_path) def get_data(self): round_complete = None if self._params['batch_params'] is not None: if self._batch_creator is not None: self._data_x, self._data_y, round_complete = self._batch_creator.get_batch(random_samples=self._params['batch_params']['use_random_batches']) else: raise Exception("Batch training chosen but batch_creator not configured") if self._params['cnn_params'] is None: feed_dict = {self._net_ops['x']:self._data_x, self._net_ops['y']:self._data_y} else: feed_dict = {self._net_ops['image_input']:self._data_x, self._net_ops['y']:self._data_y} return feed_dict, round_complete def train(self, epochs): if not self._data_configured: raise Exception("Data not configured, please configure..") if self._params['write_summary']: tf.global_variables_initializer().run() loss = np.zeros(epochs) feed_dict, _ = self.get_data() if self._tf_sumry_wrtr is not None: for i in range(epochs): print "Starting epoch \t", i round_complete = False while not round_complete: if self._params['batch_params'] is not None: feed_dict, round_complete = self.get_data() else: #this is to take care of the case when we are not doing batch training. round_complete = True if round_complete: print "Completed round" if i % 100 == 99: # Record execution stats run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() summary, loss[i] = self._sess.run(fetches=[self._tf_sumry_wrtr._merged, self._net_ops['train_step']], feed_dict=feed_dict, options=run_options, run_metadata=run_metadata) self._tf_sumry_wrtr.add_run_metadata(metadata=run_metadata, itr=i) self._tf_sumry_wrtr.add_summary(summary=summary, itr=i) print('Adding run metadata for', i) else: # Record a summary summary, loss[i] = self._sess.run(fetches=[self._tf_sumry_wrtr._merged, self._net_ops['train_step']], feed_dict=feed_dict) self._tf_sumry_wrtr.add_summary(summary=summary, itr=i) self._tf_sumry_wrtr.close_writer() else: with tf.device(self._device): # Keeping track of loss progress as we train train_step = self._net_ops['train_step'] loss_op = self._net_ops['cost'] for i in range(epochs): _, loss[i] = self._sess.run([train_step, loss_op], feed_dict=feed_dict) return loss def run_op(self, op_name, x_input): with tf.device(self._device): op = self._net_ops[op_name] out = self._sess.run(op, feed_dict={self._net_ops['x']: x_input}) return out
class MDN(object): def __init__(self, sess, network_params): self._sess = sess self._params = network_params self._device = self._params['device'] self._tf_sumry_wrtr = None if network_params['write_summary']: if 'summary_dir' in network_params: summary_dir = network_params['summary_dir'] else: summary_dir = None self._tf_sumry_wrtr = TfSummaryWriter(tf_session=sess, summary_dir=summary_dir) with tf.device(self._device): if self._params['write_summary']: tf.global_variables_initializer().run() self._mdn = MixtureDensityNetwork( network_params, tf_sumry_wrtr=self._tf_sumry_wrtr) self._mdn._init_model() self._net_ops = self._mdn._ops self._init_op = tf.global_variables_initializer() self._saver = tf.train.Saver() def init_model(self): with tf.device(self._device): self._sess.run(self._init_op) if self._params['load_saved_model']: self.load_model() # def load_model(self): # load_tf_check_point(session=self._sess, filename=self.get_model_path()) def save_model(self, path): save_path = self._saver.save(self._sess, path) print("Model saved in file: %s" % save_path) def train(self, x_data, y_data, iterations=10000): with tf.device(self._device): # Keeping track of loss progress as we train loss = np.zeros(iterations) train_op = self._net_ops['train'] loss_op = self._net_ops['loss'] for i in range(iterations): if self._params['write_summary']: run_options = tf.RunOptions( trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() summary, loss[i] = self._sess.run( fetches=[self._tf_sumry_wrtr._merged, train_op], feed_dict={ self._net_ops['x']: x_data, self._net_ops['y']: y_data }, options=run_options, run_metadata=run_metadata) self._tf_sumry_wrtr.add_run_metadata(metadata=run_metadata, itr=i) self._tf_sumry_wrtr.add_summary(summary=summary, itr=i) else: _, loss[i] = self._sess.run([train_op, loss_op], feed_dict={ self._net_ops['x']: x_data, self._net_ops['y']: y_data }) if self._tf_sumry_wrtr is not None: self._tf_sumry_wrtr.close_writer() return loss # TODO: Fix def sample_out(self, x_input, m_samples=10): with tf.device(self._device): out_pi, out_mu, out_sigma = self._sess.run( [ self._net_ops['pi'], self._net_ops['mu'], self._net_ops['sigma'] ], feed_dict={self._net_ops['x']: x_input}) samples = self._generate_mixture_samples(out_pi, out_mu, out_sigma, m_samples) return samples # TODO: Fix def sample_out_max_pi(self, x_input, m_samples=10): with tf.device(self._device): out_pi, out_mu, out_sigma = self._sess.run( [ self._net_ops['pi'], self._net_ops['mu'], self._net_ops['sigma'] ], feed_dict={self._net_ops['x']: x_input}) samples = self._generate_mixture_samples_from_max_pi( out_pi, out_mu, out_sigma, m_samples) return samples # TODO: Fix def expected_out(self, x_input, m_samples=10): with tf.device(self._device): samples = self.sample_out(x_input, m_samples)[0] return np.mean(samples) # TODO: Fix def expected_out2(self, x_input, m_samples=10): with tf.device(self._device): samples = self.sample_out(x_input, m_samples)[0] out = np.zeros(2) for i in range(m_samples): out += np.array([np.cos(samples[i]), np.sin(samples[i])]) out /= m_samples out /= np.linalg.norm(out) return out # TODO: Fix def expected_max_pi_out(self, x_input, m_samples=10): with tf.device(self._device): samples = self.sample_out_max_pi(x_input, m_samples)[0] return np.mean(samples) # TODO: Fix def expected_max_pi_out2(self, x_input, m_samples=10): with tf.device(self._device): samples = self.sample_out_max_pi(x_input, m_samples)[0] out = np.zeros(2) for i in range(m_samples): out += np.array([np.cos(samples[i]), np.sin(samples[i])]) out /= m_samples out /= np.linalg.norm(out) return out def run_op(self, op_name, x_input): with tf.device(self._device): op = self._net_ops[op_name] out = self._sess.run(op, feed_dict={self._net_ops['x']: x_input}) return out def forward(self, xs): out = self._mdn.forward(self._sess, xs) return out def predict(self, xs): out = self._mdn.predict(self._sess, xs) return out def _sample_pi_idx(self, x, pdf): N = pdf.size acc = 0 for i in range(0, N): acc += pdf[i] if (acc >= x): return i print 'failed to sample mixture weight index' return -1 def _max_pi_idx(self, pdf): i = np.argmax(pdf, axis=1) return i # TODO: Fix def _sample_gaussian(self, rn, mu, std): return mu + rn * std # TODO: Fix def _generate_mixture_samples_from_max_pi(self, out_pi, out_mu, out_sigma, m_samples=10): # Number of test inputs N = out_mu.shape[0] D = out_mu.shape[1] # mean dimension M = m_samples result = np.random.rand(N, D, M) # initially random [0, 1] rn = np.random.randn(N, D, M) # normal random matrix (0.0, 1.0) # Generates M samples from the mixture for each test input for j in range(M): for i in range(0, N): idx = self._max_pi_idx(out_pi[i]) mu = out_mu[i, idx] std = out_sigma[i, idx] result[i, :, j] = self._sample_gaussian(rn[i, :, j], mu, std) return result # TODO: Fix def _generate_mixture_samples(self, out_pi, out_mu, out_sigma, m_samples=10): # Number of test inputs N = out_mu.shape[0] D = out_mu.shape[1] # mean dimension M = m_samples result = np.random.rand(N, D, M) # initially random [0, 1] rn = np.random.randn(N, D, M) # normal random matrix (0.0, 1.0) # Generates M samples from the mixture for each test input for j in range(M): for i in range(0, N): idx = self._sample_pi_idx(np.random.rand(1)[0], out_pi[i]) mu = out_mu[i, :, idx] std = out_sigma[i, idx] result[i, :, j] = self._sample_gaussian(rn[i, :, j], mu, std) return result
class MDNPushFwdModel(object): def __init__(self, sess, network_params): self._sess = sess self._params = network_params self._device = self._params['device'] self._tf_sumry_wrtr = None self._optimiser = network_params['optimiser'] self._data_configured = False if network_params['write_summary']: if 'summary_dir' in network_params: summary_dir = network_params['summary_dir'] else: summary_dir = None self._tf_sumry_wrtr = TfSummaryWriter(tf_session=sess, summary_dir=summary_dir) cuda_path = '/usr/local/cuda/extras/CUPTI/lib64' curr_ld_path = os.environ["LD_LIBRARY_PATH"] if not cuda_path in curr_ld_path.split(os.pathsep): print "Enviroment variable LD_LIBRARY_PATH does not contain %s" % cuda_path print "Please add it, else the program will crash!" raw_input("Press Ctrl+C") # os.environ["LD_LIBRARY_PATH"] = curr_ld_path + ':'+cuda_path with tf.device(self._device): self._mdn = MixtureDensityNetwork( network_params, tf_sumry_wrtr=self._tf_sumry_wrtr) self._mdn._init_model() self._net_ops = self._mdn._ops self._init_op = tf.initialize_all_variables() self._saver = tf.train.Saver() def init_model(self): with tf.device(self._device): self._sess.run(self._init_op) if self._params['load_saved_model']: self.load_model() def configure_data(self, data_x, data_y, batch_creator): self._data_x = data_x self._data_y = data_y self._batch_creator = batch_creator self._data_configured = True def get_model_path(self, subscript=None): model_name_subscript = '_' if subscript is not None: if not isinstance(subscript, str): subscript = str(subscript) model_name_subscript = subscript + '_' if 'model_dir' in self._params: model_dir = self._params['model_dir'] else: model_path = './siam/' if not os.path.exists(model_dir): os.makedirs(model_dir) if 'model_name' in self._params: model_name = model_name_subscript + self._params['model_name'] else: model_name = model_name_subscript + 'siam_model.ckpt' return model_dir + model_name def load_model(self, epoch=None): ''' question: is it better to give filename directly or give epoch number? ''' load_tf_check_point(session=self._sess, filename=self.get_model_path(epoch)) def save_model(self, epoch=None): save_path = self._saver.save(self._sess, self.get_model_path(epoch)) print("Model saved in file: %s" % save_path) def get_data(self): round_complete = None if self._params['batch_params'] is not None: if self._batch_creator is not None: self._data_x, self._data_y, round_complete = self._batch_creator.get_batch( random_samples=self._params['batch_params'] ['use_random_batches']) else: raise Exception( "Batch training chosen but batch_creator not configured") if self._params['cnn_params'] is None: feed_dict = { self._net_ops['x']: self._data_x, self._net_ops['y']: self._data_y } else: feed_dict = { self._net_ops['image_input']: self._data_x, self._net_ops['y']: self._data_y } return feed_dict, round_complete def train(self, epochs, chk_pnt_save_invl=10): if not self._data_configured: raise Exception("Data not configured, please configure..") if self._params['write_summary']: tf.global_variables_initializer().run() loss = np.zeros(epochs) feed_dict, _ = self.get_data() if self._tf_sumry_wrtr is not None: for i in range(epochs): print "Starting epoch \t", i round_complete = False while not round_complete: if self._params['batch_params'] is not None: feed_dict, round_complete = self.get_data() else: #this is to take care of the case when we are not doing batch training. round_complete = True if round_complete: print "Completed round" if i % 100 == 99: # Record execution stats run_options = tf.RunOptions( trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() summary, loss[i] = self._sess.run( fetches=[ self._tf_sumry_wrtr._merged, self._net_ops['train'] ], feed_dict=feed_dict, options=run_options, run_metadata=run_metadata) self._tf_sumry_wrtr.add_run_metadata( metadata=run_metadata, itr=i) self._tf_sumry_wrtr.add_summary(summary=summary, itr=i) print('Adding run metadata for', i) else: # Record a summary summary, loss[i] = self._sess.run(fetches=[ self._tf_sumry_wrtr._merged, self._net_ops['train'] ], feed_dict=feed_dict) self._tf_sumry_wrtr.add_summary(summary=summary, itr=i) self._tf_sumry_wrtr.close_writer() else: with tf.device(self._device): # Keeping track of loss progress as we train train_step = self._net_ops['train'] loss_op = self._net_ops['loss'] for i in range(epochs): print "Starting epoch \t", i round_complete = False batch_no = 0 while not round_complete: if self._params['batch_params'] is not None: feed_dict, round_complete = self.get_data() else: #this is to take care of the case when we are not doing batch training. round_complete = True print "Batch number \t", batch_no batch_no += 1 _, loss[i] = self._sess.run([train_step, loss_op], feed_dict=feed_dict) if round_complete: print "That was the last round of epoch %d" % i if i % chk_pnt_save_invl == 0 and i != 0: self.save_model(epoch=i) np.savetxt('loss_values.txt', np.asarray(loss)) plt.figure() plt.plot(loss) plt.show() return loss def run_op(self, op_name, x_input): with tf.device(self._device): op = self._net_ops[op_name] out = self._sess.run(op, feed_dict={self._net_ops['x']: x_input}) return out