Skip to content

Commit e0be47d

Browse files
committed
Initial Commit
0 parents  commit e0be47d

File tree

4 files changed

+482
-0
lines changed

4 files changed

+482
-0
lines changed

Run_Simulation.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
# coding: utf-8
3+
4+
# In[4]:
5+
6+
import os
7+
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
8+
9+
import base64
10+
from datetime import datetime
11+
import os
12+
import shutil
13+
import numpy as np
14+
import socketio
15+
import eventlet
16+
import eventlet.wsgi
17+
from PIL import Image
18+
from flask import Flask
19+
from io import BytesIO
20+
21+
from keras.models import load_model
22+
23+
import utils
24+
25+
26+
# In[5]:
27+
28+
29+
model = load_model('./model.h5')
30+
31+
32+
# In[6]:
33+
34+
35+
sio = socketio.Server()
36+
app = Flask(__name__)
37+
MAX_SPEED = 25
38+
MIN_SPEED = 10
39+
speed_limit = MAX_SPEED
40+
41+
# In[7]:
42+
43+
44+
def send_control(steering_angle, throttle):
45+
sio.emit(
46+
"steer",
47+
data={
48+
'steering_angle': steering_angle.__str__(),
49+
'throttle': throttle.__str__()
50+
},
51+
skip_sid=True)
52+
53+
54+
# In[8]:
55+
56+
57+
@sio.on('connect')
58+
def connect(sid, environ):
59+
print("connect ", sid)
60+
send_control(0, 0)
61+
62+
@sio.on('telemetry')
63+
def telemetry(sid, data):
64+
if data:
65+
steering_angle = float(data["steering_angle"])
66+
throttle = float(data["throttle"])
67+
speed = float(data["speed"])
68+
#print (steering_angle, throttle, speed)
69+
70+
image = Image.open(BytesIO(base64.b64decode(data["image"])))
71+
72+
try:
73+
image = np.asarray(image)
74+
image = utils.process(image)
75+
image = image/255.0
76+
image = np.array([image])
77+
78+
steering_angle = float(model.predict(image, batch_size=1))
79+
80+
global speed_limit
81+
if speed > speed_limit:
82+
speed_limit = MIN_SPEED # slow down
83+
else:
84+
speed_limit = MAX_SPEED
85+
86+
throttle = 1.0 - ( (steering_angle)**2 ) - ( (speed/speed_limit)**2 )
87+
#throttle = 1.0
88+
89+
print('{} {} {}'.format(steering_angle, throttle, speed))
90+
send_control(steering_angle, throttle)
91+
92+
except Exception as e:
93+
print(e)
94+
95+
else:
96+
sio.emit('manual', data={}, skip_sid=True)
97+
98+
99+
# In[9]:
100+
101+
102+
app = socketio.Middleware(sio, app)
103+
eventlet.wsgi.server(eventlet.listen(('0.0.0.0', 4567)), app)
104+

model.h5

2.93 MB
Binary file not shown.

model.ipynb

