def execute_agent(self): keys = Keys() screen = Screen() print('Starting running Direct Imitation Learning in...') # Countdown to start running the agent for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) paused = False while True: if not paused: img = screen.GrabScreenBGR() # Preprocessing input image converted_img = np.array(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)) resized_img = cv2.resize(converted_img, (120, 90)) reshaped_img = resized_img.reshape(-1, 90, 120, 1) normalized_img = reshaped_img.astype('float32') / 255.0 final_img = normalized_img # Prediction and Entropy of the Labels move_prediction = self.predict_move(final_img) action_prediction = self.predict_action(final_img) move_action = np.argmax(move_prediction[0]) control_action = np.argmax(action_prediction[0]) print(move_prediction[0], ' - ', action_prediction[0]) # Move action self.execute_movement_action(move_action) #self.execute_movement_action_with_threshold(move_prediction[0]) # Action action self.execute_control_action(control_action) #self.execute_control_action_with_threshold(action_prediction[0]) keys_pressed = keys.KeyCheck() if 'Q' in keys_pressed: if paused: paused = False print('unpaused!') time.sleep(1) else: print('Pausing!') self.release_moves() paused = True time.sleep(1)
def CreateTrainingData(self): keys = Keys() screen = Screen() print('Starting Training in...') # Countdown to start the training for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) paused = False last_time = time.time() while True: if not paused: grabbed_screen = screen.GrabScreenBGR() # Get actual frame new_screen = cv2.resize( cv2.cvtColor(grabbed_screen, cv2.COLOR_BGR2GRAY), (120, 90)) # Converted and Resized frame #normalized_screen = new_screen.astype('float32') / 255 # Normalizing keys_pressed = keys.KeyCheck() # Check for pressed keys output_move = keys.KeysMovementOutput( keys_pressed) # Verifies if one move key was pressed output_action = keys.KeysActionOutput( keys_pressed) # Verifies if one action key was pressed self.training_data.append( [new_screen, output_move, output_action]) # Create an instance of training data if len(self.training_data) % 1000 == 0: print(len(self.training_data)) keys_pressed = keys.KeyCheck() # Pausing or Unpausing training if 'Q' in keys_pressed: if paused: paused = False print('Unpausing training...') time.sleep(2) else: print('Pausing training!') paused = True time.sleep(1) # Saving Data if 'P' in keys_pressed: np.save(self.path_file, self.training_data)
def execute_active_deep_imitation_learning_movement_actions(self): keys = Keys() screen = Screen() print('Starting running Active Deep Imitation Learning in...') # Countdown to start running the agent for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) paused = False number_active_samples_to_reach = self.active_sample_size * len( self.dataset_behavioral_cloning.training_data) / 100.0 print(len(self.active_samples.training_data)) print(number_active_samples_to_reach) while len(self.active_samples.training_data ) < number_active_samples_to_reach: if not paused: img = screen.GrabScreenBGR() # Preprocessing input image converted_img = np.array(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)) resized_img = cv2.resize(converted_img, (120, 90)) reshaped_img = resized_img.reshape(-1, 90, 120, 1) normalized_img = reshaped_img.astype('float32') / 255 final_img = normalized_img # Prediction and Entropy of the Labels move_prediction = self.behavioral_agent.predict_move(final_img) action_prediction = self.behavioral_agent.predict_action( final_img) move = np.argmax(move_prediction[0]) action = np.argmax(action_prediction[0]) entropy = self.calculate_entropy(action_prediction[0]) print(move_prediction[0], ' -> ', entropy) if entropy < self.threshold: ## Execute the behavioral agent's action # Move action self.behavioral_agent.execute_movement_action(move) # Control action self.behavioral_agent.execute_control_action(action) else: ## Queries a non-expert action self.query_sound() print('Query non-expert action: ') self.behavioral_agent.release_moves() # Waiting for a non-expert action while True: keys_pressed = keys.KeyCheck( ) # Check for pressed keys non_expert_move = keys.KeysMovementOutput( keys_pressed ) # Verifies if one move key was pressed if non_expert_move != [0, 0, 0, 0]: print(non_expert_move) self.active_samples.training_data.append( [resized_img, non_expert_move]) break keys_pressed = keys.KeyCheck() if 'Q' in keys_pressed: if paused: paused = False print('unpaused!') time.sleep(1) else: print('Pausing!') self.behavioral_agent.release_moves() paused = True time.sleep(1) self.active_samples.save_data()
keys = Keys() screen = Screen() print('Starting running in...') # Countdown to start running the agent for i in list(range(4))[::-1]: print(i + 1) time.sleep(1) paused = False while True: if not paused: new_screen = screen.GrabScreenBGR() new_screen = cv2.resize(new_screen, (140, 80)) move_prediction = agent.predict_move( [new_screen.reshape(1, 140, 80, 3)]) action_prediction = agent.predict_action( [new_screen.reshape(1, 140, 80, 3)])[0] moves = list(np.around(move_prediction[0])) print(moves) print(action_prediction) if moves == [0, 0, 0, 1]: agent.right() elif moves == [0, 0, 1, 0]: agent.down() elif moves == [0, 1, 0, 0]: