Skip to content

Commit

Permalink
ultralytics 8.2.37 update temporary_modules and Remove YOLOv9e `S…
Browse files Browse the repository at this point in the history
…ilence` module (#13819)

Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
  • Loading branch information
3 people committed Jun 20, 2024
1 parent 3f90100 commit 821e5fa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
4 changes: 0 additions & 4 deletions docs/en/reference/nn/modules/block.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion ultralytics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ultralytics YOLO 🚀, AGPL-3.0 license

__version__ = "8.2.36"
__version__ = "8.2.37"

import os

Expand Down
12 changes: 0 additions & 12 deletions ultralytics/nn/modules/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,18 +672,6 @@ def forward(self, x):
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."""

Expand Down
25 changes: 19 additions & 6 deletions ultralytics/nn/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ def forward(self, x, augment=False, profile=False, visualize=False):


@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`).
Expand All @@ -685,25 +685,35 @@ 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:
The changes are only in effect inside the context manager and are undone once the context manager exits.
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)
Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit 821e5fa

Please sign in to comment.