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:
Laughing 2024-06-21 01:56:08 +08:00 committed by GitHub
parent 3f90100d5e
commit 821e5fa477
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 20 additions and 23 deletions

View file

@ -135,10 +135,6 @@ keywords: Ultralytics, YOLO, neural networks, block modules, DFL, Proto, HGStem,
<br><br> <br><br>
## ::: ultralytics.nn.modules.block.Silence
<br><br>
## ::: ultralytics.nn.modules.block.CBLinear ## ::: ultralytics.nn.modules.block.CBLinear
<br><br> <br><br>

View file

@ -1,6 +1,6 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license # Ultralytics YOLO 🚀, AGPL-3.0 license
__version__ = "8.2.36" __version__ = "8.2.37"
import os import os

View file

@ -672,18 +672,6 @@ class SPPELAN(nn.Module):
return self.cv5(torch.cat(y, 1)) 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): class CBLinear(nn.Module):
"""CBLinear.""" """CBLinear."""

View file

@ -675,7 +675,7 @@ class Ensemble(nn.ModuleList):
@contextlib.contextmanager @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`). 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: Args:
modules (dict, optional): A dictionary mapping old module paths to new module paths. 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: Example:
```python ```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 import old.module.path # this will now import new.module.path
from old.module import attribute # this will now import new.module.attribute
``` ```
Note: 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 Be aware that directly manipulating `sys.modules` can lead to unpredictable results, especially in larger
applications or libraries. Use this function with caution. applications or libraries. Use this function with caution.
""" """
if not modules:
modules = {}
import importlib import importlib
import sys import sys
try: 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 # Set modules in sys.modules under their old name
for old, new in modules.items(): for old, new in modules.items():
sys.modules[old] = importlib.import_module(new) 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 file = attempt_download_asset(weight) # search online if missing locally
try: try:
with temporary_modules( with temporary_modules(
{ modules={
"ultralytics.yolo.utils": "ultralytics.utils", "ultralytics.yolo.utils": "ultralytics.utils",
"ultralytics.yolo.v8": "ultralytics.models.yolo", "ultralytics.yolo.v8": "ultralytics.models.yolo",
"ultralytics.yolo.data": "ultralytics.data", "ultralytics.yolo.data": "ultralytics.data",
} },
attributes={
"ultralytics.nn.modules.block.Silence": "torch.nn.Identity",
},
): # for legacy 8.0 Classify and Pose models ): # for legacy 8.0 Classify and Pose models
ckpt = torch.load(file, map_location="cpu") ckpt = torch.load(file, map_location="cpu")