robosat调试


#Robosat 使用说明

0.参考资料

  1. 手把手教你如何使用 RoboSat 自动提取建筑物
    2.Robosat
  2. 解密 Mapbox 卫星影像处理神器 Robosat

1. 系统准备工作

1.1 设备及系统

准备一台安装 Linux 或 MacOS 系统的机器,可以是 CentOS、Ubuntu 或 MacOS。机器可以是实体机,也可以是 VMware 虚拟机。

1.2 安装 Docker

在机器中安装Docker,不建议是Windows版Docker。MacOS安装DockerCentOS安装Docker

1.3 在 Docker 中安装 Robosat

Robosat 的 Docker Hub

​ 可以使用两种方式安装 Robosat:

  • 使用 CPU 容器:
docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu --help
docker run --runtime=nvidia -it --rm -v $PWD:/data --ipc=host mapbox/robosat:latest-gpu train --model /data/model.toml --dataset /data/dataset.toml --workers 4

在这里插入图片描述
上图文件夹路径 :/home/sari/Robosat/robosat_buildings_training/shanghaidataset

2. 数据准备工作

2.1下载osm数据

保存为china-latest.osm.pbf文件

#这里下载中国地图
wget http://download.geofabrik.de/asia/china-latest.osm.pbf

通过 geojson.io 绘制想要提取建筑物的范围,使用矩形框即可。将自动生成的 geojson 保存为 shanghaimap.geojson
在这里插入图片描述
上图左边为地图,地图右边一列点击按对应多边形选择区域,上图右边为矩形区域四个坐标。

2.2 osmium-tool工具提取2.1中矩形区域osm数据

apt-get install osmium-tool
osmium extract --bbox '38.9410400390625,-7.0545565715284955,39.70458984374999,-5.711646879515092' china-latest.osm.pbf --output shmap.osm.pbf
#bbox 参数设置坐上和右下坐标,从2.1图中5第1、3个点获取
#结果保存至文件shmap.osm.pbf

2.3 利用 rs extract 来从 OSM 的底图提取几何图形。

docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu extract --type building /data/shmap.osm.pbf /data/shbuildings.geojson
#保存至shbuildings.geojson

2.4 提取训练区覆盖的瓦片行列号

​ 使用 robosat 的 cover 命令,即可获取当前训练区矢量数据覆盖的瓦片行列号,并使用 csv 文件存储。

docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu cover  --zoom 18 /data/shbuildings.geojson /data/shbuildings.tiles

cover 命令的参数介绍:

usage: ./rs cover [-h] --zoom ZOOM features out

  • positional arguments:
    • features path to GeoJSON features
    • out path to csv file to store tiles in
  • optional arguments:
    • -h, --help show this help message and exit
    • --zoom ZOOM zoom level of tiles (default: None)

这里是获取在 18 级下训练区覆盖的瓦片行列号。18 级是国内地图通用的最大级别,如果有国外更清晰数据源,可设置更高地图级别。

2.5下载训练区遥感影像瓦片

​ 使用 robosat 的 download 工具,即可获取当前训练区矢量数据覆盖的遥感影像,下载的瓦片通过 2.3 节中的shbuildings.tiles 确定,图片保存至tiles文件夹。

docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu download https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x} /data/shbuildings.tiles /data/tiles

download 命令的参数介绍:

usage: ./rs download [-h] [--ext EXT] [--rate RATE] url tiles out

  • positional arguments:
    • url endpoint with {z}/{x}/{y} variables to fetch image tiles from
    • tiles path to .csv tiles file
    • out path to slippy map directory for storing tiles
  • optional arguments:
    • -h, --help show this help message and exit
    • --ext EXT file format to save images in (default: webp)
    • --rate RATE rate limit in max. requests per second (default: 10)

2.6 制作训练区矢量数据蒙版标记

​ 使用 2.3 节中制作的 shbuildings.geojson 数据,通过 robosat 的 rasterize 工具可制作训练区矢量数据的蒙版标记数据。蒙版标记数据与瓦片数据一一相对应,使用同样的 shbuildings.tiles 瓦片列表产生。

docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu rasterize --dataset /data/dataset-building.toml --zoom 18 --size 256 /data/shbuildings.geojson /data/shbuildings.tiles /data/masks

rasterise 命令的参数介绍:

usage: ./rs rasterize [-h] --dataset DATASET --zoom ZOOM [--size SIZE] features tiles out

  • positional arguments:

    • features path to GeoJSON features file
    • tiles path to .csv tiles file
    • out directory to write converted images
  • optional arguments:
    -h, --help show this help message and exit
    --dataset DATASET path to dataset configuration file (default: None)
    --zoom ZOOM zoom level of tiles (default: None)
    --size SIZE size of rasterized image tiles in pixels (default: 512)

这里使用到了 dataset-building.toml 配置文件,文件中配置了瓦片地图路径、分类方式、蒙版标记的颜色等信息。示例配置可以查看官方示例文件 dataset-parking.toml 。本训练中用到的 dataset-building.toml 的配置内容如下:

# Configuration related to a specific dataset.
# For syntax see: https://github.com/toml-lang/toml#table-of-contents

# Dataset specific common attributes.
[common]

  # The slippy map dataset's base directory.
  dataset = '/Users/wucan/Document/robosat/tiles/'

  # Human representation for classes.
  classes = ['background', 'buildings']

  # Color map for visualization and representing classes in masks.
  # Note: available colors can be found in `robosat/colors.py`
  colors  = ['denim', 'orange']

​ 配置文档中,最重要的是配置 dataset 目录,也就是上一步中下载的遥感影像瓦片路径。制作的蒙版标记效果如下图。

​ 至此,训练和建模所需的瓦片和蒙版标记已经全部准备完,分别在 tilesmasks 目录中。

3. 训练和建模

3.1 分配训练数据、验证数据、评估数据

​ RoboSat 分割模型是一个完全卷积的神经网络,需要将上一步准备好的数据集拆分为三部分,分别为训练数据集验证数据集评估数据集,比例分别为80%、10%、10%。每一部分的数据集中,都包含影像瓦片和蒙版标记瓦片。

  • 训练数据集:a training dataset on which we train the model on

  • 验证数据集:a validation dataset on which we calculate metrics on after training

  • 评估数据集:a hold-out evaluation dataset if you want to do hyper-parameter tuning

    将步骤 2 中的数据进行随机分配的过程非常简单:

  • 新建三个 csv 文件: csv_training.tilescsv_validation.tilescsv_evaluation.tiles

  • shbuildings.tiles 中的瓦片列表手动按 80%、10%、10% 比例进行拷贝与粘贴。。

​ 使用 RoboSat 中的 subset 命令,将 tilesmasks 中的瓦片和蒙版按照上面三个 csv 文件的瓦片列表分配进行组织影像瓦片和蒙版标记数据。

# 准备训练数据集
docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu subset /data/tiles/ /data/csv_training.tiles /data/dataset/training/images
docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu subset /data/masks/ /data/csv_training.tiles /data/dataset/training/labels
# 准备验证数据集
docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu subset /data/tiles/ /data/csv_validation.tiles /data/dataset/validation/images
docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu subset /data/masks/ /data/csv_validation.tiles /data/dataset/validation/labels
# 准备评估数据集
docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu subset /data/tiles/ /data/csv_evaluation.tiles /data/dataset/evaluation/images
docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu subset /data/masks/ /data/csv_evaluation.tiles /data/dataset/evaluation/labels

subset 命令的参数介绍:

usage: ./rs subset [-h] images tiles out

  • positional arguments:

    • images directory to read slippy map image tiles from for filtering
    • tiles csv to filter images by
    • out directory to save filtered images to
  • optional arguments:

    • -h, --help show this help message and exit

​ 分类完成以后,将会生成 /data/dataset 目录,目录结构如下:

dataset
|  training
|  |  images
|  |  labels
|  validataion
|  |  images
|  |  labels
|  evaluation
|  |  images
|  |  labels

3.2 权重计算

