MambaIR 代码复现

paper:arxiv.org/pdf/2402.15648

GitHub:csguoh/MambaIR: [ECCV2024] MambaIR 和 MambaIRv2!

MambaIR 论文解析:《MambaIR:一种基于状态空间模型的简单图像修复基线方法》学习笔记-CSDN博客 

一、环境配置

1.1 激活conda 环境

Autodl目前遇到的相关问题及其解决方案_autodl启动visdom-CSDN博客

1.2  配置环境

cd ./MambaIR
conda env create -f environment.yaml

1、报错:

Preparing metadata (setup.py): started                                                                                                                                                                                      
  Preparing metadata (setup.py): finished with status 'error'                                                                                                                                                                 
                                                                                                                                                                                                                              
Pip subprocess error:                                                                                                                                                                                                         
  error: subprocess-exited-with-error                                                                                                                                                                                         
                                                                                                                                                                                                                              
  × python setup.py egg_info did not run successfully.                                                                                                                                                                        
  │ exit code: 1                                                                                                                                                                                                              
  ╰─> [6 lines of output]                                                                                                                                                                                                     
      Traceback (most recent call last):                                                                                                                                                                                      
        File "<string>", line 2, in <module>                                                                                                                                                                                  
        File "<pip-setuptools-caller>", line 34, in <module>                                                                                                                                                                  
        File "/tmp/pip-install-6iyr0w5j/causal-conv1d_edb242b70176433eb8df95c01b7c96d9/setup.py", line 8, in <module>                                                                                                         
          from packaging.version import parse, Version                                                                                                                                                                        
      ModuleNotFoundError: No module named 'packaging'                                                                                                                                                                        
      [end of output]                                                                                                                                                                                                         
                                                                                                                                                                                                                              
  note: This error originates from a subprocess, and is likely not a problem with pip.                                                                                                                                        
error: metadata-generation-failed                                                                                                                                                                                             
                                                                                                                                                                                                                              
× Encountered error while generating package metadata.                                                                                                                                                                        
╰─> See above for output.                                                                                                                                                                                                     
                                                                                                                                                                                                                              
note: This is an issue with the package mentioned above, not pip.                                                                                                                                                             
hint: See above for details.                                                                                                                                                                                                  
                                                                                                                                                                                                                              
failed                                                                                                                                                                                                                        
                                                                                                                                                                                                                              
CondaEnvException: Pip failed                                   

解决方案: 

复制下面内容替换 requirements.txt,之后执行

conda activate mambair
pip install -r requirements.txt
# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: linux-64
absl-py==2.1.0
addict==2.4.0
cachetools==5.3.2
causal-conv1d==1.0.0
certifi==2023.11.17
cffi==1.16.0
charset-normalizer==2.0.4
cmake==3.28.1
contourpy==1.1.1
cryptography==41.0.7
cycler==0.12.1
einops==0.7.0
ffmpeg==1.4
filelock==3.13.1
fonttools==4.47.2
fsspec==2023.12.2
future==0.18.3
giflib==5.2.1
gmp==6.2.1
gmpy2==2.1.2
gnutls==3.6.15
google-auth==2.27.0
google-auth-oauthlib==1.0.0
grpcio==1.60.0
h5py==3.10.0
huggingface-hub==0.20.3
idna==3.4
imageio==2.33.1
importlib-metadata==7.0.1
importlib-resources==6.1.1
jinja2==3.1.2
kiwisolver==1.4.5
lame==3.100
lazy-loader==0.3
lcms2==2.12
lerc==3.0
lit==17.0.6
lmdb==1.4.1
markdown==3.5.2
markupsafe==2.1.3
matplotlib==3.7.4
mkl-service==2.4.0
mkl_fft==1.3.8
mkl_random==1.2.4
mpc==1.1.0
mpfr==4.0.2
mpmath==1.3.0
ncurses==6.4
nettle==3.7.3
networkx==3.1
ninja==1.11.1.1
numpy==1.24.3
oauthlib==3.2.2
opencv-python==4.9.0.80
openh264==2.1.1
openjpeg==2.4.0
openssl==3.0.12
packaging==23.2
pillow==10.0.1
pip==23.3.1
platformdirs==4.1.0
protobuf==4.25.2
pyasn1==0.5.1
pyasn1-modules==0.3.0
pycparser==2.21
pyopenssl==23.2.0
pyparsing==3.1.1
pysocks==1.7.1
python==3.8.18
python-dateutil==2.8.2
pytorch==2.0.1
pytorch-cuda==11.7
pytorch-mutex==1.0
pywavelets==1.4.1
pyyaml==6.0.1
readline==8.2
regex==2023.12.25
requests==2.31.0
requests-oauthlib==1.3.1
rsa==4.9
safetensors==0.4.2
scikit-image==0.21.0
scipy==1.10.1
setuptools==68.2.2
six==1.16.0
sqlite==3.41.2
sympy==1.12
tb-nightly==2.14.0a20230808
tbb==2021.8.0
tensorboard-data-server==0.7.2
tifffile==2023.7.10
timm==0.9.12
tk==8.6.12
tokenizers==0.15.1
tomli==2.0.1
torchaudio==2.0.2
torchtriton==2.0.0
torchvision==0.15.2
tqdm==4.66.1
transformers==4.37.1
typing_extensions==4.9.0
urllib3==1.26.18
werkzeug==3.0.1
wheel==0.41.2
xz==5.4.5
yapf==0.40.2
zipp==3.17.0
zlib==1.2.13
zstd==1.5.5

