Repository navigation
Expand file tree
/
Copy pathfire_training.py
More file actions
101 lines (76 loc) · 2.82 KB
/
Copy pathfire_training.py
File metadata and controls
101 lines (76 loc) · 2.82 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
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
# -*- coding: utf-8 -*-
"""fire_training.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1ksGLb8dlzktSeaxVqZqYEASDIYXrzTTE
"""
from google.colab import drive
drive.mount('/content/drive')
!pip install ultralytics -q
import torch
print("CUDA available:", torch.cuda.is_available())
print("Device:", torch.cuda.get_device_name(0) if torch.cuda.is_available() else "CPU")
yaml_path = "/content/drive/MyDrive/YOLOv8-Fire-and-Smoke-Detection/datasets/fire-8/data.yaml"
new_yaml = """path: /content/drive/MyDrive/YOLOv8-Fire-and-Smoke-Detection/datasets/fire-8
train: train/images
val: valid/images
test: test/images
nc: 2
names: ['fire', 'smoke']
"""
with open(yaml_path, "w") as f:
f.write(new_yaml)
print("yaml updated")
from ultralytics import YOLO
model = YOLO("yolov8n.pt") # downloads automatically
results = model.train(
data="/content/drive/MyDrive/YOLOv8-Fire-and-Smoke-Detection/datasets/fire-8/data.yaml",
epochs=50,
imgsz=640,
batch=16,
device=0, # use GPU
project="/content/drive/MyDrive/fire_training", # save to Drive so it persists
name="run1",
)
from ultralytics import YOLO
model = YOLO("/content/drive/MyDrive/fire_training/run1/weights/best.pt")
metrics = model.val(
data="/content/drive/MyDrive/YOLOv8-Fire-and-Smoke-Detection/datasets/fire-8/data.yaml",
split="test",
imgsz=640,
)
print(f"\n=== Trained FP32 Baseline ===")
print(f"mAP@0.5: {metrics.box.map50:.4f}")
print(f"mAP@0.5:0.95: {metrics.box.map:.4f}")
print(f"Precision: {metrics.box.mp:.4f}")
print(f"Recall: {metrics.box.mr:.4f}")
import torch
print("CUDA available:", torch.cuda.is_available())
print("Device:", torch.cuda.get_device_name(0) if torch.cuda.is_available() else "CPU")
from google.colab import drive
drive.mount('/content/drive')
!pip install ultralytics -q
from ultralytics import YOLO
model = YOLO("/content/drive/MyDrive/fire_training/run1/weights/best.pt")
model.export(format="engine", imgsz=640, half=True) # FP16 by default
import time
import glob
import numpy as np
from ultralytics import YOLO
model_trt = YOLO("/content/drive/MyDrive/fire_training/run1/weights/best.engine")
image_paths = sorted(glob.glob("/content/drive/MyDrive/YOLOv8-Fire-and-Smoke-Detection/datasets/fire-8/test/images/*.jpg"))
print(f"Found {len(image_paths)} test images")
# Warm-up
for _ in range(10):
model_trt.predict(image_paths[0], verbose=False)
# Time
latencies = []
for path in image_paths:
start = time.perf_counter()
model_trt.predict(path, verbose=False)
latencies.append((time.perf_counter() - start) * 1000)
latencies = np.array(latencies)
print(f"\nTensorRT FP16 Latency (T4 GPU)")
print(f"Mean: {latencies.mean():.2f} ms")
print(f"Median: {np.median(latencies):.2f} ms")
print(f"P95: {np.percentile(latencies, 95):.2f} ms")