​ 因为前景和背景在数据集中分布不均,可以使用 RoboSat 中的 weights 命令,在模型训练之前计算一下每个类的分布。

docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu weights --dataset /data/dataset-building-weights.toml

weights 命令的参数如下:

usage: ./rs weights [-h] --dataset DATASET

  • optional arguments:
    • -h, --help show this help message and exit
    • --dataset DATASET path to dataset configuration file (default: None)

​ 这里,用到了dataset-building-weights.toml ,是将前面步骤中的 dataset-building.toml 瓦片路径修改为包含训练数据集 dataset 的路径。执行权重计算命令后,得到权重为:values = [1.653415, 5.266637] 。将其追加到 dataset-building-weights.toml 文件中,结果如下。

# Configuration related to a specific dataset.
# For syntax see: https://github.com/toml-lang/toml#table-of-contents


# Dataset specific common attributes.
[common]

  # The slippy map dataset's base directory.
  dataset = '/data/dataset'

  # Human representation for classes.
  classes = ['background', 'buildings']

  # Color map for visualization and representing classes in masks.
  # Note: available colors can be found in `robosat/colors.py`
  colors  = ['denim', 'orange']

# Dataset specific class weights computes on the training data.
# Needed by 'mIoU' and 'CrossEntropy' losses to deal with unbalanced classes.
# Note: use `./rs weights -h` to compute these for new datasets.
[weights]
  values = [1.653415, 5.266637]

3.3 开始训练

​ RoboSat 使用 train 命令进行训练。

docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu train --model /data/model-unet.toml --dataset /data/dataset-building-weights.toml

train 命令的参数如下:

