Medical-SAM-Adapter icon indicating copy to clipboard operation
Medical-SAM-Adapter copied to clipboard

Name collision with YOLO

Open dzenanz opened this issue 8 months ago • 1 comments

I am trying to use a YOLOv5 trained model to provide box prompts to MedSA. However, when I try to load them both in the same script, I run into name collisions. Try 1:

from utils import cfg
args = cfg.parse_args()
detector = torch.hub.load(yolo_path, 'custom',
                          path=r'M:\Dev\...\best.pt',
                          source='local')

produces:

Traceback (most recent call last):
  File "C:\Program Files\Python39\lib\contextlib.py", line 137, in __exit__
    self.gen.throw(typ, value, traceback)
  File "M:\Dev\CXR\.venv\lib\site-packages\torch\hub.py", line 93, in _add_to_sys_path
    yield
  File "M:\Dev\CXR\.venv\lib\site-packages\torch\hub.py", line 597, in _load_local
    model = entry(*args, **kwargs)
  File "M:\Dev\CXR\yolov5\hubconf.py", line 88, in custom
    return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
  File "M:\Dev\CXR\yolov5\hubconf.py", line 34, in _create
    from models.common import AutoShape, DetectMultiBackend
ImportError: cannot import name 'AutoShape' from 'models.common' (M:\Dev\CXR\Medical-SAM-Adapter\models\common\__init__.py)

Trying to apply advice from this discussion:

yolo_path = r"M:\Dev\CXR\yolov5"
sys.path.insert(0, yolo_path)
detector = torch.hub.load(yolo_path, 'custom',
                          path=r'M:\Dev\...\best.pt',
                          source='local')
sys.path.remove(yolo_path)
sys.path.append(r'M:\Dev\CXR')
sys.path.append(r'M:\Dev\CXR\Medical_SAM_Adapter')

from Medical_SAM_Adapter.utils import cfg

produces:

YOLOv5  v7.0-312-g1bcd17ee Python-3.9.13 torch-2.3.0+cu121 CUDA:0 (NVIDIA GeForce RTX 3090, 24576MiB)

Fusing layers... 
YOLOv5s summary: 157 layers, 7012822 parameters, 0 gradients, 15.8 GFLOPs
Adding AutoShape... 
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "M:\Dev\CXR\Medical_SAM_Adapter\utils.py", line 60, in <module>
    from models.discriminator import Discriminator
ModuleNotFoundError: No module named 'models.discriminator'

Both YOLOv5 and MedSA have module named models. How to avoid this name collision?

dzenanz avatar Jun 17 '24 15:06 dzenanz