diff --git a/donkey/models.py b/donkey/models.py index b02e9ed17..6252329e6 100644 --- a/donkey/models.py +++ b/donkey/models.py @@ -1,8 +1,8 @@ """ Keras model constructors. -All models accept 120x160x3 images and output a -single floating point number (ie steering angle) +All models accept 120x160x3 images and output a +single floating point number (ie steering angle) """ @@ -11,6 +11,7 @@ from keras.models import Sequential from keras.layers import Convolution2D, MaxPooling2D, SimpleRNN, Reshape, BatchNormalization from keras.layers import Activation, Dropout, Flatten, Dense +from keras.regularizers import l2 def cnn3_full1(): @@ -81,7 +82,7 @@ def cnn1_full1(): img_in = Input(shape=(120, 160, 3), name='img_in') angle_in = Input(shape=(1,), name='angle_in') - + x = Convolution2D(1, 3, 3)(img_in) x = Activation('relu')(x) x = MaxPooling2D(pool_size=(2, 2))(x) @@ -131,7 +132,7 @@ def norm_cnn3_full1(): def vision_2D(dropout_frac=.2): - ''' + ''' Network with 4 convolutions, 2 residual shortcuts to predict angle. ''' img_in = Input(shape=(120, 160, 3), name='img_in') @@ -144,7 +145,7 @@ def vision_2D(dropout_frac=.2): #Create residual to shortcut aux1 = Flatten(name='aux1_flat')(net) - aux1 = Dense(64, name='aux1_dense')(aux1) + aux1 = Dense(64, name='aux1_dense')(aux1) net = Convolution2D(128, 3, 3, subsample=(2,2), border_mode='same', name='conv2')(net) net = Dropout(dropout_frac)(net) @@ -168,4 +169,39 @@ def vision_2D(dropout_frac=.2): angle_out = Dense(1, name='angle_out')(net) model = Model(input=[img_in], output=[angle_out]) model.compile(optimizer='adam', loss='mean_squared_error') - return model \ No newline at end of file + return model + + +def regularized_cnn4(): + reg = l2(0.005) + + img_in = Input(shape=(120, 160,3), name='img_in') + angle_in = Input(shape=(1,), name='angle_in') + + x = img_in + x = Convolution2D(4, 3, 3,W_regularizer=reg)(x) + x = Activation('relu')(x) + x = MaxPooling2D(pool_size=(2, 2))(x) + + x = Convolution2D(8, 3, 3, W_regularizer=reg)(x) + x = Activation('relu')(x) + x = MaxPooling2D(pool_size=(2, 2))(x) + + x = Convolution2D(16, 3, 3, W_regularizer=reg)(x) + x = Activation('relu')(x) + x = MaxPooling2D(pool_size=(2, 2))(x) + + x = Convolution2D(32, 3, 3, W_regularizer=reg)(x) + x = Activation('relu')(x) + x = MaxPooling2D(pool_size=(2, 2))(x) + x = Flatten()(x) + + x = Dense(128, W_regularizer=reg)(x) + x = Activation('linear')(x) + x = Dropout(.2)(x) + + angle_out = Dense(1, name='angle_out')(x) + + model = Model(input=[img_in], output=[angle_out]) + model.compile(optimizer='adam', loss='mean_squared_error') + return model