-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
71 lines (56 loc) · 2.45 KB
/
Copy pathmodel.py
File metadata and controls
71 lines (56 loc) · 2.45 KB
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
import os
import json
import pickle
import pandas
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D, AveragePooling2D
pickle_file = 'train_data.pickle'
bytes_in = bytearray(0)
max_bytes = 2 ** 31 - 1
input_size = os.path.getsize(pickle_file)
with open(pickle_file, 'rb') as p_train_data:
for _ in range(0, input_size, max_bytes):
bytes_in += p_train_data.read(max_bytes)
pickle_data = pickle.loads(bytes_in)
X_train = pickle_data['train_dataset']
y_train = pickle_data['train_labels']
X_val = pickle_data['val_dataset']
y_val = pickle_data['val_labels']
del pickle_data # Free up memory
batch_size = 100
nb_classes = 1
nb_epoch = 35
X_train = X_train.astype('float32')
X_test = X_val.astype('float32')
print(X_train.shape[0], 'train samples')
print(X_val.shape[0], 'test samples')
#---Model-Definition:
input_shape = X_train.shape[1:]
model = Sequential()
#Start wird 4 Convolutiional Layers to recognize the image
model.add(Convolution2D(60, 5, 5, subsample=(5, 5), border_mode='same', input_shape=input_shape, activation='relu', dim_ordering='tf'))
model.add(Convolution2D(60, 2, 2, border_mode='same', input_shape=input_shape, activation='relu', dim_ordering='tf'))
model.add(MaxPooling2D(pool_size=(2, 2), border_mode='same', dim_ordering='tf'))
model.add(Convolution2D(60, 2, 2, border_mode='same', input_shape=input_shape, activation='relu', dim_ordering='tf'))
model.add(MaxPooling2D(pool_size=(2, 2), border_mode='same', dim_ordering='tf'))
model.add(Convolution2D(60, 6, 6, border_mode='same', input_shape=input_shape, activation='relu', dim_ordering='tf'))
model.add(MaxPooling2D(pool_size=(2, 2), border_mode='same', dim_ordering='tf'))
model.add(Dropout(0.25))
#Flatten the Matrix to a Vektor and run 3 RELU Layers
model.add(Flatten())
model.add(Dense(40, name="hidden1"))
model.add(Activation('relu'))
model.add(Dense(20, name="hidden2"))
model.add(Activation('relu'))
model.add(Dense(10, name="hidden3"))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1, name="Steering_Angle"))
model.summary()
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
history = model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(X_val, y_val))
json_string = model.to_json()
with open('./model.json', 'w') as outfile:
json.dump(json_string, outfile)
model.save_weights('./model.h5')