-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect target precision profile #167
Draft
KarthiU
wants to merge
23
commits into
main
Choose a base branch
from
detect_target_precision_profile
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
90a0aa7
Initialized file with bootcamp code
SubwayMan 306a928
Merge branch 'main' into decision_command_struct
SubwayMan d7e87e8
Renamed and modified functions to match list from asana, adjusted data
SubwayMan 342fd65
added z axis to command struct
SubwayMan a17d917
changed documentation and docstrings
SubwayMan fa95d05
Merge branch 'main' into decision_command_struct
SubwayMan aba88a9
Renamed class to DecisionCommand
SubwayMan 0fd0635
added relative landing command
SubwayMan 1516e26
modified docstrings and command parameter names
SubwayMan 30bfaf9
Updated all coordinate command descriptions with NED
SubwayMan 7e92cb4
PR fixes: fixed argument indentation and corrected small docstring mi…
SubwayMan 2158f7b
removed extraneous newline
SubwayMan 6de6668
Merge branch 'main' into decision_command_struct
SubwayMan ad8b210
Single image profiling code
SubwayMan b9009b2
moved profiling
SubwayMan 1b163f9
removed worker
SubwayMan 749d8d6
added profiling functionality
KarthiU 0f7373c
Merge branch 'detect_target_precision_profile' of https://github.com/…
KarthiU dd7cd28
pulled and renamed file(s)
KarthiU 6c6389c
Integrate data merge worker (#166)
DylanFinlay 4ccf36e
fixed profiler
KarthiU c22952f
bug fixes
KarthiU 96d3da4
removed imgs
KarthiU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
""" | ||
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() | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to add logic within the
detect_target.py
class for profiling. Rather we could time the worker outside of the call to detect target.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The worker isn't the thing to test, something like this: