Skip to content

Instantly share code, notes, and snippets.

@tawnkramer
Created November 6, 2017 20:21
Show Gist options
  • Save tawnkramer/8e17e3cdf0d7133598c30e5a5d9c1436 to your computer and use it in GitHub Desktop.
Save tawnkramer/8e17e3cdf0d7133598c30e5a5d9c1436 to your computer and use it in GitHub Desktop.
donkey keras imu integration
import os
import numpy as np
import keras
import donkeycar as dk
from donkeycar.parts.keras import KerasPilot
class KerasIMU(KerasPilot):
def __init__(self, model=None, num_outputs=None, *args, **kwargs):
super(KerasIMU, self).__init__(*args, **kwargs)
self.model = default_imu(2)
def load(self, model_path):
self.model = keras.models.load_model(model_path)
def shutdown(self):
pass
def run(self, img_arr, accel_x, accel_y, accel_z, gyr_x, gyr_y, gyr_z, temp):
img_arr = img_arr.reshape((1,) + img_arr.shape)
imu_arr = np.array([accel_x, accel_y, accel_z, gyr_x, gyr_y, gyr_z, temp]).reshape(None,7)
outputs = self.model.predict([img_arr, imu_arr])
steering = outputs[0]
throttle = outputs[1]
return steering[0][0], throttle[0][0]
def default_imu(num_outputs):
from keras.layers import Input, Dense
from keras.models import Model
from keras.layers import Convolution2D, MaxPooling2D, Reshape, BatchNormalization
from keras.layers import Activation, Dropout, Flatten, Cropping2D, Lambda
from keras.layers.merge import concatenate
img_in = Input(shape=(120,160,3), name='img_in')
imu_in = Input(shape=(7,), name="imu_in")
x = img_in
x = Cropping2D(cropping=((60,0), (0,0)))(x) #trim 60 pixels off top
#x = Lambda(lambda x: x/127.5 - 1.)(x) # normalize and re-center
x = Convolution2D(24, (5,5), strides=(2,2), activation='relu')(x)
x = Convolution2D(32, (5,5), strides=(2,2), activation='relu')(x)
x = Convolution2D(64, (3,3), strides=(2,2), activation='relu')(x)
x = Convolution2D(64, (3,3), strides=(1,1), activation='relu')(x)
x = Convolution2D(64, (3,3), strides=(1,1), activation='relu')(x)
x = Flatten(name='flattened')(x)
x = Dense(100, activation='relu')(x)
x = Dropout(.1)(x)
y = imu_in
y = Dense(14, activation='relu')(y)
y = Dense(14, activation='relu')(y)
y = Dense(14, activation='relu')(y)
z = concatenate([x, y])
z = Dense(50, activation='relu')(z)
z = Dropout(.1)(z)
z = Dense(50, activation='relu')(z)
z = Dropout(.1)(z)
outputs = []
for i in range(num_outputs):
outputs.append(Dense(1, activation='linear', name='n_outputs' + str(i))(z))
model = Model(inputs=[img_in, imu_in], outputs=outputs)
model.compile(optimizer='adam',
loss='mse')
#print(model.summary())
return model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment