-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
190 changed files
with
5,786 additions
and
5,081 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
from .client import * | ||
from .utils import * | ||
from .types import * | ||
# from .client import * | ||
# from .types import * | ||
|
||
__version__ = "1.0.0" | ||
|
||
description = """ | ||
AutonomySim: the simulation engine for autonomous systems | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
description = """ | ||
Artificial Intelligence | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
description = """ | ||
Imitation Learning | ||
""" | ||
|
||
description_long = """ | ||
# Imitation Learning | ||
This section is about training a model to steer our Formula car using imitation learning. | ||
The code in this section is based on the [Autonomous Driving Cookbook](https://github.com/nervosys/AutonomousDrivingCookbook/tree/master/AutonomySimE2EDeepLearning) from AutonomySim and it's highly recommended to read the tutorial first. | ||
## Prerequisites | ||
* Operating system: Windows 10 | ||
* GPU: Nvidia GTX 1080 or higher (recommended) | ||
* Software: Unreal Engine 4.24 and Visual Studio 2019 (see [upgrade instructions](../../docs/unreal_upgrade.md)) | ||
* Development: CUDA 9.0 and python 3.5. | ||
* Python libraries: Keras 2.1.2, TensorFlow 1.6.0. | ||
* Note: Newer versions of keras or tensorflow are recommended but can cause syntax errors. | ||
## What's inside | ||
![imitation learning](https://github.com/nervosys/AutonomySim/wiki/images/technion/imitation_learning_example.gif) | ||
*Driving in simulation using trained imitation learning model, based on recorded data* | ||
Imitation learning includes the usage of labeled data as input to a training algorithm with the purpose of having the algorithm imitate the actions of people who recorded the data. | ||
![diagram](https://github.com/nervosys/AutonomySim/wiki/images/technion/imitation_diagram.PNG) | ||
This diagram is represented by these files: | ||
**cook_data.py** | ||
This file is responsible for preparing .h5 dataset files for the training procedure. | ||
The code rely on having two adjacent folders: | ||
'raw_data' - contains folders of recorded data by AutonomySim's recording method. | ||
'cooked_data' - empty folder to store the .h5 files. | ||
The flag "COOK_ALL_DATA" gives the option to choose all subfolders, or exclude some of them. | ||
**train_model.py** | ||
This file is responsible to train a model using the .h5 dataset files. | ||
The code rely on having two adjacent folders: | ||
'cooked_data' - contains the .h5 dataset files. | ||
'models' - empty folder to store the generated models. | ||
The file will preprocess the data, add augmentations and create a neural network model that predicts the next steering angle. | ||
**drive_model.py** | ||
This file connects to the simulation in order to upload a trained model and drive using it. | ||
By using the predicted steering value, the code calculates related control parameters and maintain driving with steady velocities. | ||
## Training Tips | ||
We recommend on using augmentation and recording techniques. | ||
We give here an example for two methods: | ||
- [CycleLight](https://github.com/nervosys/AutonomySim/wiki/graphic_features) - Animation of a day light cycle in a changeable, potentially very short period of time. | ||
- Shifted images - Altering the camera’s position to the right or the left of the car, so that it can record images in extreme conditions. To simulate driving back to the center from those extreme situations, post-process the recorded angle of the steering accordingly (manually). | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import os | ||
|
||
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" | ||
|
||
import time | ||
import numpy as np | ||
from keras.models import load_model | ||
|
||
from autonomysim.clients import CarClient, CarControls | ||
from autonomysim.types import ImageRequest, ImageType | ||
|
||
|
||
# Trained model path | ||
MODEL_PATH = "./models/example_model.h5" | ||
|
||
|
||
class CarAgent: | ||
model = None | ||
|
||
def __init__(self) -> None: | ||
pass | ||
|
||
def get_image(self, client): | ||
""" | ||
Get image from AutonomySim client | ||
""" | ||
|
||
image_response = client.simGetImages( | ||
[ImageRequest("0", ImageType.Scene, False, False)] | ||
)[0] | ||
image1d = np.fromstring(image_response.image_data_uint8, dtype=np.uint8) | ||
image_rgb = image1d.reshape(image_response.height, image_response.width, 3) | ||
return image_rgb[78:144, 27:227, 0:2].astype(float) | ||
|
||
def load(self, model_path=MODEL_PATH): | ||
""" | ||
Load the model. | ||
""" | ||
self.model = load_model(model_path) | ||
|
||
def run(self): | ||
""" | ||
Run the model. | ||
""" | ||
|
||
# Connect to autonomysim | ||
client = CarClient() | ||
client.confirmConnection() | ||
client.enableApiControl(True) | ||
car_controls = CarControls() | ||
|
||
# Start driving | ||
car_controls.steering = 0 | ||
car_controls.throttle = 0 | ||
car_controls.brake = 0 | ||
client.setCarControls(car_controls) | ||
|
||
# Initialize image buffer | ||
image_buf = np.zeros((1, 66, 200, 3)) | ||
|
||
while True: | ||
# Update throttle value according to steering angle | ||
if abs(car_controls.steering) <= 1.0: | ||
car_controls.throttle = 0.8 - (0.4 * abs(car_controls.steering)) | ||
else: | ||
car_controls.throttle = 0.4 | ||
|
||
image_buf[0] = self.get_image(client) | ||
image_buf[0] /= 255 # normalization | ||
|
||
start_time = time.time() | ||
|
||
# Prediction | ||
model_output = self.model.predict([image_buf]) | ||
|
||
end_time = time.time() | ||
received_output = model_output[0][0] | ||
|
||
# Rescale prediction to [-1,1] and factor by 0.82 for drive smoothness | ||
car_controls.steering = round( | ||
(0.82 * (float((model_output[0][0] * 2.0) - 1))), 2 | ||
) | ||
|
||
# Print progress | ||
print( | ||
"Sending steering = {0}, throttle = {1}, prediction time = {2}".format( | ||
received_output, car_controls.throttle, str(end_time - start_time) | ||
) | ||
) | ||
|
||
# Update next car state | ||
client.setCarControls(car_controls) | ||
|
||
# Wait a bit between iterations | ||
time.sleep(0.05) | ||
|
||
client.enableApiControl(False) | ||
return None |
Oops, something went wrong.