+270
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [
8+
{
9+
"name": "stderr",
10+
"output_type": "stream",
11+
"text": [
12+
"Using TensorFlow backend.\n",
13+
"c:\\users\\suran\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
14+
" from ._conv import register_converters as _register_converters\n"
15+
]
16+
}
17+
],
18+
"source": [
19+
"import os\n",
20+
"os.environ['CUDA_VISIBLE_DEVICES'] = ''\n",
21+
"import numpy as np\n",
22+
"from sklearn.model_selection import train_test_split\n",
23+
"from keras.models import Sequential, save_model\n",
24+
"from keras.optimizers import Adam\n",
25+
"from keras.callbacks import ModelCheckpoint\n",
26+
"from keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten\n",
27+
"from utils import INPUT_SHAPE, generate_dataset"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 2,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"data_X, data_Y = generate_dataset()"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": 3,
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"X_train, X_test, Y_train, Y_test = train_test_split(data_X, data_Y, test_size = 0.2 )"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 4,
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"X_train = X_train/255.0\n",
55+
"X_test = X_test/255.0"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 5,
61+
"metadata": {},
62+
"outputs": [
63+
{
64+
"name": "stdout",
65+
"output_type": "stream",
66+
"text": [
67+
"(6251, 66, 200, 3) (1563, 66, 200, 3)\n",
68+
"(6251,) (1563,)\n"
69+
]
70+
}
71+
],
72+
"source": [
73+
"print (X_train.shape, X_test.shape)\n",
74+
"print (Y_train.shape, Y_test.shape)"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 6,
80+
"metadata": {},
81+
"outputs": [
82+
{
83+
"name": "stdout",
84+
"output_type": "stream",
85+
"text": [
86+
"WARNING:tensorflow:From c:\\users\\suran\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:968: calling reduce_prod (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n",
87+
"Instructions for updating:\n",
88+
"keep_dims is deprecated, use keepdims instead\n",
89+
"____________________________________________________________________________________________________\n",
90+
"Layer (type) Output Shape Param # Connected to \n",
91+
"====================================================================================================\n",
92+
"convolution2d_1 (Convolution2D) (None, 31, 98, 24) 1824 convolution2d_input_1[0][0] \n",
93+
"____________________________________________________________________________________________________\n",
94+
"convolution2d_2 (Convolution2D) (None, 14, 47, 36) 21636 convolution2d_1[0][0] \n",
95+
"____________________________________________________________________________________________________\n",
96+
"convolution2d_3 (Convolution2D) (None, 5, 22, 48) 43248 convolution2d_2[0][0] \n",
97+
"____________________________________________________________________________________________________\n",
98+
"convolution2d_4 (Convolution2D) (None, 3, 20, 64) 27712 convolution2d_3[0][0] \n",
99+
"____________________________________________________________________________________________________\n",
100+
"convolution2d_5 (Convolution2D) (None, 1, 18, 64) 36928 convolution2d_4[0][0] \n",
101+
"____________________________________________________________________________________________________\n",
102+
"dropout_1 (Dropout) (None, 1, 18, 64) 0 convolution2d_5[0][0] \n",
103+
"____________________________________________________________________________________________________\n",
104+
"flatten_1 (Flatten) (None, 1152) 0 dropout_1[0][0] \n",
105+
"____________________________________________________________________________________________________\n",
106+
"dense_1 (Dense) (None, 100) 115300 flatten_1[0][0] \n",
107+
"____________________________________________________________________________________________________\n",
108+
"dense_2 (Dense) (None, 50) 5050 dense_1[0][0] \n",
109+
"____________________________________________________________________________________________________\n",
110+
"dense_3 (Dense) (None, 10) 510 dense_2[0][0] \n",
111+
"____________________________________________________________________________________________________\n",
112+
"dense_4 (Dense) (None, 1) 11 dense_3[0][0] \n",
113+
"====================================================================================================\n",
114+
"Total params: 252,219\n",
115+
"Trainable params: 252,219\n",
116+
"Non-trainable params: 0\n",
117+
"____________________________________________________________________________________________________\n"
118+
]
119+
}
120+
],
121+
"source": [
122+
"model = Sequential()\n",
123+
"model.add(Conv2D(24, 5, 5, activation='relu',input_shape = INPUT_SHAPE, subsample=(2, 2)))\n",
124+
"model.add(Conv2D(36, 5, 5, activation='relu', subsample=(2, 2)))\n",
125+
"model.add(Conv2D(48, 5, 5, activation='relu', subsample=(2, 2)))\n",
126+
"model.add(Conv2D(64, 3, 3, activation='relu'))\n",
127+
"model.add(Conv2D(64, 3, 3, activation='relu'))\n",
128+
"model.add(Dropout(0.5))\n",
129+
"model.add(Flatten())\n",
130+
"model.add(Dense(100, activation='relu'))\n",
131+
"model.add(Dense(50, activation='relu'))\n",
132+
"model.add(Dense(10, activation='relu'))\n",
133+
"model.add(Dense(1))\n",
134+
"model.summary()"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": 7,
140+
"metadata": {},
141+
"outputs": [
142+
{
143+
"name": "stdout",
144+
"output_type": "stream",
145+
"text": [
146+
"WARNING:tensorflow:From c:\\users\\suran\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:996: calling reduce_mean (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n",
147+
"Instructions for updating:\n",
148+
"keep_dims is deprecated, use keepdims instead\n"
149+
]
150+
}
151+
],
152+
"source": [
153+
"checkpoint = ModelCheckpoint('model-{val_loss:.4f}.h5',\n",
154+
" monitor='val_loss',\n",
155+
" verbose=0,\n",
156+
" save_best_only=True,\n",
157+
" mode='auto')\n",
158+
"model.compile(loss='mean_squared_error', optimizer=Adam(lr=1.0e-4))"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": 9,
164+
"metadata": {},
165+
"outputs": [
166+
{
167+
"name": "stdout",
168+
"output_type": "stream",
169+
"text": [
170+
"Train on 6251 samples, validate on 1563 samples\n",
171+
"Epoch 1/30\n",
172+
"6251/6251 [==============================] - 22s - loss: 0.0240 - val_loss: 0.0270\n",
173+
"Epoch 2/30\n",
174+
"6251/6251 [==============================] - 21s - loss: 0.0235 - val_loss: 0.0259\n",
175+
"Epoch 3/30\n",
176+
"6251/6251 [==============================] - 21s - loss: 0.0221 - val_loss: 0.0252\n",
177+
"Epoch 4/30\n",
178+
"6251/6251 [==============================] - 22s - loss: 0.0213 - val_loss: 0.0229\n",
179+
"Epoch 5/30\n",
180+
"6251/6251 [==============================] - 22s - loss: 0.0206 - val_loss: 0.0223\n",
181+
"Epoch 6/30\n",
182+
"6251/6251 [==============================] - 21s - loss: 0.0201 - val_loss: 0.0236\n",
183+
"Epoch 7/30\n",
184+
"6251/6251 [==============================] - 21s - loss: 0.0200 - val_loss: 0.0230\n",
185+
"Epoch 8/30\n",
186+
"6251/6251 [==============================] - 22s - loss: 0.0197 - val_loss: 0.0226\n",
187+
"Epoch 9/30\n",
188+
"6251/6251 [==============================] - 23s - loss: 0.0196 - val_loss: 0.0224\n",
189+
"Epoch 10/30\n",
190+
"6251/6251 [==============================] - 23s - loss: 0.0194 - val_loss: 0.0224\n",
191+
"Epoch 11/30\n",
192+
"6251/6251 [==============================] - 21s - loss: 0.0193 - val_loss: 0.0220\n",
193+
"Epoch 12/30\n",
194+
"6251/6251 [==============================] - 25s - loss: 0.0191 - val_loss: 0.0216\n",
195+
"Epoch 13/30\n",
196+
"6251/6251 [==============================] - 21s - loss: 0.0191 - val_loss: 0.0219\n",
197+
"Epoch 14/30\n",
198+
"6251/6251 [==============================] - 25s - loss: 0.0189 - val_loss: 0.0217\n",
199+
"Epoch 15/30\n",
200+
"6251/6251 [==============================] - 21s - loss: 0.0187 - val_loss: 0.0212\n",
201+
"Epoch 16/30\n",
202+
"6251/6251 [==============================] - 24s - loss: 0.0186 - val_loss: 0.0217\n",
203+
"Epoch 17/30\n",
204+
"6251/6251 [==============================] - 24s - loss: 0.0184 - val_loss: 0.0215\n",
205+
"Epoch 18/30\n",
206+
"6251/6251 [==============================] - 25s - loss: 0.0183 - val_loss: 0.0215\n",
207+
"Epoch 19/30\n",
208+
"6251/6251 [==============================] - 21s - loss: 0.0182 - val_loss: 0.0211\n",
209+
"Epoch 20/30\n",
210+
"6251/6251 [==============================] - 23s - loss: 0.0178 - val_loss: 0.0208\n",
211+
"Epoch 21/30\n",
212+
"6251/6251 [==============================] - 24s - loss: 0.0175 - val_loss: 0.0210\n",
213+
"Epoch 22/30\n",
214+
"6251/6251 [==============================] - 22s - loss: 0.0177 - val_loss: 0.0208\n",
215+
"Epoch 23/30\n",
216+
"6251/6251 [==============================] - 22s - loss: 0.0175 - val_loss: 0.0211\n",
217+
"Epoch 24/30\n",
218+
"6251/6251 [==============================] - 26s - loss: 0.0173 - val_loss: 0.0213\n",
219+
"Epoch 25/30\n",
220+
"6251/6251 [==============================] - 24s - loss: 0.0173 - val_loss: 0.0209\n",
221+
"Epoch 26/30\n",
222+
"6251/6251 [==============================] - 23s - loss: 0.0170 - val_loss: 0.0206\n",
223+
"Epoch 27/30\n",
224+
"6251/6251 [==============================] - 22s - loss: 0.0168 - val_loss: 0.0209\n",
225+
"Epoch 28/30\n",
226+
"6251/6251 [==============================] - 23s - loss: 0.0166 - val_loss: 0.0219\n",
227+
"Epoch 29/30\n",
228+
"6251/6251 [==============================] - 24s - loss: 0.0166 - val_loss: 0.0207\n",
229+
"Epoch 30/30\n",
230+
"6251/6251 [==============================] - 22s - loss: 0.0163 - val_loss: 0.0207\n"
231+
]
232+
},
233+
{
234+
"data": {
235+
"text/plain": [
236+
"<keras.callbacks.History at 0x18455984748>"
237+
]
238+
},
239+
"execution_count": 9,
240+
"metadata": {},
241+
"output_type": "execute_result"
242+
}
243+
],
244+
"source": [
245+
"model.fit( X_train, Y_train, batch_size=32, nb_epoch=30, validation_data=(X_test, Y_test),callbacks=[checkpoint], shuffle=True )"
246+
]
247+
}
248+
],
249+
"metadata": {
250+
"kernelspec": {
251+
"display_name": "Python 3",
252+
"language": "python",
253+
"name": "python3"
254+
},
255+
"language_info": {
256+
"codemirror_mode": {
257+
"name": "ipython",
258+
"version": 3
259+
},
260+
"file_extension": ".py",
261+
"mimetype": "text/x-python",
262+
"name": "python",
263+
"nbconvert_exporter": "python",
264+
"pygments_lexer": "ipython3",
265+
"version": "3.6.2"
266+
}
267+
},
268+
"nbformat": 4,
269+
"nbformat_minor": 2
270+
}

0 commit comments

Comments
 (0)