-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathai_drive_models.py
216 lines (174 loc) · 8.66 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
import time
import torch
import torchvision
from PIL import Image
import torch.nn as nn
import cv2
from torchvision.models import resnet18
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
# 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':
# 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':
# 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]
class LinearResModel(nn.Module):
def __init__(self):
super(LinearResModel, self).__init__()
self.resnet_rgb = resnet18(pretrained=False)
self.resnet_rgb.fc = nn.Sequential(
nn.Linear(512, 512), nn.BatchNorm1d(512), nn.ReLU(True))
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.resnet_rgb(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.ReLU(True))
self.layer_steering = nn.Sequential(
nn.Linear(256, 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(256, 256), nn.BatchNorm1d(256), nn.ReLU(True),
nn.Linear(256, 128), nn.BatchNorm1d(128), nn.ReLU(True),
nn.Linear(128, 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]