-
Notifications
You must be signed in to change notification settings - Fork 39
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
main_2024.py detect target #156
Changes from 2 commits
2dbe3c9
10c489e
bfa1978
ad9ac08
f23f62d
dd6e5a1
3ee9905
d7ca078
8819c3c
d08e556
bd2a59d
a49c660
20e4018
0da5e01
37a37bc
cb186f4
724de57
5e6c8e7
0133d97
479a361
864bcaa
0b2f4b7
ba05393
108c7c6
ca549ba
0ab48c9
435992c
73a28a8
69be32a
5117781
dea615a
3080db0
6b00c7e
ef80b03
68fab82
f8359ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert to when you were displaying the image in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Amy mentioned above that this causes issues. Is there a work around? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it work on you end? If it does then maybe I missed something in the set up? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you try adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Oh this works! @Ethan118 you can do this instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove line 7 import (no longer being used). |
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,16 +6,22 @@ | |
import cv2 | ||
import numpy as np | ||
import ultralytics | ||
import pathlib | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
# Downloaded from: https://github.com/ultralytics/assets/releases | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MODEL_PATH = "tests/model_example/yolov8s_ultralytics_pretrained_default.pt" | ||
MODEL_PATH = pathlib.Path("tests/model_example/yolov8s_ultralytics_pretrained_default.pt") | ||
|
||
BUS_IMAGE_PATH = pathlib.Path("tests/model_example/bus.jpg") | ||
ZIDANE_IMAGE_PATH = pathlib.Path("tests/model_example/zidane.jpg") | ||
|
||
SAVE_PATH = pathlib.Path("tests/model_example") | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
if __name__ == "__main__": | ||
model = ultralytics.YOLO(MODEL_PATH) | ||
image_bus = cv2.imread("tests/model_example/bus.jpg") | ||
image_zidane = cv2.imread("tests/model_example/zidane.jpg") | ||
image_bus = cv2.imread(BUS_IMAGE_PATH) | ||
image_zidane = cv2.imread(ZIDANE_IMAGE_PATH) | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# ultralytics saves as .jpg , bad for testing reproducibility | ||
results_bus = model.predict( | ||
|
@@ -35,15 +41,15 @@ | |
image_zidane_annotated = results_zidane[0].plot(conf=True) | ||
|
||
# Generate expected | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bus_expected = results_bus[0].boxes.xyxy.detach().cpu().numpy() | ||
zidane_expected = results_zidane[0].boxes.xyxy.detach().cpu().numpy() | ||
bounding_box_bus = results_bus[0].boxes.xyxy.detach().cpu().numpy() | ||
bounding_box_zidane = results_zidane[0].boxes.xyxy.detach().cpu().numpy() | ||
|
||
# Save image | ||
cv2.imwrite("tests/model_example/bus_annotated.png", image_bus_annotated) | ||
cv2.imwrite("tests/model_example/zidane_annotated.png", image_zidane_annotated) | ||
cv2.imwrite(f"{SAVE_PATH}/bus_annotated.png", image_bus_annotated) | ||
cv2.imwrite(f"{SAVE_PATH}/zidane_annotated.png", image_zidane_annotated) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary space characters. |
||
# Save expected to text file | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
np.savetxt("tests/model_example/bus_expected.txt", bus_expected) | ||
np.savetxt("tests/model_example/zidane_expected.txt", zidane_expected) | ||
np.savetxt(f"{SAVE_PATH}/bounding_box_bus.txt", bounding_box_bus) | ||
np.savetxt(f"{SAVE_PATH}/bounding_box_zidane.txt", bounding_box_zidane) | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
print("Done!") |
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
import numpy as np | ||
import pytest | ||
import torch | ||
import ultralytics | ||
|
||
from modules.detect_target import detect_target | ||
from modules import image_and_time | ||
|
@@ -19,12 +18,10 @@ | |
OVERRIDE_FULL = False # Tests are able to handle both full and half precision. | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
IMAGE_BUS_PATH = "tests/model_example/bus.jpg" | ||
IMAGE_BUS_ANNOTATED_PATH = "tests/model_example/bus_annotated.png" | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BOUNDING_BOX_BUS_PATH = "tests/model_example/bounding_box_bus.txt" | ||
IMAGE_ZIDANE_PATH = "tests/model_example/zidane.jpg" | ||
IMAGE_ZIDANE_ANNOTATED_PATH = "tests/model_example/zidane_annotated.png" | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
model = ultralytics.YOLO(MODEL_PATH) | ||
expected_bus = np.loadtxt("tests/model_example/bus_expected.txt") | ||
expected_zidane = np.loadtxt("tests/model_example/zidane_expected.txt") | ||
BOUNDING_BOX_ZIDANE_PATH = "tests/model_example/bounding_box_zidane.txt" | ||
|
||
@pytest.fixture() | ||
def detector(): | ||
|
@@ -58,85 +55,83 @@ def image_zidane(): | |
assert zidane_image is not None | ||
yield zidane_image | ||
|
||
@pytest.fixture() | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def expected_bus(): | ||
""" | ||
Load expected bus image. | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
expected_bus = np.loadtxt(BOUNDING_BOX_BUS_PATH) | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename this to something that is not the function name (e.g. |
||
assert expected_bus is not None | ||
yield expected_bus | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def rmse(actual: np.ndarray, | ||
expected: np.ndarray) -> float: | ||
""" | ||
Helper function to compute root mean squared error. | ||
""" | ||
mean_squared_error = np.square(actual - expected).mean() | ||
|
||
return np.sqrt(mean_squared_error) | ||
|
||
|
||
def test_rmse(): | ||
""" | ||
Root mean squared error. | ||
""" | ||
# Setup | ||
sample_actual = np.array([1, 2, 3, 4, 5]) | ||
sample_expected = np.array([1.6, 2.5, 2.9, 3, 4.1]) | ||
EXPECTED_ERROR = np.sqrt(0.486) | ||
|
||
# Run | ||
actual_error = rmse(sample_actual, sample_expected) | ||
|
||
# Test | ||
np.testing.assert_almost_equal(actual_error, EXPECTED_ERROR) | ||
|
||
@pytest.fixture() | ||
def expected_zidane(): | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
""" | ||
Load expected Zidane image. | ||
""" | ||
expected_zidane = np.loadtxt(BOUNDING_BOX_ZIDANE_PATH) | ||
assert expected_zidane is not None | ||
yield expected_zidane | ||
|
||
class TestDetector: | ||
""" | ||
Tests `DetectTarget.run()` . | ||
""" | ||
|
||
__IMAGE_DIFFERENCE_TOLERANCE = 1 | ||
__BOUNDING_BOX_TOLERANCE = 1e-7 | ||
|
||
def test_single_bus_image(self, | ||
detector: detect_target.DetectTarget, | ||
image_bus: image_and_time.ImageAndTime): | ||
image_bus: image_and_time.ImageAndTime, | ||
expected_bus: np.ndarray): | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Bus image. | ||
""" | ||
# Run | ||
result, actual = detector.run(image_bus) | ||
detections = actual.detections | ||
|
||
# Test | ||
assert result | ||
assert actual is not None | ||
assert detections is not None | ||
|
||
detections = actual.detections | ||
|
||
error = 0 | ||
assert len(detections) == expected_bus.shape[0] | ||
|
||
for i in range(0, len(detections)): | ||
error += rmse([detections[i].x1, detections[i].y1, detections[i].x2, detections[i].y2], expected_bus[i]) | ||
assert (error / len(detections)) < self.__IMAGE_DIFFERENCE_TOLERANCE | ||
errors = np.full((1, expected_bus.shape[1]), 1) | ||
|
||
for i in range(0, expected_bus.shape[0]): | ||
errors = np.abs(np.array([detections[i].x1, detections[i].y1, detections[i].x2, detections[i].y2]) - expected_bus[i]) | ||
assert all(e < self.__BOUNDING_BOX_TOLERANCE for e in errors) | ||
|
||
def test_single_zidane_image(self, | ||
detector: detect_target.DetectTarget, | ||
image_zidane: image_and_time.ImageAndTime): | ||
image_zidane: image_and_time.ImageAndTime, | ||
expected_zidane: np.ndarray): | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Zidane image. | ||
""" | ||
# Run | ||
result, actual = detector.run(image_zidane) | ||
detections = actual.detections | ||
|
||
# Test | ||
assert result | ||
assert actual is not None | ||
assert detections is not None | ||
|
||
error = 0 | ||
detections = actual.detections | ||
|
||
assert len(detections) == expected_zidane.shape[0] | ||
|
||
for i in range(0, len(detections)): | ||
error += rmse([detections[i].x1, detections[i].y1, detections[i].x2, detections[i].y2], expected_zidane[i]) | ||
assert (error / len(detections)) < self.__IMAGE_DIFFERENCE_TOLERANCE | ||
errors = np.full((1, expected_zidane.shape[0]), 1) | ||
|
||
for i in range(0, expected_zidane.shape[0]): | ||
errors = np.abs(np.array([detections[i].x1, detections[i].y1, detections[i].x2, detections[i].y2]) - expected_zidane[i]) | ||
assert all(e < self.__BOUNDING_BOX_TOLERANCE for e in errors) | ||
|
||
def test_multiple_zidane_image(self, | ||
detector: detect_target.DetectTarget, | ||
image_zidane: image_and_time.ImageAndTime): | ||
image_zidane: image_and_time.ImageAndTime, | ||
expected_zidane: np.ndarray): | ||
Xierumeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Multiple Zidane images. | ||
""" | ||
|
@@ -157,15 +152,16 @@ def test_multiple_zidane_image(self, | |
for i in range(0, IMAGE_COUNT): | ||
output: "tuple[bool, detections_and_time.DetectionsAndTime | None]" = outputs[i] | ||
result, actual = output | ||
|
||
detections = actual.detections | ||
|
||
assert result | ||
assert actual is not None | ||
assert detections is not None | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unnecessary space characters. |
||
detections = actual.detections | ||
|
||
assert len(detections) == expected_zidane.shape[0] | ||
|
||
error = 0 | ||
errors = np.full((1, expected_zidane.shape[0]), 1) | ||
|
||
for i in range(0, len(detections)): | ||
error += rmse([detections[i].x1, detections[i].y1, detections[i].x2, detections[i].y2], expected_zidane[i]) | ||
assert (error / len(detections)) < self.__IMAGE_DIFFERENCE_TOLERANCE | ||
for i in range(0, expected_zidane.shape[0]): | ||
errors = np.abs(np.array([detections[i].x1, detections[i].y1, detections[i].x2, detections[i].y2]) - expected_zidane[i]) | ||
assert all(e < self.__BOUNDING_BOX_TOLERANCE for e in errors) |
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.
Remove line 12 import (no longer being used).