Skip to content

Leiyi-Hu/ChangeFormer_vPaddle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A TRANSFORMER-BASED SIAMESE NETWORK FOR CHANGE DETECTION

目录

1. 简介

ChangeFormer不同于以往的基于CNN的变化监测框架,使用Transformer模块替代CNN的Conv模块达到抽取特征的目的,同时,使用双流架构从前后时相的影像里抽取多尺度的可识别地域变化的特征,在网络最后使用MLP层进行解码获取影像所示区域的变化像素,并以二值图的形式显示。

IGARS_ChangeFormer-LEVIR_DSFIN

(效果图来自原文)

论文: A TRANSFORMER-BASED SIAMESE NETWORK FOR CHANGE DETECTION

参考repo: ChangeFormer

在此非常感谢$wgcban$等人贡献的ChangeFormer,提高了本repo复现论文的效率。

aistudio体验教程: 地址若验收成功再提供。

2. 数据集和复现精度

repo用到的数据集是LEVIR数据集的采样,具体构建参见ChangeFormer。本repo直接使用其提供的处理后数据集LEVIR-CD-256

数据集简介:

  • 数据集大小:数据集总共有10192张256x256的图像,训练集:7120,验证集:1024,测试集:2048。

  • 数据集下载链接:https://www.dropbox.com/sh/lnjxpflvgjijwoj/AAAgWr3xgOjqtTWPVpbLVcfNa?dl=0

  • 数据格式:

    """

    Change detection data set with pixel-level binary labels;

    ├─A

    ├─B

    ├─label

    └─list

    """

    A: t1时相图像;

    B: t2时相图像;

    label: 真实标签;

    list: 包含 train.txt, val.txt and test.txt, 文件存储了训练集、验证集和测试集的图像名。(注:在实际训练里,使用训练集+验证集进行训练,这也是根据ChangeFormer放出的log确定的。)

    使用train+val训练的证据如下

    • 如下图所示,论文原文中提到,将LEVIR-CD数据集不重叠地切割256*256,得到train/val/test的数量分别为7120/1024/2048。训练使用的batchsize为16
    • 根据ChangeFormer提供的训练权重和log,如图所示,可以看到每个epoch的iterations数为509。只有train+val,才能满足8144/16=509,仅使用训练集7120/16=445,则和提供的log不一致

复现情况(LEVIR数据集):

论文精度(F1) 复现精度(F1) 下载链接
90.40 90.347 链接: https://pan.baidu.com/s/1fFaitZO49_Zxm4mgXnwKaQ 提取码: eq1l

3. 准备数据与环境

3.1 准备环境

repo使用硬件和框架版本等环境的要求如下:

  • 硬件:Nvidia Tesla v100 x1
  • 框架:
    • PaddlePaddle >= 2.3.1

库安装:

  • PaddlePaddle:
python -m pip install paddlepaddle-gpu==2.3.1 -i https://mirror.baidu.com/pypi/simple
  • 其余库:

AI-Studio都有安装,可直接使用。若自行配环境也可根据缺失库安装,三方库不多,安装很方便。

3.2 准备数据

可参见2中数据格式,将自定义数据集按照该格式整理即可使用。

3.3 准备模型

4. 开始使用

4.1 模型训练

下载前文提供的数据后,将其解压到项目根目录下的CDData路径下。

scripts路径下:

cd scripts
# 使用以下命令:
sh run_ChangeFormer_LEVIR.sh

run_ChangeFormer_LEVIR.sh文件内容如下:

#!/usr/bin/env bash

#GPUs
gpus=0

#Set paths
checkpoint_root=./checkpoints
vis_root=./vis
data_name=LEVIR

# hyper params.
img_size=256    
batch_size=16   
lr=0.0001         
max_epochs=200
embed_dim=256

net_G=ChangeFormerV6        #ChangeFormerV6 is the finalized verion

lr_policy=linear
optimizer=adamw                 #Choices: sgd (set lr to 0.01), adam, adamw
loss=ce                         #Choices: ce, fl (Focal Loss), miou
multi_scale_train=True
multi_scale_infer=False
shuffle_AB=False

#Initializing from pretrained weights
pretrain=./pretrained_changeformer/pretrained_changeformer.pdparams # your path to pretrain model.

#Train and Validation splits
split=trainval         #trainval
split_val=test      #test
project_name=CD_${net_G}_${data_name}_b${batch_size}_lr${lr}_${optimizer}_${split}_${split_val}_${max_epochs}_${lr_policy}_${loss}_multi_train_${multi_scale_train}_multi_infer_${multi_scale_infer}_shuffle_AB_${shuffle_AB}_embed_dim_${embed_dim}