usage: ./rs train [-h] --model MODEL --dataset DATASET [--checkpoint CHECKPOINT] [--resume RESUME] [--workers WORKERS]

  • positional arguments:

    • --model MODEL path to model configuration file (default: None)
    • --dataset DATASET path to dataset configuration file (default: None)
  • optional arguments:

    • -h, `–help show this help message and exit
    • --checkpoint CHECKPOINT path to a model checkpoint (to retrain) (default: None)
    • --resume RESUME resume training or fine-tuning (if checkpoint) (default: False)
    • --workers WORKERS number of workers pre-processi ng images (default: 0)

​ 这里多了一个配置文件 model-unet.toml ,这个配置文件主要用来配置训练过程中的参数,包括是否启用 CUDA 、训练批次大小、影像瓦片的像素大小、检查点存储路径等。官方给出了示例配置文件,根据本实验的情况做了修改如下,配置如下。

# Configuration related to a specific model.
# For syntax see: https://github.com/toml-lang/toml#table-of-contents

# Model specific common attributes.
[common]

  # Use CUDA for GPU acceleration.
  cuda       = false

  # Batch size for training.
  batch_size = 2

  # Image side size in pixels.
  image_size = 256

  # Directory where to save checkpoints to during training.
  checkpoint = '/data/checkpoint/'


# Model specific optimization parameters.
[opt]

  # Total number of epochs to train for.
  epochs     = 10

  # Learning rate for the optimizer.
  lr         = 0.01

  # Loss function name (e.g 'Lovasz', 'mIoU' or 'CrossEntropy')
  loss = 'Lovasz'

​ RoboSat 会进行多次迭代训练,每次迭代训练都会保存检查点(checkpoint)和各项指标等。其中,训练日志例如:

--- Hyper Parameters on Dataset: /data/dataset ---
Batch Size:	 2
Image Size:	 256
Learning Rate:	 0.0001
Loss function:	 Lovasz
Weights :	 [1.644471, 5.409126]
---
Epoch: 1/10
Train    loss: 0.3190, mIoU: 0.410, buildings IoU: 0.017, MCC: -0.002
Validate loss: 0.3171, mIoU: 0.405, buildings IoU: 0.000, MCC: nan

...

Epoch: 10/10
Train    loss: 0.2693, mIoU: 0.528, buildings IoU: 0.229, MCC: 0.330
Validate loss: 0.2880, mIoU: 0.491, buildings IoU: 0.167, MCC: 0.262

4. 从新数据中预测

4.1 准备预测区域数据

​ RoboSat 仅支持从影像瓦片中提取建筑物,不支持从任意的 jpg 图片中提取。所以我们需要先准备预测区域的瓦片数据。

​ 通过 geojson.io 绘制想要提取建筑物的范围,使用矩形框即可。将自动生成的 geojson 保存为 predict_test.json

​ 通过 2.3 中的 cover 命令,获取待提取范围的瓦片列表 csv 文件,保存到 buildings_predict.tiles 文件中。

docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu cover  --zoom 18 /data/shp_data/predict_test.json /data/buildings_predict.tiles

​ 使用 2.4 中的 download 命令,下载待提取范围的影像瓦片,保存到 images_predict 文件夹中。

docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu download https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x} /data/buildings_predict.tiles /data/images_predict

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QEmAkWDm-1611918155632)(https://github.com/geocompass/robosat_buildings_training/blob/master/img/predict_region_geojson.jpg)]

4.2 预测待提取建筑物概率

​ 使用保存的检查点来(checkpint)预测图像中每个像素的分割概率,这些分割概率表示每个像素是建筑物还是背景的可能性,然后可以将这些概率转换为离散的分割掩模。

​ 通过 RoboSat 的 predict 命令,将待预测区域的建筑物( images_predict )提取为分割概率(predict_segmentation-probabilities)。

docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu predict --tile_size 256 --model /data/model-unet.toml --dataset /data/dataset-building.toml --checkpoint /data/checkpoint/checkpoint-00010-of-00010.pth /data/images_predict /data/predict_segmentation-probabilities

predict 命令的参数如下:

usage: ./rs predict [-h] [--batch_size BATCH_SIZE] --checkpoint CHECKPOINT [--overlap OVERLAP] --tile_size TILE_SIZE [--workers WORKERS] --model MODEL --dataset DATASET tiles probs

  • positional arguments:

    • tiles directory to read slippy map image tiles from
    • probs directory to save slippy map probability masks to
  • optional arguments:

    • -h, --help show this help message and exit
    • --batch_size BATCH_SIZE images per batch (default: 1)
    • --checkpoint CHECKPOINT model checkpoint to load (default: None)
    • --overlap OVERLAP tile pixel overlap to predict on (default: 32)
    • --tile_size TILE_SIZE tile size for slippy map tiles (default: None)
    • --workers WORKERS number of workers pre-processing images (default: 0)
    • --model MODEL path to model configuration file (default: None)
    • --dataset DATASET path to dataset configuration file (default: None)

4.3 预测概率转换为建筑物掩模

​ 通过 RoboSat 的 masks 命令,将上一步中的建筑物预测概率结果转换为建筑物掩模(masks),保存到 predict_segmentation-masks 文件夹中。

docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu masks /data/predict_segmentation-masks /data/predict_segmentation-probabilities

usage: ./rs masks [-h] [--weights WEIGHTS [WEIGHTS ...]] masks probs [probs ...]

  • positional arguments:

    • masks slippy map directory to save masks to
    • probs slippy map directories with class probabilities
  • optional arguments:

    • -h, --help show this help message and exit
    • --weights WEIGHTS [WEIGHTS ...] weights for weighted average soft-voting (default:
      None)

5.在3.1节中划分的测试集下预测

5.1 预测过程

预测建筑物概率

docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu predict --tile_size 256 --model /data/model-unet.toml --dataset /data/dataset-building.toml --checkpoint /data/checkpoint/checkpoint-00020-of-00020.pth /data/dataset/evaluation/images /data/predict_segmentation-probabilities

转换为掩模

docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu masks /data/predict_segmentation-masks /data/predict_segmentation-probabilities

预测结果与label比较

docker run -it --rm -v $PWD:/data --ipc=host --network=host mapbox/robosat:latest-cpu compare /data/compare  /data/dataset/evaluation/labels /data/predict_segmentation-masks 

5.2 部分结果

部分较好结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

部分差的结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值