In [1]:
import numpy as np
# set random seed
np.random.seed(256)
import utils
from dataloader import DataLoader
def get_image(settings, image_name):
file_name = utils.get_image_file_name(image_name)
return utils.load_image(settings.data_path + settings.images_path + file_name.strip())
import matplotlib.pyplot as plt
%matplotlib inline
# plot a list of images in a single row
def plot_images_in_a_row(images, titles):
cols = len(images)
rows = 1
fig = plt.figure(figsize=(cols * 4, rows * 4))
for i in range(cols):
plt.subplot(rows, cols, i+1)
plt.imshow(images[i])
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()
from settings import Settings
default_settings = Settings(
data_path = '../mydata2/',
data_file = 'driving_log.csv',
images_path = 'IMG/',
batch_size = 512,
epochs = 10,
learning_rate = 0.001,
epoch_sample_size = 25000
)
# print settings
print(default_settings)
In [2]:
print("Loading data ...")
dataloader = DataLoader(default_settings)
data = dataloader.get_csv_data()
print("Done.")
In [3]:
data.iloc[:5]
Out[3]:
In [4]:
data.describe()
Out[4]:
In [5]:
# plot the distribution of steering angles
data['steering'].plot.hist(range=(-1,1), bins=41, rwidth=0.75)
Out[5]:
In [6]:
X, y = dataloader.get_data()
print("Total samples : {} Samples after normalization : {}".format(len(data), len(y)))
In [7]:
# plot the distribution of steering angles
plt.hist(y, range=(-1,1), bins=41, rwidth=0.75)
plt.show()
In [8]:
# get values of left, center, right images and steering angles
center = [utils.get_image_file_name(file_path) for file_path in data['center'].values]
left = [utils.get_image_file_name(file_path) for file_path in data['left'].values]
right = [utils.get_image_file_name(file_path) for file_path in data['right'].values]
steering = data['steering'].values
# select sample images for data exploration
samples = [1500, 100, 2750]
# load images
center_sample_images = [get_image(default_settings, center[index]) for index in samples]
left_sample_images = [get_image(default_settings, left[index]) for index in samples]
right_sample_images = [get_image(default_settings, right[index]) for index in samples]
print("Input image shape : ", center_sample_images[0].shape)
In [9]:
# display image
plt.imshow(center_sample_images[0])
plt.show()
In [10]:
for i in range(len(samples)):
samples_title = ["img{}_{}".format(i, 'left'), "img{}_{}".format(i, 'center'), "img{}_{}".format(i, 'right')]
plot_images_in_a_row([left_sample_images[i], center_sample_images[i], right_sample_images[i]], samples_title)
In [11]:
cropped_left_images = [utils.crop_image(image) for image in left_sample_images]
cropped_center_images = [utils.crop_image(image) for image in center_sample_images]
cropped_right_images = [utils.crop_image(image) for image in right_sample_images]
print("Original image shape : ", center_sample_images[0].shape)
print("Cropped image shape : ", cropped_center_images[0].shape)
In [12]:
for i in range(len(samples)):
samples_title = ["img{}_{}_cropped".format(i, 'left'), "img{}_{}_cropped".format(i, 'center'), "img{}_{}_cropped".format(i, 'right')]
plot_images_in_a_row([cropped_left_images[i], cropped_center_images[i], cropped_right_images[i]], samples_title)
Following are the list of image processing effects randomly added to augment our data, for the training of our model. Appropriate adjustments to steering angle done as well.
In [13]:
image_input = get_image(default_settings, center[100])
steering_angle = steering[100]
image_flipped = utils.flip_image(image_input)
plot_images_in_a_row([image_input, image_flipped],["Original (steering: {})".format(steering_angle), "Flipped (steering: {})".format(steering_angle * -1)])
In [14]:
image_brightness_adjusted1 = utils.adjust_brightness(image_input, 1.2)
image_brightness_adjusted2 = utils.adjust_brightness(image_input, 0.8)
plot_images_in_a_row([image_input, image_brightness_adjusted1, image_brightness_adjusted2], ['Original', "Brightness (adjustment=1.2)", "Brightness (adjustment=0.8)"])
In [15]:
from skimage.exposure import adjust_gamma
image_gamma_adjusted1 = adjust_gamma(image_input, gamma=1.2)
image_gamma_adjusted2 = adjust_gamma(image_input, gamma=0.8)
plot_images_in_a_row([image_input, image_gamma_adjusted1,image_gamma_adjusted2], ['Original', "Gamma (adjustment=1.2)", "Gamma (adjustment=0.8)"])
In [16]:
image_translated1 = utils.translate_image(image_input, x_trans=20, y_trans=0.5)
image_translated2 = utils.translate_image(image_input, x_trans=-20, y_trans=-0.5)
plot_images_in_a_row([image_input, image_translated1,image_translated2],['Original', "Trans (shift down)", "Trans (shift up)"])
In [19]:
image_shadowed1 = utils.random_shadow(image_input)
plot_images_in_a_row([image_input, image_shadowed1],['Original', "Shadow Example"])
In [20]:
from IPython import display
display.HTML('<img src="./output/recoverygif.png">')
Out[20]: