本文围绕遥感变化检测项目展开,参考多个相关项目,指出存在随机bug及解决办法。其在相同训练轮数下精度提升明显,还分享了调参思路,涉及数据集、模型、训练超参、后处理等方面。此外,详述了数据预处理、网络训练、测试、推理等流程,并附相关报错及统计数据。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

前言
-
本项目实现参考了以下项目,
- 【官方】第十一届 “中国软件杯”百度遥感赛项:变化检测功能;
- 昇腾杯-变化检测赛道复赛方案分享——PaddleCD;
- PaddleRS;
- PaddleSeg
PaddleRS版的baseline(以下简称原bl),个人测试精度为0.768,同样的配置在本项目PaddleCD下,个人测试精度则为0.764
该项目存在随机bug,暂时不知道原因以及彻底解决方法;如果出现特定报错内容(详见文末附录1),重启后重新执行代码一般可以解决;建议在理解代码的情况下,尝试在PaddleRS中修改运行
该项目在与原bl相同训练轮数下,bit单模精度达到0.86221,训练日志手动保存在model_log/bit_hr18.txt中;按照调参思路进行调整,bit单模精度可以达到0.889+;
感谢 @古代飞 大佬的baseline, 感谢@开着奔驰种地、@我不是南通 以及群内各位大佬的经验分享
调参思路
为方便阅读,以下内容中提到的xx%为调参涨点,是使用trick后提交分数的百分比变化
但因个人实验记录不完善、实验内容过多未完全控制变量 以及 某些玄学问题,以下调参涨点可能存在误差,仅供参考
1. 从数据集进行分析
1.1 A、B时相数据集光谱分布差异较大
A、B时相的训练集、测试集、全集的均值标准差统计如下(因显示问题采用图片格式,文字格式详见文末附录2):
由统计数据可见:A、B时相的均值标准差存在较大差异,但训练集与验证集差异较小
针对光谱差异问题,尝试了3种解决方案:
- 使用颜色抖动进行数据增强,+3%
- 使用不同均值标准差进行归一化,+2%
- 使用快速傅里叶变化进行光谱变换, -1%
上述方法单独使用,1、2可以提分,但在实验最后,叠加各种trick后发现 方法2会降0.2%,推测可能由于模型较强的特征表达能力以及方法1的影响,使得方法2失效,因此针对该问题仅采用方法1,即:
RandomDistort
1.2 原始图像较大,数据量较少
训练集共637共1024*1024样本,测试集共363个1024*1024样本,训练测试比约2:1
原bl中,针对该问题的解决方案是从原始样本中随机裁剪部分区域并resize为256*256。
个人尝试了2种方法:
- 将每个原始样本不重叠地裁剪为16个256*256
- 将每个原始样本裁剪为5个512*512 (除了不重叠的4个,还包括一个在样本正中心的512*512)
上述方法在不同超参下作用不同,从-2% ~ +2%均有,表现并不稳定。
此外,人工裁剪的样本生成的样本,由于目前无法使用cutmix等跨样本数据增强方法,导致单个样本的采样区域限定在上述16或5个区域内,因此在数据多样性上小于原始样本。
综上,仍然采用原bl,通过随机裁剪部分区域进行训练
1.3 类别不均衡
经过统计,背景与变化类别的像素占比为95.3:4.6,类别严重不均衡,并且变化类别仅在573个样本中存在(统计数据见附录2)
针对上述问题,可以采用以下解决方案:
- 通过loss进行控制:
- 换用diceloss,从iou进行优化,-1%
- 换用polyceloss,从iou进行优化,-0.3~+0.3%
- 在celoss中加入类别权重,-2%
- 使用focalloss, -1%
- 针对性数据加强,对变化部分样本进行过采样,增强变化比例
- 去除全背景样本
本次实验仅尝试了方案1,效果并不明显,因此针对此问题并未采用任何解决方案;后续可以尝试2、3方案
2. 从模型进行分析
2.1 backbone过于简单
在CV下游任务中,任务精度往往与backbone的特征表达能力有关,原bl中的backbone为resnet18,结构较为简单 针对该问题,采用以下解决方案:
- 采用同构高级模型
- resnet34,-0.2%
- resnet50,-1%
- 采用异构高级模型
- hrnet18,+2%
上述方法中,同构高级模型表现不佳,推测为训练轮数过少,网络训练不完全,网络过拟合,因此弃用该方案。
异构高级表现优异,此外还可以尝试swin、resnest等多种不同新backbone。因此针对该问题仅采用方法2,即:
- hrnet18,+2%
resnet18-->hrnet18
2.2 尝试不同网络结构
- 除bit外,原bl还提供了STANet、SNUNet、DSIFN、DSAMNet、ChangeStar等多种变化检测模型;
- PaddleCD中也提供了各种语义分割模型,可以修改为相应的变化检测模型;
- 此外,也可以尝试复现torch版的各种论文模型;
- 相同超参配置下,部分模型表现优于bit
3. 从训练超参进行调整
可以修改以下超参数:
- epoch/step,原bl中的训练数较少,通过增加训练数可以显著提分,可以提升的上限与其他超参相关,但建议总体训练时长不超过12h
- 学习率:
- lr/起始学习率,原bl的起始学习率略高,导致一些无效训练时长,略微降低lr后可以快速收敛至较高精度的区间
- lr_scheduler/学习率调度器,原bl采用的是等间隔固定比例衰减,尝试过poly衰减策略,效果并不明显,可以尝试不同衰减策略,增加warmup等
- optimizer/优化器,原bl使用adam,目前较为流行的有adamw、sgd、adamax等,可以尝试使用
- batch_size/批处理数,一般来说较大batch size会使得收敛较快,但在充分收敛的前提下对总体精度并无较大影响
- transform/数据增强策略:
- RandomCrop, 增大crop的尺寸,每次输入到网络中更大的图,往往会增加精度
- RandomFlipOrRotation,除了Flip进行增强外,还可以通过Rotation的方式进行增强
- RandoBlur,通过滤波平滑进行数据增强
- RandomSwap,通过交换AB时相影像进行增强
综上,最终在原bl上进行一下修改:
more steplr = 0.0004adamw optimizer RandomCrop 384RandomFlipOrRotationRandoBlurRandomSwap
4. 从后处理进行调整
后处理有多种方式,针对训练时采用的数据增强策略,可以采用以下方案:
- flip预测,多次flip后取均值得分,+0.3%
- ms预测,输入不同尺度进行预测,-0.2%
- swap预测,以ab、ba分别输入网络进行预测,-0.1%~+0.1%
- 滑窗预测,按照输入大小或者其他尺寸,将原图裁剪为不同大小图像输入网络进行预测,-0.3%
针对输出结果,有以下方案:
5. 调整阈值/缩放logit值,目的在于识别出更多概率值较小的变化区域
6. 形态学后处理,优化边缘区域
后处理的提升幅度较小且不稳定,一般最后使用
综上,最终在原bl上进行以下修改:
flip预测 缩放logit值 形态学后处理
5. 其他trick
5.1 单模集成
即 将一个模型的不同结果进行集成
在后处理中使用的flip预测、ms预测属于单模集成的内容
ema、swa等也属于单模集成内容,其主要思路为将不同epoch得到的较好的模型参数进行集成
k折交叉验证,将数据集均分为k份,每次将其中一份作为验证集其余为训练集进行训练
本次竞赛并未使用上述单模集成内容,但据以往经验看,上述内容有一定的提分作用
5.2 多模集成
即 将多个模型的不同结果进行集成
包括硬投票集成和软投票集成,区别在于使用预测的类别结果还是类别得分进行集成
经验表明不同架构的模型相互集成,得分较高
5.3 跨样本数据增强
如cutmix、mixup、cutout、mosaic、copypaste等等长得很像的、涉及多个样本之间的数据增强,据说也有效果,目前原bl和本项目暂不支持该系列操作
5.4 半监督/自监督/伪标签
本次比赛不支持增加数据集;
在其他任务中可以尝试加入未标注的图像,生成伪标签进行监督/自监督
6. 小结
以上为本次比赛中,使用或想到的调参思路,此外还有其他各种trick,大家可以多看论文与经验分享进行总结实验
为鼓励大家动手实践,以上调参思路并未完全在代码中实现,可以根据思路自己动手实践
上述tricke独使用时可能提点较高或较低,但与其他trick组合使用时可能是另一种效果,需要多实践尝试
——————————————————————————————————————分割线——————————————————————————————————————
数据预处理
# 清除cell输出结果def clear_output():
"""
clear output for both jupyter notebook and the console
"""
import os
os.system('cls' if os.name == 'nt' else 'clear') from IPython.display import clear_output as clear
clear()
# 解压数据!unzip -oq /home/aistudio/data/data134796/train_data.zip -d /home/aistudio/data/src/ !unzip -oq /home/aistudio/data/data134796/test_data.zip -d /home/aistudio/data/src/
# 安装依赖库!pip install -r /home/aistudio/PaddleCD/requirements.txt clear_output()
# 将0、255修改为0、1!python /home/aistudio/work/data_generate.py /home/aistudio/data/src/train/label# 直接拷贝已有的数据集划分文件,保证每次训练评价指标相同!cp /home/aistudio/work/val.txt /home/aistudio/data/src/val.txt# !cp /home/aistudio/work/train.txt /home/aistudio/data/src/train.txt!cp /home/aistudio/work/train8cp.txt /home/aistudio/data/src/train.txt # 将训练集复制8次,减少每次读取训练集的损耗时间
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/skimage/io/manage_plugins.py:23: UserWarning: Your installed pillow version is < 8.1.2. Several security issues (CVE-2021-27921, CVE-2021-25290, CVE-2021-25291, CVE-2021-25293, and more) have been fixed in pillow 8.1.2 or higher. We recommend to upgrade this library. from .collection import imread_collection_wrapper 100%|█████████████████████████████████████████| 637/637 [00:46<00:00, 13.60it/s]
# 数据统计# !python /home/aistudio/work/data_sta.py
gts class sta:
class sta : 100%|█████████████████████████████| 637/637 [00:06<00:00, 97.32it/s]
bg change
Pixel_sum 636876269 31066643
Pixel_pct 0.953489 0.0465109
Sample_sum 638 573
Sample_pct 0.526837 0.473163
save to /home/aistudio/data/src/train/label.csv
finish
网络训练
# 配置文件路径与模型保存路径# COF_PATH = '/home/aistudio/PaddleCD/bit_config/bit_baseline.yml'# MD_PATH = '/home/aistudio/best_model/bit_baseline.pdparams'# COF_PATH = '/home/aistudio/PaddleCD/bit_config/bit_res18.yml'# MD_PATH = '/home/aistudio/best_model/bit_res18.pdparams'COF_PATH = '/home/aistudio/PaddleCD/bit_config/bit_hr18.yml'MD_PATH = '/home/aistudio/best_model/bit_hr18.pdparams'
# 模型训练print(COF_PATH.center(100, '*'),'\n\n')print(MD_PATH.center(100, '*'),'\n\n')# 与paddlers训练轮数相同,训练时长55min!python /home/aistudio/PaddleCD/train.py \
--save_dir /home/aistudio/data/output \
--config $COF_PATH \
--do_eval \
--use_vdl \
--iters 3700 \
--save_interval 370 \
--batch_size=16 \
--log_iters 50 \
--num_workers 8 \
--seed 1919810
# 后台训练时,需要清空输出,保证notebook正常导入;notebook训练时,可以不用# clear_output()
# 将最优模型拷贝到/home/aistudio/best_model路径下,将训练日志拷贝到/home/aistudio/model_log路径下!mkdir /home/aistudio/best_model/
!cp /home/aistudio/data/output/best_model/model.pdparams $MD_PATH
!find /home/aistudio/data/output -name '*.log' -exec cp -t /home/aistudio/model_log/ "{}" +
测试
# 普通测试!python /home/aistudio/PaddleCD/val.py \
--batch_size 1 \
--config $COF_PATH \
--model_path $MD_PATH
# 增强测试,加入翻转预测!python /home/aistudio/PaddleCD/val.py \
--aug_eval \
--flip_vertical \
--batch_size 1 \
--config $COF_PATH \
--model_path $MD_PATH
# # 增强测试,加入多尺度预测# !python /home/aistudio/PaddleCD/val.py \# --aug_eval \# --flip_vertical \# --batch_size 1 \# --scales 0.5 1.0 2.0\# --config $COF_PATH \# --model_path $MD_PATH
# # 增强测试,加入交换预测# !python /home/aistudio/PaddleCD/val.py \# --aug_eval \# --swap 7\# --flip_vertical \# --batch_size 1 \# --scales 0.5 1.0 2.0\# --config $COF_PATH \# --model_path $MD_PATH
推理
# 普通推理,推理结果保存在/home/aistudio/data/src/result下# bit_hr18推理时长为1分35秒!python /home/aistudio/PaddleCD/predict.py \
--config $COF_PATH \
--model_path $MD_PATH \
--image_path /home/aistudio/data/src/test/A \
--image_path2 /home/aistudio/data/src/test/B \
--batch_size 32 \
--save_dir /home/aistudio/data/src
# 增强推理,加入翻转预测,推理结果保存在/home/aistudio/data/src/result下# bit_hr18推理时长为2分46秒!python /home/aistudio/PaddleCD/predict.py \
--config $COF_PATH \
--model_path $MD_PATH \
--image_path /home/aistudio/data/src/test/A \
--image_path2 /home/aistudio/data/src/test/B \
--aug_pred \
--flip_vertical \
--batch_size 1 \
--save_dir /home/aistudio/data/src
# # 增强推理,加入多尺度预测,推理结果保存在/home/aistudio/data/src/result下# !python /home/aistudio/PaddleCD/predict.py \# --config $COF_PATH \# --model_path $MD_PATH \# --image_path /home/aistudio/data/src/test/A \# --image_path2 /home/aistudio/data/src/test/B \# --aug_pred \# --flip_vertical \# --batch_size 1 \# --scales 0.5 1.0 2.0 \# --save_dir /home/aistudio/data/src
# # 增强推理,加入交换预测推理结果保存在/home/aistudio/data/src/result下# !python /home/aistudio/PaddleCD/predict.py \# --config $COF_PATH \# --model_path $MD_PATH \# --image_path /home/aistudio/data/src/test/A \# --image_path2 /home/aistudio/data/src/test/B \# --aug_pred \# --swap 7 \# --flip_vertical \# --batch_size 1 \# --scales 0.5 1.0 2.0 \# --save_dir /home/aistudio/data/src
# # 滑窗增强推理,推理结果保存在/home/aistudio/data/src/result下# bit_hr18推理时长为12分# !python /home/aistudio/PaddleCD/predict.py \# --config $COF_PATH \# --model_path $MD_PATH \# --image_path /home/aistudio/data/src/test/A \# --image_path2 /home/aistudio/data/src/test/B \# --aug_pred \# --flip_vertical \# --is_slide \# --batch_size 1 \# --patch_size 16 \# --crop_size 384 384 \# --stride 128 128 \# --save_dir /home/aistudio/data/src
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/skimage/io/manage_plugins.py:23: UserWarning: Your installed pillow version is < 8.1.2. Several security issues (CVE-2021-27921, CVE-2021-25290, CVE-2021-25291, CVE-2021-25293, and more) have been fixed in pillow 8.1.2 or higher. We recommend to upgrade this library.
from .collection import imread_collection_wrapper
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
from collections import MutableMapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
from collections import Iterable, Mapping
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
from collections import Sized
2022-05-29 16:54:15 [INFO]
---------------Config Information---------------
batch_size: 16
iters: 20000
loss:
coef:
- 1
types:
- type: CrossEntropyLoss
lr_scheduler:
gamma: 0.5
learning_rate: 0.0004
step_size: 1000
type: StepDecay
model:
backb: hrnet18
in_channels: 3
num_classes: 2
type: BIT
optimizer:
type: AdamW
train_dataset:
dataset_root: /home/aistudio/data/src/
mode: train
num_classes: 2
train_path: /home/aistudio/data/src/train.txt
transforms:
- aspect_ratio:
- 1.0
- 1.0
crop_size:
- 384
- 384
scaling:
- 0.25
- 1.0
type: RandomCrop
- probs:
- 0.6
- 0.0
probsf:
- 0.4
- 0.4
- 0.2
- 0.0
- 0.0
probsr:
- 0.0
- 0.0
- 0.0
type: RandomFlipOrRotation
- type: RandomDistort
- type: RandomBlur
- type: RandomSwap
- mean:
- 0.5
- 0.5
- 0.5
std:
- 0.5
- 0.5
- 0.5
type: Normalize
type: RSCD
val_dataset:
dataset_root: /home/aistudio/data/src/
mode: val
transforms:
- mean:
- 0.5
- 0.5
- 0.5
std:
- 0.5
- 0.5
- 0.5
type: Normalize
type: RSCD
val_path: /home/aistudio/data/src/val.txt
------------------------------------------------
W0529 16:54:15.094134 14743 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1
W0529 16:54:15.094177 14743 device_context.cc:465] device: 0, cuDNN Version: 7.6.
2022-05-29 16:54:18 [INFO] Loading pretrained model from https://bj.bcebos.com/paddleseg/dygraph/hrnet_w18_ssld.tar.gz
2022-05-29 16:54:19 [INFO] There are 1525/1525 variables loaded into HRNet.
2022-05-29 16:54:19 [INFO] Number of predict images = 363
2022-05-29 16:54:19 [INFO] Loading pretrained model from /home/aistudio/best_model/bit_hr18.pdparams
2022-05-29 16:54:20 [INFO] There are 1653/1653 variables loaded into BIT.
2022-05-29 16:54:20 [INFO] Start to predict...
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
if data.dtype == np.object:
/home/aistudio/PaddleCD/paddleseg/core/infer.py:213: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
rows = np.int(np.ceil(1.0 * (h_im - h_crop) / h_stride)) + 1
/home/aistudio/PaddleCD/paddleseg/core/infer.py:214: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
cols = np.int(np.ceil(1.0 * (w_im - w_crop) / w_stride)) + 1
363/363 [==============================] - 738s 2s/ste
# 将0、1变为0、255!python /home/aistudio/work/data_postprocess.py
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/skimage/io/manage_plugins.py:23: UserWarning: Your installed pillow version is < 8.1.2. Several security issues (CVE-2021-27921, CVE-2021-25290, CVE-2021-25291, CVE-2021-25293, and more) have been fixed in pillow 8.1.2 or higher. We recommend to upgrade this library. from .collection import imread_collection_wrapper 96%|███████████████████████████████████████▏ | 347/363 [00:18<00:00, 20.46it/s]
# 结果打包!zip -j /home/aistudio/submisson.zip /home/aistudio/data/src/result/* > /dev/null
——————————————————————————————————————分割线——————————————————————————————————————
附录
附录1——保存内容:
SystemError: (Fatal) DataLoader process (pid 1. If run DataLoader by DataLoader.from_generator(...), queue capacity is set by from_generator(..., capacity=xx, ...). 2. If run DataLoader by DataLoader(dataset, ...), queue capacity is set as 2 times of the max value of num_workers and len(places). 3. If run by DataLoader(dataset, ..., use_shared_memory=True), set use_shared_memory=False for not using shared memory.) exited is killed by signal: 2992. It may be caused by insufficient shared storage space. This problem usually occurs when using docker as a development environment. Please use command `df -h` to check the storage space of `/dev/shm`. Shared storage space needs to be greater than (DataLoader Num * DataLoader queue capacity * 1 batch data size). You can solve this problem by increasing the shared storage space or reducing the queue capacity appropriately. Bus error (at /paddle/paddle/fluid/imperative/data_loader.cc:177)
附录2——均值标准差统计数据:
- train A : mean [0.44647953, 0.44253506, 0.377722459], std [0.192316791, 0.181008749, 0.167992736]
- train B : mean [0.34149347, 0.334706621, 0.285607109], std [0.143612299, 0.139155252,0.131667209 ]
- test A : mean [0.438983135, 0.436034726, 0.375627972], std [0.211983594, 0.191771049, 0.178430633]
- test B : mean [0.347689202, 0.346141112, 0.306166659], std [0.155347389, 0.148615829, 0.147250213]
- train+test A : mean [0.443758338, 0.440175439, 0.37696216 ], std [0.199455841, 0.184915464, 0.171781692]
- train+test B : mean [0.343742521, 0.338857341, 0.293070225], std [0.147872137, 0.142589441, 0.13732384 ]
附录3——类别统计数据:
bg changePixel_sum 636876269 31066643Pixel_pct 0.953489 0.0465109Sample_sum 638 573Sample_pct 0.526837 0.473163