# CUDA_VISIBLE_DEVICES=0
cd ..
CUDA_VISIBLE_DEVICES=0 python main_cd.py --img_size ${img_size} --loss ${loss} --checkpoint_root ${checkpoint_root} --vis_root ${vis_root} --lr_policy ${lr_policy} --optimizer ${optimizer} --pretrain ${pretrain} --split ${split} --split_val ${split_val} --net_G ${net_G} --multi_scale_train ${multi_scale_train} --multi_scale_infer ${multi_scale_infer} --gpu_ids ${gpus} --max_epochs ${max_epochs} --project_name ${project_name} --batch_size ${batch_size} --shuffle_AB ${shuffle_AB} --data_name ${data_name}  --lr ${lr} --embed_dim ${embed_dim}

训练日志如下:

image-20220731232502857

全部日志在checkpoints路径下(需下载解压训练权重文件)。

注意⚠️

==这里使用的trainval数据集进行训练,在数据集的list路径下将train.txt和val.txt的内容拼接成trainval.txt即可。==

4.2 模型评估

scripts路径下:

cd scripts
# 使用以下命令:
sh eval_ChangeFormer_LEVIR.sh

eval_ChangeFormer_LEVIR.sh文件内容如下:

#!/usr/bin/env bash

# gpus=0
gpus=0

data_name=LEVIR
net_G=ChangeFormerV6 #This is the best version
split=test
vis_root=./vis
project_name=CD_ChangeFormerV6_LEVIR_b16_lr0.0001_adamw_trainval_test_200_linear_ce_multi_train_True_multi_infer_False_shuffle_AB_False_embed_dim_256_v0
checkpoints_root=./checkpoints
checkpoint_name=best_ckpt.pdparams
img_size=256
embed_dim=256 #Make sure to change the embedding dim (best and default = 256)

cd ..
CUDA_VISIBLE_DEVICES=0 python eval_cd.py --split ${split} --net_G ${net_G} --embed_dim ${embed_dim} --img_size ${img_size} --vis_root ${vis_root} --checkpoints_root ${checkpoints_root} --checkpoint_name ${checkpoint_name} --gpu_ids ${gpus} --project_name ${project_name} --data_name ${data_name}

训练日志如下:

image-20220731232743370

全部日志在checkpoints路径下(需下载解压训练权重文件)。

4.3 模型预测

  • demo
python demo_LEVIR.py

test_2_0000_0000

(测试结果其一)

4.4 模型导出

执行以下命令将动态图导出为静态图

python export_model.py --model_path ${model_path}$  --save_inference_dir ./inference/
  • 参数model_path.pdparams后缀的权重的路径
  • 参数save_inference_dir为静态图的保存文件夹路径

4.5 静态图推理

执行以下命令使用导出的静态图进行推理

python infer.py --model_dir ./inference/ --img_dir ./samples_LEVIR/
  • 参数model_dir为静态图保存的文件夹路径
  • 参数img_dir为待预测的图像文件夹路径,文件夹下需包含AB两个子文件夹

4.6 TIPC基础链条测试

该部分依赖auto_log,需要进行安装,安装方式如下:

auto_log的详细介绍参考https://github.com/LDOUBLEV/AutoLog

git clone https://github.com/LDOUBLEV/AutoLog
cd ./AutoLog/
pip3 install -r requirements.txt
python3 setup.py bdist_wheel
pip3 install ./dist/auto_log-1.2.0-py3-none-any.whl
cd ..
  • 运行命令,准备小批量数据
bash ./test_tipc/prepare.sh test_tipc/configs/ChangeFormer/train_infer_python.txt 'lite_train_lite_infer'
  • 运行命令,小批量数据训练、导出、推理一体化
bash test_tipc/test_train_inference_python.sh test_tipc/configs/ChangeFormer/train_infer_python.txt 'lite_train_lite_infer'

TIPC测试结果(带时间戳)

5. LICENSE

本项目的发布受Apache 2.0 license许可认证。

6. 参考链接与文献

7. 对齐

  • 前向:

image-20220731233632605

同一fake—data,torch和paddle模型输出差值,可认为0.

  • 推理:

加载同一模型权重(pytroch提供的预训练权重):

image-20220731233827682

(paddle)

image-20220731234409109

(pytorch)

测试集测试F1和loss均一致,可知模型组网和实现均已对应。

  • 组网:
    • 组网过程均按照pytorch实现一一对应。

About

implementation of changeformer in paddle.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published