
states = db['Status'].value_counts()
ts_status = db.Status
ts_diff_Ax = (db.Acc_x.to_numpy().reshape(-1,1))/9.81
ts_diff_Ay = (db.Acc_y.to_numpy().reshape(-1,1))/9.81
ts_diff_Az = (db.Acc_z.to_numpy().reshape(-1,1))/9.81
ts_time = db['Time']
Use a simple script to reshape the dataset to be compliant with the shape of the LSTM input vector (50 samples
of a three-dimensional vector). The number of LSTM input vectors is computed from the total number of samples
acquired in all the states, choosing window waveforms of 50 samples.
If for the parking state we have 350 samples, 350/50 = 7 window waveforms are generated.
TIMESERIES_LEN = 50
Y_LABELS={'P':0,'N':1,'B':2,'S':3}
def trip_framing(trip,label,frame_size,db_x,db_y):
a = np.array( trip )
for i in np.arange( 0, a.shape[0]-frame_size, frame_size ):
x = a[i:i+frame_size]
db_x.append( x )
db_y.append( Y_LABELS[label] )
rows = ts_status.shape[0]
db_x = []
db_y = []
for states_id in states.keys():
trip = []
cnt = 0
for i in range(rows):
if ts_time[i] == 100:
if len(trip) > 0:
trip_framing( trip, states_id, TIMESERIES_LEN, db_x, db_y)
trip=[]
if ts_status[i] == states_id and cnt < 7500:
trip.append( [ts_diff_Ax[i],ts_diff_Ay[i],ts_diff_Az[i]] )
cnt += 1
Note:
The cnt variable contains the number of available window waveforms according to the dataset acquired during
each specific car status.
2.4.3
Model fitting and compilation
Firstly, for model fitting, select the training configuration:
•
Training test percentage:
–
Percentage of the window waveform available over the total, used to perform the training task.
•
Batch size:
–
Percentage of the window waveform available over the total, used to perform the estimation of the
network accuracy (model error).
•
Number of epochs:
–
Number of loops through the training dataset.
x_train, x_test, y_train, y_test = train_test_split(db_x, db_y, test_size=0.4,
random_state=21,stratify=db_y)
x_train = np.asarray(x_train)[:,:,:,0]
x_test = np.asarray(x_test)[:,:,:,0]
y_train = np.asarray( y_train )
y_test = np.asarray( y_test )
db_stats = pd.Series( y_test )
To train the model, an algorithm is required to minimize the accumulated errors in identifying the correct car state.
This algorithm consists of a loss function to be passed to the
model.compile
API to generate the complete
runnable code.
The chosen loss function is the
sparse categorical cross-entropy
able to correlate the state label (for
example, “P” with the neural network prediction).
UM3053
AI-car sensing node life cycle
UM3053
-
Rev 1
page 11/39