<jupyter_start><jupyter_text>Get Data and Generate Dataset<jupyter_code>DATASET_PATH="/home/milhouse/datasets/steering_angle/data2"
!ls $DATASET_PATH
#import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as npimg
import os

## Keras
import keras
from keras.models import Sequential
from keras.optimizers import Adam
from keras.layers import Convolution2D, MaxPooling2D, Dropout, Flatten, Dense

import cv2
import pandas as pd
#import random
import ntpath

## Sklearn
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
## Store data
#datadir = 'self-driving-car-data-track-1'
datadir = DATASET_PATH
#columns = ['center', 'left', 'right', 'steering', 'throttle', 'reverse', 'speed']
data = pd.read_csv(os.path.join(datadir, 'driving_log.csv'))
pd.set_option('display.max_colwidth', -1)
data.head()

def path_leaf(path):
  """Get tail of path"""
  head, tail = ntpath.split(path)
  return tail

## Remove path of images
data['center'] = data['center'].apply(path_leaf)
data['left'] = data['left'].apply(path_leaf)
data['right'] = data['right'].apply(path_leaf)
print(data.shape)
data.head()
def load_img_steering(datadir, df):
  """Get img and steering data into arrays"""
  image_path = []
  steering = []
  for i in range(len(data)):
    indexed_data = data.iloc[i]
    center, left, right = indexed_data[0], indexed_data[1], indexed_data[2]
    image_path.append(os.path.join(datadir, center.strip()))
    steering.append(float(indexed_data[3]))
  image_paths = np.asarray(image_path)
  steerings = np.asarray(steering)
  return image_paths, steerings

image_paths, steerings = load_img_steering(datadir + '/IMG', data)
  
## Visualize data
num_bins = 25
samples_per_bin = 200
hist, bins = np.histogram(data['steering'], num_bins)
center = bins[:-1] + bins[1:] * 0.5  # center the bins to 0

## Plot
plt.bar(center, hist, width=0.05)
plt.plot((np.min(data['steering']), np.max(data['steering'])), (samples_per_bin, samples_per_bin))<jupyter_output><empty_output><jupyter_text>Preprocessing required 1.- Balance dataset<jupyter_code>## Unskew data: for bins with >samples_per_bin, shuffle them and remove enough to have samples_per_bin 
print('Total data: {0}'.format(len(data)))

## Make list of indices to remove
remove_list = []
for j in range(num_bins):
  list_ = []
  for i in range(len(data['steering'])):
    steering_angle = data['steering'][i]
    if steering_angle >= bins[j] and steering_angle <= bins[j+1]:
      list_.append(i)
  list_ = shuffle(list_)
  list_ = list_[samples_per_bin:]
  remove_list.extend(list_)
  
## Remove from extras from list
data.drop(data.index[remove_list], inplace=True)
print('Removed: {0}'.format(len(remove_list)))
print('Remaining: {0}'.format(len(data)))

## Plot
hist, _ = np.histogram(data['steering'], (num_bins))
plt.bar(center, hist, width=0.05)
plt.plot((np.min(data['steering']), np.max(data['steering'])), (samples_per_bin, samples_per_bin))
image_paths, steerings = load_img_steering(datadir + '/IMG', data)

## Split data into training and validation
X_train_uri, X_valid_uri, Y_train, Y_valid = train_test_split(image_paths, steerings, test_size=0.2, random_state=0)

## Check that data is valid
print("Training Samples: {}\nÇalid Samples: {}".format(len(X_train), len(X_valid)))
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
axes[0].hist(Y_train, bins=num_bins, width=0.05, color='blue')
axes[0].set_title('Training set')
axes[1].hist(Y_valid, bins=num_bins, width=0.05, color='red')
axes[1].set_title('Validation set')<jupyter_output>Training Samples: 1491
Çalid Samples: 373<jupyter_text>More preprocessing<jupyter_code>from PIL import Image
from skimage.feature import *
s
# Resize pics:
Y_PIX = 80
X_PIX = 160
Y_CROP = 60
Y_CROP2 = 100
def img_preprocess(img_uri, my_visualize=False):
    image = npimg.imread(img_uri)
    img = cv2.normalize(image, image, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
    # crop image:
    img = img[Y_CROP:Y_CROP2, :, :]
    # resize image:
    img = cv2.resize(img, (X_PIX, Y_PIX), interpolation=cv2.INTER_CUBIC)
    # grayscale
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # resize again
    img_gray = Image.fromarray(np.uint8(256 * img_gray))
    img_gray = img_gray.resize([int(0.5 * s) for s in img_gray.size], Image.ANTIALIAS)

    # hog features
    img_gray = hog(img_gray, pixels_per_cell=(8, 8), visualize=my_visualize)

    return img_gray

fd, img = img_preprocess(X_train_uri[0], my_visualize=True)
plt.imshow(img)

## Preprocess data (< 1min)
X_train = np.array(list(map(img_preprocess, X_train_uri)))
X_valid = np.array(list(map(img_preprocess, X_valid_uri)))
#plt.imshow(X_train[rn.randint(0, len(X_train)-1)])
plt.axis('off')
print(X_train.shape)
IMG_SIZE=X_train[0].shape
print(IMG_SIZE)<jupyter_output>(1491, 1944)
(1944,)<jupyter_text>Our dummy baseline model<jupyter_code>import time
from sklearn.neighbors import KNeighborsRegressor
print("Training model...")
now = time.time()

regressor = KNeighborsRegressor(n_neighbors=5, weights='distance')

#Fit/train
regressor.fit(X_train, Y_train)

#Evaluate
print("Evaluating model...")
y_pred = regressor.predict(X_valid)
from sklearn import metrics

print('Mean Squared Error:', metrics.mean_squared_error(Y_valid, y_pred))

later = time.time()
print ("Elapsed time: {}s".format(later - now))

PROJECT_PATH="/home/milhouse/projects/car-behavioral-cloning/"
PATH_TO_MODEL = '{}/models/model_meetup_knnr.pickle'.format(PROJECT_PATH)

import pickle
with open(PATH_TO_MODEL, 'wb') as handle:
    pickle.dump(regressor, handle, protocol=pickle.HIGHEST_PROTOCOL)
!/home/milhouse/miniconda3/envs/car-behavioral-cloning-keras2/bin/python $PROJECT_PATH/my_drive.py $PROJECT_PATH/models/model_meetup_knnr.pickle $PROJECT_PATH/models/images_model_meetup & $PROJECT_PATH/beta_simulator_linux/beta_simulator.x86_64
!convert $PROJECT_PATH/models/images_model_meetup/*5.jpg $PROJECT_PATH/models/images_model_knnr.gif<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14171122994652408, 'fraction_numerical': 0.01888368983957219, 'mean_word_length': 6.193509615384615, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '478682', 'license': 'MIT', 'n_tokens_mistral': 2378, 'n_tokens_neox': 2233, 'n_words': 443, 'path': 'notebooks/Behavioral_Cloning_with_KNNRegressor.ipynb', 'repo_name': 'muoten/car-behavioral-cloning', 'size': '5984'}