measurements.append(steering_center - correction) #right images.append(np.fliplr(img_right)) measurements.append(-(steering_center - correction)) # right images.append(np.fliplr(image)) measurements.append(-steering_center) X_train = np.array(images) y_train = np.array(measurements) np.save("X_train.npy", X_train) np.save("y_train.npy", y_train) model = Sequential([ Lambda(lambda x: (x / 255.0) - 0.5, input_shape=(160, 320, 3)), Cropping2D(((70, 25), (0, 0))), Conv2D(8, (5, 5)), MaxPooling2D((2, 4)), Conv2D(16, (3, 3)), MaxPooling2D((2, 4)), Conv2D(24, (3, 3)), Flatten(), Dropout(.4), Dense(108, activation='elu'), Dense(21, activation='elu'), Dense(7, activation='elu'), Dense(1) ]) # Using adam optimizer so that learning rate will be controlled by it, applying a small decay.
# Strip out some of the data remove_straights(lines) train_samples, validation_samples = train_test_split(lines, test_size=.20) train_generator = generator(train_samples, batch_size=BATCH_SIZE) validation_generator = generator(validation_samples, batch_size=BATCH_SIZE) #Instantiate the model model = Sequential() #Normalize the data model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160, 320, 3))) #normalize the data and give it a mean of 0 # Crop the data model.add(Cropping2D(cropping=((50, 25), (0, 0)))) # Nvidia model taken from: https://devblogs.nvidia.com/deep-learning-self-driving-cars/ model.add(Convolution2D(24, 5, 5, subsample=(2, 2), activation='relu')) #model.add(SpatialDropout2D(.2)) model.add(Convolution2D(36, 5, 5, subsample=(2, 2), activation='relu')) #model.add(SpatialDropout2D(.2)) model.add(Convolution2D(48, 5, 5, subsample=(2, 2), activation='relu')) #model.add(SpatialDropout2D(.2)) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(Flatten()) model.add(Dense(1164, activation='relu')) model.add(Dense(100, activation='relu')) #model.add(Dropout(.5)) model.add(Dense(50, activation='relu')) model.add(Dense(10, activation='relu'))
X_train = np.array(augmented_images) y_train = np.array(augmented_measurements) from keras.models import Sequential from keras.layers import Flatten, Dense, Lambda from keras.layers.convolutional import Convolution2D from keras.layers.pooling import MaxPooling2D input_shape = [160, 320, 3] #theta.crop_shape = ((80,20),(1,1)) crop_shape= ((80,20),(1,1)) #input_shape=(120,300,3) model = Sequential() # cropping data below to remove noise such as sky and scenery around which does not influence steering angle model.add(Cropping2D(crop_shape, input_shape=input_shape, name="Crop")) # Normalize input. model.add(Lambda(lambda x:x/255.0 - 0.5 )) # conv2d layer added model.add(Convolution2D(6,5,5,activation="relu")) model.add(MaxPooling2D()) model.add(Convolution2D(6,5,5,activation="relu")) model.add(MaxPooling2D()) model.add(Convolution2D(6,5,5,activation="relu")) model.add(MaxPooling2D()) model.add(Dropout(0.05, name="Dropout")) model.add(Flatten()) model.add(Dense(120)) model.add(Dense(84)) model.add(Dense(1)) # adding optimizer below
def build_fcn8(img_shape=(3, None, None), nclasses=8, l2_reg=0., init='glorot_uniform', path_weights=None, freeze_layers_from=None): # Regularization warning if l2_reg > 0.: print ("Regularizing the weights: " + str(l2_reg)) # Build network # CONTRACTING PATH # Input layer inputs = Input(shape=img_shape) padded = ZeroPadding2D(padding=(100, 100), name='pad100')(inputs) # Block 1 conv1_1 = Conv2D(64, (3, 3), kernel_initializer=init, activation='relu', padding='valid', name='conv1_1', kernel_regularizer=l2(l2_reg))(padded) conv1_2 = Conv2D(64, (3, 3), kernel_initializer=init, activation='relu', padding='same', name='conv1_2', kernel_regularizer=l2(l2_reg))(conv1_1) pool1 = MaxPooling2D((2, 2), (2, 2), name='pool1')(conv1_2) # Block 2 conv2_1 = Conv2D(128, (3, 3), kernel_initializer=init, activation='relu', padding='same', name='conv2_1', kernel_regularizer=l2(l2_reg))(pool1) conv2_2 = Conv2D(128, (3, 3), kernel_initializer=init, activation='relu', padding='same', name='conv2_2', kernel_regularizer=l2(l2_reg))(conv2_1) pool2 = MaxPooling2D((2, 2), (2, 2), name='pool2')(conv2_2) # Block 3 conv3_1 = Conv2D(256, (3, 3), kernel_initializer=init, activation='relu', padding='same', name='conv3_1', kernel_regularizer=l2(l2_reg))(pool2) conv3_2 = Conv2D(256, (3, 3), kernel_initializer=init, activation='relu', padding='same', name='conv3_2', kernel_regularizer=l2(l2_reg))(conv3_1) conv3_3 = Conv2D(256, (3, 3), kernel_initializer=init, activation='relu', padding='same', name='conv3_3', kernel_regularizer=l2(l2_reg))(conv3_2) pool3 = MaxPooling2D((2, 2), (2, 2), name='pool3')(conv3_3) # Block 4 conv4_1 = Conv2D(512, (3, 3), kernel_initializer=init, activation='relu', padding='same', name='conv4_1', kernel_regularizer=l2(l2_reg))(pool3) conv4_2 = Conv2D(512, (3, 3), kernel_initializer=init, activation='relu', padding='same', name='conv4_2', kernel_regularizer=l2(l2_reg))(conv4_1) conv4_3 = Conv2D(512, (3, 3), kernel_initializer=init, activation='relu', padding='same', name='conv4_3', kernel_regularizer=l2(l2_reg))(conv4_2) pool4 = MaxPooling2D((2, 2), (2, 2), name='pool4')(conv4_3) # Block 5 conv5_1 = Conv2D(512, (3, 3), kernel_initializer=init, activation='relu', padding='same', name='conv5_1', kernel_regularizer=l2(l2_reg))(pool4) conv5_2 = Conv2D(512, (3, 3), kernel_initializer=init, activation='relu', padding='same', name='conv5_2', kernel_regularizer=l2(l2_reg))(conv5_1) conv5_3 = Conv2D(512, (3, 3), kernel_initializer=init, activation='relu', padding='same', name='conv5_3', kernel_regularizer=l2(l2_reg))(conv5_2) pool5 = MaxPooling2D((2, 2), (2, 2), name='pool5')(conv5_3) # Block 6 (fully conv) fc6 = Conv2D(4096, (7, 7), kernel_initializer=init, activation='relu', padding='valid', name='fc6', kernel_regularizer=l2(l2_reg))(pool5) fc6 = Dropout(0.5)(fc6) # Block 7 (fully conv) fc7 = Conv2D(4096, (1, 1), kernel_initializer=init, activation='relu', padding='valid', name='fc7', kernel_regularizer=l2(l2_reg), )(fc6) fc7 = Dropout(0.5)(fc7) score_fr = Conv2D(nclasses, (1, 1), kernel_initializer=init, activation='relu', padding='valid', name='score_fr')(fc7) # DECONTRACTING PATH # Unpool 1 score_pool4 = Conv2D(nclasses, (1, 1), kernel_initializer=init, activation='relu', padding='same', name='score_pool4', kernel_regularizer=l2(l2_reg))(pool4) score2 = Conv2DTranspose(nclasses, (4, 4), kernel_initializer=init, activation='linear', padding='valid', strides=(2, 2), name='score2', kernel_regularizer=l2(l2_reg))(score_fr) # crop size # in_shape = score2.shape.as_list() # out_shape = score_pool4.shape.as_list() # crop_shape = out_shape - in_shape score_pool4_crop = Cropping2D(cropping=(5, 5), name='score_pool4_crop')(score_pool4) #score_fused = merge([score_pool4_crop, score2], mode=custom_sum, output_shape=custom_sum_shape, name='score_fused') score_fused = Add(name='score_fused')([score_pool4_crop, score2]) # Unpool 2 score_pool3 = Conv2D(nclasses, (1, 1), kernel_initializer=init, activation='relu', padding='valid', name='score_pool3', kernel_regularizer=l2(l2_reg))(pool3) score4 = Conv2DTranspose(nclasses, (4, 4), kernel_initializer=init, activation='linear', padding='valid', strides=(2, 2), use_bias=True, # TODO: No bias?? name='score4', kernel_regularizer=l2(l2_reg))(score_fused) # in_shape = score4.output_shape # out_shape = score_pool3.output_shape # crop_shape = out_shape - in_shape score_pool3_crop = Cropping2D(cropping=(9, 9), name='score_pool3_crop')(score_pool3) #score_final = merge([score_pool3_crop, score4], mode=custom_sum, output_shape=custom_sum_shape, name='score_final') score_final = Add(name='score_final')([score_pool3_crop, score4]) upsample = Conv2DTranspose(nclasses, (16, 16), kernel_initializer=init, activation='linear', padding='valid', strides=(8, 8), use_bias=False, # TODO: No bias?? name='upsample', kernel_regularizer=l2(l2_reg))(score_final) # in_shape = inputs.output_shape # out_shape = upsample.output_shape crop_shape = (28,28) #out_shape - in_shape score = Cropping2D(cropping=(crop_shape[0], crop_shape[1]), name='score')(upsample) # Softmax # softmax_fcn8 = Activation('softmax')(score) softmax_fcn8 = Conv2D(nclasses, (1, 1), kernel_initializer=init, activation='softmax', padding='same', strides=(1, 1), use_bias=False, name='softmax_fcn8', kernel_regularizer=l2(l2_reg))(score) #NdSoftmax()(score) # Complete model model = Model(inputs=inputs, outputs=softmax_fcn8) try: import pydot except: print('unable to plot model. Install pydot to plot model.') else: from keras.utils import plot_model #plot_model(model, to_file='model.png', show_shapes=True) # Load pretrained Model if path_weights: load_matcovnet(model, n_classes=nclasses) # Freeze some layers if freeze_layers_from is not None: freeze_layers(model, freeze_layers_from) return model
def pilotnet(): """ the pilot net from nvidia team https://devblogs.nvidia.com/parallelforall/deep-learning-self-driving-cars/ """ dropout_rate = 0.05 model = Sequential() # corp non-necessary part of the image model.add( Cropping2D(cropping=((74, 20), (0, 0)), input_shape=(160, 320, 3))) # normalize the image model.add(Lambda(lambda x: (x / 255.0) - 0.5)) # convolution 5x5: 3 x 66 x 320 -> 24 x 31 x 158 model.add( Convolution2D(filters=24, kernel_size=(5, 5), strides=(2, 2), padding='valid', activation='relu')) model.add(Dropout(dropout_rate)) # convolution 5x5: 24 x 31 x 158 -> 36 x 14 x 77 model.add( Convolution2D(filters=36, kernel_size=(5, 5), strides=(2, 2), padding='valid', activation='relu')) model.add(Dropout(dropout_rate)) # convolution 5x5: 36 x 14 x 77 -> 48 x 5 x 37 model.add( Convolution2D(filters=48, kernel_size=(5, 5), strides=(2, 2), padding='valid', activation='relu')) model.add(Dropout(dropout_rate)) # convolution 3x3: 48 x 5 x 37 -> 64 x 3 x 35 model.add( Convolution2D(filters=64, kernel_size=(3, 3), strides=(1, 1), padding='valid', activation='relu')) model.add(Dropout(dropout_rate)) # convolution 3x3: 64 x 3 x 35 -> 64 x 1 x 33 model.add( Convolution2D(filters=64, kernel_size=(3, 3), strides=(1, 1), padding='valid', activation='relu')) model.add(Dropout(dropout_rate)) # a flatten layer model.add(Flatten()) # a fully connected layer with 1164 outputs model.add(Dense(1164, activation='relu')) model.add(Dropout(dropout_rate)) # a fully connected layer with 100 outputs model.add(Dense(100, activation='relu')) model.add(Dropout(dropout_rate)) # a fully connected layer with 50 outputs model.add(Dense(50, activation='relu')) model.add(Dropout(dropout_rate)) # a fully connected layer with 10 ouputs model.add(Dense(10, activation='relu')) model.add(Dropout(dropout_rate)) # output layer model.add(Dense(1)) model.compile(loss='mse', optimizer='adam') return model
augmented_measurements.append(measurement) flipped_image = cv2.flip(image, 1) flipped_measurement = measurement * -1.0 augmented_images.append(flipped_image) augmented_measurements.append(flipped_measurement) X_train = np.asarray(augmented_images) y_train = np.asarray(augmented_measurements) # Use Lenet architecture for the Model model = Sequential() # images are normalized model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160, 320, 3))) # Top 70 pixels includes the sky and bottom 20 pixels includes hood of car # Hence they are cropped from each image model.add(Cropping2D(cropping=((70, 20), (0, 0)))) model.add(Convolution2D(6, 5, 5, activation='relu')) model.add(MaxPooling2D()) model.add(Convolution2D(16, 5, 5, activation='relu')) model.add(MaxPooling2D()) model.add(Flatten()) model.add(Dense(120)) model.add(Dense(84)) # Dropout layer added with keep_prob = 0.5 to reduce overfitting model.add(Dropout(0.5)) model.add(Dense(1)) model.compile(optimizer="Adam", loss="mse") # 10 epochs was found to be appropriate model.fit(X_train, y_train, epochs=10, validation_split=0.2, shuffle=True)
#I have moved the data folder under the opt folder # with open('../../opt/data/driving-log.csv', 'r') as f: with open('./data/driving_log.csv', 'r') as f: reader = csv.reader(f) for line in reader: lines.append(line) train_samples, validation_samples = train_test_split(lines, test_size=0.2) print(len(train_samples)) print(len(validation_samples)) model = Sequential() # normalize the images model.add(Lambda(lambda x: (x / 255.0) - 0.5, input_shape=(160, 320, 3), name = 'Normalization')) # crop the iamge model.add(Cropping2D(cropping=((50, 20), (0, 0)), input_shape=(160, 320, 3), name = 'Cropping')) # model architecture # convolution model.add(Conv2D(24, 5, strides=(2, 2), activation="elu", name ='Conv1')) #output = 158x43x24 model.add(Conv2D(36, 5, strides=(2, 2), activation="elu", name = 'Conv2' )) #output = 77x22x36 model.add(Conv2D(48, 5, strides=(2, 2), activation="elu", name = 'Conv3')) #output = 37x7x64 model.add(Conv2D(64, 3, activation="elu", name = 'Conv4' )) #output = 35x7x64 model.add(Conv2D(64, 3, activation="elu", name = 'Conv5')) #output = 32x5x64 model.add(Flatten(name ='Flat1')) #output = 10240 model.add(Dropout(0.5, name = 'Dropout1')) # fully connection model.add(Dense(100, name = 'FullyCon1')) #output = 100 model.add(Dropout(0.5, name = 'Dropout2')) model.add(Dense(50, name = 'FullyCon2')) #output = 50 model.add(Dense(10, name = 'FullyCon3')) #output = 10 model.add(Dense(1, name = 'Output')) #output = 1
activation='relu', use_bias=True)(p4) # Layer 26 ''' Cropping is needed, but accessing the deconvolution shape isn't possible, via xc.get_shape(), which outputs (None, None, ...) due to an outstanding Keras bug. A cludge to find this shape: Try to add, Add()([x, xc]). Since these shapes don't match, the resulting error message reveals their shapes, in this case, (11, 20, 4) and (24, 42, 4), respectively. p4.get_shape() outputs (35, 52), hence this output from Pool4 needs to be cropped before it can be added to xc which has shape (24, 42). ''' TL = ((35 - 24) / 2, (52 - 42) / 2) BR = (35 - TL[0] - 24, 52 - TL[1] - 42) p4c = Cropping2D(((TL[0], BR[0]), (TL[1], BR[1])))(p4) # from block4_pool # Now the shape is correct: p4c.get_shape() ---> (24, 42, 4) # Layer 27 x = Add()([xc, p4c]) # this has shape (24, 42, 4) # Layer 28 xc = Conv2DTranspose(filters=nb_classes, kernel_size=32, strides=16, padding='valid', activation=None, use_bias=False)(x) # NOTE: xc.get_shape() = (400, 688, 4), if that command worked, # as revealed by Add()[x, xc]
from keras.layers.convolutional import Conv2D, Cropping2D from keras.layers.pooling import MaxPooling2D # Common neural network variables batch_size = 12 row, col, ch = 160, 320, 3 # original image format # Retrieve the generator train_samples, validation_samples = train_test_split(samples, test_size = 0.2) train_generator = generator(train_samples, batch_size=batch_size) validation_generator = generator(validation_samples, batch_size=batch_size) # Build the neural network model = Sequential() model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(row, col, ch))) # (160, 320, 3) model.add(Cropping2D(cropping=((50, 20),(0, 0)))) # (90, 320, 3) model.add(Conv2D(filters=3, kernel_size=(5, 5), strides=(2,2), activation='relu', padding='valid')) # (43, 158, 3) #model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(filters=24, kernel_size=(5, 5), strides=(2,2), activation='relu', padding='valid')) # (20, 77, 24) #model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(filters=36, kernel_size=(5, 5), strides=(2,2), activation='relu', padding='valid')) #(8, 37, 36) #model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(filters=48, kernel_size=(3, 3), activation='relu', padding='valid')) # (6, 35, 48) #model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='valid')) # (4, 33, 64) #model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) # (1, 8448) model.add(Dense(units=1164, activation='relu')) # (1164) #model.add(Dropout(0.5)) model.add(Dense(units=100, activation='relu')) # (100) #model.add(Dropout(0.5))
def train_model(model_chosen='Nvidia'): model = Sequential() #img_sample = train_generator[0:1,:,:,:] # crop the images inside Keras and get one sample image model.add(Cropping2D(cropping=((70, 0), (0, 0)), input_shape=(160, 320, 3))) #cropped_img_sample = model.predict(img_sample, batch_size=128) # resize the images inside Keras and get one sample image # Keras 1.2 has some issues when using the syntax lambda x: function_name(x). # model.add(Lambda(lambda x: resize_function(x))) model.add(Lambda(resize_img)) #resized_img_sample = model.predict(img_sample, batch_size=128) # normolize the features inside Keras and get one sample image model.add(Lambda(lambda x: x / 255.0 - 0.5)) #normalized_img_sample = model.predict(img_sample, batch_size=128) """ f, ax = plt.subplots(4, sharex=True, figsize=(7, 10)) ax[0].imshow(img_sample[0]) ax[0].set_title('Sample Image') ax[1].imshow(cropped_img_sample[0].astype(np.uint8)) ax[1].set_title('Cropped Image') ax[2].imshow(resized_img_sample[0].astype(np.uint8)) ax[2].set_title('Resized Image') ax[3].imshow(normalized_img_sample[0,:,:,0], cmap = 'gray') ax[3].set_title('Normalized image') plt.savefig('Preprocessing_sample_image_generator.png') plt.show() """ if model_chosen == 'Lenet': # Lenet model model.add(Convolution2D(6, 5, 5, activation='relu')) model.add(MaxPooling2D()) model.add(Convolution2D(16, 5, 5, activation='relu')) model.add(MaxPooling2D()) model.add(Flatten()) model.add(Dense(120)) model.add(Dense(84)) model.add(Dense(1)) elif model_chosen == 'Nvidia': # Nvidia model model.add(Convolution2D(24, 5, 5, subsample=(2, 2), activation='relu')) model.add(Convolution2D(36, 5, 5, subsample=(2, 2), activation='relu')) model.add(Convolution2D(48, 5, 5, subsample=(2, 2), activation='relu')) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(Flatten()) model.add(Dense(100)) model.add(Dropout(.5)) model.add(Activation('relu')) model.add(Dense(50)) model.add(Dropout(.5)) model.add(Activation('relu')) model.add(Dense(10)) model.add(Activation('relu')) model.add(Dense(1)) # Print a summary of a Keras model details # such as the number of layers and size of each layer model.summary() # Initialize the optimizer with a learning rate = 0.0001 and complie it optimizer = Adam(lr=0.0001) model.compile(optimizer=optimizer, loss='mse') # save the model from the best epoch checkpoint = ModelCheckpoint(filepath='model.h5', verbose=1, save_best_only=True, monitor='val_loss') # stop the training after the model stops improving over a specified delta callback = EarlyStopping(monitor='val_loss', patience=2, verbose=1) # Train the model history_object = model.fit_generator(train_generator, samples_per_epoch= len(train_samples),\ validation_data=validation_generator, nb_val_samples=len(validation_samples),\ nb_epoch = 50, callbacks = [checkpoint, callback]) """ Visulize the model, plot model into a graph Install extra packages below: pip install graphviz pip install pydot pip install pydot-ng (In case :module 'pydot' has no attribute 'find_graphviz') """ plot(model, to_file="model_generator.png") """ Visulize loss """ ### print the keys contained in the history object print(history_object.history.keys()) ### plot the training and validation loss for each epoch plt.plot(history_object.history['loss']) plt.plot(history_object.history['val_loss']) plt.title('model mean squared error loss') plt.ylabel('mean squared error loss') plt.xlabel('epoch') plt.legend(['training set', 'validation set'], loc='upper right') plt.savefig('loss_visulization_generator.png') plt.show() return model
def build_nvidia_model(input_shape): model = Sequential() # --- Crop image to save only the region of interest model.add(Cropping2D(cropping=((65, 25), (0, 0)), input_shape=input_shape)) # --- Normalize and mean center the data model.add(Lambda(normalize_image)) # --- Layer 1 : Convolution + ReLu activation + maxpooling model.add( Conv2D(filters=24, kernel_size=(5, 5), strides=(1, 1), kernel_regularizer=regularizers.l2(0.001))) model.add(ELU()) model.add(MaxPooling2D(pool_size=(2, 2))) # --- Layer 2 : Convolution + ReLu activation + maxpooling model.add( Conv2D(filters=36, kernel_size=(5, 5), strides=(1, 1), kernel_regularizer=regularizers.l2(0.001))) model.add(ELU()) model.add(MaxPooling2D(pool_size=(2, 2))) # --- Layer 3 : Convolution + ReLu activation + maxpooling model.add( Conv2D(filters=48, kernel_size=(5, 5), strides=(1, 1), kernel_regularizer=regularizers.l2(0.001))) model.add(ELU()) model.add(MaxPooling2D(pool_size=(2, 2))) # --- Layer 4 : Convolution + ReLu activation model.add( Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), kernel_regularizer=regularizers.l2(0.001))) model.add(ELU()) # --- Layer 5 : Convolution + ReLu activation model.add( Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), kernel_regularizer=regularizers.l2(0.001))) model.add(ELU()) # --- Flatten the weights model.add(Flatten()) # --- Layer 6 : Fully-connected + ReLu activation model.add(Dense(100, kernel_regularizer=regularizers.l2(0.001))) model.add(ELU()) # --- Layer 7 : Fully-connected + ReLu activation model.add(Dense(50, kernel_regularizer=regularizers.l2(0.001))) model.add(ELU()) # --- Layer 8 : Fully-connected + ReLu activation model.add(Dense(10, kernel_regularizer=regularizers.l2(0.001))) model.add(ELU()) # --- Layer 9 : Fully-connected model.add(Dense(1)) return model
def create_model(wt_reg=0.01, do_ratio=0.2) -> Model: ''' Creates Network model similar or same as NVidia's end2end learning model. http://images.nvidia.com/content/tegra/automotive/images/2016/solutions/pdf/end-to-end-dl-using-px.pd Tuned the model mostly using carnd-forums feedbacks. https://carnd-forums.udacity.com/questions/38548107/p3-exhausted-a-lot-of-methods-and-my-model-is-still-not-performing-well Removed 2 convolution layers from original NVIDIA model and added MaxPool layers. Used ELU activations instead Tanh or ReLU. :return: Model ''' model = Sequential() # Pre-processing # Normalize pixel through Lambda layer. pixel range from -0.5 to +0.5 model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160, 320, 3))) # Crop the image as mentioned the course video by David Silver. top 75 and bottom 25 rows of pixel model.add(Cropping2D(cropping=((75, 25), (0, 0)))) # Convolution layers model.add( Convolution2D(16, 3, 3, subsample=(1, 1), W_regularizer=l2(wt_reg))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(ELU()) model.add(Dropout(do_ratio)) model.add( Convolution2D(32, 3, 3, subsample=(1, 1), W_regularizer=l2(wt_reg))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(ELU()) model.add(Dropout(do_ratio)) model.add( Convolution2D(64, 3, 3, subsample=(2, 2), W_regularizer=l2(wt_reg))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(ELU()) model.add(Dropout(do_ratio)) # Remove below 2 convolution layers # model.add(Convolution2D(64, 3, 3, W_regularizer=l2(wt_reg))) # model.add(MaxPooling2D(pool_size=(2, 2))) # model.add(ELU()) # model.add(Dropout(do_ratio)) # Cropping top 75 rows instead 70 causing this layer to become -ve. # model.add(Convolution2D(64, 3, 3, W_regularizer=l2(wt_reg))) # model.add(ELU()) # model.add(Dropout(do_ratio)) model.add(Flatten()) model.add(Dense(100)) model.add(ELU()) model.add(Dropout(do_ratio)) model.add(Dense(50)) model.add(ELU()) model.add(Dropout(do_ratio)) model.add(Dense(10)) model.add(ELU()) model.add(Dropout(do_ratio)) model.add(Dense(1, activation='linear')) return model
# compile and train the model using the generator function train_generator = generator(train_samples, batch_size=32, mode=0) validation_generator = generator(validation_samples, batch_size=32, mode=1) row, col, ch = 160, 320, 3 # image format # Preprocess incoming data, centered around zero with small standard deviation inputs = Input(shape=(row, col, ch)) x = Lambda(lambda x: x/255.0 - 0.5, input_shape=(row, col, ch), output_shape=(row, col, ch))(inputs) #(160,320,3) ---> (90,320,3) x = Cropping2D(cropping=((50,20), (0,0)), input_shape=(row, col, ch))(x) #(90,320,3) ---> (84,314,16) x = Conv2D(16, 7, 7, activation='relu', init='he_normal')(x) #(84,314,16) ---> (42,157,16) x = BatchNormalization()(x) x = MaxPooling2D(pool_size=(2, 2))(x) #(42,157,16) ---> (36,151,16) x = BatchNormalization()(x) x = Conv2D(16, 7, 7, activation='relu', init='he_normal')(x) #(36,151,6) ---> (18,75,16) x = BatchNormalization()(x) x = MaxPooling2D(pool_size=(2, 2))(x)
def create_convnet_nvidia(): ''' Create a convnet using the network architecture documented in NVIDIA's paper ''' # Create the model pipeline, including image preprocessing (avoids having to change drive.py) model = Sequential([ # Crop the area above the horizon, resize and normalize the image Cropping2D(cropping=((22, 0), (0, 0)), input_shape=(160, 320, 3)), Lambda(resize), Lambda(normalize), # Conv1 Convolution2D(24, 5, 5, border_mode='valid', activation='elu', subsample=(2, 2), init="he_normal"), SpatialDropout2D(0.2), # Conv2 Convolution2D(36, 5, 5, border_mode='valid', activation='elu', subsample=(2, 2), init="he_normal"), SpatialDropout2D(0.2), # Conv3 Convolution2D(48, 5, 5, border_mode='valid', activation='elu', subsample=(2, 2), init="he_normal"), SpatialDropout2D(0.2), # Conv4 Convolution2D(64, 3, 3, border_mode='valid', activation='elu', init="he_normal"), SpatialDropout2D(0.2), # Conv5 Convolution2D(64, 3, 3, border_mode='valid', activation='elu', init="he_normal"), SpatialDropout2D(0.2), # FC1 Flatten(), Dense(100, activation='elu', init="he_normal"), Dropout(0.5), # FC2 Dense(50, activation='elu', init="he_normal"), # FC3 Dense(10, activation='elu', init="he_normal"), Dropout(0.5), # Final layer Dense(1) ]) model.summary() model.compile(optimizer=Adam(lr=INITIAL_LR), loss="mse") return model
def image_transform_net(img_width, img_height, tv_weight=1): x = Input(shape=(img_width, img_height, 3)) a = ZeroPadding2D(padding=(40, 40))(x) a = Conv2D(32, (9, 9), strides=(1, 1), padding='same')(a) a = BatchNormalization()(a) a = Activation("relu")(a) a = Conv2D(64, (9, 9), strides=(2, 2), padding='same')(a) a = BatchNormalization()(a) a = Activation("relu")(a) a = Conv2D(128, (3, 3), strides=(2, 2), padding='same')(a) a = BatchNormalization()(a) a = Activation("relu")(a) for i in range(5): nb_filter = 128 (nb_row, nb_col) = (3, 3) stride = (1, 1) identity = Cropping2D(cropping=((2, 2), (2, 2)))(a) a = Conv2D(nb_filter, (nb_row, nb_col), strides=stride, padding='valid')(a) a = BatchNormalization()(a) a = Activation("relu")(a) a = Conv2D(nb_filter, (nb_row, nb_col), strides=stride, padding='valid')(a) y = BatchNormalization()(a) a = add([identity, y]) (nb_row, nb_col) = (3, 3) stride = (2, 2) nb_filter = 64 activation = "relu" a = UpSampling2D(size=stride)(a) #a = ZeroPadding2D(padding=(32,32))(a) a = ZeroPadding2D(padding=stride)(a) a = Conv2D(nb_filter, (nb_row, nb_col), padding='valid')(a) a = BatchNormalization()(a) a = Activation(activation)(a) nb_filter = 32 a = UpSampling2D(size=stride)(a) K.print_tensor(a) #a = ZeroPadding2D(padding=(65,65))(a) a = ZeroPadding2D(padding=stride)(a) a = Conv2D(nb_filter, (nb_row, nb_col), padding='valid')(a) a = BatchNormalization()(a) a = Activation(activation)(a) (nb_row, nb_col) = (9, 9) stride = (1, 1) nb_filter = 3 activation = "tanh" a = UpSampling2D(size=stride)(a) a = ZeroPadding2D(padding=stride)(a) a = Conv2D(nb_filter, (nb_row, nb_col), padding='valid')(a) a = BatchNormalization()(a) a = Activation(activation)(a) model = Model(inputs=x, outputs=a) return model
train_samples, validation_samples = train_test_split(lines, test_size=0.2) train_batch_size = int(round(len(train_samples) / BATCH_SIZE_DIV)) val_batch_size = int(round(len(validation_samples) / BATCH_SIZE_DIV)) train_generator = generator(train_samples, batch_size=train_batch_size) validation_generator = generator(validation_samples, batch_size=val_batch_size) ##### # Pixels of image were normalized (divide by 255) and mean centered (subtract 0.5). # This is done in tensorflow/Keras so that it is efficient on GPU, but also means # any image can be put into the model thus making it more flexible. ## model = Sequential() model.add(Lambda(lambda x: (x / 255.0) - 0.5, input_shape=(160, 320, 3))) model.add( Cropping2D(cropping=((TOP_CROP, BOTTOM_CROP), (LEFT_CROP, RIGHT_CROP)))) ##### # Can selecte LENET. I experimented wiht it but chose not to use it ## if LENET: model.add(Convolution2D(6, 5, 5, activation='relu')) model.add(MaxPooling2D()) model.add(Convolution2D(16, 5, 5, activation='relu')) model.add(MaxPooling2D()) model.add(Flatten()) model.add(Dense(120)) model.add(Dense(84)) model.add(Dense(1)) #####
X_train = np.array(images) y_train = np.array(angles) yield sklearn.utils.shuffle(X_train, y_train) # compile and train the model using the generator function train_generator = generator_multi_camera(train_samples, batch_size=32) validation_generator = generator_multi_camera(validation_samples, batch_size=32) ch, row, col = 3, 160, 320 # Trimmed image format model = Sequential() # Preprocess incoming data, centered around zero with small standard deviation model.add(Lambda(lambda x: x / 255 - 0.5, input_shape=(row, col, ch))) model.add(Cropping2D(cropping=((70, 20), (10, 10)))) # model from NVIDIA model.add(Convolution2D(16, 3, 3, activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(32, 3, 3, activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(400, activation='relu')) model.add(Dense(100, activation='relu')) model.add(Dense(20, activation='relu')) model.add(Dense(1))
def run(): driving_log = pd.read_csv(data_path, names=('Center Image', 'Left Image', 'Right Image', 'Steering Angle', 'Throttle', 'Break', 'Speed')) image_names_full = [] y_data_full = [] for index, row in driving_log.iterrows(): center_img = row['Center Image'] left_img = row['Left Image'].strip() right_img = row['Right Image'].strip() steering_angle = row['Steering Angle'] image_names_full.append(center_img) y_data_full.append(steering_angle) left = steering_angle + angle_correction right = steering_angle - angle_correction image_names_full.append(left_img) y_data_full.append(left) image_names_full.append(right_img) y_data_full.append(right) image_names_full, y_data_full = np.array(image_names_full), np.array( y_data_full) print('CSV loaded') #split data X_train, X_val, y_train, y_val = train_test_split(image_names_full, y_data_full, test_size=0.2) #model model = Sequential() model.add( Cropping2D(cropping=((60, 20), (0, 0)), input_shape=(160, 320, 3))) model.add(Lambda(resize)) model.add(BatchNormalization(axis=1)) model.add(Convolution2D(24, 5, 5, border_mode='same', activation='elu')) model.add(MaxPooling2D(border_mode='same')) model.add(SpatialDropout2D(0.2)) model.add(Convolution2D(36, 5, 5, border_mode='same', activation='elu')) model.add(MaxPooling2D(border_mode='same')) model.add(SpatialDropout2D(0.2)) model.add(Convolution2D(48, 5, 5, border_mode='same', activation='elu')) model.add(MaxPooling2D(border_mode='same')) model.add(SpatialDropout2D(0.2)) model.add(Convolution2D(64, 3, 3, border_mode='same', activation='elu')) model.add(MaxPooling2D(border_mode='same')) model.add(SpatialDropout2D(0.2)) model.add(Convolution2D(64, 3, 3, border_mode='same', activation='elu')) model.add(MaxPooling2D(border_mode='same')) model.add(SpatialDropout2D(0.2)) model.add(Flatten()) # Fully connected layers model.add(Dense(100, activation='elu', W_regularizer=l2(1e-6))) model.add(Dense(50, activation='elu', W_regularizer=l2(1e-6))) model.add(Dense(10, activation='elu', W_regularizer=l2(1e-6))) model.add(Dense(1)) #summary model.summary() #training print('Start training') model.compile(optimizer='adam', loss='mse') datagen = MyDataGenerator() history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=batch_size, shuffle=True, flip_prob=flip_prob), samples_per_epoch=len(y_train), nb_epoch=nb_epoch, validation_data=datagen.flow( X_val, y_val, batch_size=batch_size, shuffle=True), nb_val_samples=len(y_val)) #save model print('Save model') with open('model.json', 'w') as f: json.dump(model.to_json(), f) model.save_weights('model.h5')
def get_model(): Model = Sequential() Model.add( Cropping2D(cropping=((50, 20), (0, 0)), input_shape=(160, 320, 3))) Model.add(Lambda(lambda x: x / 127.5 - 1.0)) Model.add( Convolution2D(filters=24, kernel_size=(5, 5), strides=(2, 2), border_mode="same", use_bias=True, activation='relu', kernel_initializer='random_uniform', bias_initializer='zeros')) Model.add( Convolution2D(filters=36, kernel_size=(5, 5), strides=(2, 2), border_mode="same", use_bias=True, activation='relu', kernel_initializer='random_uniform', bias_initializer='zeros')) Model.add( Convolution2D(filters=48, kernel_size=(5, 5), strides=(2, 2), border_mode="same", use_bias=True, activation='relu', kernel_initializer='random_uniform', bias_initializer='zeros')) Model.add( Convolution2D(filters=64, kernel_size=(3, 3), strides=(1, 1), border_mode="same", use_bias=True, activation='relu', kernel_initializer='random_uniform', bias_initializer='zeros')) Model.add( Convolution2D(filters=64, kernel_size=(3, 3), strides=(1, 1), border_mode="same", use_bias=True, activation='relu', kernel_initializer='random_uniform', bias_initializer='zeros')) Model.add(Flatten()) Model.add( Dense(100, use_bias=True, activation='relu', kernel_initializer='random_uniform', bias_initializer='zeros')) Model.add(Dropout(.5)) Model.add( Dense(50, use_bias=True, activation='relu', kernel_initializer='random_uniform', bias_initializer='zeros')) Model.add(Dropout(.5)) Model.add( Dense(10, use_bias=True, activation='relu', kernel_initializer='random_uniform', bias_initializer='zeros')) Model.add(Dense(1)) Model.compile(loss='mse', optimizer='adam') return Model
strides=(1, 1), activation=None, name='Deconv4')(up_4) # 256 x 256 x 16 #Upsampling 5 up_5 = UpSampling2D(size=pool_size)(deconv4_1) # 512 x 512 x 16 #Deconv 5 deconv5_1 = Conv2DTranspose(1, (1, 1), padding='valid', strides=(1, 1), activation=None, name='Deconv5')(up_5) # 512 x 512 x 1 #Outputs outputs = Cropping2D(cropping=((6, 6), (6, 6)), input_shape=(512, 512, 1))(deconv5_1) # 500 x 500 x 1 ### End of network ### model = Model(inputs=[inputs_img, inputs_cnt, inputs_loc], outputs=outputs) # Compiling and training the model # model.load_weights('') # Compiling and training the model model.compile(optimizer='Adam', loss='mean_squared_error') # TODO:edit inputs, img_train, cnt_train, loc_train # check syntax weight_dir = "sfcn_loc_results/save_weights/" model_dir = "sfcn_loc_results/save_models/" weight_path = weight_dir + 'sfcn_loc_weight_{epoch:02d}_{loss:.4f}_{val_loss:.4f}.h5'
from keras.models import Sequential from keras.layers import Flatten, Dense, Lambda from keras.layers.convolutional import Convolution2D, Cropping2D from keras.layers.pooling import MaxPooling2D model = Sequential() # Implementing the NVIDIA architecture below, # https://devblogs.nvidia.com/parallelforall/deep-learning-self-driving-cars/ # Preprocess incoming data, centered around zero with small standard deviation model.add( Lambda(lambda x: x / 255.0 - 0.5, input_shape=(row, col, ch), output_shape=(row, col, ch))) # lambda for normalization model.add(Cropping2D( cropping=((70, 25), (0, 0)))) #how much to crop row(i.e top,bottom),col(i.e left,right) model.add(Convolution2D(24, 5, 5, subsample=(2, 2), activation='relu')) model.add(Convolution2D(36, 5, 5, subsample=(2, 2), activation='relu')) model.add(Convolution2D(48, 5, 5, subsample=(2, 2), activation='relu')) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(Flatten()) model.add(Dense(100)) model.add(Dense(50)) model.add(Dense(10))
def getModel(): convDropoutProb = 0.5 fcnDropoutProb = 0.5 model = Sequential() model.add(Lambda(lambda x: (x / 255.0) - 0.5, input_shape=(160, 320, 3))) model.add( Cropping2D(cropping=((70, 25), (0, 0)), input_shape=(160, 320, 3), dim_ordering='tf')) model.add( Convolution2D(24, 5, 5, subsample=(2, 2), init='normal', border_mode='valid', dim_ordering='tf', activation='relu')) model.add(Dropout(convDropoutProb)) model.add( Convolution2D(36, 5, 5, subsample=(2, 2), init='normal', border_mode='valid', dim_ordering='tf', activation='relu')) model.add(Dropout(convDropoutProb)) model.add( Convolution2D(48, 5, 5, subsample=(2, 2), init='normal', border_mode='valid', dim_ordering='tf', activation='relu')) model.add(Dropout(convDropoutProb)) model.add( Convolution2D(64, 3, 3, subsample=(1, 1), init='normal', activation='relu')) model.add( Convolution2D(64, 3, 3, subsample=(1, 1), init='normal', activation='relu')) model.add(Flatten()) model.add(Dropout(fcnDropoutProb)) model.add(Dense(1164, init='normal', activation='relu')) model.add(Dropout(fcnDropoutProb)) model.add(Dense(100, init='normal', activation='relu')) model.add(Dropout(fcnDropoutProb)) model.add(Dense(50, init='normal', activation='relu')) model.add(Dropout(fcnDropoutProb)) model.add(Dense(10, init='normal', activation='relu')) model.add(Dropout(fcnDropoutProb)) model.add(Dense(1)) return model
y_train_data = '../data/P3/images_data/y_train.pickle' with open(y_train_data, 'rb') as dump_file: y_train = pickle.load(dump_file) # compile and train the model using the generator function # train_generator = generator(train_samples, batch_size=32) # validation_generator = generator(validation_samples, batch_size=32) from keras.layers import Dense, Flatten, Lambda from keras.models import Sequential from keras.layers.convolutional import Convolution2D, MaxPooling2D, Cropping2D, Conv2D model = Sequential() model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160, 320, 3))) model.add(Cropping2D(cropping=((75, 25), (0, 0)), input_shape=(3, 160, 320))) model.add(Conv2D(24, (5, 5), strides=(2, 2), activation="relu")) model.add(Conv2D(36, (5, 5), strides=(2, 2), activation="relu")) model.add(Conv2D(48, (5, 5), strides=(2, 2), activation="relu")) model.add(Conv2D(64, (3, 3), activation="relu")) model.add(Conv2D(64, (2, 2), activation="relu")) model.add(Flatten()) model.add(Dense(100)) model.add(Dense(50)) model.add(Dense(10)) model.add(Dense(1)) model.compile(loss='mse', optimizer='adam') # history_object = model.fit_generator(train_generator, steps_per_epoch= len(train_samples)*6, # validation_data=validation_generator, validation_steps=len(validation_samples)*6, epochs=1, verbose = 1)
def __init__(self): # size of noise to feed into the generator self.random_dim = 100 # optimizers for generator and discriminator self.adam_g = Adam(lr=0.0002, beta_1=0.5, beta_2=0.999) self.adam_d = Adam(lr=0.0002, beta_1=0.5, beta_2=0.999) self.n_critic = 5 self.clip_value = 0.01 #---------------- Create Generator ----------------# inputs = Input(shape=(self.random_dim, )) # number of resblocks to include resblocks = 0 x = Dense( 1024 * 4 * 4, input_dim=self.random_dim, kernel_initializer=initializers.RandomNormal(stddev=0.02))(inputs) x = LeakyReLU(0.2)(x) x = Reshape((1024, 4, 4))(x) x = UpSampling2D(size=(2, 2))(x) x = Conv2D(filters=512, kernel_size=(5, 5), padding='same')(x) x = BatchNormalization()(x) x = Activation('relu')(x) #Apply num ResNet blocks, for i in range(resblocks): x = res_block(x, 512, use_dropout=True) x = UpSampling2D(size=(2, 2))(x) x = Conv2D(filters=256, kernel_size=(5, 5), strides=1, padding='same')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = UpSampling2D(size=(2, 2))(x) x = Conv2D(filters=128, kernel_size=(5, 5), strides=1, padding='same')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = UpSampling2D(size=(2, 2))(x) x = Conv2D(filters=64, kernel_size=(5, 5), strides=1, padding='same')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = UpSampling2D(size=(2, 2))(x) x = Conv2D(filters=32, kernel_size=(5, 5), strides=1, padding='same')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = UpSampling2D(size=(2, 2))(x) x = Conv2D(filters=1, kernel_size=(5, 5), padding='same')(x) x = Cropping2D(((53, 0), (53, 0)))(x) x = Activation('sigmoid')(x) # Add direct connection from input to output to improve training # outputs = Add()([x, inputs]) # outputs = Lambda(lambda z: z/2)(outputs) self.generator = Model(inputs=inputs, outputs=x) self.generator.compile(loss=self.wasserstein_loss, optimizer=self.adam_g) #---------------- Create Discriminator ----------------# # change this for dif shapes inputs = Input(shape=(1, 203, 203)) x = Conv2D(64, kernel_size=(5, 5), strides=2, padding="same", input_shape=(1, 203, 203))(inputs) x = LeakyReLU(alpha=0.2)(x) x = Conv2D(128, kernel_size=(5, 5), strides=2, padding="same")(x) x = LeakyReLU(alpha=0.2)(x) x = Dropout(0.25)(x) x = Conv2D(256, kernel_size=(5, 5), strides=2, padding="same")(x) x = LeakyReLU(alpha=0.2)(x) x = Dropout(0.25)(x) x = Conv2D(512, kernel_size=(5, 5), strides=2, padding="same")(x) x = LeakyReLU(alpha=0.2)(x) x = Dropout(0.25)(x) x = Flatten()(x) x = Dense(1, activation='sigmoid')(x) self.discriminator = Model(inputs=inputs, outputs=x) self.discriminator.compile(loss=self.wasserstein_loss, optimizer=self.adam_d, metrics=['accuracy']) #---------------- Create Stacked Model ----------------# self.discriminator.trainable = False gan_in = Input(shape=(self.random_dim, )) x = self.generator(gan_in) gan_out = self.discriminator(x) self.stacked = Model(inputs=gan_in, outputs=gan_out) self.stacked.compile(loss=self.wasserstein_loss, optimizer=self.adam_g)
def decoder(): ''' Decoder model based on Inverse VGG16 Network. The fully connected layers are removed from the top. Input:(None,7,7,512) Elu is used as activation function instead of Relu. ''' model=Sequential() model.add(UpSampling2D(size=(2, 2),input_shape=(7,7,512))) model.add(Conv2DTranspose(512, (3, 3), activation="elu")) model.add(Cropping2D((1,1))) model.add(Conv2DTranspose(512, (3, 3), activation="elu")) model.add(Cropping2D((1,1))) model.add(Conv2DTranspose(512, (3, 3), activation="elu")) model.add(Cropping2D((1,1))) model.add(UpSampling2D(size=(2, 2))) model.add(Conv2DTranspose(512, (3, 3), activation="elu")) model.add(Cropping2D((1,1))) model.add(Conv2DTranspose(512, (3, 3), activation="elu")) model.add(Cropping2D((1,1))) model.add(Conv2DTranspose(512, (3, 3), activation="elu")) model.add(Cropping2D((1,1))) model.add(UpSampling2D(size=(2, 2))) model.add(Conv2DTranspose(256, (3, 3), activation="elu")) model.add(Cropping2D((1,1))) model.add(Conv2DTranspose(256, (3, 3), activation="elu")) model.add(Cropping2D((1,1))) model.add(Conv2DTranspose(256, (3, 3), activation="elu")) model.add(Cropping2D((1,1))) model.add(UpSampling2D(size=(2, 2))) model.add(Conv2DTranspose(128, (3, 3), activation="elu")) model.add(Cropping2D((1,1))) model.add(Conv2DTranspose(128, (3, 3), activation="elu")) model.add(Cropping2D((1,1))) model.add(UpSampling2D(size=(2, 2))) model.add(Conv2DTranspose(64, (3, 3), activation="elu")) model.add(Cropping2D((1,1))) model.add(Conv2DTranspose(3, (3, 3), activation="elu")) model.add(Cropping2D((1,1))) return model
y.append(steering) X = np.array(X) y = np.array(y) X, y = shuffle(X, y) yield (X, y) model_name = "model" if args.model: model_name = args.model # model definition - NVidia architecture model = Sequential() # cropping model.add( Cropping2D(cropping=(cropping_tb, cropping_lr), input_shape=image_size)) # normalizing image ( to [-0.5, 0.5] ) model.add(Lambda(lambda x: (x / 255.0) - 0.5)) model.add(Conv2D(24, (5, 5), strides=(2, 2), activation='relu')) model.add(Conv2D(36, (5, 5), strides=(2, 2), activation='relu')) model.add(Conv2D(48, (5, 5), strides=(2, 2), activation='relu')) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(Flatten()) model.add(Dense(100, activation='relu')) model.add(Dense(50, activation='relu')) model.add(Dense(10)) model.add(Dense(1)) model.compile(loss='mse', optimizer='adam') steps_train = int(math.ceil(num_train_samples / batch_size))
output_steerings += 10 * [center_steering] #images += 1 * [np.fliplr(imageC)] #output_steerings += 1 * [-center_steering] X_train = np.array(images) y_train = np.array(output_steerings) from keras.models import Sequential from keras.layers import Flatten, Dense, Lambda, Activation, Dropout, ELU, Reshape from keras.regularizers import l2 from keras.layers.convolutional import Convolution2D, Cropping2D, MaxPooling2D, AveragePooling2D from keras.optimizers import Adam from keras.preprocessing.image import ImageDataGenerator model = Sequential() model.add(Cropping2D(cropping=((60,25), (0,0)),dim_ordering='tf', input_shape=(160,320,3))) model.add(Lambda(lambda x: x /255.0 - 0.5)) model.add(AveragePooling2D(pool_size=(2,2))) model.add(Convolution2D(24, 2, 2, subsample=(2, 2))) model.add(Activation('relu')) model.add(Convolution2D(36, 2, 2, subsample=(2, 2))) model.add(Activation('relu')) model.add(Dropout(0.7)) model.add(Convolution2D(48, 2, 2, subsample=(2, 2))) model.add(Activation('relu')) model.add(Convolution2D(64, 1, 1, subsample=(1, 1))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Flatten()) model.add(Dense(48)) model.add(Activation('relu'))
def deconv_layers(model): model.add(Deconvolution2D(512, 7, 7, output_shape=(None, 512, 7, 7), subsample=(1, 1), border_mode='valid', activation="relu")) model.add(UpSampling2D((2,2))) #unpool_5 for _ in range(3): #deconv_5 model.add(Deconvolution2D(512, 3, 3, output_shape=(None, 512, 16, 16), subsample=(1, 1), border_mode='valid', activation="relu")) model.add(Cropping2D(cropping=((1, 1), (1, 1)))) # 512*14*14 model.add(UpSampling2D((2,2))) #unpool_4 for _ in range(2): #deconv_4 model.add(Deconvolution2D(512, 3, 3, output_shape=(None, 512, 30, 30), subsample=(1, 1), border_mode='valid', activation="relu")) model.add(Cropping2D(cropping=((1, 1), (1, 1)))) model.add(Deconvolution2D(256, 3, 3, output_shape=(None, 256, 30, 30), subsample=(1, 1), border_mode='valid', activation="relu")) model.add(Cropping2D(cropping=((1, 1), (1, 1)))) #256*28*28 model.add(UpSampling2D((2,2))) #unpool_3 for _ in range(2): #deconv_3 model.add(Deconvolution2D(256, 3, 3, output_shape=(None, 256, 58, 58), subsample=(1, 1), border_mode='valid', activation="relu")) model.add(Cropping2D(cropping=((1, 1), (1, 1)))) model.add(Deconvolution2D(128, 3, 3, output_shape=(None, 128, 58, 58), subsample=(1, 1), border_mode='valid', activation="relu")) model.add(Cropping2D(cropping=((1, 1), (1, 1)))) #128*56*56 model.add(UpSampling2D((2,2))) #unpool_2 model.add(Deconvolution2D(128, 3, 3, output_shape=(None, 128, 114, 114), subsample=(1, 1), border_mode='valid', activation="relu")) model.add(Cropping2D(cropping=((1, 1), (1, 1)))) model.add(Deconvolution2D(64, 3, 3, output_shape=(None, 64, 114, 114), subsample=(1, 1), border_mode='valid', activation="relu")) model.add(Cropping2D(cropping=((1, 1), (1, 1)))) #64*112*112 model.add(UpSampling2D((2,2))) model.add(Deconvolution2D(64, 3, 3, output_shape=(None, 64, 226, 226), subsample=(1, 1), border_mode='valid', activation="relu")) model.add(Cropping2D(cropping=((1, 1), (1, 1)))) model.add(Deconvolution2D(21, 3, 3, output_shape=(None, 21, 226, 226), subsample=(1, 1), border_mode='valid', activation="relu")) model.add(Cropping2D(cropping=((1, 1), (1, 1)))) model.add(Reshape((21, 224*224))) model.add(Permute((2, 1))) model.add(Activation(("softmax"))) return model
angles.append(center_angle) # Process image to add random polygon shadows on the track, to allow car # to easily differentiate between track and shadow images.append(shadowPolygon(center_image)) angles.append(center_angle) X_train = np.array(images) y_train = np.array(angles) # Return samples, and yield yield sklearn.utils.shuffle(X_train, y_train) ############################## Model creation ##################### model=Sequential() # Cropping image to process only the interested portion model.add(Cropping2D(cropping=((60,25), (0,0)), input_shape=(160,320,3))) # Normalizing the image model.add(Lambda(lambda x: (x / 255.0) - 0.5, input_shape=(160,320,3))) # 5 Convolution layer with 'relu' for handling non linearity model.add(Convolution2D(32, 5, 5)) model.add(MaxPooling2D((2,2))) model.add(Activation('relu')) model.add(Convolution2D(32, 3, 3)) model.add(MaxPooling2D((2,2))) # Dropout avoids overfitting of training data and keeps model generalized Dropout(0.2, noise_shape=None, seed=None) model.add(Activation('relu')) model.add(Convolution2D(64, 3, 3))
from keras.models import Sequential from keras.layers import Flatten, Dense, Lambda from keras.layers import Conv2D, MaxPooling2D, Dropout from keras.layers.convolutional import Cropping2D from keras.regularizers import l2 from keras.layers.advanced_activations import ELU ch, row, col = 66, 200, 3 model = Sequential() model.add( Lambda(lambda x: x / 255.0 - 0.5, input_shape=(ch, row, col), output_shape=(ch, row, col))) model.add(Cropping2D(cropping=((20, 0), (0, 0)), input_shape=(66, 200, 3))) model.add( Conv2D(24, kernel_size=(5, 5), strides=(2, 2), padding='valid', activation='relu', name='Conv1')) model.add( Conv2D(36, kernel_size=(5, 5), strides=(2, 2), padding='valid', activation='relu', name='Conv2'))