In [1]:
import numpy as np
from PIL import Image, ImageEnhance, ImageOps
import pandas as pd
path = '/data1/udacity/simulator/data'
img_path = path +'/IMG'
csv_file = path +'/driving_log.csv'
csv_array=pd.read_csv(csv_file)
print("Number of lines in CSV: " + str(csv_array.shape))
import matplotlib.pyplot as plt
%matplotlib inline
plt.hist(csv_array.steering, bins=50)
print("Min Steering Angle:" + str(min(csv_array.steering)))
print("Max Steering Angle:" + str(max(csv_array.steering)))
In [2]:
# center, left, right, steering angle, throttle, break, speed
# preprocess the data
X_full_name = []
y_full_angle= []
line = csv_array.iloc[0]
#print(line)
def plotCameraImages(line):
fig, axes = plt.subplots(1, 3)
fig.set_figwidth(10)
fig.set_figheight(20)
i=0
for camera in ['left','center','right']:
print(i)
axes.flat[i].imshow(Image.open(path+'/'+line[camera].decode('UTF-8').strip()))
axes.flat[i].tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off')
if (camera=='center'):
axes.flat[i].set_title(camera+' ' +str(line['steering']))
else:
axes.flat[i].set_title(camera)
i=i+1
plotCameraImages(csv_array.iloc[0])
plotCameraImages(csv_array.iloc[1900])
plotCameraImages(csv_array[csv_array.steering < 0.26 ][csv_array.steering>0.24].iloc[4])
In [3]:
csv_array.head()
Out[3]:
In [4]:
import driving_data
import cv2
filename = driving_data.val_xs[0]
img=[]
img.append( np.array(Image.open(filename)))
#img.append( driving_data.process_image_comma(filename).transpose( 1, 2, 0) )
img.append(driving_data.process_image_gray(filename))
img.append( driving_data.process_image_sully(filename) )
fig, axes = plt.subplots(1, 3)
fig.set_figwidth(10)
fig.set_figheight(20)
i=0
for camera in ['raw','comma','sully']:
print(i)
print(img[i].shape)
axes.flat[i].imshow(img[i])
axes.flat[i].tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off')
axes.flat[i].set_title(camera)
i=i+1
plt.show()
print('brightness')
img3 = np.array(Image.open(filename))
img3 = driving_data.augment_brightness_camera_images(img3)
plt.imshow(img3)
plt.show()
print('brightness')
img3 = np.array(Image.open(filename))
img3 = driving_data.augment_brightness_camera_images(img3)
plt.imshow(img3)
plt.show()
img3 = cv2.resize(img3, (200, 66) )
plt.imshow(img3)
plt.show()
print(filename)
image = cv2.imread(filename)
plt.imshow(image)
plt.show()
print('early process')
image = np.array(Image.open(filename))
image =driving_data.process_image_sully_pixels(image)
plt.imshow(image)
plt.show()
print('process')
img2 = driving_data.process_image_sully(filename)
img4 = np.add(img2,0.5)
img4 = np.multiply(img4,255)
plt.imshow(img4)
plt.show()
img2 = driving_data.process_image_sully(filename)
plt.imshow(img2)
plt.show()
img2 = driving_data.process_image_sully(filename)
plt.imshow(img2)
plt.show()
img2 = driving_data.process_image_sully(filename)
plt.imshow(img2)
plt.show()
img2 = driving_data.process_image_comma(filename).transpose( 1, 2, 0)
plt.imshow(img2)
plt.show()
img2 = driving_data.process_image_comma(filename).transpose( 1, 2, 0)
plt.imshow(img2)
plt.show()
img2 = driving_data.process_image_comma(filename).transpose( 1, 2, 0)
plt.imshow(img2)
plt.show()
img2 = driving_data.process_image_comma(filename).transpose( 1, 2, 0)
plt.imshow(img2)
plt.show()
img2 = driving_data.process_image_comma(filename).transpose( 1, 2, 0)
plt.imshow(img2)
plt.show()
In [5]:
plt.hist(driving_data.val_ys, bins=50)
Out[5]:
In [6]:
from keras.layers import Convolution2D, MaxPooling2D, Activation
from keras.models import Sequential
from keras.layers import Dense, Activation, Reshape, Merge
import numpy as np
import matplotlib.pyplot as plt
import cv2 # only used for loading the image, you can use anything that returns the image as a np.ndarray
%matplotlib inline
In [7]:
img = driving_data.process_image_gray(filename)
In [8]:
plt.imshow(img)
Out[8]:
In [ ]:
model = Sequential ([
Reshape ((160, 320, 1), input_shape=(160, 320)),
Convolution2D (24, 8, 8, border_mode='valid'),
MaxPooling2D (pool_size=(2, 2)),
Activation ('relu'),
#77x157
Convolution2D (36, 5, 5, border_mode='valid'),
MaxPooling2D (pool_size=(2, 2)),
Activation ('relu'),
#37x77
Convolution2D (48, 5, 5, border_mode='valid'),
MaxPooling2D (pool_size=(2, 2)),
Activation ('relu'),
#17x37
Convolution2D (64, 3, 3, border_mode='valid'),
MaxPooling2D (pool_size=(2, 2)),
Activation ('relu'),
#8x18
Convolution2D (64, 2, 2, border_mode='valid'),
MaxPooling2D (pool_size=(2, 2)),
Activation ('relu'),
])
In [ ]:
from keras import backend as K
print(model.layers)
get_3rd_layer_output = K.function([model.layers[0].input, model.layers[2].input, K.learning_phase()], [model.layers[3].output])
print(get_3rd_layer_output)
img_batch = np.expand_dims(img,axis=0)
layer_output = get_3rd_layer_output([img_batch])[0]
print(layer_output.shape)
In [ ]:
#model = Sequential()
#model.add(Convolution2D(3, # number of filter layers
# 3, # y dimension of kernel (we're going for a 3x3 kernel)
# 3, # x dimension of kernel
# input_shape=cat.shape))
In [ ]:
img_batch = np.expand_dims(img,axis=0)
In [ ]:
conv_img = model.predict(img_batch)
print(conv_img.shape)
In [ ]:
# here we get rid of that added dimension and plot the image
def visualize_img(model, img):
# Keras expects batches of images, so we have to add a dimension to trick it into being nice
img_batch = np.expand_dims(img,axis=0)
conv_img = model.predict(img_batch)
conv_img = np.squeeze(conv_img, axis=0)
print conv_img.shape
plt.imshow(conv_img)
In [ ]:
#visualize_img(model, img)
end = conv_img.shape[3]
for i in range(0,end):
a = conv_img[0,:,:,i]
plt.imshow(a)
plt.show()
In [ ]: