opencd变化检测工具箱从一系列开源的通用视觉任务工具开始,包括 OpenMMLab Toolkits、PyTorch Image Models 等。它逐渐演变成一个统一的平台,涵盖了许多流行的变化检测方法和现代模块。

1. 运行环境

类别 详细信息
CPU 16 vCPU Intel(R) Xeon(R) Platinum 8352V CPU @ 2.10GHz
CPU 核心数 16核心
GPU RTX 4090
GPU 显存 24 GB
CUDA 版本 11.8
操作系统 ubuntu20.04
Python 版本 3.8
PyTorch 版本 2.0.0

2. 安装 conda

参考这篇文章: https://docs.aheadai.cn/60.html

3. conda环境创建

conda create -n opencd python==3.8
conda activate opencd

4. 源码下载

wget https://mirrors.aheadai.cn/scripts/open-cd-main.zip
unzip open-cd-main
cd open-cd-main

5. torch下载

pip install torch==2.4.1 --index-url https://pypi.mirrors.ustc.edu.cn/simple
conda install cudatoolkit=12.1

6. 环境配置

如果下载超时或者过慢,可以在pip或mim命令后加上: --index-url https://pypi.mirrors.ustc.edu.cn/simple

# Install OpenMMLab Toolkits as Python packages
pip install -U openmim
mim install mmengine==0.10.4
mim install mmcv==2.1.0
mim install mmpretrain==1.2.0
pip install mmsegmentation==1.2.2
pip install mmdet==3.3.0

可能遇到的报错如下:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
datasets 2.20.0 requires requests>=2.32.2, but you have requests 2.28.2 which is incompatible.
datasets 2.20.0 requires tqdm>=4.66.3, but you have tqdm 4.65.2 which is incompatible.

报错原因:

requests 版本冲突

  • datasets 需要 requests>=2.32.2,但当前安装的是 requests==2.28.2

tqdm 版本冲突

  • datasets 需要 tqdm>=4.66.3,但当前安装的是 tqdm==4.65.2

解决办法:

#先卸载原来的requests和tqdm,再安装符合要求的版本
pip install requests>=2.32.2 tqdm>=4.66.3

7. 依赖安装

pip install -v -e .
pip install ftfy
pip install regex
pip install torchvision
pip install pynvml

8. 预训练过程

运行过程的可选参数如下:

参数名 必选/可选 作用
config 必选 训练配置文件路径。
--work-dir 可选 指定保存日志和模型的路径。
--resume 可选 是否从最新的检查点继续训练。
--amp 可选 是否启用自动混合精度训练。
--cfg-options 可选 覆盖配置文件中的设置。
--launcher 可选 分布式训练启动器类型(none, pytorch, slurm, mpi)。
--local_rank 可选 当前进程的 GPU ID(分布式训练时由启动器传递,无需手动设置)。

数据集下载:

wget https://mirrors.aheadai.cn/data/opencd_train.zip
wget https://mirrors.aheadai.cn/data/opencd_val.zip
mkdir -p data/LEVIR-CD
cd data/LEVIR-CD
mkdir train val
cd ../..
unzip opencd_train.zip -d data/LEVIR-CD/train
unzip opencd_val.zip -d data/LEVIR-CD/val

训练脚本下载:

wget https://mirrors.aheadai.cn/scripts/opencd_train1.py
mv opencd_train1.py ./tools/

开始预训练

python tools/opencd_train1.py configs/changer/changer_ex_r18_512x512_40k_levircd.py --work-dir ./changer_r18_levir_workdir

由于该模型的配置文件时跨仓库载入的,如果需要修改模型其他参数,可以手动创建自定义模块并配置文件,详细操作请查看MMEngine仓库教程:配置 — mmengine 0.10.5 文档

最终花费173.91分钟,训练了80个epoch,共40000步得到如下输出与log文件:

changer_r18_levir_workdir
    |--20241129_122434
        |-- ...
    |--best_mIoU_iter_32000.pth
    |--changer_ex_r18_512x512_40k_levircd.py
    |--iter_4000.pth
    |--iter_8000.pth
    |-- ...
    |--iter_32000.pth
    |-- last_checkpoint

https://mirrors.aheadai.cn/log/opencd_4090dl_20241129_101035.log

https://mirrors.aheadai.cn/log/opencd_4090dl_metrics.log

2024/11/29 13:03:36 - mmengine - INFO - Iter(train) [39800/40000]  lr: 2.5642e-05  eta: 0:00:52  time: 0.2273  data_time: 0.1430  memory: 4309  loss: 0.0181  decode.loss_ce: 0.0181  decode.acc_seg: 99.1360
2024/11/29 13:03:49 - mmengine - INFO - Iter(train) [39850/40000]  lr: 1.9231e-05  eta: 0:00:39  time: 0.2915  data_time: 0.2081  memory: 4309  loss: 0.0161  decode.loss_ce: 0.0161  decode.acc_seg: 99.3088
2024/11/29 13:04:02 - mmengine - INFO - Iter(train) [39900/40000]  lr: 1.2821e-05  eta: 0:00:26  time: 0.2237  data_time: 0.1398  memory: 4309  loss: 0.0167  decode.loss_ce: 0.0167  decode.acc_seg: 99.2259
2024/11/29 13:04:15 - mmengine - INFO - Iter(train) [39950/40000]  lr: 6.4104e-06  eta: 0:00:13  time: 0.2986  data_time: 0.2151  memory: 4309  loss: 0.0130  decode.loss_ce: 0.0130  decode.acc_seg: 99.9657
2024/11/29 13:04:28 - mmengine - INFO - Exp name: changer_ex_r18_512x512_40k_levircd_20241129_101035
2024/11/29 13:04:28 - mmengine - INFO - Iter(train) [40000/40000]  lr: 0.0000e+00  eta: 0:00:00  time: 0.2218  data_time: 0.1401  memory: 4309  loss: 0.0131  decode.loss_ce: 0.0131  decode.acc_seg: 99.3009
2024/11/29 13:04:28 - mmengine - INFO - Saving checkpoint at 40000 iterations
本文系作者 @ admin 原创发布在 文档中心 | AheadAI ,未经许可,禁止转载。