i run a scipt in conda env and get following errors: anonimizevideo.py", line 20, in…
- User
i run a scipt in conda env and get following errors:
anonimize_video.py", line 20, in from isuor.loops import inference_loop File "/mnt/c/Users/xyz/Downloads/Tools/Development/08_monocular_depth_estimation/isuor/src/isuor/loops.py", line 5, in from isuor.pipelines import PipelineAction File "/mnt/c/Users/xyz/Downloads/Tools/Development/08_monocular_depth_estimation/isuor/src/isuor/pipelines/init.py", line 1, in from depth.datasets.pipelines import Compose File "/home/xyz/miniconda3/envs/isuor_mde/lib/python3.10/site-packages/depth/datasets/init.py", line 2, in from .kitti import KITTIDataset File "/home/xyz/miniconda3/envs/isuor_mde/lib/python3.10/site-packages/depth/datasets/kitti.py", line 14, in from depth.utils import get_root_logger File "/home/xyz/miniconda3/envs/isuor_mde/lib/python3.10/site-packages/depth/utils/init.py", line 4, in from .position_encoding import SinePositionalEncoding, LearnedPositionalEncoding File "/home/xyz/miniconda3/envs/isuor_mde/lib/python3.10/site-packages/depth/utils/position_encoding.py", line 11, in class SinePositionalEncoding(BaseModule): File "/home/xyz/miniconda3/envs/isuor_mde/lib/python3.10/site-packages/mmcv/utils/registry.py", line 337, in _register self._register_module(module=module, module_name=name, force=force) File "/home/xyz/miniconda3/envs/isuor_mde/lib/python3.10/site-packages/mmcv/utils/misc.py", line 340, in new_func output = old_func(*args, **kwargs) File "/home/xyz/miniconda3/envs/isuor_mde/lib/python3.10/site-packages/mmcv/utils/registry.py", line 272, in _register_module raise KeyError(f'{name} is already registered ' KeyError: 'SinePositionalEncoding is already registered in position encoding'
- Assistant
The error you're encountering suggests that the class
SinePositionalEncodingis being registered twice in the registry, which is not allowed. This typically happens when the module definingSinePositionalEncodingis being imported more than once under different module names or paths, causing the class definition to execute multiple times.Here's what's likely happening:
-
Multiple Imports of the Same Module: The module
depth.utils.position_encodingis being imported more than once, possibly due to different import paths (e.g., absolute vs. relative imports) or because of conflicting module names in different directories. -
Duplicate
depthPackages: There might be multiple copies of thedepthpackage in your environment—one installed in yoursite-packagesand another in your local directory or project.
Steps to Resolve the Issue:
-
Check for Multiple Copies of the
depthPackage:- Search your project directory and your
site-packagesfor any instances of thedepthpackage. - If you find that
depthexists both in your project directory and insite-packages, decide which one you want to use and remove or rename the other to avoid conflicts.
- Search your project directory and your
-
Ensure Consistent Import Paths:
- Use absolute imports consistently throughout your code to prevent Python from interpreting the same module as two different ones.
- Avoid using relative imports if they can cause ambiguity in module paths.
-
Verify Your
PYTHONPATHandsys.path:- Print out
sys.pathat the start of your script to see which directories are being searched for modules. - Ensure that the directories in
sys.pathdo not contain conflicting module names that could lead to the same module being imported from different locations.
- Print out
-
Modify the Registry Registration:
- In the module where
SinePositionalEncodingis defined (usually with a decorator like@POSITION_ENCODING.register_module()), you can add aforce=Trueparameter to allow re-registration:@POSITION_ENCODING.register_module(force=True) class SinePositionalEncoding(BaseModule): ...- Note: This is generally not recommended as it may mask underlying issues with your imports. It's better to resolve the root cause.
- In the module where
-
Avoid Circular Imports:
- Check your code for circular imports that could cause modules to be imported multiple times.
-
Clean and Reinstall Your Environment:
- Sometimes, stale
.pycfiles or leftover old module versions can cause issues. - Delete
__pycache__directories and recompile your code. - If necessary, create a fresh conda environment and reinstall your dependencies carefully.
- Sometimes, stale
Example of Checking for Multiple Copies:
# Find all instances of the 'depth' package find / -type d -name 'depth' 2>/dev/nullExample of Printing
sys.path:import sys print(sys.path)By ensuring that the
depthpackage (and specifically theposition_encodingmodule) is only present once in your module search path and that it's being imported consistently, you should be able to resolve theKeyErroryou're encountering.Summary:
- The
KeyErrorarises becauseSinePositionalEncodingis being registered twice. - This is likely due to the module being imported more than once under different paths.
- Ensure there's only one version of the module and that imports are consistent.
- Check for and resolve any conflicts in your module paths or names.
-