1.3 mamba_ssm 下载 

运行代码显示缺少 mamba_ssm :

Traceback (most recent call last):
  File "basicsr/test.py", line 17, in <module>
    from basicsr.data import build_dataloader, build_dataset  # 数据加载相关函数
  File "/root/autodl-tmp/MambaIR-main/basicsr/__init__.py", line 1, in <module>
    from .archs import *
  File "/root/autodl-tmp/MambaIR-main/basicsr/archs/__init__.py", line 16, in <module>
    _arch_modules = [importlib.import_module(f'basicsr.archs.{file_name}') for file_name in arch_filenames]
  File "/root/autodl-tmp/MambaIR-main/basicsr/archs/__init__.py", line 16, in <listcomp>
    _arch_modules = [importlib.import_module(f'basicsr.archs.{file_name}') for file_name in arch_filenames]
  File "/root/miniconda3/envs/mambair/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/root/autodl-tmp/MambaIR-main/basicsr/archs/mambair_arch.py", line 11, in <module>
    from mamba_ssm.ops.selective_scan_interface import selective_scan_fn, selective_scan_ref
ModuleNotFoundError: No module named 'mamba_ssm'

使用命令:

pip install mamba_ssm

网络连接问题,显示:

error: subprocess-exited-with-error
  
  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [8 lines of output]
      
      
      torch.__version__  = 2.0.1
      
      
      running bdist_wheel
      Guessing wheel URL:  https://github.com/state-spaces/mamba/releases/download/v2.2.2/mamba_ssm-2.2.2+cu118torch2.0cxx11abiFALSE-cp38-cp38-linux_x86_64.whl
      error: <urlopen error [Errno 110] Connection timed out>
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for mamba_ssm
  Running setup.py clean for mamba_ssm
Failed to build mamba_ssm
ERROR: Could not build wheels for mamba_ssm, which is required to install pyproject.toml-based projects

 手动点击 Guessing wheel URL 链接下载:

 https://github.com/state-spaces/mamba/releases/download/v2.2.2/mamba_ssm-2.2.2+cu118torch2.0cxx11abiFALSE-cp38-cp38-linux_x86_64.whl

 手动安装

pip install mamba_ssm-2.2.2+cu118torch2.0cxx11abiFALSE-cp38-cp38-linux_x86_64.whl

二、文件

2.1 basicsr 

存放 train 和 test 的代码

2.2 datasets 

用来存放数据集的位置。

2.3 experiments/pretrained_models

存放模型的位置。

三、推理

本次仅使用 MambaIR_guassian15 模型

首先,修改模型对应的 yml 文件,yml 文件路径如下

MambaIR-main/options/test/mambair

