在一些场景中,需要分析图片在不同时刻的变化情况,比如居家的早上、晚上变化,卫星图片不同年份的变化,通过分析变化,掌握空间内事物的变化趋势,那么应该如何分析不同时刻下图片的变化呢?
所谓变化,就是场景中新增那些目标、那些目标去除了,由于图片不是对齐的,所以无法直接通过图片加减法去找到图片变化位置,并且如果涉及变化的目标类别,该方法也无法做到。
本文使用语义分割模型实现图片变化分析,常规的语义分割模型输入一张图片,然后分割图片目标,这里将范式改为:输入前后时刻的图片,输出变化的二值图,最终实现下图效果
![利用分割模型分析场景变化-20250203082348]()
上图分别是前后不同时刻同一位置的卫星图片、人工认定的变化区域、语义分割模型分析的变化区域。可以看出语义分割模型基本找到变化区域
模型定义
本文使用 deeplabv3 + 模型,输入通道由原来的 3 通道变为 6 通道(两张 3 通道的图片合并),输出为 1 个类别的概率图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class DeepLabV3Plus(nn.Module): def __init__(self, aspp_dilations=None, aspp_dropout=True, dropout_prob=0.2, num_classes=1, pretrained=True): super(DeepLabV3Plus, self).__init__()
self.get_input=nn.Conv2d(6, 3, kernel_size=7, stride=2, padding=3, bias=False) self.backbone = resnet18(pretrained=pretrained)
self.aspp = ASPP(aspp_dilations=aspp_dilations, aspp_dropout=aspp_dropout) self.decoder = Decoder(num_classes=num_classes, dropout_prob=dropout_prob) self.freeze_modules = self.backbone
def forward(self, input): input=self.get_input(input) x, feat_2x, feat_4x = self.backbone(input) x = self.aspp(x) x = self.decoder(x, feat_2x, feat_4x) x = F.interpolate(x, size=input.size()[2:], mode="bilinear", align_corners=True)
return x
|
数据加载
由于此处分析的图片是卫星图片,分辨率很大,这里首先使用 sahi 将图片重叠切分为小图,然后再进行训练,切割后图片如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| from sahi.slicing import slice_image
img1_path='/home/wushaogui/MyCodes/Pytorch_Change_detection/CD_Data_GZ/labels_change/P_GZ_test4_2010_2019.png' data_name='test'
SliceImageResult=slice_image( image=img1_path, output_file_name=data_name, output_dir=None, slice_height=1024, slice_width=1024, overlap_height_ratio=0.5, overlap_width_ratio=0.5 )
show_images(SliceImageResult.images)
|
![利用分割模型分析场景变化-20250203084716]()
训练过程
使用 tensorboard 查看训练过程,可以将看出 fscore 逐渐提升,最终训练集 fscore>0.98,验证集 fscore~0.87
![利用分割模型分析场景变化-20250203084109]()