Skip to content

End2End Modelの作成

教師データ、テストデータの作成

型をfloat32に定義する

1
2
3
4
import numpy as np

center_data  = np.array(center_data).astype('float32')
direction_handle = np.array(direction_handle).astype('float32')

値のシャッフル

1
2
3
from sklearn.utils import shuffle

center_data, direction_handle = shuffle(center_data, direction_handle)

教師データ、テストデータの切り出し

1
2
3
from sklearn.model_selection import train_test_split

train_center, test_center, train_direction, test_direction = train_test_split(center_data, direction_handle, random_state=0, test_size=0.1)

Reshape

1
2
3
4
rows = 10
cols = 40
train_center = train_center.reshape(train_center.shape[0], rows, cols, 1)
test_center = test_center.reshape(test_center.shape[0], rows, cols, 1)

Modelの作成

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from keras.models import *
from keras.layers import *
from keras.optimizers import Adam

def end2end_model():
    model = Sequential()
    model.add(Lambda(lambda x: x/127.5 - 1.,input_shape=(rows,cols,1)))    

    model.add(Convolution2D(8, 3, 3, init='normal',border_mode='valid'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D((2,2),border_mode='valid'))

    model.add(Convolution2D(8, 3, 3,init='normal',border_mode='valid'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D((2,2),border_mode='valid'))

    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(50))
    model.add(Activation('relu'))
    model.add(Dense(1))

    model.summary()

    return model

学習の開始

1
2
3
4
5
6
7
8
batch = 128
epoc = 10
adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)

model = end2end()
model.compile(loss='mean_squared_error',optimizer='adam',metrics=['accuracy'])

history = model.fit(train_center, train_direction,batch_size=batch, nb_epoch=epoc,verbose=1, validation_data=(test_center, test_direction))

ここまでのソース

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import csv
from matplotlib import pyplot as plt
import cv2
import numpy as np
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from keras.models import *
from keras.layers import *
from keras.optimizers import Adam

def load_data():
    folder_path = ''
    center_img = []
    direction_handle = []

    f = open(folder_path + 'driving_log.csv', 'r')

    reader = csv.reader(f)
    header = next(reader)
    for row in reader:
        img_name = row[0]
        img_path = folder_path  +  img_name
        img = plt.imread(img_path)
        center_img.append(img)
        direction_handle.append(float(row[3]))
    f.close()

    return (center_img, direction_handle)

def draw_img(center_img, direction_handle, pos=0):
    g_rows = 3
    g_cols = 3
    size = g_rows * g_cols
    fig, axs = plt.subplots(ncols=g_rows, nrows=size/g_rows)

    for h in range(g_rows):
        for i in range(g_cols):
            axs[h][i].imshow(center_img[pos + i * h], cmap=plt.cm.gray_r,) 
            axs[h][i].set_title(str(direction_handle[pos + h * i]))
    plt.show()

def process_img(center_img):
    new_center = []
    for img in center_img:
        crop_img = img[60:140, 0:320, :]
        resize_img = cv2.resize((cv2.cvtColor(crop_img, cv2.COLOR_RGB2XYZ))[:,:,2],(40,10))
        new_center.append(resize_img)

    return new_center    

def grow_data(center_img, direction_handle, delta):
    new_center = []
    new_direction = []
    for i in range(len(center_img)):
        for j in range(3):
            new_center.append(center_img[i])
            if j == 0:
                new_direction.append(direction_handle[i])
            elif j == 1:
                new_direction.append(direction_handle[i] + float(delta))
            elif j == 2:
                new_direction.append(direction_handle[i] - float(delta))
    return (new_center,  new_direction)

def end2end_model():
    model = Sequential()
    model.add(Lambda(lambda x: x/127.5 - 1.,input_shape=(rows,cols,1)))    

    model.add(Convolution2D(8, 3, 3, init='normal',border_mode='valid'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D((2,2),border_mode='valid'))

    model.add(Convolution2D(8, 3, 3,init='normal',border_mode='valid'))
    model.add(Activation('relu'))
    model.add(MaxPooling2D((2,2),border_mode='valid'))

    model.add(Dropout(0.2))
    model.add(Flatten())
    model.add(Dense(50))
    model.add(Activation('relu'))
    model.add(Dense(1))

    model.summary()

    return model

if __name__ == '__main__':
    (center_img, direction_handle) = load_data()

    delta = 0.2
    (center_img, direction_handle) = grow_data(center_img, direction_handle, delta)

    center_data = process_img(center_img)

    draw_img(center_data, direction_handle, 50)

    center_data  = np.array(center_data).astype('float32')
    direction_handle = np.array(direction_handle).astype('float32')

    center_data, direction_handle = shuffle(center_data, direction_handle)

    train_center, test_center, train_direction, test_direction = train_test_split(center_data, direction_handle, random_state=0, test_size=0.1)

    rows = 10
    cols = 40
    train_center = train_center.reshape(train_center.shape[0], rows, cols, 1)
    test_center = test_center.reshape(test_center.shape[0], rows, cols, 1)

    batch = 128
    epoc = 10
    adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)

    model = end2end()
    model.compile(loss='mean_squared_error',optimizer='adam',metrics=['accuracy'])

    history = model.fit(train_center, train_direction,batch_size=batch, nb_epoch=epoc,verbose=1, validation_data=(test_center, test_direction))