赛题:
耕地的数量和质量是保持农业可持续发展的关键,利用卫星遥感影像可以识别并提取耕地,并对耕地进行遥感制图,准确的耕地分布能够为国家决策部门提供重要支撑。目前高精度的耕地信息提取主要还是依靠人工解译,耗费大量人力、财力且效率较低,因此,遥感图像的耕地识别算法研究将对耕地遥感制图提供重要帮助。
资源三号(ZY-3)卫星是中国第一颗自主的民用高分辨率立体测绘卫星,通过立体观测,可以测制1∶5万比例尺地形图,为国土资源、农业、林业等领域提供服务,资源三号填补了中国立体测图这一领域的空白。图片来源于资源三号卫星获取的遥感图像数据,空间分辨率为2米,光谱为可见光波段(红,绿,蓝)。
题目提供8幅图像和相应耕地标签,用于参赛者模型训练和测试,图像为tif格式。原图像预览图如图1所示,标签图预览图如图2所示,标签图中白色(值为1)代表的是耕地类,黑色(值为0)代表的是背景类。另提供2幅图像作为测试实例。
代码:
使用Paddlepaddle的DeepLab V3 + 语义分割模型:
! pip install paddlex -i https://mirror.baidu.com/pypi/simple
# 设置使用0号GPU卡(如无GPU,执行此代码后仍然会使用CPU训练模型)
import matplotlib
matplotlib.use('Agg')
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
import paddlex as pdx
from paddlex.seg import transforms
train_transforms = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.Resize(target_size=512),
transforms.RandomBlur(),
transforms.RandomRotate(),
transforms.RandomDistort(),
transforms.RandomPaddingCrop(crop_size=500),
transforms.Normalize()
])
eval_transforms = transforms.Compose([
transforms.Resize(512),
transforms.Normalize()
])
train_dataset = pdx.datasets.SegDataset(
data_dir='optic_disc_seg',
file_list='optic_disc_seg/train_list.txt',
label_list='optic_disc_seg/labels.txt',
transforms=train_transforms,
shuffle=True)
eval_dataset = pdx.datasets.SegDataset(
data_dir='optic_disc_seg',
file_list='optic_disc_seg/val_list.txt',
label_list='optic_disc_seg/labels.txt',
transforms=eval_transforms)
2020-12-27 09:38:56 [INFO] 112 samples in file optic_disc_seg/train_list.txt
2020-12-27 09:38:56 [INFO] 8 samples in file optic_disc_seg/val_list.txt
num_classes = len(train_dataset.labels)
model = pdx.seg.DeepLabv3p(num_classes=num_classes, backbone='Xception65')
model.train(
num_epochs=80,
train_dataset=train_dataset,
train_batch_size=4,
eval_dataset=eval_dataset,
learning_rate=0.01,
save_interval_epochs=20,
save_dir='output/deeplab',
use_vdl=True)
import matplotlib.pyplot as plt
import paddlex as pdx
import numpy as np
import cv2
%matplotlib inline
model = pdx.load_model('output/deeplab/best_model')
image_name = 'optic_disc_seg/JPEGImages/Data6.png'
result = model.predict(image_name)
pdx.seg.visualize(image_name, result, weight=0.4, save_dir='./')
pred_map = result['label_map']
anno = cv2.imread('optic_disc_seg/Annotations/Data6_reference.png', -1)
print(np.max(pred_map), np.max(anno))
plt.imshow(anno)
plt.title('True')
plt.show()
plt.imshow(pred_map)
plt.title('Pred')
plt.show()
2020-12-27 09:53:00 [INFO] Model[DeepLabv3p] loaded.
2020-12-27 09:53:00 [INFO] The visualized result is saved as ./visualize_Data6.png
1 1
import paddlex as pdx
import cv2
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
model = pdx.load_model('output/deeplab/best_model')
for i in range(2):
image_name = 'Test{}.png'.format(i+1)
result = model.predict(image_name)
pdx.seg.visualize(image_name, result, weight=0.4, save_dir='./')
pred_map = result['label_map']
pred_map = cv2.merge([pred_map, pred_map, pred_map]) * 255
img = cv2.imread(image_name)
result = np.hstack([img, pred_map])
plt.imshow(result)
plt.show()
2020-12-27 09:53:03 [INFO] Model[DeepLabv3p] loaded.
2020-12-27 09:53:04 [INFO] The visualized result is saved as ./visualize_Test1.png
2020-12-27 09:53:04 [INFO] The visualized result is saved as ./visualize_Test2.png
扫描二维码关注公众号,回复:
12481093 查看本文章

!zip -r code ./
关注我的公众号:
感兴趣的同学关注我的公众号——可达鸭的深度学习教程: