-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactive_crash_detection.yaml
More file actions
76 lines (56 loc) · 2.57 KB
/
active_crash_detection.yaml
File metadata and controls
76 lines (56 loc) · 2.57 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
72
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, models
def build_crash_detector(input_shape):
"""
Builds a Bi-Directional LSTM for detecting high-impact anomalies.
Input Shape: (window_size, features) - e.g., (50, 6) for 50Hz data
Features: acc_x, acc_y, acc_z, gyro_x, gyro_y, gyro_z
"""
model = models.Sequential([
# Bi-directional layer to capture pre-impact and impact context
layers.Bidirectional(layers.LSTM(64, return_sequences=True), input_shape=input_shape),
layers.Dropout(0.2),
layers.Bidirectional(layers.LSTM(32)),
layers.Dense(32, activation='relu'),
# Output: Probability of a crash (0 to 1)
layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy', tf.keras.metrics.Precision(), tf.keras.metrics.Recall()])
return model
# Example dimensions: 1 second of data at 50Hz with 6 sensor axes
input_dim = (50, 6)
crash_ai = build_crash_detector(input_dim)
crash_ai.summary()
import tensorflow as tf
from tensorflow.keras import layers, Model
def build_advanced_crash_ai(imu_shape=(100, 6), gps_shape=(10, 2)):
"""
Advanced Multimodal Fusion Model
- IMU: 100 samples (2 seconds @ 50Hz) of Accel(3) + Gyro(3)
- GPS: 10 samples of Speed + Heading
"""
# 1. IMU Branch (Temporal Analysis)
imu_input = layers.Input(shape=imu_shape, name="imu_data")
x = layers.Bidirectional(layers.LSTM(64, return_sequences=True))(imu_input)
# Self-Attention Layer to pinpoint the impact moment
query = layers.Dense(128)(x)
value = layers.Dense(128)(x)
attention = layers.Attention()([query, value])
imu_feat = layers.GlobalAveragePooling1D()(attention)
# 2. GPS Branch (Contextual Analysis)
gps_input = layers.Input(shape=gps_shape, name="gps_data")
gps_feat = layers.Flatten()(gps_input)
gps_feat = layers.Dense(16, activation='relu')(gps_feat)
# 3. Fusion Layer
combined = layers.Concatenate()([imu_feat, gps_feat])
z = layers.Dense(64, activation='swish')(combined) # Swish is smoother for gradients
z = layers.Dropout(0.3)(z)
# Output: 0 (Normal), 1 (Minor Impact), 2 (Severe Crash)
output = layers.Dense(3, activation='softmax', name="crash_severity")(z)
model = Model(inputs=[imu_input, gps_input], outputs=output)
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
return model
crash_ai = build_advanced_crash_ai()