-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_yolo.py
89 lines (71 loc) · 2.25 KB
/
test_yolo.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
84
85
86
87
88
89
# Standard library imports
import os
import time
import warnings
print(os.getcwd())
os.environ.update(os.environ)
os.environ["RAMP_HOME"] = os.getcwd()
print(os.environ["RAMP_HOME"])
# Third party imports
import tensorflow as tf
# Reader imports
from hot_fair_utilities import polygonize, predict, preprocess
from hot_fair_utilities.preprocessing.yolo_format import yolo_format
from train_yolo 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")
# os.environ.update(os.environ)
# os.environ["RAMP_HOME"] = os.getcwd()
# print(os.environ["RAMP_HOME"])
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=True, # new arg
)
yolo_data_dir = f"{base_path}/yolo"
with print_time("yolo conversion"):
yolo_format(
preprocessed_dirs=preprocess_output,
yolo_dir=yolo_data_dir,
multimask=True,
p_val=0.05,
)
output_model_path = train_yolo(
data=f"{base_path}",
weights=f"{os.getcwd()}/ylov8_seg_alb_best.pt",
gpu="cpu",
epochs=2,
batch_size=16,
pc=2.0,
)
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")