# To view graph: tensorboard --logdir=/Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/tf_visual_data/runs/1/ tf.scalar_summary('accuracy', accuracy) merged = tf.merge_all_summaries() train_dir = mkdir(tfboard_run_dir+"/trn/convnet/") train_activation_layer_1 = mkdir(tfboard_run_dir+"/trn_actvtn_lyr1") train_activation_layer_2 = mkdir(tfboard_run_dir+"/trn_actvtn_lyr2") train_activation_layer_3 = mkdir(tfboard_run_dir+"/trn_actvtn_lyr3") train_activation_layer_4 = mkdir(tfboard_run_dir+"/trn_actvtn_lyr4") validation_dir = mkdir(tfboard_run_dir+"/vld/convnet/") # Archive this script to document model design in event of good results that need to be replicated model_file_path = os.path.dirname(os.path.realpath(__file__))+'/'+os.path.basename(__file__) shell_command('cp {model_file} {archive_path}'.format(model_file=model_file_path,archive_path=tfboard_run_dir+'/')) train_writer = tf.train.SummaryWriter(train_dir,sess.graph) validation_writer = tf.train.SummaryWriter(validation_dir,sess.graph) validation_predictors[:1000] = validation_predictors[:1000] / 255 sess.run(tf.initialize_all_variables()) batch_index = 0 batches_per_epoch = (train_predictors.shape[0] - train_predictors.shape[0] % 50)/50 for i in range(batch_iterations): # Shuffle in the very beginning and after each epoch if batch_index % batches_per_epoch == 0: train_predictors, train_targets = shuffle_dataset(train_predictors, train_targets) batch_index = 0
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # To view graph: tensorboard --logdir=/Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/tf_visual_data/runs tf.scalar_summary('accuracy', accuracy) merged = tf.merge_all_summaries() tfboard_basedir = '/Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/tf_visual_data/runs/' tfboard_run_dir = mkdir_tfboard_run_dir(tfboard_basedir) train_dir = mkdir(tfboard_run_dir + "/trn/mlp/") validation_dir = mkdir(tfboard_run_dir + "/vld/mlp/") # Archive this script to document model design in event of good results that need to be replicated model_file_path = os.path.dirname( os.path.realpath(__file__)) + '/' + os.path.basename(__file__) shell_command('cp {model_file} {archive_path}'.format( model_file=model_file_path, archive_path=tfboard_run_dir + '/')) train_writer = tf.train.SummaryWriter(train_dir, sess.graph) validation_writer = tf.train.SummaryWriter(validation_dir, sess.graph) sess.run(tf.initialize_all_variables()) batch_index = 0 batches_per_epoch = (train_predictors.shape[0] - train_predictors.shape[0] % 50) / 50 for i in range(batch_iterations): # Shuffle in the very beginning and after each epoch if batch_index % batches_per_epoch == 0: train_predictors, train_targets = shuffle_dataset( train_predictors, train_targets) batch_index = 0
input_file_path = input_dir_path + '/predictors_and_targets.npz' npzfile = np.load(input_file_path) image_count = len(npzfile['predictors']) metadata_file_path = input_dir_path + '/metadata.txt' with open(metadata_file_path, 'w') as writer: writer.write('image_count:' + str(image_count) + '\n') data_path = '/Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/data' data_folders = os.listdir(data_path) data_folders = sanitize_data_folders(data_folders) gamma_map = make_gamma_tables([1]) # I should refactor-out the gamma at some point. It's not needed here rgb = True for folder in data_folders: cmd = 'ls '+data_path + '/' + folder dir_contents = str(shell_command(cmd)) print("Started work on "+str(folder)) print(dir_contents) input_dir_path = data_path + '/' + folder if 'predictors_and_targets.npz' not in dir_contents: predictors, targets = process_session(data_path + '/' + folder, gamma_map, rgb) video_to_rgb_npz(input_dir_path,predictors,targets) print("Completed work on: "+str(folder)+". Created new npz and metadata files.") write_metadata(input_dir_path) elif 'metadata.csv' not in dir_contents: write_metadata(input_dir_path) print("Added only metadata file for: " + str(folder) + ".") else: print("Completed work on "+str(dir)+". File already exists. No processing necessary.") print("Finished.")
import numpy as np import argparse from util import shell_command, remove_file_if_exists ''' python frame_count.py \ -i /Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/final_processed_data_3_channels.npz \ -o /Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/ ''' ap = argparse.ArgumentParser() ap.add_argument("-i", "--input", required = True, help = "path to input numpy dataset") ap.add_argument("-o", "--output", required = True, help = "path to shape output") args = vars(ap.parse_args()) input_path = args["input"] output_path = args["output"] npzfile = np.load(input_path) train_predictors = npzfile['train_predictors'] train_targets = npzfile['train_targets'] shape = train_predictors.shape shape = shape[0] remove_file_if_exists(output_path+'/shape') shell_command('echo "{shape}" >> {output}/shape'.format(shape=str(shape),output=output_path))
input_file_path = input_dir_path + '/predictors_and_targets.npz' npzfile = np.load(input_file_path) image_count = len(npzfile['predictors']) metadata_file_path = input_dir_path + '/metadata.txt' with open(metadata_file_path, 'w') as writer: writer.write('image_count:' + str(image_count) + '\n') data_path = '/Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/data' data_folders = os.listdir(data_path) data_folders = sanitize_data_folders(data_folders) gamma_map = make_gamma_tables([1]) # I should refactor-out the gamma at some point. It's not needed here rgb = True for folder in data_folders: cmd = 'ls '+data_path + '/' + folder dir_contents = str(shell_command(cmd)) print("Started work on "+str(folder)) print(dir_contents) input_dir_path = data_path + '/' + folder if 'predictors_and_targets.npz' not in dir_contents: predictors, targets = process_session(data_path + '/' + folder, gamma_map, rgb) video_to_rgb_npz(input_dir_path,predictors,targets) print("Completed work on: "+str(folder)+". Created new npz and metadata files.") write_metadata(input_dir_path) elif 'metadata.csv' not in dir_contents: write_metadata(input_dir_path) print("Added only metadata file for: " + str(folder) + ".") else: print("Completed work on "+str(dir)+". File already exists. No processing necessary.") print("Finished.")
sys.exit(1) processed_images = np.array(processed_images) return processed_images # Generates negative images from previous driving session. def random_negative_images(): pass pixel_size = 24 # Assumes positive images are each separate files positive_image_dir = "/Users/ryanzotti/Dropbox/StopSigns" shell_cmd = 'ls {dir}'.format(dir=positive_image_dir) dir_contents = str(shell_command(shell_cmd)).replace("b\'", "").split("\\n") file_names = [file_name for file_name in dir_contents if "JPG" in file_name] positive_images = process_image_set(positive_image_dir, pixel_size) # Assumes negative images are taken from existing numpy zip file. # I used my training data. This is ideal because it is # representive of backgrounds that I would realistically see in a # production setting negative_image_file_path = "/Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/final_processed_data_3_channels.npz" npzfile = np.load(negative_image_file_path) train_predictors = npzfile['train_predictors'] shape = train_predictors.shape total_frames = shape[0] height, width = shape[1], shape[2] negative_images = [] negative_images_count = 3000
else: print(file_name + " is not a square! It has dimensions " + str(image.shape)) sys.exit(1) processed_images = np.array(processed_images) return processed_images # Generates negative images from previous driving session. def random_negative_images(): pass pixel_size = 24 # Assumes positive images are each separate files positive_image_dir = "/Users/ryanzotti/Dropbox/StopSigns" shell_cmd = 'ls {dir}'.format(dir=positive_image_dir) dir_contents = str(shell_command(shell_cmd)).replace("b\'","").split("\\n") file_names = [file_name for file_name in dir_contents if "JPG" in file_name] positive_images = process_image_set(positive_image_dir,pixel_size) # Assumes negative images are taken from existing numpy zip file. # I used my training data. This is ideal because it is # representive of backgrounds that I would realistically see in a # production setting negative_image_file_path = "/Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/final_processed_data_3_channels.npz" npzfile = np.load(negative_image_file_path) train_predictors = npzfile['train_predictors'] shape = train_predictors.shape total_frames = shape[0] height, width = shape[1], shape[2] negative_images = [] negative_images_count = 3000
def train(self, sess, x, y_, accuracy, train_step, train_feed_dict, test_feed_dict): # To view graph: tensorboard --logdir=/Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/tf_visual_data/runs tf.summary.scalar('accuracy_summary', accuracy) merged = tf.summary.merge_all() # Archive the model script in case of good results that need to be replicated # If model is being restored, then assume model file has already been saved somewhere # and that self.model_file is None if self.model_file is not None: cmd = 'cp {model_file} {archive_path}' shell_command( cmd.format(model_file=self.model_file, archive_path=self.model_dir + '/')) if not self.restored_model: # Don't want to erase restored model weights sess.run(tf.global_variables_initializer()) dataset = Dataset(input_file_path=self.data_path, max_sample_records=self.max_sample_records) # TODO: Document and understand what RunOptions does run_opts = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_opts_metadata = tf.RunMetadata() train_images, train_labels = process_data( dataset.get_sample(train=True)) train_feed_dict[x] = train_images train_feed_dict[y_] = train_labels train_summary, train_accuracy = sess.run( [merged, accuracy], feed_dict=train_feed_dict, options=run_opts, run_metadata=run_opts_metadata) test_images, test_labels = process_data( dataset.get_sample(train=False)) test_feed_dict[x] = test_images test_feed_dict[y_] = test_labels test_summary, test_accuracy = sess.run([merged, accuracy], feed_dict=test_feed_dict, options=run_opts, run_metadata=run_opts_metadata) # Always worth printing accuracy, even for a restored model, since it provides an early sanity check message = "epoch: {0}, training accuracy: {1}, validation accuracy: {2}" print(message.format(self.start_epoch, train_accuracy, test_accuracy)) if self.tf_timeline: # Used for debugging slow Tensorflow code create_tf_timeline(self.model_dir, run_opts_metadata) # Don't double-count. A restored model already has its last checkpoint and results.txt entry available if not self.restored_model: with open(self.results_file, 'a') as f: f.write( message.format(self.start_epoch, train_accuracy, test_accuracy) + '\n') self.save_model(sess, epoch=self.start_epoch) sync_to_aws(s3_path=self.s3_data_dir, local_path=self.data_path) # Save to AWS for epoch in range(self.start_epoch + 1, self.start_epoch + self.n_epochs): prev_time = datetime.now() train_batches = dataset.get_batches(train=True) for batch_id, batch in enumerate(train_batches): images, labels = process_data(batch) train_feed_dict[x] = images train_feed_dict[y_] = labels sess.run(train_step, feed_dict=train_feed_dict) # Track speed to better compare GPUs and CPUs now = datetime.now() diff_seconds = (now - prev_time).total_seconds() if self.show_speed: speed_results = 'batch {batch_id} of {total_batches}, {seconds} seconds' speed_results = speed_results.format( batch_id=batch_id, seconds=diff_seconds, total_batches=dataset.batches_per_epoch) with open(self.speed_file, 'a') as f: f.write(speed_results + '\n') print(speed_results) prev_time = datetime.now() # TODO: Document and understand what RunOptions does run_opts = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_opts_metadata = tf.RunMetadata() train_images, train_labels = process_data( dataset.get_sample(train=True)) train_feed_dict[x] = train_images train_feed_dict[y_] = train_labels train_summary, train_accuracy = sess.run( [merged, accuracy], feed_dict=train_feed_dict, options=run_opts, run_metadata=run_opts_metadata) test_images, test_labels = process_data( dataset.get_sample(train=False)) test_feed_dict[x] = test_images test_feed_dict[y_] = test_labels test_summary, test_accuracy = sess.run( [merged, accuracy], feed_dict=test_feed_dict, options=run_opts, run_metadata=run_opts_metadata) print(message.format(epoch, train_accuracy, test_accuracy)) with open(self.results_file, 'a') as f: f.write( message.format(epoch, train_accuracy, test_accuracy) + '\n') # Save a model checkpoint after every epoch self.save_model(sess, epoch=epoch) sync_to_aws(s3_path=self.s3_data_dir, local_path=self.data_path) # Save to AWS # Marks unambiguous successful completion to prevent deletion by cleanup script shell_command('touch ' + self.model_dir + '/SUCCESS')
def train(self, sess, x, y_, accuracy, train_step, train_feed_dict, test_feed_dict): # To view graph: tensorboard --logdir=/Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/tf_visual_data/runs tf.scalar_summary('accuracy', accuracy) merged = tf.merge_all_summaries() # Archive this script to document model design in event of good results that need to be replicated model_file_path = os.path.dirname( os.path.realpath(__file__)) + '/' + os.path.basename(__file__) cmd = 'cp {model_file} {archive_path}' shell_command( cmd.format(model_file=self.model_file, archive_path=self.tfboard_run_dir + '/')) sess.run(tf.initialize_all_variables()) dataset = Dataset(input_file_path=self.data_path, max_sample_records=self.max_sample_records) # Not sure what these two lines do run_opts = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_opts_metadata = tf.RunMetadata() train_images, train_labels = process_data( dataset.get_sample(train=True)) train_feed_dict[x] = train_images train_feed_dict[y_] = train_labels train_summary, train_accuracy = sess.run( [merged, accuracy], feed_dict=train_feed_dict, options=run_opts, run_metadata=run_opts_metadata) test_images, test_labels = process_data( dataset.get_sample(train=False)) test_feed_dict[x] = test_images test_feed_dict[y_] = test_labels test_summary, test_accuracy = sess.run([merged, accuracy], feed_dict=test_feed_dict, options=run_opts, run_metadata=run_opts_metadata) message = "epoch: {0}, training accuracy: {1}, validation accuracy: {2}" print(message.format(-1, train_accuracy, test_accuracy)) with open(self.results_file, 'a') as f: f.write(message.format(-1, train_accuracy, test_accuracy) + '\n') for epoch in range(self.epochs): train_batches = dataset.get_batches(train=True) for batch in train_batches: images, labels = process_data(batch) train_feed_dict[x] = images train_feed_dict[y_] = labels train_step.run(feed_dict=train_feed_dict) # TODO: remove all this hideous boilerplate run_opts = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_opts_metadata = tf.RunMetadata() train_images, train_labels = process_data( dataset.get_sample(train=True)) train_feed_dict[x] = train_images train_feed_dict[y_] = train_labels train_summary, train_accuracy = sess.run( [merged, accuracy], feed_dict=train_feed_dict, options=run_opts, run_metadata=run_opts_metadata) test_images, test_labels = process_data( dataset.get_sample(train=False)) test_feed_dict[x] = test_images test_feed_dict[y_] = test_labels test_summary, test_accuracy = sess.run( [merged, accuracy], feed_dict=test_feed_dict, options=run_opts, run_metadata=run_opts_metadata) print(message.format(epoch, train_accuracy, test_accuracy)) with open(self.results_file, 'a') as f: f.write( message.format(epoch, train_accuracy, test_accuracy) + '\n') # Save the trained model to a file saver = tf.train.Saver() save_path = saver.save(sess, self.tfboard_run_dir + "/model.ckpt") # Marks unambiguous successful completion to prevent deletion by cleanup script shell_command('touch ' + self.tfboard_run_dir + '/SUCCESS')
def train(self, sess, x, y_, accuracy, train_step, train_feed_dict, test_feed_dict): # To view graph: tensorboard --logdir=/Users/ryanzotti/Documents/repos/Self_Driving_RC_Car/tf_visual_data/runs tf.summary.scalar('accuracy', accuracy) merged = tf.summary.merge_all() # Archive the model script in case of good results that need to be replicated # If model is being restored, then assume model file has already been saved somewhere # and that self.model_file is None if self.model_file is not None: cmd = 'cp {model_file} {archive_path}' shell_command( cmd.format(model_file=self.model_file, archive_path=self.tfboard_run_dir + '/')) sess.run(tf.global_variables_initializer()) dataset = Dataset(input_file_path=self.data_path, max_sample_records=self.max_sample_records) # TODO: Document and understand what RunOptions does run_opts = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_opts_metadata = tf.RunMetadata() train_images, train_labels = process_data( dataset.get_sample(train=True)) train_feed_dict[x] = train_images train_feed_dict[y_] = train_labels train_summary, train_accuracy = sess.run( [merged, accuracy], feed_dict=train_feed_dict, options=run_opts, run_metadata=run_opts_metadata) test_images, test_labels = process_data( dataset.get_sample(train=False)) test_feed_dict[x] = test_images test_feed_dict[y_] = test_labels test_summary, test_accuracy = sess.run([merged, accuracy], feed_dict=test_feed_dict, options=run_opts, run_metadata=run_opts_metadata) message = "epoch: {0}, training accuracy: {1}, validation accuracy: {2}" print(message.format(self.start_epoch, train_accuracy, test_accuracy)) with open(self.results_file, 'a') as f: f.write( message.format(self.start_epoch, train_accuracy, test_accuracy) + '\n') # Save a model checkpoint after every epoch self.save_model(sess, epoch=self.start_epoch) for epoch in range(self.start_epoch + 1, self.start_epoch + self.n_epochs): train_batches = dataset.get_batches(train=True) for batch in train_batches: images, labels = process_data(batch) train_feed_dict[x] = images train_feed_dict[y_] = labels sess.run(train_step, feed_dict=train_feed_dict) # TODO: Document and understand what RunOptions does run_opts = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_opts_metadata = tf.RunMetadata() train_images, train_labels = process_data( dataset.get_sample(train=True)) train_feed_dict[x] = train_images train_feed_dict[y_] = train_labels train_summary, train_accuracy = sess.run( [merged, accuracy], feed_dict=train_feed_dict, options=run_opts, run_metadata=run_opts_metadata) test_images, test_labels = process_data( dataset.get_sample(train=False)) test_feed_dict[x] = test_images test_feed_dict[y_] = test_labels test_summary, test_accuracy = sess.run( [merged, accuracy], feed_dict=test_feed_dict, options=run_opts, run_metadata=run_opts_metadata) print(message.format(epoch, train_accuracy, test_accuracy)) with open(self.results_file, 'a') as f: f.write( message.format(epoch, train_accuracy, test_accuracy) + '\n') # Save a model checkpoint after every epoch self.save_model(sess, epoch=epoch) # Marks unambiguous successful completion to prevent deletion by cleanup script shell_command('touch ' + self.tfboard_run_dir + '/SUCCESS')