<jupyter_start><jupyter_text>Analyse data collected from the simulator<jupyter_code>import csv
import cv2

lines = []
with open('simulator_images/driving_log.csv') as csvfile:
    reader = csv.reader(csvfile)
    for line in reader:
        lines.append(line)
        
correction = 0.2
images = []
steering_angles = []
for line in lines:
    for i in range(3):
        source_path = line[i]
        filename = source_path.split('\\')[-1] #windows in local environment
        current_path = 'simulator_images/IMG/' + filename
        image = cv2.imread(current_path)
        images.append(image)
        if i == 1: #left image
            steering_angle = float(line[3]) + correction
            steering_angles.append(steering_angle)
        if i == 2: #right image
            steering_angle = float(line[3]) - correction
            steering_angles.append(steering_angle)
        if i == 0: #center image
            steering_angle = float(line[3])
            steering_angles.append(steering_angle)
        
    
augmented_images, augmented_steering_angles = [], []
for image, steering_angle in zip(images, steering_angles):
    augmented_images.append(image)
    augmented_steering_angles.append(steering_angle)
    augmented_images.append(cv2.flip(image, 1))
    augmented_steering_angles.append(steering_angle*-1.0)<jupyter_output><empty_output><jupyter_text>Alternative to load image as memory restrictions started to be visible<jupyter_code>from sklearn.model_selection import train_test_split

import cv2
import numpy as np
import sklearn
from sklearn.utils import shuffle

import os
import csv

samples = []
with open('training_simulator_images/driving_log.csv') as csvfile:
    reader = csv.reader(csvfile)
    for line in reader:
        samples.append(line)

correction = 0.2

train_samples, validation_samples = train_test_split(samples*6, test_size=0.2)

print("Train samples: " + str(len(train_samples)))
print("Validation samples: " + str(len(validation_samples)))

def generator(samples, batch_size=32):
    num_samples = len(samples)
    print(num_samples)
    while 1: # Loop forever so the generator never terminates
        shuffle(samples)
        for offset in range(0, num_samples, batch_size):
            batch_samples = samples[offset:offset+batch_size]

            images = []
            angles = []
            for batch_sample in batch_samples:
                name = 'training_simulator_images/IMG/'+batch_sample[0].split('\\')[-1]
                center_image = cv2.imread(name)
                center_angle = float(batch_sample[3])
                images.append(center_image)
                angles.append(center_angle)
                #repeat process for left image
                left_name = 'training_simulator_images/IMG/'+batch_sample[1].split('\\')[-1]
                left_image = cv2.imread(left_name)
                left_angle = float(batch_sample[3]) + correction
                images.append(left_image)
                angles.append(left_angle)
                #repeat process for right image
                right_name = 'training_simulator_images/IMG/'+batch_sample[2].split('\\')[-1]
                right_image = cv2.imread(right_name)
                right_angle = float(batch_sample[3]) - correction
                images.append(right_image)
                angles.append(right_angle)
                
                #augmented images
                images.append(cv2.flip(center_image, 1))
                images.append(cv2.flip(left_image, 1))
                images.append(cv2.flip(right_image, 1))
                angles.append(center_angle*-1.0)
                angles.append(left_angle*-1.0)
                angles.append(right_angle*-1.0)

            # trim image to only see section with road
            X_train = np.array(images)
            y_train = np.array(angles)
            yield sklearn.utils.shuffle(X_train, y_train)
            
# Set our batch size
batch_size=6

print(len(train_samples))
# compile and train the model using the generator function
train_generator = generator(train_samples, batch_size=batch_size)
validation_generator = generator(validation_samples, batch_size=batch_size)<jupyter_output>Train samples: 78614
Validation samples: 19654
78614<jupyter_text>Check size of images collected from training on Udacity Simulator<jupyter_code># this is no longer used

print(len(steering_angles))
print(len(images))

print(len(augmented_steering_angles))
print(len(augmented_images))<jupyter_output>49134
49134
98268
98268<jupyter_text>Transform data into numpy arrays as Keras requires<jupyter_code>import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator

#first used images and angles, then augmented
X_train = np.array(augmented_images) # dataset for training
y_train = np.array(augmented_steering_angles) #labels, which in this case are the steering angles<jupyter_output><empty_output><jupyter_text>Build a simple neural networks with keras just to validate that it works<jupyter_code>from keras.models import Sequential
from keras.layers import Flatten, Dense
import math

model = Sequential()
model.add(Flatten(input_shape=(160, 320, 3)))
model.add(Dense(1))

