<jupyter_start><jupyter_text><jupyter_code># Getting the into colab
!git clone https://github.com/imdeepmind/UdacitySelfDrivingCar.git
!mv UdacitySelfDrivingCar/data ./data
!rm -r UdacitySelfDrivingCar
# Importing dependencies
import pandas as pd
import numpy as np
import cv2

from sklearn.model_selection import train_test_split

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, Dense, MaxPooling2D, Dropout, Flatten
class ImageDataGenerator:
  def __init__(self, training_images, validation_images, batch_size=32):
    self.__training_counter = 0
    self.__validation_counter  = 0

    self.__training_images = training_images
    self.__validation_images = validation_images

    self.__batch_size = batch_size

  def __read_images(self, path):
    return cv2.imread(path, 1)
  
  def __image_augmenration(self, image):
    # TODO: Need to do some augmentation
    return image
  
  def __stack_images(self, left, center, right):
    return np.hstack([left, center, right])

  def training_generator(self):
    for i in range(0, len(self.__training_images), self.__batch_size):
      batch_data = self.__training_images[self.__training_counter:self.__training_counter+self.__batch_size]

      images = []
      steers = []

      for element in batch_data:
        center_image = self.__image_augmenration(self.__read_images(element[0]))
        left_image = self.__image_augmenration(self.__read_images(element[1]))
        right_image = self.__image_augmenration(self.__read_images(element[2]))

        image = self.__stack_images(left_image, center_image, right_image)

        images.append(image)
        steers.append(element[3])

      self.__training_counter += self.__batch_size

      yield np.array(images), np.array(steers)
  
  def validation_generator(self):
    for i in range(0, len(self.__validation_images), self.__batch_size):
      batch_data = self.__validation_images[self.__validation_counter:self.__validation_counter+self.__batch_size]

      images = []
      steers = []

      for element in batch_data:
        center_image = self.__read_images(element[0])
        left_image = self.__read_images(element[1])
        right_image = self.__read_images(element[2])

        image = self.__stack_images(left_image, center_image, right_image)

        images.append(image)
        steers.append(element[3])

      self.__validation_counter += self.__batch_size

      yield np.array(images), np.array(steers)
# The Model
model = Sequential()

model.add(Conv2D(filters = 64, kernel_size = 3, padding = 'Same', activation ='relu', input_shape = (160,960,3)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(filters = 64, kernel_size = 3, padding = 'Same', activation ='relu', input_shape = (640,480,3)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(filters = 128, kernel_size = 3, padding = 'Same', activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(filters = 128, kernel_size = 3, padding = 'Same', activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(filters = 256, kernel_size = 3, padding = 'Same', activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(filters = 256, kernel_size = 3, padding = 'Same', activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(filters = 256, kernel_size = 3, padding = 'Same', activation ='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(4096, activation = "relu"))
model.add(Dropout(0.5))

model.add(Dense(4096, activation = "relu"))
model.add(Dropout(0.5))

model.add(Dense(4096, activation = "relu"))
model.add(Dropout(0.5))

model.add(Dense(1))

model.summary()
data = pd.read_csv('./data/driver_log_cleaned.csv')

train_data, test_data = train_test_split(data.values, test_size=0.1)
gen = ImageDataGenerator(train_data, test_data)

training_generator = gen.training_generator()
validation_generator = gen.validation_generator()
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mae'])
model.fit(training_generator, steps_per_epoch=len(train_data) // 32, validation_data=validation_generator, validation_steps=len(test_data) // 32, epochs=10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13196942446043167, 'fraction_numerical': 0.03035071942446043, 'mean_word_length': 5.283898305084746, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '574358', 'license': 'MIT', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1498, 'n_words': 267, 'path': 'Base_Model.ipynb', 'repo_name': 'imdeepmind/UdacitySelfDrivingCar', 'size': '4448'}