|
1 | 1 | # MIT License
|
2 | 2 | #
|
3 |
| -# Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2022 |
| 3 | +# Copyright (C) The Adversarial Robustness Toolbox (ART) Authors 2025 |
4 | 4 | #
|
5 |
| -# Test for PyTorchYoloLossWrapper |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 6 | +# documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 7 | +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +# persons to whom the Software is furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 11 | +# Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 14 | +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 15 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 16 | +# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 17 | +# SOFTWARE. |
6 | 18 | import pytest
|
7 | 19 | import torch
|
| 20 | +import os |
8 | 21 | from art.estimators.object_detection.pytorch_yolo_loss_wrapper import PyTorchYoloLossWrapper
|
| 22 | +from ultralytics import YOLO |
| 23 | + |
| 24 | +os.environ["TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD"] = "1" |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.only_with_platform("pytorch") |
| 28 | +def test_yolov8_loss_wrapper(): |
| 29 | + """Test the loss wrapper with YOLOv8 model.""" |
| 30 | + # Load YOLOv8 model |
| 31 | + model_path = "/tmp/yolo_v8.3.0/yolov8n.pt" |
| 32 | + model = YOLO(model_path).model |
| 33 | + |
| 34 | + # Create wrapper |
| 35 | + wrapper = PyTorchYoloLossWrapper(model, name="yolov8n") |
| 36 | + wrapper.train() |
| 37 | + |
| 38 | + # Create sample input |
| 39 | + batch_size = 2 |
| 40 | + x = torch.randn((batch_size, 3, 640, 640)) # YOLOv8 expects (B, 3, 640, 640) |
| 41 | + |
| 42 | + # Create targets |
| 43 | + targets = [] |
| 44 | + for _ in range(batch_size): |
| 45 | + boxes = torch.tensor([[0.1, 0.1, 0.3, 0.3], [0.5, 0.5, 0.8, 0.8]]) # [x1, y1, x2, y2] |
| 46 | + labels = torch.zeros(2, dtype=torch.long) # Use class 0 for testing |
| 47 | + targets.append({"boxes": boxes, "labels": labels}) |
| 48 | + |
| 49 | + # Test training mode |
| 50 | + losses = wrapper(x, targets) |
| 51 | + |
| 52 | + # Validate loss structure |
| 53 | + expected_loss_keys = {"loss_total", "loss_box", "loss_cls", "loss_dfl"} |
| 54 | + assert set(losses.keys()) == expected_loss_keys |
| 55 | + assert all(isinstance(v, torch.Tensor) for v in losses.values()) |
| 56 | + assert all(not torch.isnan(v).any() for v in losses.values()), "Loss values contain NaN" |
| 57 | + assert all(not torch.isinf(v).any() for v in losses.values()), "Loss values contain Inf" |
| 58 | + |
| 59 | + # Test inference mode |
| 60 | + wrapper.eval() |
| 61 | + with torch.no_grad(): |
| 62 | + predictions = wrapper(x) |
| 63 | + |
| 64 | + # Validate predictions |
| 65 | + assert isinstance(predictions, list) |
| 66 | + assert len(predictions) == batch_size |
| 67 | + for pred in predictions: |
| 68 | + assert set(pred.keys()) == {"boxes", "scores", "labels"} |
| 69 | + assert isinstance(pred["boxes"], torch.Tensor) |
| 70 | + assert isinstance(pred["scores"], torch.Tensor) |
| 71 | + assert isinstance(pred["labels"], torch.Tensor) |
| 72 | + assert pred["boxes"].ndim == 2 and pred["boxes"].shape[1] == 4 |
| 73 | + assert pred["scores"].ndim == 1 |
| 74 | + assert pred["labels"].ndim == 1 |
| 75 | + assert pred["scores"].shape[0] == pred["labels"].shape[0] == pred["boxes"].shape[0] |
| 76 | + assert pred["boxes"].dtype == torch.float32 |
| 77 | + assert pred["labels"].dtype in (torch.int32, torch.int64) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.only_with_platform("pytorch") |
| 81 | +def test_yolov10_loss_wrapper(): |
| 82 | + """Test the loss wrapper with YOLOv10 model.""" |
| 83 | + # Load YOLOv10 model |
| 84 | + model_path = "/tmp/yolo_v8.3.0/yolov10n.pt" |
| 85 | + model = YOLO(model_path).model |
| 86 | + |
| 87 | + # Create wrapper |
| 88 | + wrapper = PyTorchYoloLossWrapper(model, name="yolov10n") |
| 89 | + wrapper.train() |
| 90 | + |
| 91 | + # Create sample input |
| 92 | + batch_size = 2 |
| 93 | + x = torch.randn((batch_size, 3, 640, 640)) # Standard YOLO input size |
| 94 | + |
| 95 | + # Create targets |
| 96 | + targets = [] |
| 97 | + for _ in range(batch_size): |
| 98 | + boxes = torch.tensor([[0.1, 0.1, 0.3, 0.3], [0.5, 0.5, 0.8, 0.8]]) # [x1, y1, x2, y2] |
| 99 | + labels = torch.zeros(2, dtype=torch.long) # Use class 0 for testing |
| 100 | + targets.append({"boxes": boxes, "labels": labels}) |
| 101 | + |
| 102 | + # Test training mode |
| 103 | + losses = wrapper(x, targets) |
| 104 | + |
| 105 | + # Validate loss structure |
| 106 | + expected_loss_keys = {"loss_total", "loss_box", "loss_cls", "loss_dfl"} |
| 107 | + assert set(losses.keys()) == expected_loss_keys |
| 108 | + assert all(isinstance(v, torch.Tensor) for v in losses.values()) |
| 109 | + assert all(not torch.isnan(v).any() for v in losses.values()), "Loss values contain NaN" |
| 110 | + assert all(not torch.isinf(v).any() for v in losses.values()), "Loss values contain Inf" |
| 111 | + assert all(v.item() >= 0 for v in losses.values()), "Loss values should be non-negative" |
| 112 | + assert losses["loss_total"].item() > 0, "Total loss should be positive" |
| 113 | + |
| 114 | + # Test inference mode |
| 115 | + wrapper.eval() |
| 116 | + with torch.no_grad(): |
| 117 | + predictions = wrapper(x) |
| 118 | + |
| 119 | + # Validate predictions |
| 120 | + assert isinstance(predictions, list) |
| 121 | + assert len(predictions) == batch_size |
| 122 | + for pred in predictions: |
| 123 | + assert set(pred.keys()) == {"boxes", "scores", "labels"} |
| 124 | + assert isinstance(pred["boxes"], torch.Tensor) |
| 125 | + assert isinstance(pred["scores"], torch.Tensor) |
| 126 | + assert isinstance(pred["labels"], torch.Tensor) |
| 127 | + assert pred["boxes"].ndim == 2 and pred["boxes"].shape[1] == 4 |
| 128 | + assert pred["scores"].ndim == 1 |
| 129 | + assert pred["labels"].ndim == 1 |
| 130 | + assert pred["scores"].shape[0] == pred["labels"].shape[0] == pred["boxes"].shape[0] |
| 131 | + assert pred["boxes"].dtype == torch.float32 |
| 132 | + assert pred["labels"].dtype in (torch.int32, torch.int64) |
9 | 133 |
|
10 | 134 |
|
11 | 135 | @pytest.mark.only_with_platform("pytorch")
|
|
0 commit comments