Table of Contents
If you didn't know, CARLA is an open-source simulator for autonomous driving research.
It can be used as an environment for training ADAS, and also for Reinforcement Learning.
This guide will help you set up the CARLA environment for RL. Most of my code here is inspired from Intel Coach's setup of CARLA. I thought it'd be helpful to have a separte guide for this, to implement our own RL algorithms on top of it, instead of relying on Nervana Coach.
- Download the CARLA release (v0.8) from here.
(Tested using CARLA 0.8.0 only, check this for 0.8.2) - Any Debian-based OS (Preferably Ubuntu 16.04 or later)
- Python 3.x installed
- To install python packages:
pip install -r requirements.txt
After downloading the release version, place in any accessible directory, preferably something like /home/username/CARLA
or whatever.
Now open up your terminal, enter nano ~/.bashrc
and include the PATH of the CARLA environment like:
export CARLA_ROOT=/home/username/CARLA
Just clone (or fork) this repo by
git clone https://github.com/GokulNC/Setting-Up-CARLA-RL
All the required files for Environment's RL interface is present in the Environment
directory (which you need not worry about)
Note: Most of the files are obtained from Intel Coach's interface for RL, with modifications from my side.
The environment interface provided here is more or less similar to that of OpenAI Gym for standardization purpose ;)
from Environment.carla_environment_wrapper import CarlaEnvironmentWrapper as CarlaEnv
env = CarlaEnv() # To create an env
# returns the initial output values (as described in sections below)
initial_observation = env.reset()
observation, reward, done, info = env.step(action_idx)
where action_idx
is the discretized value of action corresponding to a specific action.
As of now, there are 9 discretized values, each corresponding to different actions as defined in self.actions
of carla_environment_wrapper.py
like
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]}
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']
(Feel free to modify it as you see fit)
# observation : observation after taking the action
# To get RGB image from the observation:
state = observation['rgb_image']
# TODO: In future, will add supoort for LiDAR sensors, etc. as required
# reward : immediate reward after taking the action
# done : boolean True/False indicating if episode is finished
# (collision has occured or time limit exceeded)
# info : information about the action taken & consequences
# To get the id of the last action taken
last_action_idx = info['action']
# more info will be added later
CARLA automatically renders everything as you play (take actions/pass controls). So no need of explicitly rendering.
If you need to render the camera view,
env = CarlaEnv(is_render_enabled=True) # To create an env
# To render after each action:
env.render()
env = CarlaEnv(save_screens=True) # To create an env
# To save after each action:
env.save_screenshots()
I have included a file human_play.py
which you can run by
python human_play.py
and play the game manually to get an understanding of it. (Make sure the focus is on the terminal window)
Use the arrow keys to play (Up
to accelerate, Down
to brake, Left/Right
to steer)
- You can change resolution of server window, render window and other configs in
Environment/carla_config.py
- You can get the following outputs, instead of just RGB image:
- For Segmentated output:
env = CarlaEnv(cameras=['SemanticSegmentation'])
andsegmented_output = observation['segmented_image']
- For depth output:
env = CarlaEnv(cameras=['Depth'])
anddepth_map = observation['depth_map']
- (Note: You can also use a combination of everything. For RGB output,
cameras=['SceneFinal']
)
(To play with your own cameras, feel free to modify things as described here in docs)
- For Segmentated output:
- As of now, the CarlaEnvironmentWrapper supports both continous & hardcoded discretized values. I think discretized action values can be removed
- Make it Gym compliant (for benchmarks)
Feel free to contribute!