forked from caipeide/autorace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_drive_models.py
311 lines (254 loc) · 13.1 KB
/
ai_drive_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import time
import torch
import torchvision
from PIL import Image
import torch.nn as nn
import cv2
from torchvision.models import resnet18, squeezenet1_1
import numpy as np
from torchvision import transforms
class DriveClass:
# used for inference, with the ai-drive-model packed.
def __init__(self, cfg, model_type, drive_model, device, cam = None, half = False):
self.drive_model = drive_model
self.run_steering = 0.0
self.run_throttle = 0.0
self.cam = cam
self.device = device
self.half = half
self.model_type = model_type
self.img_seq = []
self.seq_length = cfg.SEQUENCE_LENGTH
self.channels = cfg.IMAGE_DEPTH
# initialize the deep network model, the first inference time is a bit slow
print('waiting for the camera image to warm up the network.')
while True:
img_arr = self.cam.run() # cv2, numpy array
time.sleep(0.5)
if img_arr is not None:
break
print('warming up the deep network model...')
t0 = time.time()
# self.run(np.ones([cfg.IMAGE_H, cfg.IMAGE_W, cfg.IMAGE_DEPTH]).astype(np.uint8)*255)
self.run(img_arr)
print('network initialized, time cost: %.2f s'%(time.time()-t0))
def update(self):
while self.cam.running:
# read the memory and compute
img_arr = self.cam.run_threaded() # cv2, numpy array
if img_arr is None:
continue
if self.model_type == 'linear' or self.model_type == 'resnet18' or self.model_type == 'squeez':
# print(img_arr.shape) # 224, 224, 3
img_arr = Image.fromarray(img_arr)
if self.half:
img_arr = transforms.ToTensor()(img_arr).half()
else:
img_arr = transforms.ToTensor()(img_arr)
img_arr = torch.unsqueeze(img_arr, 0).to(self.device)
run_steering, run_throttle = self.drive_model(img_arr)
elif self.model_type == 'rnn':
while len(self.img_seq) < self.seq_length:
self.img_seq.append(Image.fromarray(img_arr))
self.img_seq = self.img_seq[1:]
self.img_seq.append(Image.fromarray(img_arr))
rgbs = torch.stack( [transforms.ToTensor()(self.img_seq[k]) for k in range(self.seq_length)], dim=0 )
if self.half:
rgbs = rgbs.half()
rgbs = torch.unsqueeze(rgbs, 0).to(self.device)
run_steering, run_throttle = self.drive_model(rgbs)
run_steering = float(run_steering.detach().cpu().numpy())
run_throttle = float(run_throttle.detach().cpu().numpy())
self.run_steering = run_steering
self.run_throttle = run_throttle
def run(self, img_arr):
if self.model_type == 'linear' or self.model_type == 'resnet18' or self.model_type == 'squeez':
# print(img_arr.shape) # 224, 224, 3
img_arr = Image.fromarray(img_arr)
if self.half:
img_arr = transforms.ToTensor()(img_arr).half()
else:
img_arr = transforms.ToTensor()(img_arr)
img_arr = torch.unsqueeze(img_arr, 0).to(self.device)
run_steering, run_throttle = self.drive_model(img_arr)
elif self.model_type == 'rnn':
while len(self.img_seq) < self.seq_length:
self.img_seq.append(Image.fromarray(img_arr))
self.img_seq = self.img_seq[1:]
self.img_seq.append(Image.fromarray(img_arr))
rgbs = torch.stack( [transforms.ToTensor()(self.img_seq[k]) for k in range(self.seq_length)], dim=0 )
if self.half:
rgbs = rgbs.half()
rgbs = torch.unsqueeze(rgbs, 0).to(self.device)
run_steering, run_throttle = self.drive_model(rgbs)
run_steering = float(run_steering.detach().cpu().numpy())
run_throttle = float(run_throttle.detach().cpu().numpy())
return run_steering, run_throttle
def run_threaded(self, img_arr):
return self.run_steering, self.run_throttle
# class LinearModel(nn.Module):
# def __init__(self):
# super(LinearModel, self).__init__()
# # similar to the self-driving car model from Nvidia in 2016: https://developer.nvidia.com/blog/deep-learning-self-driving-cars/
# self.layer_cnn = nn.Sequential(
# nn.Conv2d(in_channels= 3, out_channels= 32, kernel_size=3, stride=2, padding=1), #size: 224-->112
# nn.BatchNorm2d(32),
# nn.ReLU(inplace=True),
# nn.Conv2d(in_channels= 32, out_channels= 64, kernel_size=3, stride=2, padding=1), #112-->56
# nn.BatchNorm2d(64),
# nn.ReLU(inplace=True),
# nn.Conv2d(in_channels= 64, out_channels= 128, kernel_size=3, stride=2, padding=1), #56-->28
# nn.BatchNorm2d(128),
# nn.ReLU(inplace=True),
# nn.Conv2d(in_channels= 128, out_channels= 256, kernel_size=3, stride=2, padding=1), #28-->14
# nn.BatchNorm2d(256),
# nn.ReLU(inplace=True),
# nn.Conv2d(in_channels= 256, out_channels= 512, kernel_size=3, stride=2, padding=1), #14-->7, final size: batch_size*512*7*7
# nn.BatchNorm2d(512),
# nn.ReLU(inplace=True)
# )
# self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) # pooling, change the size to batch_size*512*1*1
# self.layer_steering = nn.Sequential(
# nn.Linear(512, 256), nn.BatchNorm1d(256), nn.ReLU(True),
# nn.Linear(256, 128), nn.BatchNorm1d(128), nn.ReLU(True),
# nn.Linear(128, 1)
# )
# self.layer_throttle = nn.Sequential(
# nn.Linear(512, 256), nn.BatchNorm1d(256), nn.ReLU(True),
# nn.Linear(256, 128), nn.BatchNorm1d(128), nn.ReLU(True),
# nn.Linear(128, 1)
# )
# def forward(self, rgb):
# x = self.layer_cnn(rgb) # batch, 512, 7,7
# x = self.avgpool(x) # batch, 512, 1, 1
# x = torch.flatten(x, start_dim=1) # flatten to size of: batch_size*512
# steering = self.layer_steering(x)
# throttle = self.layer_throttle(x)
# return steering[:,0], throttle[:,0]
#Modified Linear Model Josef
class LinearModel(nn.Module):
def __init__(self):
super(LinearModel, self).__init__()
# similar to the self-driving car model from Nvidia in 2016: https://developer.nvidia.com/blog/deep-learning-self-driving-cars/
self.layer_cnn = nn.Sequential(
nn.Conv2d(in_channels= 3, out_channels= 24, kernel_size=5, stride=2, padding=1), #size: 224-->112
nn.BatchNorm2d(24),
nn.ELU(inplace=True),
nn.Conv2d(in_channels= 24, out_channels= 36, kernel_size=5, stride=2, padding=1), #112-->56
nn.BatchNorm2d(36),
nn.ELU(inplace=True),
nn.Conv2d(in_channels= 36, out_channels= 48, kernel_size=5, stride=2, padding=1), #56-->28
nn.BatchNorm2d(48),
nn.ELU(inplace=True),
nn.Conv2d(in_channels= 48, out_channels= 64, kernel_size=3, stride=1, padding=1), #28-->14
nn.BatchNorm2d(64),
nn.ELU(inplace=True),
nn.Conv2d(in_channels= 64, out_channels= 64, kernel_size=3, stride=1, padding=1), #14-->7, final size: batch_size*512*7*7
nn.BatchNorm2d(64),
nn.ELU(inplace=True),
nn.Dropout(p=0.25)
)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) # pooling, change the size to batch_size*512*1*1
self.layer_steering = nn.Sequential(
# nn.Linear(64*1*18, 100), nn.BatchNorm1d(100), nn.ELU(True), nn.Dropout(p=0.4),
nn.Linear(64, 32), nn.BatchNorm1d(32), nn.ELU(True),
nn.Linear(32, 10), nn.BatchNorm1d(10), nn.ELU(True),
nn.Linear(10, 1)
)
self.layer_throttle = nn.Sequential(
# nn.Linear(64*1*18, 100), nn.BatchNorm1d(100), nn.ELU(True), nn.Dropout(p=0.4),
nn.Linear(64, 32), nn.BatchNorm1d(32), nn.ELU(True),
nn.Linear(32, 10), nn.BatchNorm1d(10), nn.ELU(True),
nn.Linear(10, 1)
)
def forward(self, rgb):
x = self.layer_cnn(rgb) # batch, 512, 7,7
x = self.avgpool(x) # batch, 512, 1, 1
x = torch.flatten(x, start_dim=1) # flatten to size of: batch_size*512
steering = self.layer_steering(x)
throttle = self.layer_throttle(x)
return steering[:,0], throttle[:,0]
class LinearResModel(nn.Module):
def __init__(self, channels):
super(LinearResModel, self).__init__()
self.resnet_rgb = resnet18(pretrained=False)
if channels == 1:
self.resnet_rgb.conv1 = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
elif channels != 3:
raise NotImplementedError
self.resnet_rgb.fc = nn.Sequential(
nn.Linear(512, 512), nn.BatchNorm1d(512), nn.ELU(True))
self.layer_steering = nn.Sequential(
nn.Dropout(p=0.25),
nn.Linear(512, 256), nn.BatchNorm1d(256), nn.ELU(True),
nn.Linear(256, 128), nn.BatchNorm1d(128), nn.ELU(True),
nn.Linear(128, 50), nn.BatchNorm1d(50), nn.ELU(True),
nn.Linear(50, 1)
)
self.layer_throttle = nn.Sequential(
nn.Dropout(p=0.25),
nn.Linear(512, 256), nn.BatchNorm1d(256), nn.ELU(True),
nn.Linear(256, 128), nn.BatchNorm1d(128), nn.ELU(True),
nn.Linear(128, 50), nn.BatchNorm1d(50), nn.ELU(True),
nn.Linear(50, 1)
)
def forward(self, rgb):
x = self.resnet_rgb(rgb)
steering = self.layer_steering(x)
throttle = self.layer_throttle(x)
return steering[:,0], throttle[:,0]
# Josef
class Squeezenet(nn.Module):
def __init__(self):
super(Squeezenet, self).__init__()
#torchvision.models.squeezenet1_1(pretrained=False, progress=True, **kwargs)
self.squeez = squeezenet1_1(pretrained=False)
self.squeez.classifier[1] = nn.Conv2d(512,512, kernel_size=(3,3), stride=(1,1))
self.layer_steering = nn.Sequential(
nn.Dropout(p=0.25),
nn.Linear(512, 256), nn.BatchNorm1d(256), nn.ELU(True),
nn.Linear(256, 128), nn.BatchNorm1d(128), nn.ELU(True),
nn.Linear(128, 50), nn.BatchNorm1d(50), nn.ELU(True),
nn.Linear(50, 1)
)
self.layer_throttle = nn.Sequential(
nn.Dropout(p=0.25),
nn.Linear(512, 256), nn.BatchNorm1d(256), nn.ELU(True),
nn.Linear(256, 128), nn.BatchNorm1d(128), nn.ELU(True),
nn.Linear(128, 50), nn.BatchNorm1d(50), nn.ELU(True),
nn.Linear(50, 1)
)
def forward(self, rgb):
x = self.squeez(rgb)
steering = self.layer_steering(x)
throttle = self.layer_throttle(x)
return steering[:,0], throttle[:,0]
class RNNModel(nn.Module):
def __init__(self):
super(RNNModel, self).__init__()
self.resnet_rgb = resnet18(pretrained=False)
self.resnet_rgb.fc = nn.Sequential(
nn.Linear(512, 512), nn.BatchNorm1d(512), nn.ELU(True))
self.layer_steering = nn.Sequential(
nn.Dropout(p=0.25),
nn.Linear(256, 128), nn.BatchNorm1d(128), nn.ELU(True),
nn.Linear(128, 50), nn.BatchNorm1d(50), nn.ELU(True),
nn.Linear(50, 1)
)
self.layer_throttle = nn.Sequential(
nn.Dropout(p=0.25),
nn.Linear(256, 128), nn.BatchNorm1d(128), nn.ELU(True),
nn.Linear(128, 50), nn.BatchNorm1d(50), nn.ELU(True),
nn.Linear(50, 1)
)
self.layer_lstm = nn.LSTM(512, hidden_size=256, num_layers=3, batch_first=True)
def forward(self, rgbs):
batch_size_actual_val = rgbs.size(0)
img_features = torch.Tensor(batch_size_actual_val,rgbs.size(1),512).cuda()
for p in range(rgbs.size(1)):
img_features[:,p,:] = self.resnet_rgb(rgbs[:,p,:,:,:]) # 2,12,3,224,224
seq_features,_ = self.layer_lstm(img_features)
seq_feature = seq_features[:,-1,:]
steering = self.layer_steering(seq_feature)
throttle = self.layer_throttle(seq_feature)
return steering[:,0], throttle[:,0]