-
Notifications
You must be signed in to change notification settings - Fork 0
Make yolo labels optional #87
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
Open
zhong-al
wants to merge
14
commits into
main
Choose a base branch
from
bug/yolo
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.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c7ce804
Replace hardcoding
zhong-al 711f89d
Update label logic
zhong-al da39c68
Add tests for yolo
zhong-al b336623
Add extra test
zhong-al e17683b
Resolve naming conflict
zhong-al 3db7046
Update path
zhong-al d941d8f
Update detector2cvat to have new yolo params
zhong-al 5c875ee
Test yolo
zhong-al 117de5a
Fix typo
zhong-al f0cd4cd
Update readme
zhong-al 6bc6888
Add tests
zhong-al e115555
Merge remote-tracking branch 'origin/main' into bug/yolo
EmersonFras 33ee8df
Add baboon to yolo_labels
EmersonFras 86ce121
Revert YOLO labels by removing 'baboon'R
EmersonFras 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 hidden or 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 |
|---|---|---|
|
|
@@ -184,6 +184,7 @@ cython_debug/ | |
| *.jpg | ||
| *.yaml | ||
| !.github/workflows/*.yaml | ||
| !ethogram/*.json | ||
| *.csv | ||
| *.txt | ||
|
|
||
|
|
||
This file contains hidden or 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 @@ | ||
| {"horse": "zebra"} |
This file contains hidden or 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 @@ | ||
| ["zebra", "horse", "giraffe"] |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,139 @@ | ||
| import unittest | ||
| from unittest.mock import MagicMock, patch | ||
| from collections import OrderedDict | ||
| import numpy as np | ||
| import torch | ||
| from ultralytics import YOLO | ||
| from kabr_tools.utils.yolo import YOLOv8 | ||
|
|
||
| # from yolov8x.pt | ||
| LABELS = {"zebra": 22, "horse": 17, "giraffe": 23, "bear": 21} | ||
|
|
||
| def rescale(box, width, height): | ||
| return [box[0] * width, box[1] * height, box[2] * width, box[3] * height] | ||
|
|
||
|
|
||
| class MockBox: | ||
| def __init__(self, box=[[0, 0, 0, 0]], cls=["zebra"], conf=[0.95]): | ||
| self.xyxyn = None | ||
| self.cls = None | ||
| self.conf = None | ||
|
|
||
| def mock(self, boxes, classes, confs): | ||
| self.xyxyn = torch.Tensor(boxes) | ||
| self.cls = torch.Tensor([LABELS[cls] for cls in classes]) | ||
| self.conf = torch.Tensor(confs) | ||
| return self | ||
|
|
||
|
|
||
| class TestYolo(unittest.TestCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| cls.im = np.zeros((100, 101, 3), dtype=np.uint8) | ||
| cls.box = OrderedDict([("x1", 10), ("y1", 20), ("x2", 30), ("y2", 40)]) | ||
| cls.box_values = list(cls.box.values()) | ||
|
|
||
| @patch("kabr_tools.utils.yolo.YOLO") | ||
| def test_forward(self, yolo_mock): | ||
| im = TestYolo.im | ||
| yolo_model = MagicMock() | ||
| yolo_model.predict.return_value.__getitem__ = lambda x, _: x | ||
| yolo_model.names = YOLO("yolov8x.pt").names | ||
| yolo_mock.return_value = yolo_model | ||
|
|
||
| # horse -> zebra | ||
| points = [[0] * 4] * 3 | ||
| labels = ["zebra", "horse", "giraffe"] | ||
| expect_labels = ["Zebra", "Zebra", "Giraffe"] | ||
| probs = [0.7, 0.8, 0.9] | ||
| yolo_boxes = MockBox().mock(points, labels, probs) | ||
| yolo_model.predict.return_value.boxes.cpu.return_value = yolo_boxes | ||
|
|
||
| yolo = YOLOv8() | ||
| preds = yolo.forward(im) | ||
|
|
||
| self.assertEqual(len(preds), 3) | ||
| for i, pred in enumerate(preds): | ||
| self.assertEqual(preds[i][0], points[i]) | ||
| self.assertEqual(preds[i][1], probs[i]) | ||
| self.assertEqual(preds[i][2], expect_labels[i]) | ||
|
|
||
| # bear -> filtered | ||
| points = [[0] * 4] * 3 | ||
| labels = ["bear", "horse", "giraffe"] | ||
| expect_labels = [None, "Zebra", "Giraffe"] | ||
| probs = [0.9, 0.8, 0.9] | ||
| yolo_boxes = MockBox().mock(points, labels, probs) | ||
| yolo_model.predict.return_value.boxes.cpu.return_value = yolo_boxes | ||
|
|
||
| yolo = YOLOv8() | ||
| preds = yolo.forward(im) | ||
|
|
||
| self.assertEqual(len(preds), 2) | ||
| index = 0 | ||
| for pred in preds: | ||
| while expect_labels[index] is None: | ||
| index += 1 | ||
| self.assertEqual(pred[0], rescale(points[index], im.shape[1], im.shape[0])) | ||
| self.assertEqual(pred[1], probs[index]) | ||
| self.assertEqual(pred[2], expect_labels[index]) | ||
| index += 1 | ||
|
|
||
| # low prob -> filtered | ||
| points = [[i] * 4 for i in range(8)] | ||
| labels = ["bear", "horse", "zebra", "giraffe", "bear", "horse", "zebra", "giraffe"] | ||
| expect_labels = [None, "Zebra", None, "Giraffe", None, "Zebra", None, None] | ||
| probs = [0.5, 0.9, 0.4, 0.8, 0.7, 0.6, 0.3, 0.5] | ||
| yolo_boxes = MockBox().mock(points, labels, probs) | ||
| yolo_model.predict.return_value.boxes.cpu.return_value = yolo_boxes | ||
|
|
||
| yolo = YOLOv8() | ||
| preds = yolo.forward(im) | ||
|
|
||
| self.assertEqual(len(preds), 3) | ||
| index = 0 | ||
| for pred in preds: | ||
| while expect_labels[index] is None: | ||
| index += 1 | ||
| self.assertEqual(pred[0], rescale(points[index], im.shape[1], im.shape[0])) | ||
| self.assertEqual(pred[1], probs[index]) | ||
| self.assertEqual(pred[2], expect_labels[index]) | ||
| index += 1 | ||
|
|
||
| @patch("kabr_tools.utils.yolo.YOLO") | ||
| def test_yolo_with_params(self, yolo_mock): | ||
| im = TestYolo.im | ||
| yolo_model = MagicMock() | ||
| yolo_model.predict.return_value.__getitem__ = lambda x, _: x | ||
| yolo_model.names = YOLO("yolov8x.pt").names | ||
| yolo_mock.return_value = yolo_model | ||
|
|
||
| points = [[i] * 4 for i in range(8)] | ||
| labels = ["bear", "horse", "zebra", "giraffe", "bear", "horse", "zebra", "giraffe"] | ||
| expect_labels = ["Panda", "Fish", None, None, None, None, None, "Giraffe"] | ||
| probs = [0.91, 0.99, 0.92, 0.55, 0.9, 0.89, 0.85, 0.93] | ||
| yolo_boxes = MockBox().mock(points, labels, probs) | ||
| yolo_model.predict.return_value.boxes.cpu.return_value = yolo_boxes | ||
|
|
||
| yolo = YOLOv8(weights="yolov8x.pt", | ||
| imgsz=640, conf=0.9, | ||
| target_labels=["bear", "horse", "giraffe"], | ||
| label_map={"bear": "panda", "horse": "fish"}) | ||
| preds = yolo.forward(im) | ||
|
|
||
| self.assertEqual(len(preds), 3) | ||
| index = 0 | ||
| for pred in preds: | ||
| while expect_labels[index] is None: | ||
| index += 1 | ||
| self.assertEqual(pred[0], rescale(points[index], im.shape[1], im.shape[0])) | ||
| self.assertEqual(pred[1], probs[index]) | ||
| self.assertEqual(pred[2], expect_labels[index]) | ||
| index += 1 | ||
|
|
||
| def test_get_centroid(self): | ||
| box = TestYolo.box | ||
| box_values = TestYolo.box_values | ||
| x, y = YOLOv8.get_centroid(box_values) | ||
| self.assertEqual(x, (box["x1"] + box["x2"]) // 2) | ||
| self.assertEqual(y, (box["y1"] + box["y2"]) // 2) |
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.
Uh oh!
There was an error while loading. Please reload this page.