-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_yolo_v2.py
83 lines (67 loc) · 2.23 KB
/
test_yolo_v2.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
# Standard library imports
import os
import time
import warnings
import ultralytics
os.environ.update(os.environ)
os.environ["RAMP_HOME"] = os.getcwd()
# Reader imports
from hot_fair_utilities import polygonize, predict, preprocess
from hot_fair_utilities.preprocessing.yolo_v8_v2.yolo_format import yolo_format
from hot_fair_utilities.training.yolo_v8_v2.train import train as train_yolo
warnings.simplefilter(action="ignore", category=FutureWarning)
class print_time:
def __init__(self, name):
self.name = name
def __enter__(self):
self.start = time.perf_counter()
return self
def __exit__(self, type, value, traceback):
print(f"{self.name} took {round(time.perf_counter() - self.start, 2)} seconds")
start_time = time.time()
base_path = f"{os.getcwd()}/ramp-data/sample_2"
model_input_image_path = f"{base_path}/input"
preprocess_output = f"{base_path}/preprocessed"
with print_time("preprocessing"):
preprocess(
input_path=model_input_image_path,
output_path=preprocess_output,
rasterize=True,
rasterize_options=["binary"],
georeference_images=True,
multimasks=False,
epsg=4326
)
yolo_data_dir = f"{base_path}/yolo_v2"
with print_time("yolo conversion"):
yolo_format(
input_path=preprocess_output,
output_path=yolo_data_dir,
)
output_model_path,output_model_iou_accuracy = train_yolo(
data=f"{base_path}",
weights=f"{os.getcwd()}/yolov8s_v2-seg.pt",
# gpu="cpu",
epochs=2,
batch_size=16,
pc=2.0,
output_path=yolo_data_dir,
dataset_yaml_path=os.path.join(yolo_data_dir,'yolo_dataset.yaml')
)
print(output_model_iou_accuracy)
prediction_output = f"{base_path}/prediction/output"
# model_path = f"{output_path}/weights/best.pt"
with print_time("inference"):
predict(
checkpoint_path=output_model_path,
input_path=f"{base_path}/prediction/input",
prediction_path=prediction_output,
)
geojson_output = f"{prediction_output}/prediction.geojson"
with print_time("polygonization"):
polygonize(
input_path=prediction_output,
output_path=geojson_output,
remove_inputs=False,
)
print(f"\n Total Process Completed in : {time.time()-start_time} sec")