def train_path_planning_network(): """Train Path Planning Network Trains an Evolino LSTM neural network for long-term path planning for use in the surgical simulator. Returns: A copy of the fully-trained path planning neural network. """ # Build up the list of files to use as training set training_dir = constants.G_TRAINING_DATA_DIR # Find all data files in the training data directory training_files = pathutils.list_data_files(training_dir) # Get the training data and place it into a dataset training_dataset = None # Store all training set ratings ratings = np.array([]) for training_file in training_files: training_data = datastore.retrieve(training_file) # Normalize the time input of the data training_data = pathutils.normalize_time(training_data, t_col=constants.G_TIME_IDX) # Add this data sample to the training dataset training_dataset = datastore.list_to_dataset( training_data[:,constants.G_RNN_INPUT_IDX:constants.G_RNN_INPUT_IDX+constants.G_RNN_NUM_INPUTS], training_data[:,constants.G_RNN_OUTPUT_IDX:constants.G_RNN_OUTPUT_IDX+constants.G_RNN_NUM_OUTPUTS], dataset=training_dataset ) # Store the rating of the data this_rating = training_data[1:,constants.G_RATING_IDX] ratings = np.hstack((ratings, this_rating)) # Get the starting point information for testing output_start_idx = constants.G_RNN_OUTPUT_IDX output_end_idx = output_start_idx + constants.G_RNN_NUM_OUTPUTS output_initial_condition = training_data[0,output_start_idx:output_end_idx] # Generate the time sequence input data for testing time_steps = constants.G_RNN_GENERATED_TIME_STEPS t_input = np.linspace(start=0.0, stop=1.0, num=time_steps) t_input = np.reshape(t_input, (len(t_input), 1)) gate_start_idx = constants.G_GATE_IDX gate_end_idx = gate_start_idx + constants.G_NUM_GATE_INPUTS # Pull the gate data from the last training dataset gate_data = training_data[0:1,gate_start_idx:gate_end_idx] gate_data = np.tile(gate_data, (time_steps, 1)) # Build up a full ratings matrix nd_ratings = None for rating in ratings: this_rating = rating * np.ones((1, constants.G_RNN_NUM_OUTPUTS)) if nd_ratings is None: nd_ratings = this_rating else: nd_ratings = np.vstack((nd_ratings, this_rating)) # Create network and trainer print('>>> Building Network...') net = PathPlanningNetwork() print('>>> Initializing Trainer...') trainer = PathPlanningTrainer( evolino_network=net, dataset=training_dataset, nBurstMutationEpochs=10, importance=nd_ratings ) # Begin the training iterations fitness_list = [] max_fitness = None max_fitness_epoch = None # Draw the generated path plot fig = plt.figure(1, facecolor='white') testing_axis = fig.add_subplot(111, projection='3d') fig.show() idx = 0 current_convergence_streak = 0 while True: print('>>> Training Network (Iteration: %3d)...' % (idx+1)) trainer.train() # Determine fitness of this network current_fitness = trainer.evaluation.max_fitness fitness_list.append(current_fitness) print('>>> FITNESS: %f' % current_fitness) # Determine if this is the minimal error network if max_fitness is None or max_fitness < current_fitness: # This is the minimum, record it max_fitness = current_fitness max_fitness_epoch = idx # Draw the generated path after training print('>>> Testing Network...') generated_output = net.extrapolate(t_input, [output_initial_condition], len(t_input)-1) generated_output = np.vstack((output_initial_condition, generated_output)) generated_input = np.hstack((t_input, gate_data)) # Smash together the input and output generated_data = np.hstack((generated_input, generated_output)) print('>>> Drawing Generated Path...') pathutils.display_path(testing_axis, generated_data, title='Generated Testing Path') plt.draw() if current_fitness > constants.G_RNN_CONVERGENCE_THRESHOLD: # We've encountered a fitness higher than threshold current_convergence_streak += 1 else: # Streak ended. Reset the streak counter current_convergence_streak = 0 if current_convergence_streak == constants.G_RNN_REQUIRED_CONVERGENCE_STREAK: print('>>> Convergence Achieved: %d Iterations' % idx) break elif idx == constants.G_RNN_MAX_ITERATIONS - 1: print('>>> Reached maximum iterations (%d)' % constants.G_RNN_MAX_ITERATIONS) break idx += 1 # Draw the iteration fitness plot plt.figure(facecolor='white') plt.cla() plt.title('Fitness of RNN over %d Iterations' % (idx-1)) plt.xlabel('Training Iterations') plt.ylabel('Fitness') plt.grid(True) plt.plot(range(len(fitness_list)), fitness_list, 'r-') plt.annotate('local max', xy=(max_fitness_epoch, fitness_list[max_fitness_epoch]), xytext=(max_fitness_epoch, fitness_list[max_fitness_epoch]+0.01), arrowprops=dict(facecolor='black', shrink=0.05)) plt.show() # Return a full copy of the trained neural network return copy.deepcopy(net)
def train_lt_network(): """Train Long-Term Network Trains an Evolino LSTM neural network for long-term path planning for use in the surgical simulator. Returns: A copy of the fully-trained path planning neural network. """ # Build up the list of files to use as training set filepath = '../../results/' training_filenames = [ 'sample1.dat', 'sample2.dat', 'sample3.dat', 'sample4.dat', ] testing_filename = 'sample5.dat' # Get the training data and place it into a dataset training_dataset = None # Store all training set ratings ratings = np.array([]) for training_filename in training_filenames: training_file = filepath + training_filename training_data = datastore.retrieve(training_file) # Normalize the time input of the data training_data = pathutils.normalize_time(training_data, t_col=constants.G_TIME_IDX) # Add this data sample to the training dataset training_dataset = datastore.list_to_dataset( training_data[:, constants.G_LT_INPUT_IDX:constants.G_LT_INPUT_IDX + constants.G_LT_NUM_INPUTS], training_data[:, constants.G_LT_OUTPUT_IDX:constants.G_LT_OUTPUT_IDX + constants.G_LT_NUM_OUTPUTS], dataset=training_dataset) # Store the rating of the data this_rating = training_data[1:, constants.G_RATING_IDX] ratings = np.hstack((ratings, this_rating)) # Get testing data testing_file = filepath + testing_filename testing_data = datastore.retrieve(testing_file) testing_data = pathutils.normalize_time(testing_data, t_col=constants.G_TIME_IDX) # Store the testing data in a datastore object testing_dataset = datastore.list_to_dataset( testing_data[:, constants.G_LT_INPUT_IDX:constants.G_LT_INPUT_IDX + constants.G_LT_NUM_INPUTS], testing_data[:, constants.G_LT_OUTPUT_IDX:constants.G_LT_OUTPUT_IDX + constants.G_LT_NUM_OUTPUTS], dataset=None) # Build up a full ratings matrix nd_ratings = None for rating in ratings: this_rating = rating * np.ones((1, constants.G_LT_NUM_OUTPUTS)) if nd_ratings is None: nd_ratings = this_rating else: nd_ratings = np.vstack((nd_ratings, this_rating)) # Create network and trainer print('>>> Building Network...') net = network.LongTermPlanningNetwork() print('>>> Initializing Trainer...') trainer = network.LongTermPlanningTrainer(evolino_network=net, dataset=training_dataset, nBurstMutationEpochs=20, importance=nd_ratings) # Begin the training iterations fitness_list = [] max_fitness = None max_fitness_epoch = None # Draw the generated path plot fig = plt.figure(1, facecolor='white') testing_axis = fig.add_subplot(111, projection='3d') fig.show() # Define paramters for convergence CONVERGENCE_THRESHOLD = -0.000005 REQUIRED_CONVERGENCE_STREAK = 20 idx = 0 current_convergence_streak = 0 while True: print('>>> Training Network (Iteration: %3d)...' % (idx + 1)) trainer.train() # Determine fitness of this network current_fitness = trainer.evaluation.max_fitness fitness_list.append(current_fitness) print('>>> FITNESS: %f' % current_fitness) # Determine if this is the minimal error network if max_fitness is None or max_fitness < current_fitness: # This is the minimum, record it max_fitness = current_fitness max_fitness_epoch = idx # Draw the generated path after training print('>>> Testing Network...') washout_ratio = 1.0 / len(testing_data) _, generated_output, _ = net.calculateOutput( testing_dataset, washout_ratio=washout_ratio) generated_input = testing_data[:len(generated_output), :constants. G_TOTAL_NUM_INPUTS] # Smash together the input and output generated_data = np.hstack((generated_input, generated_output)) print('>>> Drawing Generated Path...') pathutils.display_path(testing_axis, generated_data, testing_data, title='Generated Testing Path') plt.draw() if current_fitness > CONVERGENCE_THRESHOLD: # We've encountered a fitness higher than threshold current_convergence_streak += 1 else: # Streak ended. Reset the streak counter current_convergence_streak = 0 if current_convergence_streak == REQUIRED_CONVERGENCE_STREAK: print('>>> Convergence Achieved: %d Iterations' % idx) break idx += 1 # Draw the iteration fitness plot plt.figure(facecolor='white') plt.cla() plt.title('Fitness of RNN over %d Iterations' % (idx - 1)) plt.xlabel('Training Iterations') plt.ylabel('Fitness') plt.grid(True) plt.plot(range(len(fitness_list)), fitness_list, 'r-') plt.annotate('local max', xy=(max_fitness_epoch, fitness_list[max_fitness_epoch]), xytext=(max_fitness_epoch, fitness_list[max_fitness_epoch] + 0.01), arrowprops=dict(facecolor='black', shrink=0.05)) plt.show() # Return a full copy of the trained neural network return copy.deepcopy(net)
def main(): path_file = '../../neuralsim/generated.dat' #'../../results/sample5.dat' path = datastore.retrieve(path_file) # A list of the the segments of the optimized path segments = pathutils._detect_segments(path) # The new path generated by original path and corrective algorithm new_path = None x_path_offset = np.array([0.0, 0.0, 0.0]) # [m] v_curr = np.array([0.0, 0.0, 0.0]) # [m/s] a_max = constants.G_MAX_ACCEL # [m/s^2] for i, _ in enumerate(path): if i == len(path) - 1: continue # Detect current segment seg_idx = 0 for j in range(len(segments)): if i <= segments[j]: seg_idx = j break # Get current time and position t_curr = pathutils.get_path_time(path, i) * t_total t_next = pathutils.get_path_time(path, i + 1) * t_total dt = (t_next - t_curr) x_curr = pathutils.get_path_tooltip_pos(path, i) + x_path_offset x_next = pathutils.get_path_tooltip_pos(path, i + 1) + x_path_offset # Get the expected gate position at this timestep x_gate_expected = pathutils.get_path_gate_pos(path, segments[seg_idx], seg_idx) # Get current gate position x_gate_actual = generate_gate_pos(t_curr, path, seg_idx) dx_gate = x_gate_actual - (x_gate_expected + x_path_offset) # Calculate the new position with positional change from target to gate x_new = x_next + dx_gate # Calculate the new velocity v_new = (x_new - x_curr) / dt # Calculate the new acceleration a_new = (v_new - v_curr) / dt # Calculate the acceleration vector norm a_new_norm = np.linalg.norm(a_new) # Limit the norm vector a_new_norm_clipped = np.clip(a_new_norm, -a_max, a_max) # Determine the ratio of the clipped norm if a_new_norm != 0: ratio_unclipped = a_new_norm_clipped / a_new_norm else: ratio_unclipped = 0.0 # Scale the acceleration vector by this ratio a_new = a_new * ratio_unclipped # Calculate the new change in velocity dv_new = a_new * dt v_new = v_curr + dv_new # Calculate the new change in position dx_new = v_new * dt x_new = x_curr + dx_new # Store the next movement for later if new_path is None: new_path = x_new else: new_path = np.vstack((new_path, x_new)) # Store this velocity for the next time step v_curr = v_new # Recalculate the current offset x_path_offset += x_new - x_next # Plot the inputted path fig = plt.figure(facecolor='white') axis = fig.gca(projection='3d') pos_start_idx = constants.G_POS_IDX pos_end_idx = pos_start_idx + constants.G_NUM_POS_DIMS full_path = path[:-1].copy() full_path[:, pos_start_idx:pos_end_idx] = new_path pathutils.display_path(axis, full_path, title='Path') plt.show() return
def train_lt_network(): """Train Long-Term Network Trains an Evolino LSTM neural network for long-term path planning for use in the surgical simulator. Returns: A copy of the fully-trained path planning neural network. """ # Build up the list of files to use as training set filepath = "../../results/" training_filenames = ["sample1.dat", "sample2.dat", "sample3.dat", "sample4.dat"] testing_filename = "sample5.dat" # Get the training data and place it into a dataset training_dataset = None # Store all training set ratings ratings = np.array([]) for training_filename in training_filenames: training_file = filepath + training_filename training_data = datastore.retrieve(training_file) # Normalize the time input of the data training_data = pathutils.normalize_time(training_data, t_col=constants.G_TIME_IDX) # Add this data sample to the training dataset training_dataset = datastore.list_to_dataset( training_data[:, constants.G_LT_INPUT_IDX : constants.G_LT_INPUT_IDX + constants.G_LT_NUM_INPUTS], training_data[:, constants.G_LT_OUTPUT_IDX : constants.G_LT_OUTPUT_IDX + constants.G_LT_NUM_OUTPUTS], dataset=training_dataset, ) # Store the rating of the data this_rating = training_data[1:, constants.G_RATING_IDX] ratings = np.hstack((ratings, this_rating)) # Get testing data testing_file = filepath + testing_filename testing_data = datastore.retrieve(testing_file) testing_data = pathutils.normalize_time(testing_data, t_col=constants.G_TIME_IDX) # Store the testing data in a datastore object testing_dataset = datastore.list_to_dataset( testing_data[:, constants.G_LT_INPUT_IDX : constants.G_LT_INPUT_IDX + constants.G_LT_NUM_INPUTS], testing_data[:, constants.G_LT_OUTPUT_IDX : constants.G_LT_OUTPUT_IDX + constants.G_LT_NUM_OUTPUTS], dataset=None, ) # Build up a full ratings matrix nd_ratings = None for rating in ratings: this_rating = rating * np.ones((1, constants.G_LT_NUM_OUTPUTS)) if nd_ratings is None: nd_ratings = this_rating else: nd_ratings = np.vstack((nd_ratings, this_rating)) # Create network and trainer print(">>> Building Network...") net = network.LongTermPlanningNetwork() print(">>> Initializing Trainer...") trainer = network.LongTermPlanningTrainer( evolino_network=net, dataset=training_dataset, nBurstMutationEpochs=20, importance=nd_ratings ) # Begin the training iterations fitness_list = [] max_fitness = None max_fitness_epoch = None # Draw the generated path plot fig = plt.figure(1, facecolor="white") testing_axis = fig.add_subplot(111, projection="3d") fig.show() # Define paramters for convergence CONVERGENCE_THRESHOLD = -0.000005 REQUIRED_CONVERGENCE_STREAK = 20 idx = 0 current_convergence_streak = 0 while True: print(">>> Training Network (Iteration: %3d)..." % (idx + 1)) trainer.train() # Determine fitness of this network current_fitness = trainer.evaluation.max_fitness fitness_list.append(current_fitness) print(">>> FITNESS: %f" % current_fitness) # Determine if this is the minimal error network if max_fitness is None or max_fitness < current_fitness: # This is the minimum, record it max_fitness = current_fitness max_fitness_epoch = idx # Draw the generated path after training print(">>> Testing Network...") washout_ratio = 1.0 / len(testing_data) _, generated_output, _ = net.calculateOutput(testing_dataset, washout_ratio=washout_ratio) generated_input = testing_data[: len(generated_output), : constants.G_TOTAL_NUM_INPUTS] # Smash together the input and output generated_data = np.hstack((generated_input, generated_output)) print(">>> Drawing Generated Path...") pathutils.display_path(testing_axis, generated_data, testing_data, title="Generated Testing Path") plt.draw() if current_fitness > CONVERGENCE_THRESHOLD: # We've encountered a fitness higher than threshold current_convergence_streak += 1 else: # Streak ended. Reset the streak counter current_convergence_streak = 0 if current_convergence_streak == REQUIRED_CONVERGENCE_STREAK: print(">>> Convergence Achieved: %d Iterations" % idx) break idx += 1 # Draw the iteration fitness plot plt.figure(facecolor="white") plt.cla() plt.title("Fitness of RNN over %d Iterations" % (idx - 1)) plt.xlabel("Training Iterations") plt.ylabel("Fitness") plt.grid(True) plt.plot(range(len(fitness_list)), fitness_list, "r-") plt.annotate( "local max", xy=(max_fitness_epoch, fitness_list[max_fitness_epoch]), xytext=(max_fitness_epoch, fitness_list[max_fitness_epoch] + 0.01), arrowprops=dict(facecolor="black", shrink=0.05), ) plt.show() # Return a full copy of the trained neural network return copy.deepcopy(net)
def main(): path_file = '../../neuralsim/generated.dat'#'../../results/sample5.dat' path = datastore.retrieve(path_file) # A list of the the segments of the optimized path segments = pathutils._detect_segments(path) # The new path generated by original path and corrective algorithm new_path = None x_path_offset = np.array([0.0, 0.0, 0.0]) # [m] v_curr = np.array([0.0, 0.0, 0.0]) # [m/s] a_max = constants.G_MAX_ACCEL # [m/s^2] for i, _ in enumerate(path): if i == len(path) - 1: continue # Detect current segment seg_idx = 0 for j in range(len(segments)): if i <= segments[j]: seg_idx = j break # Get current time and position t_curr = pathutils.get_path_time(path, i) * t_total t_next = pathutils.get_path_time(path, i+1) * t_total dt = (t_next - t_curr) x_curr = pathutils.get_path_tooltip_pos(path, i) + x_path_offset x_next = pathutils.get_path_tooltip_pos(path, i+1) + x_path_offset # Get the expected gate position at this timestep x_gate_expected = pathutils.get_path_gate_pos(path, segments[seg_idx], seg_idx) # Get current gate position x_gate_actual = generate_gate_pos(t_curr, path, seg_idx) dx_gate = x_gate_actual - (x_gate_expected + x_path_offset) # Calculate the new position with positional change from target to gate x_new = x_next + dx_gate # Calculate the new velocity v_new = (x_new - x_curr) / dt # Calculate the new acceleration a_new = (v_new - v_curr) / dt # Calculate the acceleration vector norm a_new_norm = np.linalg.norm(a_new) # Limit the norm vector a_new_norm_clipped = np.clip(a_new_norm, -a_max, a_max) # Determine the ratio of the clipped norm if a_new_norm != 0: ratio_unclipped = a_new_norm_clipped / a_new_norm else: ratio_unclipped = 0.0 # Scale the acceleration vector by this ratio a_new = a_new * ratio_unclipped # Calculate the new change in velocity dv_new = a_new * dt v_new = v_curr + dv_new # Calculate the new change in position dx_new = v_new * dt x_new = x_curr + dx_new # Store the next movement for later if new_path is None: new_path = x_new else: new_path = np.vstack((new_path, x_new)) # Store this velocity for the next time step v_curr = v_new # Recalculate the current offset x_path_offset += x_new - x_next # Plot the inputted path fig = plt.figure(facecolor='white') axis = fig.gca(projection='3d') pos_start_idx = constants.G_POS_IDX pos_end_idx = pos_start_idx + constants.G_NUM_POS_DIMS full_path = path[:-1].copy() full_path[:,pos_start_idx:pos_end_idx] = new_path pathutils.display_path(axis, full_path, title='Path') plt.show() return