-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added unit tests, integration and e2e
- Loading branch information
Showing
11 changed files
with
158 additions
and
40 deletions.
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
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
4 changes: 2 additions & 2 deletions
4
otx/algorithms/segmentation/configs/ham_segnext_t/template_experimental.yaml
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
31 changes: 31 additions & 0 deletions
31
tests/unit/algorithms/segmentation/adapters/mmseg/models/heads/test_prototype_head.py
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,31 @@ | ||
import pytest | ||
import torch | ||
|
||
from otx.algorithms.segmentation.adapters.mmseg.models.heads.proto_head import ProtoNet | ||
from tests.test_suite.e2e_test_system import e2e_pytest_unit | ||
|
||
|
||
class TestProtoNet: | ||
@pytest.fixture(autouse=True) | ||
def setup(self): | ||
self.proto_net = ProtoNet( | ||
gamma=0.99, num_prototype=4, in_proto_channels=512, in_channels=512, channels=512, num_classes=4 | ||
) | ||
|
||
def test_prototype_learning(self): | ||
dummy_input = torch.rand(32768, 512) | ||
dummy_out_seg = torch.rand(8, 4, 64, 64) | ||
dummy_masks = torch.rand(32768, 4, 4) | ||
dummy_gt_seg = torch.randint(low=0, high=5, size=(32768,)) | ||
proto_logits, proto_target = self.proto_net.prototype_learning( | ||
dummy_input, dummy_out_seg, dummy_gt_seg, dummy_masks | ||
) | ||
assert proto_logits is not None | ||
assert proto_target is not None | ||
|
||
def test_forward(self): | ||
dummy_input = torch.rand(8, 512, 64, 64) | ||
dummy_gt_seg = torch.randint(low=0, high=5, size=(8, 1, 512, 512)) | ||
proto_out = self.proto_net(inputs=dummy_input, gt_semantic_seg=dummy_gt_seg) | ||
assert isinstance(proto_out, dict) | ||
assert proto_out["out_seg"] is not None |
24 changes: 24 additions & 0 deletions
24
...unit/algorithms/segmentation/adapters/mmseg/models/losses/test_pixel_prototype_ce_loss.py
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,24 @@ | ||
import pytest | ||
import torch | ||
|
||
from otx.algorithms.segmentation.adapters.mmseg.models.losses import ( | ||
PixelPrototypeCELoss, | ||
) | ||
|
||
from tests.test_suite.e2e_test_system import e2e_pytest_unit | ||
|
||
|
||
class TestPixelPrototypeCELoss: | ||
@pytest.fixture(autouse=True) | ||
def setup(self): | ||
self.loss_proto_ce = PixelPrototypeCELoss() | ||
|
||
@e2e_pytest_unit | ||
def test_forward(self): | ||
dummy_out = torch.rand(4, 5, 512, 512) | ||
proto_logits = torch.rand(1280, 16) | ||
proto_target = torch.rand(1280) | ||
target = torch.randint(low=0, high=5, size=(4, 1, 512, 512)) | ||
loss = self.loss_proto_ce(dummy_out, proto_logits, proto_target, target) | ||
assert loss is not None | ||
assert loss >= 0 |
51 changes: 51 additions & 0 deletions
51
...t/algorithms/segmentation/adapters/mmseg/models/segmentors/test_mean_teacher_segmentor.py
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,51 @@ | ||
import pytest | ||
import torch | ||
|
||
from otx.algorithms.segmentation.adapters.mmseg import MeanTeacherSegmentor | ||
from tests.test_suite.e2e_test_system import e2e_pytest_unit | ||
|
||
|
||
class TestMeanTeacherSegmentor: | ||
@pytest.fixture(autouse=True) | ||
def setup(self, mocker) -> None: | ||
mocker.patch( | ||
"otx.algorithms.segmentation.adapters.mmseg.models.segmentors.mean_teacher_segmentor.build_segmentor" | ||
) | ||
self.mean_teacher = MeanTeacherSegmentor(None, 100, test_cfg=dict(), decode_head={"align_corners": False}) | ||
self.mean_teacher.proto_net = mocker.MagicMock() | ||
self.mean_teacher.use_prototype_head = True | ||
self.input = torch.rand(4, 3, 512, 512) | ||
self.gt_seg = torch.randint(low=0, high=5, size=(4, 1, 512, 512)) | ||
|
||
@e2e_pytest_unit | ||
def test_decode_proto_network(self, mocker): | ||
mocker_update_loss = mocker.patch.object(self.mean_teacher, "_update_summary_loss") | ||
self.mean_teacher.decode_proto_network(self.input, self.gt_seg) | ||
mocker_update_loss.assert_called_once() | ||
# dummy input | ||
self.mean_teacher.decode_proto_network(self.input, self.gt_seg, self.input, self.gt_seg) | ||
|
||
@e2e_pytest_unit | ||
def test_generate_pseudo_labels(self, mocker): | ||
mocker.patch( | ||
"otx.algorithms.segmentation.adapters.mmseg.models.segmentors.mean_teacher_segmentor.resize", | ||
return_value=self.input, | ||
) | ||
pl_from_teacher, reweight_unsup = self.mean_teacher.generate_pseudo_labels( | ||
ul_w_img=self.input, ul_img_metas=dict() | ||
) | ||
assert isinstance(pl_from_teacher, torch.Tensor) | ||
assert pl_from_teacher.shape == (4, 1, 512, 512) | ||
assert round(reweight_unsup.item(), 2) == 1.25 | ||
|
||
@e2e_pytest_unit | ||
def test_forward_train(self, mocker): | ||
loss = self.mean_teacher(self.input, img_metas=dict(), gt_semantic_seg=self.gt_seg) | ||
assert loss is not None | ||
self.mean_teacher.semisl_start_iter = -1 | ||
mocker.patch.object(self.mean_teacher, "decode_proto_network") | ||
mocker.patch.object(self.mean_teacher, "generate_pseudo_labels", return_value=(self.gt_seg, 1.0)) | ||
ul_kwargs = dict(extra_0=dict(img=self.input, ul_w_img=self.input, img_metas=dict())) | ||
loss = self.mean_teacher(self.input, img_metas=dict(), gt_semantic_seg=self.gt_seg, **ul_kwargs) | ||
assert loss is not None | ||
assert loss["sum_loss"] == 0.0 |