├── AppleSilicon_version.py
├── Colab_version.ipynb
├── LICENSE
└── README.md
/AppleSilicon_version.py:
--------------------------------------------------------------------------------
1 | """
2 | Copyright 2022 Jiancheng Zhang
3 |
4 | Licensed under the Apache License, Version 2.0 (the "License");
5 | you may not use this file except in compliance with the License.
6 | You may obtain a copy of the License at
7 |
8 | http://www.apache.org/licenses/LICENSE-2.0
9 |
10 | Unless required by applicable law or agreed to in writing, software
11 | distributed under the License is distributed on an "AS IS" BASIS,
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | See the License for the specific language governing permissions and
14 | limitations under the License.
15 | """
16 |
17 | import numpy as np
18 | import pandas as pd
19 |
20 | import tensorflow as tf
21 | from tensorflow.python.keras.models import load_model, Model
22 | from tensorflow.python.keras.layers import LSTM, Dense, Input, Masking, SimpleRNN, concatenate
23 |
24 | import os
25 | import re
26 | import gc
27 |
28 | """
29 | A lot of code is same in Colab_version.ipynb, so the comment is omitted for same code, but I commented different part
30 | """
31 |
32 |
33 | # ----------------------------------------------------------------------------------------------------------------------
34 | def loading_data(folder):
35 | check_name = re.compile('^ML')
36 |
37 | datasets = []
38 |
39 | # https://stackoverflow.com/questions/4813061/non-alphanumeric-list-order-from-os-listdir
40 | for filename in sorted(os.listdir(folder)):
41 |
42 | files = os.path.join(folder, filename)
43 |
44 | if re.match(check_name, filename) and os.path.isfile(files):
45 | temp = pd.read_csv(files, skiprows=1, header=None).iloc[:, :-1]
46 |
47 | temp[0] = temp[0].map(lambda t: t / 16.0)
48 | temp[1] = temp[1].map(lambda t: t / 0.192)
49 |
50 | datasets.append(np.array(temp, dtype=float))
51 |
52 | gc.collect()
53 |
54 | return datasets
55 |
56 |
57 | # ----------------------------------------------------------------------------------------------------------------------
58 | def loading_speed_steering_data(folder):
59 | check_name = re.compile('^car_state_blue')
60 |
61 | datasets = []
62 |
63 | for filename in sorted(os.listdir(folder)):
64 |
65 | files = os.path.join(folder, filename)
66 |
67 | if re.match(check_name, filename) and os.path.isfile(files):
68 | datasets.append(np.array(pd.read_csv(files).iloc[:, [3, 5]], dtype=float))
69 |
70 | gc.collect()
71 |
72 | return datasets
73 |
74 |
75 | # ----------------------------------------------------------------------------------------------------------------------
76 | if __name__ == '__main__':
77 | # ----------------------------------------------------------------------------------------------------------------------
78 | # loading dataset ------------------------------------------------------------------------------------------------------
79 | # ----------------------------------------------------------------------------------------------------------------------
80 |
81 | folder = "/path/to/your/dataset"
82 |
83 | datasets = loading_data(folder)
84 | speed_steering = loading_speed_steering_data(folder)
85 |
86 | new_datasets = datasets
87 | new_speed_steering = speed_steering
88 |
89 | X = []
90 | y = []
91 | # I cannot find tensorflow.keras.preprocessing.sequence.pad_sequences() function in macOS version
92 | # So, I am going to pad dataset manually
93 | # We need to know the max size in the whole dataset
94 | max_seq_len = 0
95 |
96 | for x in new_datasets:
97 | X.append(x[:, 2:])
98 | y.append(x[:, 0:2])
99 | # shape[0] is how many timestamps in this instance, shape[1] is 1083
100 | max_seq_len = max(max_seq_len, x.shape[0])
101 |
102 | # ----------------------------------------------------------------------------------------------------------------------
103 | # manually pad the dataset ---------------------------------------------------------------------------------------------
104 | # ----------------------------------------------------------------------------------------------------------------------
105 | # https://datascience.stackexchange.com/questions/48796/how-to-feed-lstm-with-different-input-array-sizes
106 |
107 | special_value = -100.0
108 |
109 | Xpad = []
110 | ypad = []
111 |
112 | # creating two lists that have same number of instances as new_datasets,
113 | # each instance is a numpy array that full of special_value, and shape is max_seq_len times 1081 and 2
114 | # this makes all the instance have same size
115 | for i in range(len(new_datasets)):
116 | Xpad.append(np.full((max_seq_len, 1081), fill_value=special_value))
117 | ypad.append(np.full((max_seq_len, 2), fill_value=special_value))
118 |
119 | # we can start copying data from X and y to Xpad and ypad
120 | # s is the index, x is the data
121 | for s, x in enumerate(X):
122 | # get the size of this instance (original)
123 | seq_len = x.shape[0]
124 | # copy data from the original instance to new instance
125 | # due to first dimension is list, second and third dimension is numpy array.
126 | # So, we need two square brackets
127 | # after copying, 0 to seq_len is the original data, and seq_len to max_seq_len is special_value
128 | Xpad[s][0:seq_len, :] = x
129 |
130 | for s, x in enumerate(y):
131 | seq_len = x.shape[0]
132 | ypad[s][0:seq_len, :] = x
133 |
134 | # need to convert to numpy array, because the first dimension is a list
135 | # the result is same as using tensorflow.keras.preprocessing.sequence.pad_sequences()
136 | Xpad_A = np.asarray(Xpad)
137 | ypad = np.asarray(ypad)
138 |
139 | datasets = []
140 | new_datasets = []
141 | X = []
142 | y = []
143 | Xpad = []
144 | gc.collect()
145 |
146 | # ----------------------------------------------------------------------------------------------------------------------
147 | speed_steering_Pad = []
148 |
149 | for i in range(len(new_speed_steering)):
150 | speed_steering_Pad.append(np.full((max_seq_len, 2), fill_value=special_value))
151 |
152 | for s, x in enumerate(new_speed_steering):
153 | seq_len = x.shape[0]
154 | speed_steering_Pad[s][0:seq_len, :] = x
155 |
156 | XPad_B = np.asarray(speed_steering_Pad)
157 |
158 | speed_steering = []
159 | new_speed_steering = []
160 | speed_steering_Pad = []
161 | gc.collect()
162 |
163 | # ----------------------------------------------------------------------------------------------------------------------
164 | # Option A build a new model -------------------------------------------------------------------------------------------
165 | # ----------------------------------------------------------------------------------------------------------------------
166 | # https://stackoverflow.com/questions/46982616/batch-input-shape-tuple-on-keras-lstm
167 |
168 | inputA = Input(shape=(None, 1081))
169 | A = Masking(input_shape=(None, 1081), mask_value=special_value)(inputA)
170 | x = Dense(500, activation="relu")(A)
171 | x = SimpleRNN(150, return_sequences=True, input_shape=(None, 1081))(x)
172 |
173 | inputB = Input(shape=(None, 2))
174 | B = Masking(input_shape=(None, 2), mask_value=special_value)(inputB)
175 |
176 | combined = concatenate([x, B], axis=2)
177 |
178 | z = Dense(256, activation="relu")(combined)
179 | z = Dense(128, activation="relu")(z)
180 | z = Dense(32, activation="tanh")(z)
181 | z = Dense(2, activation="tanh")(z)
182 |
183 | SimpleRNN_model = Model(inputs=[inputA, inputB], outputs=z)
184 |
185 | # ----------------------------------------------------------------------------------------------------------------------
186 | # Option B load an old model -------------------------------------------------------------------------------------------
187 | # ----------------------------------------------------------------------------------------------------------------------
188 |
189 | # SimpleRNN_model = load_model('/path/to/your/model')
190 |
191 | # ----------------------------------------------------------------------------------------------------------------------
192 | # Start training -------------------------------------------------------------------------------------------------------
193 | # ----------------------------------------------------------------------------------------------------------------------
194 |
195 | SimpleRNN_model.compile(loss="mean_squared_error", optimizer="RMSprop", metrics=['mean_squared_error'])
196 | SimpleRNN_model.summary()
197 |
198 | # https://github.com/tensorflow/tensorflow/issues/56082
199 | # you can open Activity Monitor->Window->GPU History to see your GPU usage during training
200 | with tf.device("/device:CPU:0"):
201 | SimpleRNN_model.fit([Xpad_A, XPad_B], ypad, epochs=10, batch_size=5, verbose=1, shuffle=True)
202 |
203 | SimpleRNN_model.save("/path/to/your/model")
204 |
--------------------------------------------------------------------------------
/Colab_version.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "Untitled0.ipynb",
7 | "provenance": [],
8 | "collapsed_sections": [],
9 | "mount_file_id": "https://github.com/JZ76/f1tenth_simulator_two_agents/blob/master/simpleRNN.ipynb",
10 | "authorship_tag": "ABX9TyP767KI5mb06dhHAkekBTk6",
11 | "include_colab_link": true
12 | },
13 | "kernelspec": {
14 | "name": "python3",
15 | "display_name": "Python 3"
16 | },
17 | "language_info": {
18 | "name": "python"
19 | },
20 | "gpuClass": "standard"
21 | },
22 | "cells": [
23 | {
24 | "cell_type": "markdown",
25 | "metadata": {
26 | "id": "view-in-github",
27 | "colab_type": "text"
28 | },
29 | "source": [
30 | "
"
31 | ]
32 | },
33 | {
34 | "cell_type": "code",
35 | "source": [
36 | "# Note: better using colab PRO, because you can have more memory, about 35GB, and longer run time duration. \n",
37 | "# When training the model, will take a lot of memory depends on the size of dataset, rather than need a very powerful GPU\n",
38 | "# you will find the training is quite slow, because SimpleRNN cannot use CUDA cores to accelerate\n",
39 | "# The free colab version is enough for the Australia dataset, but not enough for larger datasets.\n",
40 | "# One is memory limit, another is duration limit.\n",
41 | "\n",
42 | "# This code sheet should also work in Windows machine, after changing folder's path and installing correct version of libraries\n",
43 | "# Need A LOT OF memory, recommend 32GB memory"
44 | ],
45 | "metadata": {
46 | "id": "6eRVlDrbmx7c"
47 | },
48 | "execution_count": null,
49 | "outputs": []
50 | },
51 | {
52 | "cell_type": "code",
53 | "source": [
54 | "Copyright 2022 Jiancheng Zhang\n",
55 | "\n",
56 | "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
57 | "you may not use this file except in compliance with the License.\n",
58 | "You may obtain a copy of the License at\n",
59 | "\n",
60 | " http://www.apache.org/licenses/LICENSE-2.0\n",
61 | "\n",
62 | "Unless required by applicable law or agreed to in writing, software\n",
63 | "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
64 | "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
65 | "See the License for the specific language governing permissions and\n",
66 | "limitations under the License."
67 | ],
68 | "metadata": {
69 | "id": "keK9rSocIyTL"
70 | },
71 | "execution_count": null,
72 | "outputs": []
73 | },
74 | {
75 | "cell_type": "code",
76 | "source": [
77 | "# Please use numpy 1.18.5 version, otherwise will have an error. \n",
78 | "# Have to RESTART runtime after installed, not DELETE"
79 | ],
80 | "metadata": {
81 | "id": "i-pvHjwvEra0"
82 | },
83 | "execution_count": null,
84 | "outputs": []
85 | },
86 | {
87 | "cell_type": "code",
88 | "source": [
89 | "pip install -U numpy==1.18.5"
90 | ],
91 | "metadata": {
92 | "id": "59g-KUSOjrdR"
93 | },
94 | "execution_count": null,
95 | "outputs": []
96 | },
97 | {
98 | "cell_type": "code",
99 | "source": [
100 | "import tensorflow\n",
101 | "print(tensorflow.__version__)"
102 | ],
103 | "metadata": {
104 | "id": "HjSLPTNCItg2"
105 | },
106 | "execution_count": null,
107 | "outputs": []
108 | },
109 | {
110 | "cell_type": "code",
111 | "execution_count": null,
112 | "metadata": {
113 | "id": "3H6GBlaC8kVo"
114 | },
115 | "outputs": [],
116 | "source": [
117 | "import numpy as np\n",
118 | "import pandas as pd\n",
119 | "\n",
120 | "from tensorflow.python.keras.models import Model, load_model\n",
121 | "from tensorflow.python.keras.layers import LSTM, Dense, Input, CuDNNLSTM, Masking, Embedding, SimpleRNN, concatenate\n",
122 | "\n",
123 | "import os\n",
124 | "import re\n",
125 | "import gc"
126 | ]
127 | },
128 | {
129 | "cell_type": "code",
130 | "source": [
131 | "def loading_data(folder):\n",
132 | "\n",
133 | " # use regular expression to filter files\n",
134 | " # file's name start with 'ML'\n",
135 | " check_name = re.compile('^ML')\n",
136 | "\n",
137 | " datasets = []\n",
138 | "\n",
139 | " # due to different operating system has different way in keeping files, here I would like to read files in sorted by name order and say it explicitly\n",
140 | " # the reason why must sorted by name will be explained later\n",
141 | " for filename in sorted(os.listdir(folder)):\n",
142 | "\n",
143 | " files = os.path.join(folder, filename)\n",
144 | "\n",
145 | " # check whether the name match the regular expression and actually is a file\n",
146 | " if re.match(check_name, filename) and os.path.isfile(files):\n",
147 | "\n",
148 | " # using pandas to read csv file\n",
149 | " # there are only three headers: Speed, Steering_angle, LiDAR_scan. \n",
150 | " # But there are 1083 columns data, which means the number of header doesn't match number of column\n",
151 | " # So, we need to skip first row, and set header is None.\n",
152 | " # Besides, the value in last column is all None, we have to drop column at index -1\n",
153 | " temp = pd.read_csv(files, skiprows=1, header=None).iloc[:, :-1]\n",
154 | "\n",
155 | " # Because the speed is a very large number compare to steering angle,\n",
156 | " # We have to normalize it into [-1, 1], and do the same to steering angle\n",
157 | " # When apply the model in the simulator, remember to product corresponding value to the output of the model\n",
158 | " # index 0 is speed, index 1 is steering angle\n",
159 | " temp[0] = temp[0].map(lambda t : t/16.0)\n",
160 | " temp[1] = temp[1].map(lambda t : t/0.192)\n",
161 | "\n",
162 | " # append this csv file to the result, and turn it into numpy array with float format\n",
163 | " datasets.append(np.array(temp, dtype=float))\n",
164 | "\n",
165 | " # when append data to a list, there will be a copy of old list, which took a lot of memory\n",
166 | " # but there is no reference to them, so we can call the garbage collector\n",
167 | " gc.collect()\n",
168 | "\n",
169 | " # datasets is a 3D list, shape 0 is number of csv files, shape 1 is number of rows in that csv file, shape 2 is 1083\n",
170 | " return datasets"
171 | ],
172 | "metadata": {
173 | "id": "qbyrkpPFFCFK"
174 | },
175 | "execution_count": null,
176 | "outputs": []
177 | },
178 | {
179 | "cell_type": "code",
180 | "source": [
181 | "def loading_speed_steering_data(folder):\n",
182 | "\n",
183 | " # file's name start with 'car_state_blue'\n",
184 | " check_name = re.compile('^car_state_blue')\n",
185 | "\n",
186 | " datasets = []\n",
187 | "\n",
188 | " # Also need to iterate in sorted by name order\n",
189 | " for filename in sorted(os.listdir(folder)):\n",
190 | "\n",
191 | " files = os.path.join(folder, filename)\n",
192 | "\n",
193 | " if re.match(check_name, filename) and os.path.isfile(files):\n",
194 | "\n",
195 | " # We need another two columns from car_state_blue files,\n",
196 | " # one is Velocity_X, another is Steering_angle\n",
197 | " datasets.append(np.array(pd.read_csv(files).iloc[:, [3, 5]], dtype=float))\n",
198 | "\n",
199 | " gc.collect()\n",
200 | " # datasets is a 3D list, shape 0 is number of csv files, shape 1 is number of rows in that csv file, shape 2 is 2\n",
201 | " return datasets"
202 | ],
203 | "metadata": {
204 | "id": "ijuQd7Zex1W8"
205 | },
206 | "execution_count": null,
207 | "outputs": []
208 | },
209 | {
210 | "cell_type": "code",
211 | "source": [
212 | "# I made my dataset public, the link can be found in GitHub: https://github.com/JZ76/Training-Overtaking-Algorithm\n",
213 | "# Here, we will use google drive to store data\n",
214 | "# after you connect to runtime, you can mount your drive on left-hand-side, in files icon, there is a Mount Drive button on the top\n",
215 | "# And the drive will in path /content/drive/MyDrive/\n",
216 | "# replace name_of_the_datasets to the folder you want to use\n",
217 | "# OR, if you are using personal computer, feel free to replace the whole path\n",
218 | "folder = \"/content/drive/MyDrive/Australia_dataset\"\n",
219 | "\n",
220 | "# Currently, datasets contains partial input and all output, speed_steering contains partial input\n",
221 | "datasets = loading_data(folder)\n",
222 | "speed_steering = loading_speed_steering_data(folder)"
223 | ],
224 | "metadata": {
225 | "id": "YCJQh8HmFWmR"
226 | },
227 | "execution_count": null,
228 | "outputs": []
229 | },
230 | {
231 | "cell_type": "code",
232 | "source": [
233 | "# if you want to use part of the data, feel free to add bracket after datasets and speed_steering, like [0:100], make sure the size of sub data matches\n",
234 | "new_datasets = datasets\n",
235 | "new_speed_steering = speed_steering"
236 | ],
237 | "metadata": {
238 | "id": "TNdLpRpOnrAr"
239 | },
240 | "execution_count": null,
241 | "outputs": []
242 | },
243 | {
244 | "cell_type": "code",
245 | "source": [
246 | "X = []\n",
247 | "y = []\n",
248 | "\n",
249 | "# First two columns are speed and steering angle where are the output, aka y value\n",
250 | "# rest of columns are LiDAR data where are part of the input, aka X value\n",
251 | "for x in new_datasets:\n",
252 | " X.append(x[:, 2:])\n",
253 | " y.append(x[:, 0:2])"
254 | ],
255 | "metadata": {
256 | "id": "vccjXNqZge2n"
257 | },
258 | "execution_count": null,
259 | "outputs": []
260 | },
261 | {
262 | "cell_type": "code",
263 | "source": [
264 | "\"\"\"\n",
265 | "As you can see, different csv file has different size, which means different number of rows.\n",
266 | "However, the input matrix must have same shape in each instance (each csv file), for example 5000 * 1083\n",
267 | "So, we need to add values to those instance have less rows compare to the largest instance\n",
268 | "For example, if the largest instance is 5000 * 1083, and rest instances are x * 1083, where x is (0, 5000),\n",
269 | "Then, we add values to those instances to make all of them have 5000 * 1083 shape.\n",
270 | "The value need to be unique in the dataset, let's list data ranges in our new_datasets:\n",
271 | " speed: [-1, 1]\n",
272 | " steering angle: [-1, 1]\n",
273 | " LiDAR: [0, 10]\n",
274 | "new_speed_steering:\n",
275 | " Velocity_X: [-16, 16]\n",
276 | " Steering_angle: [-0.192, 0.192]\n",
277 | "any value that not in the ranges is ok, such as -100.0\n",
278 | "All these can be done by using tensorflow.keras.preprocessing.sequence.pad_sequences()\n",
279 | "\"\"\""
280 | ],
281 | "metadata": {
282 | "id": "xjTe2zQKDWpg"
283 | },
284 | "execution_count": null,
285 | "outputs": []
286 | },
287 | {
288 | "cell_type": "code",
289 | "source": [
290 | "special_value = -100.0"
291 | ],
292 | "metadata": {
293 | "id": "lds_5E2QhIu2"
294 | },
295 | "execution_count": null,
296 | "outputs": []
297 | },
298 | {
299 | "cell_type": "code",
300 | "source": [
301 | "# Sidenote: here is the most out of memory failure happened place\n",
302 | "Xpad_A = tensorflow.keras.preprocessing.sequence.pad_sequences(\n",
303 | " X, padding=\"post\", value=special_value\n",
304 | ")"
305 | ],
306 | "metadata": {
307 | "id": "bKOfIl6-JEgW"
308 | },
309 | "execution_count": null,
310 | "outputs": []
311 | },
312 | {
313 | "cell_type": "code",
314 | "source": [
315 | "ypad = tensorflow.keras.preprocessing.sequence.pad_sequences(\n",
316 | " y, padding=\"post\", value=special_value\n",
317 | ")"
318 | ],
319 | "metadata": {
320 | "id": "l985mVrylk7H"
321 | },
322 | "execution_count": null,
323 | "outputs": []
324 | },
325 | {
326 | "cell_type": "code",
327 | "source": [
328 | "# free some memory\n",
329 | "datasets = []\n",
330 | "new_datasets = []\n",
331 | "X = []\n",
332 | "y = []\n",
333 | "gc.collect()"
334 | ],
335 | "metadata": {
336 | "id": "ceWJcXCoz6Lj"
337 | },
338 | "execution_count": null,
339 | "outputs": []
340 | },
341 | {
342 | "cell_type": "code",
343 | "source": [
344 | "Xpad_B = tensorflow.keras.preprocessing.sequence.pad_sequences(\n",
345 | " new_speed_steering, padding=\"post\", value=special_value\n",
346 | ")"
347 | ],
348 | "metadata": {
349 | "id": "3s7e20TA1aWu"
350 | },
351 | "execution_count": null,
352 | "outputs": []
353 | },
354 | {
355 | "cell_type": "code",
356 | "source": [
357 | "speed_steering = []\n",
358 | "new_speed_steering = []\n",
359 | "gc.collect()"
360 | ],
361 | "metadata": {
362 | "id": "7cHpmYJ03J5A"
363 | },
364 | "execution_count": null,
365 | "outputs": []
366 | },
367 | {
368 | "cell_type": "code",
369 | "source": [
370 | "# starting with an Input layer, None means the length of each instance is varaible\n",
371 | "# but the number of columns is fixed, 1081\n",
372 | "inputA = Input(shape=(None, 1081))\n",
373 | "\n",
374 | "# Masking layer is to tell other layer that when see the special_value in the data, just ignore them\n",
375 | "# Padding and Masking usually used togather, \n",
376 | "# because special_value is useless, we don't want them have effects on the result\n",
377 | "A = Masking(input_shape=(None, 1081), mask_value=special_value)(inputA)\n",
378 | "\n",
379 | "# this Dense layer has similar effects to Embedding layer\n",
380 | "x = Dense(500, activation=\"relu\")(A)\n",
381 | "\n",
382 | "x = SimpleRNN(150, return_sequences=True, input_shape=(None, 1081))(x)\n",
383 | "\n",
384 | "\n",
385 | "# Second Input layer\n",
386 | "inputB = Input(shape=(None, 2))\n",
387 | "\n",
388 | "# Still need another Masking layer\n",
389 | "B = Masking(input_shape=(None, 2), mask_value=special_value)(inputB)\n",
390 | "\n",
391 | "# I will concatenate output from SimpleRNN and data from Xpad_B which is from car_state_blue csv files\n",
392 | "# In order to make sure data from two different files can be matched, \n",
393 | "# we need to make sure they are matched when reading the dataset\n",
394 | "# Otherwise it is impossible to sort them after read them as Dataframe\n",
395 | "# Here is the reason why I must read every csv files in sorted by name order\n",
396 | "# When I created the dataset, I used current Time as part of the file name\n",
397 | "# So, if we sort files by name, there will be no ambiguous, a newer csv can only after an older csv\n",
398 | "# Although the exact time that creating ML and car_state_blue file probably don't match, \n",
399 | "# their position in the csv file list definitely matched\n",
400 | "combined = concatenate([x, B], axis=2)\n",
401 | "\n",
402 | "z = Dense(256, activation=\"relu\")(combined)\n",
403 | "z = Dense(128, activation=\"relu\")(z)\n",
404 | "z = Dense(32, activation=\"tanh\")(z)\n",
405 | "z = Dense(2, activation=\"tanh\")(z)\n",
406 | "\n",
407 | "# build the model\n",
408 | "RNN_model = Model(inputs=[inputA, inputB], outputs=z)"
409 | ],
410 | "metadata": {
411 | "id": "o0Ol5sJtgp0i"
412 | },
413 | "execution_count": null,
414 | "outputs": []
415 | },
416 | {
417 | "cell_type": "code",
418 | "source": [
419 | "# OR, you can using an existing model\n",
420 | "# change the path or model name as you want\n",
421 | "RNN_model = load_model(\"/content/drive/MyDrive/models/model_RNN_x\")"
422 | ],
423 | "metadata": {
424 | "id": "IZsvOpai2HBb"
425 | },
426 | "execution_count": null,
427 | "outputs": []
428 | },
429 | {
430 | "cell_type": "code",
431 | "source": [
432 | "RNN_model.compile(loss=\"mean_squared_error\", optimizer=\"RMSprop\", metrics=['mean_squared_error'])\n",
433 | "\n",
434 | "RNN_model.summary()\n",
435 | "\n",
436 | "# shape of Xpad_A: [number of csv files, None, 1081]\n",
437 | "# shape of Xpad_B: [number of csv files, None, 2]\n",
438 | "# shape of ypad: [number of csv files, None, 2]\n",
439 | "# epochs is how many iterations of all csv files, it depends on the dataset\n",
440 | "# like when you creating the model, you probably need larger number of epochs,\n",
441 | "# but when using an existing model, small number of epochs is enough,\n",
442 | "# Again, be aware of duration limit in Colab\n",
443 | "# batch_size means update the params after processing how many instances, \n",
444 | "#here, one instance is one csv file\n",
445 | "RNN_model.fit([Xpad_A, Xpad_B], ypad, epochs=10, batch_size=5)"
446 | ],
447 | "metadata": {
448 | "id": "4BVEOqXghYlh"
449 | },
450 | "execution_count": null,
451 | "outputs": []
452 | },
453 | {
454 | "cell_type": "code",
455 | "source": [
456 | "# change the path or model name as you want\n",
457 | "RNN_model.save(\"/content/drive/MyDrive/models/new_model_RNN_x\")"
458 | ],
459 | "metadata": {
460 | "id": "xQ_CdiAKvB6-"
461 | },
462 | "execution_count": null,
463 | "outputs": []
464 | },
465 | {
466 | "cell_type": "code",
467 | "source": [
468 | "\"\"\"\n",
469 | "you may ask, where is the model testing code? how would we evaluate the model?\n",
470 | "Well, since this is for autonomous racing, higher accuracy doesn't necessary means better behaviour in racing\n",
471 | "And our data is quite small tbh, so I decided to use all data as training data\n",
472 | "and put the model into the simulator to evaluate it, rather than split data into training and testing\n",
473 | "\"\"\""
474 | ],
475 | "metadata": {
476 | "id": "oYC4zaI4DKix"
477 | },
478 | "execution_count": null,
479 | "outputs": []
480 | }
481 | ]
482 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # F1TENTH: An Over-taking Algorithm Using Machine Learning
2 |
3 |
4 | > This repository is part of the paper ***"[F1TENTH: An Over-taking Algorithm Using Machine Learning](https://ieeexplore.ieee.org/document/10275226)"***
5 | > The model, in the simulator repository, can be only used in the simulator, because not test on a real car yet.
6 | > If you want to apply this overtaking algorithm to a real F1tenth car, you have to minimize the gap between real car and simulated car first, then collecting some data and training a new model. More detail can be found below and wiki page in the simulator repository. Be prepared for facing a lot of extra work that maybe not relate to the algorithm, such as configuring signal between hardware and software.
7 |
8 | Please feel free to raise topics in Issues section, I will try my best to answer them!
9 |
10 | ## Citation
11 |
12 | @INPROCEEDINGS{10275226,
13 | author={Zhang, Jiancheng and Loidl, Hans–Wolfgang},
14 | booktitle={2023 28th International Conference on Automation and Computing (ICAC)},
15 | title={F1TENTH: An Over-taking Algorithm Using Machine Learning},
16 | year={2023},
17 | volume={},
18 | number={},
19 | pages={01-06},
20 | doi={10.1109/ICAC57885.2023.10275226}}
21 |
22 | ## Datasets
23 |
24 |
25 | ### How to create this?
26 |
27 |
28 | > Assuming the simulator is running well on your computer, and familiar with the basic usages first
29 |
30 | **Make sure the parameters of vehicle model won't be changed during creating dataset!!!**
31 |
32 | I implemented a feature that if you press a key on keyboard or a button on controller, the program will start recording data, and press again will stop recording data and save data as csv files. Which key or button can be found in params.yaml file.
33 |
34 | 1. Let's setup the leading vehicle first. You can use any algorithm you like to drive it, such as MPC, RRT, follow the gap, etc., but I implemented Model Predictive Control as controller. If you want to use MPC, the pipeline of how to generate minimum time trajectory waypoints can be found in [another repository](https://github.com/JZ76/Racetrack-Preparation). There are a lot of parameters in MPC algorithm, where can have huge effects on behaviour of driving, it will take sometime to fine-tune those parameters. I listed some of my experience of how to set parameters in the wiki page of simulator repository.
35 | 2. The ego vehicle can be controlled by a human driver, highly recommend using a Xbox controller, and drive it just like playing a racing game.
36 | 3. Before start creating dataset, it is **NECESSARY** to be familiar with *the feeling of the car*
37 | 4. You can change initial position of two cars in simulator.cpp file
38 | 5. When start recording, the leading vehicle must shown up in ego vehicle's LiDAR, you should see a little square in the simulator. And better start recording with both car is moving rather than static. You should stop recording data when finish overtaking i.e. you cannot see the leading vehicle in LiDAR
39 | 6. During recording, make sure there is no collision with either racetrack or car. If there is collision, you can press Ctrl+C to kill the simulator, so the data won't be saved, or you can save the data and delete them in folder. It would be easier if you sort your dataset in added time order, the last three files always the latest csv files.
40 | 7. Try to overtake with different strategies, sometimes overtake from left side, sometimes from right side.
41 |
42 | ### Dataset Info
43 |
44 |
45 | | Name | Size | Number of Files |
46 | | :--- | --- | :---: |
47 | | [Australia racetrack dataset](https://drive.google.com/drive/folders/15dC_8rJeR8NdOGdYL9UcydJbIoYOcNKv?usp=sharing) | 3.16 GB | 408 |
48 | | [Shanghai racetrack dataset](https://drive.google.com/drive/folders/1LTy3OdV9xb5wfOrsg5az78shDPH4PdlK?usp=sharing) | 4.13 GB | 579 |
49 | | [Gulf racetrack dataset](https://drive.google.com/drive/folders/1n7G9ZCKGhQVXsBCFU-rgMqVMhx6Wda88?usp=sharing) | 2.56 GB | 201 |
50 | | [Malaysian racetrack dataset](https://drive.google.com/drive/folders/1BNZFJpgWyqisEXyiZrBeysqKMa03_5Eu?usp=sharing) | 3.51 GB | 576 |
51 |
52 |
53 | ### Data Preview
54 |
55 |
56 | car_state_blue_XXXX or car_state_red_XXXX
57 |
58 | > Sidenote: vehicle model is bicycle model with dynamic equations.
59 | > blue car is the car perform overtaking behaviour, aka ego vehicle.
60 | > red car is the leading vehicle.
61 |
62 |
63 | | Position_X | Position_Y | Theta | Velocity_X | Velocity_Y | Steering_angle | Angular_velocity | slip_angle |
64 | | :---: | :----: | :---: | :---: | :---: | :---: | :---: | :---: |
65 | | x value of position coordinate in world frame of reference | y value of position coordinate in world frame of reference | vehicle heading direction in world frame of reference, 0 is the positive direction of X axis, and rotate toward positive of Y axis is increase theta | velocity in x axis direction but under vehicle frame of reference | velocity in y axis direction but under vehicle frame of reference | steering angle of the front wheels | angular velocity of center point of mass | slip angle of front wheels |
66 | | meter | meter | radians | m/s | m/s | radians | dθ/dt | radians |
67 |
68 |
69 | ML_dataset_blueXXXX
70 |
71 | > Speed and steering angle are the driving command, the real speed and steer angle are not the same as driving command
72 | > the vehicle model determines the mapping between driving command and real value
73 |
74 | | Speed | Steering_angle | LiDAR_scan |
75 | | :---: | :---: | :---: |
76 | | desired speed | desired steering angle | raw LiDAR data (simulated) |
77 | | m/s | radians | meter |
78 |
79 |
80 | ## Training A Model
81 |
82 |
83 | ### Environment
84 |
85 |
86 | As you can see, there are two files in this repo, one is called Colab_version.ipynb, this is for training on Colab or on Windows machine,
87 | another is called AppleSilicon_version.py, this is for training on M1/M1PRO/M1MAX/M1Ultra machine (probably works on M2/M2PRO?/M2MAX?/M2Ultra? machine).
88 |
89 | You don't have to install all packages as exact the same version as I listed, try to run the code first, see if there is any error that related to package version.
90 |
91 | > [Due to SimpleRNN cannot use cuda cores](https://www.tensorflow.org/guide/keras/rnn#performance_optimization_and_cudnn_kernels), it may take hours to train a model
92 |
93 | - Colab/Windows
94 | - numpy==1.18.5
95 | - tensorflow==2.8.2
96 | - pandas==1.4.2
97 |
98 | > Due to [some unknown bugs](https://github.com/tensorflow/tensorflow/issues/56082) and incompatible features in tensorflow, I changed some code to make it working on a Apple silicon
99 |
100 | - Apple Silicon
101 | - numpy==1.21.6
102 | - tensorflow-macos==2.8.0
103 | - pandas==1.4.2
104 |
105 |
106 | ### Overtaking Algorithm Design Principles (my appoarch)
107 |
108 |
109 | First of all, the dataset should include overtaking maneuvers in different scenario, i.e. different overtaking strategies in different racetracks. The way of creating models is called *imitation learning* , i.e. a machine learning model will try to copy behaviour from a dataset. Hence, the quality of a dataset determines the performance of a model.
110 |
111 | There are three hypothesis:
112 | - the leading vehicle will use minimum time trajectory
113 | - the max speed of leading vehicle is lower than the ego vehicle
114 | - the leading vehicle cannot defence overtaking, because the LiDAR sensor has 270° field of view rather than 360°, the car doesn't know what happened in the back
115 |
116 | These three hyothesis define boundary of the problem, the leading car playing their best but the top speed is lower than the overtaking car, so that the overtaking car can have opportunities to overtake, and [the competition rules](https://icra2022-race.f1tenth.org/rules.html) said penalty will be applied if upon crashing the opponent.
117 |
118 | After we have a dataset, we need to think about what kind of neural network is more suitable and how do we feed data into the neural network. Since this is imitation learning, we can start thinking by how do we drive a F1tenth car. Imagine you are sitting in front of screen, stare at the car in the simulator, hold a Xbox controller, if this is the first time you drive it, you will build a mapping relationship in your brain which from joystick and trigger to the movement of the car. Usually you will have a good understanding of how to drive well after few attempts. But how did you know the movement of the car? The only way is through LiDAR data, if the LiDAR pattern rotated fast, means the car is rotating fast; if the LiDAR pattern went back fast, means the car is going forward fast. Here, I said we actually combined LiDAR data from past few timestamps together **implicitly**, it is impossible to know the movement of the car from just one LiDAR data. This is the reason why Dense model doesn't work well, and this is where **Recurrent Neural Network** kicks in. RNN is very suitable for process sequence data, such as language, sounds, video. Although it seems like I should use computer vision algorithm because this is how we process it, numerical data here is more precise than image data.
119 |
120 | Is that it? However, there is one failure case, a long straight equal-width racetrack. From the LiDAR data only, you can only know whether the car is moving or not in horizontal direction, but you don't know the speed in vertical direction (along the racetrack). But you still know the car is going forward, why? Because your finger tells you that I am holding the trigger right now, the car should have speed. Hence, we need another input data for neural network: current car speed and steering angle. Note that the real speed and steering angle is different from driving command.
121 |
122 | Now, I need to think about how to glue different layers together to maximize the performance. There are a lot of models in RNN family, such as SimpleRNN, LSTM, GRU, Transformer. LSTM and GRU is designed for remembering elements at the beginning of a very long sequence. When you try to overtake the opponent, you won't remember LiDAR data from 10 seconds ago, because they are useless for current decision making. So, I use the SimpleRNN as one of the layers.
123 | For rest of layers, I took a inspiration from natural language process, added a Dense layer as [Embedding layer](https://www.youtube.com/watch?v=OuNH5kT-aD0) before the SimpleRNN layer. This Dense layer will filter useless information in LiDAR data, and reinforce important information, such as too close to the racetrack, the position of the component, etc. After the SimpleRNN layer, there are 4 Dense layers as final decision making, making decision by a fusion of the SimpleRNN output and real car speed and steering angle.
124 |
125 | Finally, have a look at the structure
126 |
127 |
128 |
129 | ### Fine-tuning parameters
130 |
131 |
132 | You can change number of nodes in each layer, and their activation function, as well as the loss function. It may take a while to find a good one that can perform very well on all dataset. You can select a fairly easy racetrack first, like Australia dataset, and create dozens of models, and test them on the same racetrack to see whether the model has good performance or not. Next, you can use a more difficult racetrack, like Shanghai dataset, some of models probably cannot drive well on that, then pick out good ones. And using a easier racetrack again, this is to avoid overfitting problem, one easy racetrack, one hard racetrack. In the end, maybe only very few models can pass all datasets, testing and evaluating them on racetracks that never seen before.
133 | I put some very good models on the simulator repository, welcome to have a try!
134 |
135 |
136 | ## Results (these racetracks are not in the training dataset)
137 |
138 |
139 | ## Self-driving
140 |
141 |
142 | 
143 |
144 |
145 | ## Overtaking
146 |
147 |
148 | 
149 |
150 |
151 | ## Following
152 |
153 |
154 | 
155 |
156 |
157 | ## Static obstacle avoiding
158 |
159 |
160 | 
161 |
162 |
163 |
--------------------------------------------------------------------------------