model.compile(loss='mse', optimizer='adam')
model.fit_generator(train_generator, 
            steps_per_epoch=math.ceil(len(train_samples)/batch_size), 
            validation_data=validation_generator, 
            validation_steps=math.ceil(len(validation_samples)/batch_size), 
            epochs=5, verbose=1, shuffle=1)

model.save('model.h5')<jupyter_output>Epoch 1/5
training_simulator_images/IMG/center_2020_06_01_14_18_15_216.jpg
training_simulator_images/IMG/center_2020_06_01_14_17_32_251.jpg
training_simulator_images/IMG/center_2020_06_01_09_38_20_829.jpg
training_simulator_images/IMG/center_2020_06_01_10_21_33_495.jpg
training_simulator_images/IMG/center_2020_06_01_09_37_46_164.jpg
training_simulator_images/IMG/center_2020_06_01_09_37_12_103.jpg
training_simulator_images/IMG/center_2020_06_01_09_35_25_379.jpg
training_simulator_images/IMG/center_2020_06_01_09_48_14_279.jpg
training_simulator_images/IMG/center_2020_06_01_09_47_24_133.jpg
training_simulator_images/IMG/center_2020_06_01_14_16_34_495.jpg
training_simulator_images/IMG/center_2020_06_01_14_15_44_929.jpg
training_simulator_images/IMG/center_2020_06_01_13_22_04_702.jpg
training_simulator_images/IMG/center_2020_06_01_14_19_22_905.jpg
training_simulator_images/IMG/center_2020_06_01_09_47_51_838.jpg
training_simulator_images/IMG/center_2020_06_01_13_23_55_029.jpg
training_simula[...]<jupyter_text>Preprocess data<jupyter_code>from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda

model = Sequential()
model.add(Lambda(lambda x: x/255.0 - 0.5, input_shape=(160, 320, 3)))
model.add(Flatten())
model.add(Dense(1))

model.compile(loss='mse', optimizer='adam')
model.fit(X_train, y_train, validation_split=0.2, shuffle=True, nb_epoch=2) #2 epochs

model.save('model.h5')<jupyter_output>C:\Users\gusta\Anaconda3\envs\Project3\lib\site-packages\ipykernel_launcher.py:10: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.
  # Remove the CWD from sys.path while we load stuff.<jupyter_text>Testing training dataset against LeNet with changes<jupyter_code>from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from keras.layers import Dropout, Flatten, Lambda, Cropping2D
from keras.layers.convolutional import Conv2D, MaxPooling2D
import math

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))))
#results into an image of shape 156x316x60
model.add(Conv2D(60, (5,5), activation='relu'))

#results into images of shape 152x312x60
model.add(Conv2D(60, (5,5), activation='relu'))
#pooling layers
#by applying a filter of 2x2, scales down the image to 72x156x60
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))

#results into an image shape of 68x152x60
model.add(Conv2D(60, (5,5), activation='relu'))
#results into an image shape of 34x76x60
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))

#results into an image shape of 30x72x30
model.add(Conv2D(30, (5,5), activation='relu'))\
#results into and image shape of 15x36x30
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.5))

#results into an image shape of 11x32x30
#model.add(Conv2D(15, (5,5), activation='relu'))\
#results into and image shape of 6x18x15
#model.add(MaxPooling2D(pool_size=(2,2)))
#model.add(Dropout(0.5))

#6x18x15 = 720 nodes
model.add(Flatten())
#model.add(Dense(500))
model.add(Dense(120))
model.add(Dense(84))
model.add(Dense(1))

model.compile(loss='mse', optimizer='adam')
history_object = model.fit_generator(train_generator, 
            steps_per_epoch=math.ceil(len(train_samples)/batch_size), 
            validation_data=validation_generator, 
            validation_steps=math.ceil(len(validation_samples)/batch_size), 
            epochs=5, verbose=1, shuffle=1)

model.save('model.h5')
import matplotlib.pyplot as plt

print(history_object.history.keys())

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.show()
import keras
import tensorflow as tf

config = tf.ConfigProto( device_count = {'GPU': 1 , 'CPU': 8} ) 
sess = tf.Session(config=config) 
keras.backend.set_session(sess)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10433152852977925, 'fraction_numerical': 0.05518533944189921, 'mean_word_length': 4.309563294637922, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '355812', 'license': 'MIT', 'n_tokens_mistral': 3527, 'n_tokens_neox': 3180, 'n_words': 615, 'path': 'data-processing.ipynb', 'repo_name': 'GustavoMoreiraPT/CarND-Behavioral-Cloning-P3', 'size': '9604'}