修改 .yml 文件内的数据集路径

  test_1:
    task: denoising_color  # 任务类型:彩色图像去噪
    name: CBSD68  # 数据集的名称
    type: PairedImageDataset  # 数据集类型,表示使用成对的图像(低质量和高质量)
    dataroot_gt: /root/autodl-tmp/MambaIR-main/datasets/ColorDN/CBSD68HQ  # 高质量图像的存储路径
    dataroot_lq: /root/autodl-tmp/MambaIR-main/datasets/ColorDN/CBSD68HQ  # 低质量图像的存储路径
    filename_tmpl: '{}'  # 文件名模板,用于加载图像文件
    noise: 15  # 图像的噪声水平,15、25、50等
    io_backend:
      type: disk  # 数据存储方式:从硬盘读取数据

在终端输入命令:

python basicsr/test.py -opt options/test/mambair/test_MambaIR_ColorDN_level15.yml

 测试成功:

2025-02-03 22:29:32,451 INFO: Validation CBSD68
         # psnr: 34.4823        Best: 34.4823 @ test_MambaIR_ColorDN_level15 iter
         # ssim: 0.9362 Best: 0.9362 @ test_MambaIR_ColorDN_level15 iter

2025-02-03 22:29:32,451 INFO: Testing Kodak24...
2025-02-03 22:30:29,313 INFO: Validation Kodak24
         # psnr: 35.4232        Best: 35.4232 @ test_MambaIR_ColorDN_level15 iter
         # ssim: 0.9309 Best: 0.9309 @ test_MambaIR_ColorDN_level15 iter

2025-02-03 22:30:29,317 INFO: Testing McMaster...
2025-02-03 22:31:11,530 INFO: Validation McMaster
         # psnr: 35.6990        Best: 35.6990 @ test_MambaIR_ColorDN_level15 iter
         # ssim: 0.9355 Best: 0.9355 @ test_MambaIR_ColorDN_level15 iter

2025-02-03 22:31:11,534 INFO: Testing Urban100...
2025-02-03 22:44:21,444 INFO: Validation Urban100
         # psnr: 35.3711        Best: 35.3711 @ test_MambaIR_ColorDN_level15 iter
         # ssim: 0.9537 Best: 0.9537 @ test_MambaIR_ColorDN_level15 iter

效果展示:

 

四、训练

运行命令

python basicsr/train.py -opt options/train/mambair/train_MambaIR_ColorDN_level15.yml

开始迭代:

### 可能的原因分析 当尝试通过 `conda activate mambair` 激活 Conda 环境时遇到失败的情况,可能由以下几个原因引起: 1. **环境名称错误**:如果环境中不存在名为 `mambair` 的环境,则激活命令会失败[^1]。 2. **路径配置问题**:Conda 的二进制文件未正确添加到系统的 PATH 中可能导致无法识别 `conda` 命令或其子命令[^2]。 3. **Shell 初始化不完全**:某些情况下 Shell 配置文件(如 `.bashrc`, `.zshrc`)中的初始化脚本缺失或损坏也会阻止 Conda 正常工作[^3]。 ### 解决方法 #### 方法一:验证目标环境是否存在 可以运行以下命令来查看当前已创建的所有 Conda 环境列表: ```bash conda env list ``` 确认是否有名字为 `mambair` 的条目存在。如果没有找到该环境名,则需先创建它或者修正指定的环境名称[^4]。 #### 方法二:检查并修复PATH变量设置 确保系统能够访问 Conda 安装目录下的 bin 文件夹。通常可以通过重新安装 Miniconda 或 Anaconda 来自动完成此操作;也可以手动编辑 shell profile 文件加入如下行(假设 Conda 被安放在默认位置 `/home/username/miniconda3/bin` 下): ```bash export PATH="/home/username/miniconda3/bin:$PATH" source ~/.bashrc # 对于 bash 用户而言 # source ~/.zshrc 如果您使用的是 zsh 则应执行这一句代替上一句 ``` #### 方法三:重设shell初始化状态 对于 Bash 和 Zsh 用户来说,有时需要显式加载 Conda 提供的 init 脚本来使后续功能可用。这一步骤可通过下面指令实现一次性的效果测试: ```bash . /path/to/your/anaconda3/etc/profile.d/conda.sh ``` 长期生效则建议把上述语句追加至个人 dotfiles 当中去永久化应用[^5]。 --- ### 总结说明 以上三种方式分别针对不同层面可能出现的问题给出了排查方向以及解决方案。实际处理过程中可以根据具体情况逐一试验直至恢复正常运作为止。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值