Skip to content
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

Change load_stat_dict to on_load_checkpoint #3443

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/otx/core/model/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
],
}

def load_state_dict(self, ckpt: dict[str, Any], *args, **kwargs) -> None:
def on_load_checkpoint(self, ckpt: dict[str, Any]) -> None:
"""Load state_dict from checkpoint.

For detection, it is need to update confidence threshold information when
Expand All @@ -148,7 +148,7 @@
and (best_confidence_threshold := hyper_parameters.get("best_confidence_threshold", None))
):
self.hparams["best_confidence_threshold"] = best_confidence_threshold
super().load_state_dict(ckpt, *args, **kwargs)
super().on_load_checkpoint(ckpt)

def _log_metrics(self, meter: Metric, key: Literal["val", "test"], **compute_kwargs) -> None:
if key == "val":
Expand Down Expand Up @@ -539,7 +539,7 @@

if model_adapter.model.has_rt_info(["model_info", "confidence_threshold"]):
best_confidence_threshold = model_adapter.model.get_rt_info(["model_info", "confidence_threshold"]).value
self.hparams["best_confidence_threshold"] = best_confidence_threshold
self.hparams["best_confidence_threshold"] = float(best_confidence_threshold)

Check warning on line 542 in src/otx/core/model/detection.py

View check run for this annotation

Codecov / codecov/patch

src/otx/core/model/detection.py#L542

Added line #L542 was not covered by tests
else:
msg = (
"Cannot get best_confidence_threshold from OpenVINO IR's rt_info. "
Expand Down
14 changes: 7 additions & 7 deletions src/otx/core/model/instance_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@
return super()._export_parameters.wrap(
model_type="MaskRCNN",
task_type="instance_segmentation",
confidence_threshold=self.hparams.get("best_confidence_threshold", 0.0),
confidence_threshold=self.hparams.get("best_confidence_threshold", None),
iou_threshold=0.5,
tile_config=self.tile_config if self.tile_config.enable_tiler else None,
)

def load_state_dict(self, ckpt: dict[str, Any], *args, **kwargs) -> None:
def on_load_checkpoint(self, ckpt: dict[str, Any]) -> None:
"""Load state_dict from checkpoint.

For detection, it is need to update confidence threshold information when
Expand All @@ -129,7 +129,7 @@
and (best_confidence_threshold := hyper_parameters.get("best_confidence_threshold", None))
):
self.hparams["best_confidence_threshold"] = best_confidence_threshold
super().load_state_dict(ckpt, *args, **kwargs)
super().on_load_checkpoint(ckpt)

Check warning on line 132 in src/otx/core/model/instance_segmentation.py

View check run for this annotation

Codecov / codecov/patch

src/otx/core/model/instance_segmentation.py#L132

Added line #L132 was not covered by tests

def _log_metrics(self, meter: Metric, key: Literal["val", "test"], **compute_kwargs) -> None:
if key == "val":
Expand Down Expand Up @@ -597,16 +597,16 @@

if model_adapter.model.has_rt_info(["model_info", "confidence_threshold"]):
best_confidence_threshold = model_adapter.model.get_rt_info(["model_info", "confidence_threshold"]).value
self.hparams["best_confidence_threshold"] = best_confidence_threshold
self.hparams["best_confidence_threshold"] = float(best_confidence_threshold)

Check warning on line 600 in src/otx/core/model/instance_segmentation.py

View check run for this annotation

Codecov / codecov/patch

src/otx/core/model/instance_segmentation.py#L600

Added line #L600 was not covered by tests
else:
msg = (
"Cannot get best_confidence_threshold from OpenVINO IR's rt_info. "
"Please check whether this model is trained by OTX or not. "
"Without this information, it can produce a wrong F1 metric score. "
"At this time, it will be set as the default value = 0.0."
"At this time, it will be set as the default value = None."
)
log.warning(msg)
self.hparams["best_confidence_threshold"] = 0.0
self.hparams["best_confidence_threshold"] = None

Check warning on line 609 in src/otx/core/model/instance_segmentation.py

View check run for this annotation

Codecov / codecov/patch

src/otx/core/model/instance_segmentation.py#L609

Added line #L609 was not covered by tests

return Model.create_model(model_adapter, model_type=self.model_type, configuration=self.model_api_configuration)

Expand Down Expand Up @@ -729,6 +729,6 @@
return {"preds": pred_info, "target": target_info}

def _log_metrics(self, meter: Metric, key: Literal["val", "test"], **compute_kwargs) -> None:
best_confidence_threshold = self.hparams.get("best_confidence_threshold", 0.0)
best_confidence_threshold = self.hparams.get("best_confidence_threshold", None)

Check warning on line 732 in src/otx/core/model/instance_segmentation.py

View check run for this annotation

Codecov / codecov/patch

src/otx/core/model/instance_segmentation.py#L732

Added line #L732 was not covered by tests
compute_kwargs = {"best_confidence_threshold": best_confidence_threshold}
return super()._log_metrics(meter, key, **compute_kwargs)
2 changes: 1 addition & 1 deletion tests/unit/core/model/test_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_configure_metric_with_ckpt(
metric=FMeasureCallable,
)

model.load_state_dict(mock_ckpt)
model.on_load_checkpoint(mock_ckpt)

assert model.hparams["best_confidence_threshold"] == 0.35

Expand Down
Loading