Skip to content

Commit

Permalink
Remove left mmdet dependency (#3488)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaegukhyun authored May 13, 2024
1 parent c0aff65 commit 00e7f28
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions src/otx/algo/detection/backbones/pytorchcv_backbones.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING

import torch
from mmdet.registry import MODELS
from pytorchcv.model_provider import _models
from pytorchcv.models.model_store import download_model
from torch import distributed, nn
Expand All @@ -19,9 +17,6 @@
from otx.algo.modules.norm import build_norm_layer
from otx.algo.utils.mmengine_utils import get_dist_info

if TYPE_CHECKING:
from mmdet.registry import Registry

# ruff: noqa: SLF001


Expand Down Expand Up @@ -99,32 +94,25 @@ def init_weights(self: nn.Module, pretrained: bool = True) -> None:
download_model(net=self, model_name=self.model_name, local_model_store_dir_path=self.models_cache_root)


ori_build_func = MODELS.build_func


def _pytorchcv_model_reduce(self) -> nn.Module: # noqa: ANN001
return (_build_model_including_pytorchcv, (self.otx_cfg,))


def _build_model_including_pytorchcv(
cfg: dict,
registry: Registry = MODELS,
default_args: dict | None = None,
) -> nn.Module:
"""Try to build model from mmdet first and build from pytorchcv."""
try:
model = ori_build_func(cfg, registry, default_args)
except KeyError: # build from pytorchcv
args = cfg.copy()
if default_args is not None:
for name, value in default_args.items():
args.setdefault(name, value)
"""Build model from pytorchcv."""
args = cfg.copy()
if default_args is not None:
for name, value in default_args.items():
args.setdefault(name, value)

Check warning on line 109 in src/otx/algo/detection/backbones/pytorchcv_backbones.py

View check run for this annotation

Codecov / codecov/patch

src/otx/algo/detection/backbones/pytorchcv_backbones.py#L108-L109

Added lines #L108 - L109 were not covered by tests

model = _build_pytorchcv_model(**args)
model = _build_pytorchcv_model(**args)

# support pickle
model.otx_cfg = args
model.__class__.__reduce__ = _pytorchcv_model_reduce.__get__(model, model.__class__)
# support pickle
model.otx_cfg = args
model.__class__.__reduce__ = _pytorchcv_model_reduce.__get__(model, model.__class__)

return model

Expand Down Expand Up @@ -175,6 +163,3 @@ def _build_pytorchcv_model(
)

return model


MODELS.build_func = _build_model_including_pytorchcv

0 comments on commit 00e7f28

Please sign in to comment.