This repository was archived by the owner on Sep 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_samples.py
More file actions
150 lines (120 loc) · 4.11 KB
/
Copy pathtest_samples.py
File metadata and controls
150 lines (120 loc) · 4.11 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python
import argparse
import enum
import os
import torch
from datetime import datetime
from ultralytics import YOLO
# Set CUDA Allocation to 512MB
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:512"
# Run script from current working directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))
class ModelSizes(str, enum.Enum):
nano = "nano"
small = "small"
medium = "medium"
large = "large"
extralarge = "extralarge"
debug = False
epoch = 0
last_epoch = 0
last_run = None
def main(arg):
global epoch
global last_epoch
global last_run
# Fetch code size from args
code = arg["size"][0]
# Fix code used for naming
if code == "e":
code = "x"
# Make sure file exists
if os.path.isfile("fbc-ml-models/fbc-seg-{}/results.csv".format(code)):
with open(
"fbc-ml-models/fbc-seg-{}/results.csv".format(code),
"r",
encoding="utf-8",
errors="ignore",
) as scraped:
final_line = scraped.readlines()[-1]
epoch = int(final_line.split(",")[0]) + 1
if epoch > last_epoch:
last_epoch = epoch
if epoch % 10 == 0:
current_time = datetime.now()
if last_run is not None and debug is True:
time_elapsed = current_time - last_run
last_run = current_time
print("› Next Run ETA: {}".format(time_elapsed))
if not os.path.exists(
"predictions/fbc-seg-{}-e{}".format(code, last_epoch)
):
if debug is True:
print(
"› Generating Samples from Epoch #{} {}".format(
epoch, current_time.strftime("%Y-%m-%d %H:%M:%S")
)
)
# Run Predictions
predict(vars(parser.parse_args()))
def predict(arg):
global epoch
# Create Timer
if debug is True:
start_time = datetime.now()
print(
"› Starting Predictions: {}".format(
start_time.strftime("%Y-%m-%d %H:%M:%S")
)
)
# Create Timer
if torch.cuda.is_available():
device = "0"
else:
device = "cpu"
# Load the last model from previous training session
model = YOLO("fbc-ml-models/fbc-seg-{}/weights/last.pt".format(code))
# Define Args for both YOLO and ClearML
args = dict(
device=device,
exist_ok=True,
imgsz=640,
name="fbc-seg-{}-e{}".format(code, last_epoch),
project="predictions",
verbose=True,
save=True,
save_txt=False, # Save masks as .txt file
save_conf=False, # save results with confidence scores
save_crop=False, # save cropped images with results
retina_masks=True,
conf=0.01,
)
# Perform object detection on an image using the newly trained model
model.predict("samples", **args)
# Output Run Time
if debug is True:
end_time = datetime.now()
time_elapsed = end_time - start_time
print("\n› Completed: {}".format(end_time.strftime("%Y-%m-%d %H:%M:%S")))
print("› Total Time: {}\n".format(time_elapsed))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Run YOLOv8 Model Predictions",
epilog="Find By Color - OID Segmentation Model",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("size", type=ModelSizes)
try:
if debug is True:
start_time = datetime.now()
print(
"› Starting Test Sample Generation: {}".format(
start_time.strftime("%Y-%m-%d %H:%M:%S")
)
)
print("› Process will run every 5 Epochs\n")
main(vars(parser.parse_args()))
except KeyboardInterrupt:
if debug is True:
print("Exited Application")
exit(0)