From 7cbbb8f718fec9822475c4e9fde8b24b9aa95075 Mon Sep 17 00:00:00 2001 From: Roman Dobosz Date: Thu, 10 May 2018 09:24:40 +0200 Subject: [PATCH] Removed carla environ --- coach/cli.py | 8 +- coach/configurations.py | 20 -- coach/environments/CarlaSettings.ini | 62 ----- coach/environments/__init__.py | 14 -- .../environments/carla_environment_wrapper.py | 229 ------------------ coach/presets.py | 38 --- 6 files changed, 5 insertions(+), 366 deletions(-) delete mode 100644 coach/environments/CarlaSettings.ini delete mode 100644 coach/environments/carla_environment_wrapper.py diff --git a/coach/cli.py b/coach/cli.py index 7d75287..f753e06 100644 --- a/coach/cli.py +++ b/coach/cli.py @@ -17,7 +17,6 @@ import argparse import atexit import json import os -import re import subprocess import sys import time @@ -25,8 +24,8 @@ import time import tensorflow as tf from coach import agents # noqa -from coach import configurations as conf from coach import environments +from coach.environments import gym_environment_wrapper as gym_w from coach import logger from coach import presets from coach import utils @@ -214,7 +213,10 @@ def main(): # Single-thread runs tuning_parameters.task_index = 0 - env_instance = environments.create_environment(tuning_parameters) # noqa + + # create environment + gym_w.GymEnvironmentWrapper(tuning_parameters) + agent = eval('agents.' + tuning_parameters.agent.type + '(env_instance, tuning_parameters)') diff --git a/coach/configurations.py b/coach/configurations.py index 18127f6..e4d929e 100644 --- a/coach/configurations.py +++ b/coach/configurations.py @@ -295,26 +295,6 @@ class Atari(EnvironmentParameters): crop_observation = False # in the original paper the observation is cropped but not in the Nature paper -class Carla(EnvironmentParameters): - type = 'Carla' - frame_skip = 1 - observation_stack_size = 4 - desired_observation_height = 128 - desired_observation_width = 180 - normalize_observation = False - server_height = 256 - server_width = 360 - config = 'environments/CarlaSettings.ini' - level = 'town1' - verbose = True - stereo = False - semantic_segmentation = False - depth = False - episode_max_time = 100000 # miliseconds for each episode - continuous_to_bool_threshold = 0.5 - allow_braking = False - - class NStepQ(AgentParameters): type = 'NStepQAgent' input_types = {'observation': InputTypes.Observation} diff --git a/coach/environments/CarlaSettings.ini b/coach/environments/CarlaSettings.ini deleted file mode 100644 index 236500f..0000000 --- a/coach/environments/CarlaSettings.ini +++ /dev/null @@ -1,62 +0,0 @@ -[CARLA/Server] -; If set to false, a mock controller will be used instead of waiting for a real -; client to connect. -UseNetworking=true -; Ports to use for the server-client communication. This can be overridden by -; the command-line switch `-world-port=N`, write and read ports will be set to -; N+1 and N+2 respectively. -WorldPort=2000 -; Time-out in milliseconds for the networking operations. -ServerTimeOut=10000000000 -; In synchronous mode, CARLA waits every frame until the control from the client -; is received. -SynchronousMode=true -; Send info about every non-player agent in the scene every frame, the -; information is attached to the measurements message. This includes other -; vehicles, pedestrians and traffic signs. Disabled by default to improve -; performance. -SendNonPlayerAgentsInfo=false - -[CARLA/LevelSettings] -; Path of the vehicle class to be used for the player. Leave empty for default. -; Paths follow the pattern "/Game/Blueprints/Vehicles/Mustang/Mustang.Mustang_C" -PlayerVehicle= -; Number of non-player vehicles to be spawned into the level. -NumberOfVehicles=15 -; Number of non-player pedestrians to be spawned into the level. -NumberOfPedestrians=30 -; Index of the weather/lighting presets to use. If negative, the default presets -; of the map will be used. -WeatherId=1 -; Seeds for the pseudo-random number generators. -SeedVehicles=123456789 -SeedPedestrians=123456789 - -[CARLA/SceneCapture] -; Names of the cameras to be attached to the player, comma-separated, each of -; them should be defined in its own subsection. E.g., Uncomment next line to add -; a camera called MyCamera to the vehicle - -Cameras=CameraRGB - -; Now, every camera we added needs to be defined it in its own subsection. -[CARLA/SceneCapture/CameraRGB] -; Post-processing effect to be applied. Valid values: -; * None No effects applied. -; * SceneFinal Post-processing present at scene (bloom, fog, etc). -; * Depth Depth map ground-truth only. -; * SemanticSegmentation Semantic segmentation ground-truth only. -PostProcessing=SceneFinal -; Size of the captured image in pixels. -ImageSizeX=360 -ImageSizeY=256 -; Camera (horizontal) field of view in degrees. -CameraFOV=90 -; Position of the camera relative to the car in centimeters. -CameraPositionX=200 -CameraPositionY=0 -CameraPositionZ=140 -; Rotation of the camera relative to the car in degrees. -CameraRotationPitch=0 -CameraRotationRoll=0 -CameraRotationYaw=0 diff --git a/coach/environments/__init__.py b/coach/environments/__init__.py index a02e5a6..65ca517 100644 --- a/coach/environments/__init__.py +++ b/coach/environments/__init__.py @@ -13,17 +13,3 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from coach.environments.gym_environment_wrapper import GymEnvironmentWrapper -from coach.environments.carla_environment_wrapper import CarlaEnvironmentWrapper -from coach import utils - - -class EnvTypes(utils.Enum): - Gym = "GymEnvironmentWrapper" - Carla = "CarlaEnvironmentWrapper" - - -def create_environment(tuning_parameters): - env_type_name, env_type = EnvTypes().verify(tuning_parameters.env.type) - env = eval(env_type)(tuning_parameters) - return env diff --git a/coach/environments/carla_environment_wrapper.py b/coach/environments/carla_environment_wrapper.py deleted file mode 100644 index 333d109..0000000 --- a/coach/environments/carla_environment_wrapper.py +++ /dev/null @@ -1,229 +0,0 @@ -import logging -import os -import signal -import subprocess -import sys - -import numpy as np - -from coach import logger -try: - if 'CARLA_ROOT' in os.environ: - sys.path.append(os.path.join(os.environ.get('CARLA_ROOT'), - 'PythonClient')) - from carla import client as carla_client - from carla import settings as carla_settings - from carla import sensor as carla_sensor -except ImportError: - logger.failed_imports.append("CARLA") -from coach.environments import environment_wrapper as ew -from coach import utils - - -# enum of the available levels and their path -class CarlaLevel(utils.Enum): - TOWN1 = "/Game/Maps/Town01" - TOWN2 = "/Game/Maps/Town02" - - -key_map = { - 'BRAKE': (274,), # down arrow - 'GAS': (273,), # up arrow - 'TURN_LEFT': (276,), # left arrow - 'TURN_RIGHT': (275,), # right arrow - 'GAS_AND_TURN_LEFT': (273, 276), - 'GAS_AND_TURN_RIGHT': (273, 275), - 'BRAKE_AND_TURN_LEFT': (274, 276), - 'BRAKE_AND_TURN_RIGHT': (274, 275), -} - - -class CarlaEnvironmentWrapper(ew.EnvironmentWrapper): - def __init__(self, tuning_parameters): - ew.EnvironmentWrapper.__init__(self, tuning_parameters) - - self.tp = tuning_parameters - - # server configuration - self.server_height = self.tp.env.server_height - self.server_width = self.tp.env.server_width - self.port = utils.get_open_port() - self.host = 'localhost' - self.map = CarlaLevel().get(self.tp.env.level) - - # client configuration - self.verbose = self.tp.env.verbose - self.depth = self.tp.env.depth - self.stereo = self.tp.env.stereo - self.semantic_segmentation = self.tp.env.semantic_segmentation - self.height = self.server_height * (1 + int(self.depth) + int(self.semantic_segmentation)) - self.width = self.server_width * (1 + int(self.stereo)) - self.size = (self.width, self.height) - - self.config = self.tp.env.config - if self.config: - # load settings from file - with open(self.config, 'r') as fp: - self.settings = fp.read() - else: - # hard coded settings - self.settings = carla_settings.CarlaSettings() - self.settings.set( - SynchronousMode=True, - SendNonPlayerAgentsInfo=False, - NumberOfVehicles=15, - NumberOfPedestrians=30, - WeatherId=1) - self.settings.randomize_seeds() - - # add cameras - camera = carla_sensor.Camera('CameraRGB') - camera.set_image_size(self.width, self.height) - camera.set_position(200, 0, 140) - camera.set_rotation(0, 0, 0) - self.settings.add_sensor(camera) - - # open the server - self.server = self._open_server() - - logging.disable(40) - - # open the client - self.game = carla_client.CarlaClient(self.host, self.port, timeout=99999999) - self.game.connect() - scene = self.game.load_settings(self.settings) - - # get available start positions - positions = scene.player_start_spots - self.num_pos = len(positions) - self.iterator_start_positions = 0 - - # action space - self.discrete_controls = False - self.action_space_size = 2 - self.action_space_high = [1, 1] - self.action_space_low = [-1, -1] - self.action_space_abs_range = np.maximum(np.abs(self.action_space_low), np.abs(self.action_space_high)) - self.steering_strength = 0.5 - self.gas_strength = 1.0 - self.brake_strength = 0.5 - self.actions = {0: [0., 0.], - 1: [0., -self.steering_strength], - 2: [0., self.steering_strength], - 3: [self.gas_strength, 0.], - 4: [-self.brake_strength, 0], - 5: [self.gas_strength, -self.steering_strength], - 6: [self.gas_strength, self.steering_strength], - 7: [self.brake_strength, -self.steering_strength], - 8: [self.brake_strength, self.steering_strength]} - self.actions_description = ['NO-OP', 'TURN_LEFT', 'TURN_RIGHT', 'GAS', 'BRAKE', - 'GAS_AND_TURN_LEFT', 'GAS_AND_TURN_RIGHT', - 'BRAKE_AND_TURN_LEFT', 'BRAKE_AND_TURN_RIGHT'] - for idx, action in enumerate(self.actions_description): - for key in key_map.keys(): - if action == key: - self.key_to_action[key_map[key]] = idx - self.num_speedup_steps = 30 - - # measurements - self.measurements_size = (1,) - self.autopilot = None - - # env initialization - self.reset(True) - - # render - if self.is_rendered: - image = self.get_rendered_image() - self.renderer.create_screen(image.shape[1], image.shape[0]) - - def _open_server(self): - log_path = os.path.join(logger.logger.experiments_path, "CARLA_LOG_{}.txt".format(self.port)) - with open(log_path, "wb") as out: - cmd = [os.path.join(os.environ.get('CARLA_ROOT'), 'CarlaUE4.sh'), self.map, - "-benchmark", "-carla-server", "-fps=10", "-world-port={}".format(self.port), - "-windowed -ResX={} -ResY={}".format(self.server_width, self.server_height), - "-carla-no-hud"] - if self.config: - cmd.append("-carla-settings={}".format(self.config)) - p = subprocess.Popen(cmd, stdout=out, stderr=out) - - return p - - def _close_server(self): - os.killpg(os.getpgid(self.server.pid), signal.SIGKILL) - - def _update_state(self): - # get measurements and observations - measurements = [] - while type(measurements) == list: - measurements, sensor_data = self.game.read_data() - - self.location = (measurements.player_measurements.transform.location.x, - measurements.player_measurements.transform.location.y, - measurements.player_measurements.transform.location.z) - - is_collision = measurements.player_measurements.collision_vehicles != 0 \ - or measurements.player_measurements.collision_pedestrians != 0 \ - or measurements.player_measurements.collision_other != 0 - - speed_reward = measurements.player_measurements.forward_speed - 1 - if speed_reward > 30.: - speed_reward = 30. - self.reward = speed_reward \ - - (measurements.player_measurements.intersection_otherlane * 5) \ - - (measurements.player_measurements.intersection_offroad * 5) \ - - is_collision * 100 \ - - np.abs(self.control.steer) * 10 - - # update measurements - self.state = { - 'observation': sensor_data['CameraRGB'].data, - 'measurements': [measurements.player_measurements.forward_speed], - } - self.autopilot = measurements.player_measurements.autopilot_control - - # action_p = ['%.2f' % member for member in [self.control.throttle, self.control.steer]] - # screen.success('REWARD: %.2f, ACTIONS: %s' % (self.reward, action_p)) - - if (measurements.game_timestamp >= self.tp.env.episode_max_time) or is_collision: - # screen.success('EPISODE IS DONE. GameTime: {}, Collision: {}'.format(str(measurements.game_timestamp), - # str(is_collision))) - self.done = True - - def _take_action(self, action_idx): - if type(action_idx) == int: - action = self.actions[action_idx] - else: - action = action_idx - self.last_action_idx = action - - self.control = carla_client.VehicleControl() - self.control.throttle = np.clip(action[0], 0, 1) - self.control.steer = np.clip(action[1], -1, 1) - self.control.brake = np.abs(np.clip(action[0], -1, 0)) - if not self.tp.env.allow_braking: - self.control.brake = 0 - self.control.hand_brake = False - self.control.reverse = False - - self.game.send_control(self.control) - - def _restart_environment_episode(self, force_environment_reset=False): - self.iterator_start_positions += 1 - if self.iterator_start_positions >= self.num_pos: - self.iterator_start_positions = 0 - - try: - self.game.start_episode(self.iterator_start_positions) - except: - self.game.connect() - self.game.start_episode(self.iterator_start_positions) - - # start the game with some initial speed - state = None - for i in range(self.num_speedup_steps): - state = self.step([1.0, 0])['state'] - self.state = state - - return state diff --git a/coach/presets.py b/coach/presets.py index a954359..c4509fd 100644 --- a/coach/presets.py +++ b/coach/presets.py @@ -1151,44 +1151,6 @@ class Breakout_A3C(conf.Preset): self.agent.middleware_type = conf.MiddlewareTypes.FC -class Carla_A3C(conf.Preset): - def __init__(self): - conf.Preset.__init__(self, conf.ActorCritic, conf.Carla, conf.EntropyExploration) - self.agent.embedder_complexity = conf.EmbedderComplexity.Deep - self.agent.policy_gradient_rescaler = 'GAE' - self.learning_rate = 0.0001 - self.num_heatup_steps = 0 - # self.env.reward_scaling = 1.0e9 - self.agent.discount = 0.99 - self.agent.apply_gradients_every_x_episodes = 1 - self.agent.num_steps_between_gradient_updates = 30 - self.agent.gae_lambda = 1 - self.agent.beta_entropy = 0.01 - self.clip_gradients = 40 - self.agent.middleware_type = conf.MiddlewareTypes.FC - - -class Carla_DDPG(conf.Preset): - def __init__(self): - conf.Preset.__init__(self, conf.DDPG, conf.Carla, conf.OUExploration) - self.agent.embedder_complexity = conf.EmbedderComplexity.Deep - self.learning_rate = 0.0001 - self.num_heatup_steps = 1000 - self.agent.num_consecutive_training_steps = 5 - - -class Carla_BC(conf.Preset): - def __init__(self): - conf.Preset.__init__(self, conf.BC, conf.Carla, conf.ExplorationParameters) - self.agent.embedder_complexity = conf.EmbedderComplexity.Deep - self.agent.load_memory_from_file_path = 'datasets/carla_town1.p' - self.learning_rate = 0.0005 - self.num_heatup_steps = 0 - self.evaluation_episodes = 5 - self.batch_size = 120 - self.evaluate_every_x_training_iterations = 5000 - - class MontezumaRevenge_BC(conf.Preset): def __init__(self): conf.Preset.__init__(self, conf.BC, conf.Atari, conf.ExplorationParameters)