forked from JinnAIGroup/B5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_modelB5.py
143 lines (122 loc) · 5.57 KB
/
train_modelB5.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
""" YPL, JLL, 2021.9.8 - 2022.3.1
modelB5.dlc = supercombo079.dlc
ignore valid_len in Lines 74, 110, 112, 157, 158 of datagenB5
1. Tasks: temporal state (features) + path planning (PP)
The temporal state (a 512 array) represents (are features of) all path planning details:
path prediction, lane detection, lead car xyva, desire etc., i.e., outs[0]...[10] in output.txt.
2. Ground Truth from pathdata.h5, radardata.h5:
Pose: 56 = Ego path 51 (x, y) + 5 radar lead car's dRel (relative distance), yRel, vRel (velocity), aRel (acceleration), prob
3. Loss: mean squared error (mse)
Input:
/home/jinn/dataB/UHD--2018-08-02--08-34-47--32/yuv.h5, pathdata.h5, radardata.h5
/home/jinn/dataB/UHD--2018-08-02--08-34-47--33/yuv.h5, pathdata.h5, radardata.h5
Output:
/B5/saved_model/modelB5_lossB.npy
Run: on 3 terminals
(YPN) jinn@Liu:~/YPN/B5$ python serverB5.py --port 5557
(YPN) jinn@Liu:~/YPN/B5$ python serverB5.py --port 5558 --validation
(YPN) jinn@Liu:~/YPN/B5$ python train_modelB5.py --port 5557 --port_val 5558
Training History:
BATCH_SIZE = 16 EPOCHS = 2
Road Tests:
"""
import os
import h5py
import time
import argparse
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from modelB5 import get_model
from serverB5 import client_generator, BATCH_SIZE
EPOCHS = 2
os.environ['CUDA_VISIBLE_DEVICES'] = "-1"
def get_data(hwm, host, port, model):
for tup in client_generator(hwm=hwm, host=host, port=port):
Ximgs, Xin1, Xin2, Xin3, Ytrue0, Ytrue1, Ytrue2, Ytrue3, Ytrue4, Ytrue5, Ytrue6, Ytrue7, Ytrue8, Ytrue9, Ytrue10, Ytrue11 = tup
Xins = [Ximgs, Xin1, Xin2, Xin3] # (imgs, traffic_convection, desire, rnn_state)
Ytrue = np.hstack((Ytrue0, Ytrue1, Ytrue2, Ytrue3, Ytrue4, Ytrue5, Ytrue6, Ytrue7, Ytrue8, Ytrue9, Ytrue10, Ytrue11))
#--- Xins[0].shape = (16, 12, 128, 256)
#--- Ytrue.shape = (16, 2383)
# we need the following two lines, otherwise we always get #--- y_true.shape[0] = None
p = model.predict(x=Xins)
loss = custom_loss(Ytrue, p)
#print('#--- loss =', loss)
yield Xins, Ytrue
def custom_loss(y_true, y_pred):
#--- y_true.shape = (None, None)
#--- y_pred.shape = (None, 2383)
#--- y_true.shape = (16, 2383)
#--- y_pred.shape = (16, 2383)
loss = tf.keras.losses.mse(y_true, y_pred)
return loss
if __name__=="__main__":
start = time.time()
parser = argparse.ArgumentParser(description='Training modelB5')
parser.add_argument('--host', type=str, default="localhost", help='Data server ip address.')
parser.add_argument('--port', type=int, default=5557, help='Port of server.')
parser.add_argument('--port_val', type=int, default=5558, help='Port of server for validation dataset.')
args = parser.parse_args()
# Build model
img_shape = (12, 128, 256)
desire_shape = (8)
traffic_convection_shape = (2)
rnn_state_shape = (512)
num_classes = 6
model = get_model(img_shape, desire_shape, traffic_convection_shape, rnn_state_shape, num_classes)
#model.summary()
filepath = "./saved_model/modelB5-BestWeights.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1,
save_best_only=True, mode='min')
callbacks_list = [checkpoint]
#model.load_weights('./saved_model/modelB5-BestWeights.hdf5', by_name=True)
from tensorflow.keras import backend
def rmse(y_true, y_pred):
return backend.sqrt(backend.mean(backend.square(y_pred - y_true), axis=-1))
adam = tf.keras.optimizers.Adam(lr=0.0001)
model.compile(optimizer=adam, loss=custom_loss, metrics=[rmse, 'mae']) # metrics=custom_loss???
history = model.fit(
get_data(20, args.host, port=args.port, model=model),
steps_per_epoch=1150//BATCH_SIZE, epochs=EPOCHS,
validation_data=get_data(20, args.host, port=args.port_val, model=model),
validation_steps=1150//BATCH_SIZE, verbose=1, callbacks=callbacks_list)
# steps_per_epoch = total images//BATCH_SIZE
model.save('./saved_model/modelB5.h5')
end = time.time()
hours, rem = divmod(end-start, 3600)
minutes, seconds = divmod(rem, 60)
print("Training Time: {:0>2}:{:0>2}:{:05.2f}".format(int(hours),int(minutes),seconds))
plt.subplot(311)
plt.plot(history.history["loss"])
plt.plot(history.history["val_loss"])
train_loss = np.array(history.history['loss'])
np.savetxt("./saved_model/train_loss_out2.txt", train_loss, delimiter=",")
valid_loss = np.array(history.history['val_loss'])
np.savetxt("./saved_model/valid_loss_out2.txt", valid_loss, delimiter=",")
plt.ylabel("loss")
plt.legend(['train', 'validate'], loc='upper right')
plt.subplot(312)
plt.plot(history.history["rmse"])
plt.plot(history.history["val_rmse"])
train_rmse = np.array(history.history['rmse'])
np.savetxt("./saved_model/train_rmse_out2.txt", train_rmse, delimiter=",")
valid_rmse = np.array(history.history['val_rmse'])
np.savetxt("./saved_model/valid_rmse_out2.txt", valid_rmse, delimiter=",")
plt.ylabel("rmse")
plt.legend(['train', 'validate'], loc='upper right')
plt.subplot(313)
plt.plot(history.history["mae"])
plt.plot(history.history["val_mae"])
train_mae = np.array(history.history['mae'])
np.savetxt("./saved_model/train_mae_out2.txt", train_mae, delimiter=",")
valid_mae = np.array(history.history['val_mae'])
np.savetxt("./saved_model/valid_mae_out2.txt", valid_mae, delimiter=",")
plt.ylabel("mae")
plt.xlabel("epoch")
plt.legend(['train', 'validate'], loc='upper right')
plt.draw()
#plt.savefig('./saved_model/modelB5_out2.png')
plt.pause(0.5)
input("Press ENTER to close ...")
plt.close()