Skip to content

Commit

Permalink
Switched inferencing from predict -> call as this is faster! (#861)
Browse files Browse the repository at this point in the history
  • Loading branch information
DocGarbanzo committed May 9, 2021
1 parent 8a97a7e commit 1c4569c
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions donkeycar/parts/keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ def inference(self, img_arr, other_arr):
return 0.0, 0.0

img_arr = img_arr.reshape((1,) + img_arr.shape)
angle_binned, throttle_binned = self.model.predict(img_arr)
angle_binned_tensor, throttle_binned_tensor = self.model(img_arr)
angle_binned = angle_binned_tensor.numpy()
throttle_binned = throttle_binned_tensor.numpy()
N = len(throttle_binned[0])
throttle = dk.utils.linear_unbin(throttle_binned, N=N,
offset=0.0, R=self.throttle_range)
Expand Down Expand Up @@ -303,9 +305,9 @@ def compile(self):

def inference(self, img_arr, other_arr):
img_arr = img_arr.reshape((1,) + img_arr.shape)
outputs = self.model.predict(img_arr)
steering = outputs[0]
throttle = outputs[1]
outputs = self.model(img_arr)
steering = outputs[0].numpy()
throttle = outputs[1].numpy()
return steering[0][0], throttle[0][0]

def y_transform(self, record: TubRecord):
Expand Down Expand Up @@ -339,8 +341,8 @@ def compile(self):

def inference(self, img_arr, other_arr):
img_arr = img_arr.reshape((1,) + img_arr.shape)
outputs = self.model.predict(img_arr)
steering = outputs[0]
outputs = self.model(img_arr)
steering = outputs[0].numpy()
return steering[0], dk.utils.throttle(steering[0])

def y_transform(self, record: TubRecord):
Expand Down Expand Up @@ -398,9 +400,9 @@ def compile(self):
def inference(self, img_arr, other_arr):
img_arr = img_arr.reshape((1,) + img_arr.shape)
imu_arr = np.array(other_arr).reshape(1, self.num_imu_inputs)
outputs = self.model.predict([img_arr, imu_arr])
steering = outputs[0]
throttle = outputs[1]
outputs = self.model([img_arr, imu_arr])
steering = outputs[0].numpy()
throttle = outputs[1].numpy()
return steering[0][0], throttle[0][0]

def y_transform(self, record: TubRecord):
Expand All @@ -425,7 +427,9 @@ def compile(self):
def inference(self, img_arr, state_array):
img_arr = img_arr.reshape((1,) + img_arr.shape)
bhv_arr = np.array(state_array).reshape(1, len(state_array))
angle_binned, throttle = self.model.predict([img_arr, bhv_arr])
angle_binned_tensor, throttle_tensor = self.model([img_arr, bhv_arr])
angle_binned = angle_binned_tensor.numpy()
throttle = throttle_tensor.numpy()
# In order to support older models with linear throttle,we will test for
# shape of throttle to see if it's the newer binned version.
N = len(throttle[0])
Expand Down Expand Up @@ -461,7 +465,10 @@ def compile(self):

def inference(self, img_arr, other_arr):
img_arr = img_arr.reshape((1,) + img_arr.shape)
angle, throttle, track_loc = self.model.predict([img_arr])
angle_t, throttle_t, track_loc_t = self.model([img_arr])
angle = angle_t.numpy()
throttle = throttle_t.numpy()
track_loc = track_loc_t.numpy()
loc = np.argmax(track_loc[0])
return angle, throttle, loc

Expand Down Expand Up @@ -651,9 +658,9 @@ def inference(self, img_arr, other_arr):

img_arr = np.array(self.img_seq).reshape((1, self.seq_length,
*self.input_shape))
outputs = self.model.predict([img_arr])
steering = outputs[0][0]
throttle = outputs[0][1]
outputs = self.model([img_arr])
steering = outputs[0][0].numpy()
throttle = outputs[0][1].numpy()
return steering, throttle


Expand Down Expand Up @@ -718,9 +725,9 @@ def inference(self, img_arr, other_arr):

img_arr = np.array(self.img_seq).reshape((1, self.seq_length,
*self.input_shape))
outputs = self.model.predict([img_arr])
steering = outputs[0][0]
throttle = outputs[0][1]
outputs = self.model([img_arr])
steering = outputs[0][0].numpy()
throttle = outputs[0][1].numpy()
return steering, throttle


Expand Down Expand Up @@ -806,9 +813,9 @@ def compile(self):

def inference(self, img_arr, other_arr):
img_arr = img_arr.reshape((1,) + img_arr.shape)
outputs = self.model.predict(img_arr)
steering = outputs[1]
throttle = outputs[2]
outputs = self.model(img_arr)
steering = outputs[1].numpy()
throttle = outputs[2].numpy()
return steering[0][0], throttle[0][0]


Expand Down

0 comments on commit 1c4569c

Please sign in to comment.