-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
62 lines (49 loc) · 1.38 KB
/
Copy pathtrain.py
File metadata and controls
62 lines (49 loc) · 1.38 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
import tensorflow as tf
from dataset import load_dataset
from model import build_model
def iou_metric(y_true, y_pred):
# y_true, y_pred shape: (batch, 4) in [0,1]
xA = tf.maximum(y_true[:, 0], y_pred[:, 0])
yA = tf.maximum(y_true[:, 1], y_pred[:, 1])
xB = tf.minimum(y_true[:, 2], y_pred[:, 2])
yB = tf.minimum(y_true[:, 3], y_pred[:, 3])
inter = tf.maximum(0.0, xB - xA) * tf.maximum(0.0, yB - yA)
area_true = (y_true[:, 2] - y_true[:, 0]) * (y_true[:, 3] - y_true[:, 1])
area_pred = (y_pred[:, 2] - y_pred[:, 0]) * (y_pred[:, 3] - y_pred[:, 1])
union = area_true + area_pred - inter + 1e-6
return tf.reduce_mean(inter / union)
# Load datasets
train_ds = load_dataset(
"data/annotations/train_annotations.csv",
"data/images/train",
batch_size=16
)
val_ds = load_dataset(
"data/annotations/test_annotations.csv",
"data/images/test",
batch_size=16,
shuffle=False
)
# Build model
model = build_model()
# Compile
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),
loss={
"bbox": tf.keras.losses.Huber(),
"class": tf.keras.losses.SparseCategoricalCrossentropy()
},
metrics={
"class": ["accuracy"],
"bbox": [iou_metric]
}
)
model.summary()
# Train
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=10
)
# Save model
model.save("fallguard_model")