This repository was archived by the owner on Sep 16, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathpredict.py
More file actions
109 lines (91 loc) · 2.85 KB
/
Copy pathpredict.py
File metadata and controls
109 lines (91 loc) · 2.85 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
#!/usr/bin/python
import argparse
import enum
import os
import pathlib
import torch
from datetime import datetime
from ultralytics import YOLO
from src.utils import range_confidence
class ModelSizes(str, enum.Enum):
nano = "nano"
small = "small"
medium = "medium"
large = "large"
extralarge = "extralarge"
def main(arg):
# Make sure image exists before processing
if not os.path.exists(arg["image"].resolve()):
return print("❌ Unable to locate file: {}".format(arg["image"].resolve()))
# Create Timer
start_time = datetime.now()
print("› Starting Prediction: {}".format(start_time.strftime("%Y-%m-%d %H:%M:%S")))
# Fetch code size from args
code = arg["size"][0]
# Fix code used for naming
if code == "e":
code = "x"
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))
with open(
"fbc-ml-models/fbc-seg-{}/results.csv".format(code),
"r",
encoding="utf-8",
errors="ignore",
) as scraped:
final_line = scraped.readlines()[-1]
last_epoch = int(final_line.split(",")[0]) + 1
# 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=True, # Save masks as .txt file
save_conf=True, # save results with confidence scores
save_crop=True, # save cropped images with results
retina_masks=True,
conf=arg["confidence"],
)
# Perform object detection on an image using the newly trained model
model.predict(arg["image"].resolve(), **args)
# Output Run Time
end_time = datetime.now()
time_elapsed = end_time - start_time
print("› Completed: {}".format(end_time.strftime("%Y-%m-%d %H:%M:%S")))
print("› Total Time: {}".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)
parser.add_argument(
"-i",
"--image",
type=pathlib.Path,
metavar="\b",
default="samples",
help="Absolute Image Path",
)
parser.add_argument(
"-c",
"--confidence",
type=range_confidence(0, 1),
metavar="\b",
default=0.01,
help="Prediction Confidence [0-1]",
)
try:
main(vars(parser.parse_args()))
except KeyboardInterrupt:
print("Exited Application")
exit(0)