This repository was archived by the owner on Nov 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathprofiler_detect_target_2024.py
More file actions
185 lines (124 loc) · 4.79 KB
/
profiler_detect_target_2024.py
File metadata and controls
185 lines (124 loc) · 4.79 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
Profile detect target using full/half precision.
"""
import multiprocessing as mp
import time
import gc
import pathlib
import yaml
import argparse
import cv2
import numpy as np
import os
import pandas as pd
from modules.detect_target import detect_target
from modules.image_and_time import ImageAndTime
CONFIG_FILE_PATH = pathlib.Path("config.yaml")
GRASS_DATA_DIR = "profiler/profile_data/Grass"
ASPHALT_DATA_DIR = "profiler/profile_data/Asphalt"
FIELD_DATA_DIR = "profiler/profile_data/Field"
MS_TO_NS_CONV = 1000000
def load_images(dir):
images = []
for filename in os.listdir(dir):
if filename.endswith(".png"):
img = cv2.imread(os.path.join(dir, filename))
if img is not None:
success, image_with_time = ImageAndTime.create(img)
if success:
images.append(image_with_time)
return images
def profile_detector(detector: detect_target.DetectTarget, images: "list[np.ndarray]") -> dict:
times_arr = []
for image in images:
gc.disable() # This disables the garbage collector
start = time.time_ns()
result, value = detector.run(image) # Might or might not want to keep the bounding boxes
end = time.time_ns()
gc.enable() # This enables the garbage collector
if not result:
pass
# Handle error
else:
times_arr.append(end - start)
if len(times_arr) > 0:
average = np.nanmean(times_arr) / MS_TO_NS_CONV
mins = np.nanmin(times_arr) /MS_TO_NS_CONV
maxs = np.nanmax(times_arr) / MS_TO_NS_CONV
median = np.median(times_arr) /MS_TO_NS_CONV
else:
average, mins, maxs, median = -1,-1,-1,-1
data = {
"Average (ms)": average,
"Min (ms)": mins,
"Max (ms)": maxs,
"Median (ms)": median
}
# Create and prints DF
return data
def run_detector(detector_full: detect_target.DetectTarget, detector_half: detect_target.DetectTarget, images: "list[np.ndarray]") -> pd.DataFrame:
# Initial run just to warm up CUDA
_ = profile_detector(detector_full, images[:10])
half_data = profile_detector(detector_half, images)
full_data = profile_detector(detector_full, images)
full_df = pd.DataFrame(full_data, index=['full'])
half_df = pd.DataFrame(half_data, index=['half'])
return pd.concat([half_df, full_df])
def main() -> int:
#Configurations
try:
with CONFIG_FILE_PATH.open("r", encoding="utf8") as file:
try:
config = yaml.safe_load(file)
except yaml.YAMLError as exc:
print(f"Error parsing YAML file: {exc}")
return -1
except FileNotFoundError:
print(f"File not found: {CONFIG_FILE_PATH}")
return -1
except IOError as exc:
print(f"Error when opening file: {exc}")
return -1
parser = argparse.ArgumentParser()
parser.add_argument("--cpu", action="store_true", help="option to force cpu")
args = parser.parse_args()
DETECT_TARGET_MODEL_PATH = config["detect_target"]["model_path"]
DETECT_TARGET_DEVICE = "cpu" if args.cpu else config["detect_target"]["device"]
#Optional logging parameters
LOG_DIRECTORY_PATH = config["log_directory_path"]
DETECT_TARGET_SAVE_NAME_PREFIX = config["detect_target"]["save_prefix"]
DETECT_TARGET_SAVE_PREFIX = f"{LOG_DIRECTORY_PATH}/{DETECT_TARGET_SAVE_NAME_PREFIX}"
#Creating detector instances
detector_half = detect_target.DetectTarget(
DETECT_TARGET_DEVICE,
DETECT_TARGET_MODEL_PATH,
False,
"" #not logging imgs
)
detector_full = detect_target.DetectTarget(
DETECT_TARGET_DEVICE,
DETECT_TARGET_MODEL_PATH,
True, #forces full precision
"" #not logging imgs
)
#Loading images
grass_images = load_images(GRASS_DATA_DIR)
asphalt_images = load_images(ASPHALT_DATA_DIR)
field_images = load_images(FIELD_DATA_DIR)
#Running detector
grass_results = run_detector(detector_full, detector_half, grass_images)
asphalt_results = run_detector(detector_full, detector_half, asphalt_images)
field_results = run_detector(detector_full, detector_half, field_images)
#Printing results to console
print("=================GRASS==================")
print(grass_results)
print("=================ASPHALT==================")
print(asphalt_results)
print("=================FIELD==================")
print(field_results)
#save to csvs
grass_results.to_csv(f"profiler/profile_data/results/results_grass.csv")
asphalt_results.to_csv(f"profiler/profile_data/results/results_asphalt.csv")
field_results.to_csv(f"profiler/profile_data/results/results_field.csv")
if __name__ == "__main__":
main()