mirror of
https://github.com/ultralytics/ultralytics.git
synced 2025-09-15 15:48:41 +08:00
ultralytics 8.2.37 update temporary_modules and Remove YOLOv9e Silence module (#13819)
Co-authored-by: UltralyticsAssistant <web@ultralytics.com> Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
parent
3f90100d5e
commit
821e5fa477
@ -135,10 +135,6 @@ keywords: Ultralytics, YOLO, neural networks, block modules, DFL, Proto, HGStem,
|
||||
|
||||
<br><br>
|
||||
|
||||
## ::: ultralytics.nn.modules.block.Silence
|
||||
|
||||
<br><br>
|
||||
|
||||
## ::: ultralytics.nn.modules.block.CBLinear
|
||||
|
||||
<br><br>
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||||
|
||||
__version__ = "8.2.36"
|
||||
__version__ = "8.2.37"
|
||||
|
||||
import os
|
||||
|
||||
|
||||
@ -672,18 +672,6 @@ class SPPELAN(nn.Module):
|
||||
return self.cv5(torch.cat(y, 1))
|
||||
|
||||
|
||||
class Silence(nn.Module):
|
||||
"""Silence."""
|
||||
|
||||
def __init__(self):
|
||||
"""Initializes the Silence module."""
|
||||
super(Silence, self).__init__()
|
||||
|
||||
def forward(self, x):
|
||||
"""Forward pass through Silence layer."""
|
||||
return x
|
||||
|
||||
|
||||
class CBLinear(nn.Module):
|
||||
"""CBLinear."""
|
||||
|
||||
|
||||
@ -675,7 +675,7 @@ class Ensemble(nn.ModuleList):
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def temporary_modules(modules=None):
|
||||
def temporary_modules(modules={}, attributes={}):
|
||||
"""
|
||||
Context manager for temporarily adding or modifying modules in Python's module cache (`sys.modules`).
|
||||
|
||||
@ -685,11 +685,13 @@ def temporary_modules(modules=None):
|
||||
|
||||
Args:
|
||||
modules (dict, optional): A dictionary mapping old module paths to new module paths.
|
||||
attributes (dict, optional): A dictionary mapping old module attributes to new module attributes.
|
||||
|
||||
Example:
|
||||
```python
|
||||
with temporary_modules({'old.module.path': 'new.module.path'}):
|
||||
with temporary_modules({'old.module.path': 'new.module.path'}, {'old.module.attribute': 'new.module.attribute'}):
|
||||
import old.module.path # this will now import new.module.path
|
||||
from old.module import attribute # this will now import new.module.attribute
|
||||
```
|
||||
|
||||
Note:
|
||||
@ -697,13 +699,21 @@ def temporary_modules(modules=None):
|
||||
Be aware that directly manipulating `sys.modules` can lead to unpredictable results, especially in larger
|
||||
applications or libraries. Use this function with caution.
|
||||
"""
|
||||
if not modules:
|
||||
modules = {}
|
||||
|
||||
import importlib
|
||||
import sys
|
||||
|
||||
try:
|
||||
# Set attributes in sys.modules under their old name
|
||||
for old, new in attributes.items():
|
||||
old_module, old_attr = old.rsplit(".", 1)
|
||||
new_module, new_attr = new.rsplit(".", 1)
|
||||
setattr(
|
||||
importlib.import_module(old_module),
|
||||
old_attr,
|
||||
getattr(importlib.import_module(new_module), new_attr),
|
||||
)
|
||||
|
||||
# Set modules in sys.modules under their old name
|
||||
for old, new in modules.items():
|
||||
sys.modules[old] = importlib.import_module(new)
|
||||
@ -734,11 +744,14 @@ def torch_safe_load(weight):
|
||||
file = attempt_download_asset(weight) # search online if missing locally
|
||||
try:
|
||||
with temporary_modules(
|
||||
{
|
||||
modules={
|
||||
"ultralytics.yolo.utils": "ultralytics.utils",
|
||||
"ultralytics.yolo.v8": "ultralytics.models.yolo",
|
||||
"ultralytics.yolo.data": "ultralytics.data",
|
||||
}
|
||||
},
|
||||
attributes={
|
||||
"ultralytics.nn.modules.block.Silence": "torch.nn.Identity",
|
||||
},
|
||||
): # for legacy 8.0 Classify and Pose models
|
||||
ckpt = torch.load(file, map_location="cpu")
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user