Merge branch 'PaddleCV-SIG:develop' into document

own
Yizhou Chen 3 years ago committed by GitHub
commit a6f5407ea9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      README.md
  2. 26
      docs/data/tools.md
  3. 3
      paddlers/custom_models/cd/models/dsifn.py
  4. 2
      paddlers/custom_models/cd/models/snunet.py
  5. 2
      paddlers/custom_models/cd/models/unet_ef.py
  6. 2
      paddlers/custom_models/cd/models/unet_siamconc.py
  7. 2
      paddlers/custom_models/cd/models/unet_siamdiff.py
  8. 2
      requirements.txt
  9. 0
      tools/coco2mask.py
  10. 76
      tools/mask2geojson.py
  11. 3
      tutorials/train/object_detection/data/.gitignore
  12. 98
      tutorials/train/object_detection/faster_rcnn.py
  13. 64
      tutorials/train/object_detection/faster_rcnn_sar_ship.py
  14. 99
      tutorials/train/object_detection/ppyolo.py
  15. 99
      tutorials/train/object_detection/ppyolotiny.py
  16. 99
      tutorials/train/object_detection/ppyolov2.py
  17. 28
      tutorials/train/object_detection/readme.md
  18. 98
      tutorials/train/object_detection/yolov3.py

@ -113,8 +113,9 @@ PaddleRS是xxx、xxx、xxx等遥感科研院所共同基于飞桨开发的遥感
<td> <td>
<b>数据格式转换</b><br> <b>数据格式转换</b><br>
<ul> <ul>
<li>geojson to mask</li> <li>coco to mask</li>
<li>mask to shpfile</li> <li>mask to shpfile</li>
<li>mask to geojson</li>
</ul> </ul>
<b>数据预处理</b><br> <b>数据预处理</b><br>
<ul> <ul>

@ -2,8 +2,9 @@
工具箱位于`tools`文件夹下,目前有如下工具: 工具箱位于`tools`文件夹下,目前有如下工具:
- `geojson2mask`:用于将geojson格式的分割标注标签转换为png格式。 - `coco2mask`:用于将geojson格式的分割标注标签转换为png格式。
- `mask2shp`:用于对推理得到的png提取shapefile。 - `mask2shp`:用于对推理得到的png提取shapefile。
- `mask2geojson`:用于对推理得到的png提取geojson。
- `matcher`:用于在推理前匹配两个时段的影响。 - `matcher`:用于在推理前匹配两个时段的影响。
- `spliter`:用于将大图数据进行分割以作为训练数据。 - `spliter`:用于将大图数据进行分割以作为训练数据。
@ -18,12 +19,12 @@ git clone https://github.com/PaddleCV-SIG/PaddleRS.git
dc PaddleRS\tools dc PaddleRS\tools
``` ```
### geojson2mask ### coco2mask
`geojson2mask`的主要功能是将图像以及对应json格式的分割标签转换为图像与png格式的标签,结果会分别存放在`img`和`gt`两个文件夹中。相关的数据样例可以参考[中国典型城市建筑物实例数据集](https://www.scidb.cn/detail?dataSetId=806674532768153600&dataSetType=journal)。保存结果为单通道的伪彩色图像。使用代码如下: `coco2mask`的主要功能是将图像以及对应json格式的分割标签转换为图像与png格式的标签,结果会分别存放在`img`和`gt`两个文件夹中。相关的数据样例可以参考[中国典型城市建筑物实例数据集](https://www.scidb.cn/detail?dataSetId=806674532768153600&dataSetType=journal)。保存结果为单通道的伪彩色图像。使用代码如下:
```shell ```shell
python geojson2mask.py --raw_folder xxx --save_folder xxx python coco2mask.py --raw_folder xxx --save_folder xxx
``` ```
其中: 其中:
@ -42,10 +43,27 @@ python mask2shp.py --srcimg_path xxx.tif --mask_path xxx.png [--save_path output
其中: 其中:
- `srcimg_path`:原始图像的路径,需要带有地理信息,以便为生成的shapefile提供crs等信息。 - `srcimg_path`:原始图像的路径,需要带有地理信息,以便为生成的shapefile提供crs等信息。
- `mask_path`:推理得到的png格式的标签的路径。 - `mask_path`:推理得到的png格式的标签的路径。
- `save_path`:保存shapefile的路径,默认为`output`。 - `save_path`:保存shapefile的路径,默认为`output`。
- `ignore_index`:忽略生成shp的索引,如背景等,默认为255。 - `ignore_index`:忽略生成shp的索引,如背景等,默认为255。
### mask2geojson
`mask2geojson`的主要功能是将推理得到的png格式的分割结果转换为geojson格式。使用代码如下:
```shell
python mask2geojson.py --mask_path xxx.tif --save_path xxx.json [--epsilon 0]
```
其中:
- `mask_path`:推理得到的png格式的标签的路径。
- `save_path`:保存geojson的路径。
- `epsilon`:opencv的简化参数,默认为0。
### matcher ### matcher
` matcher`的主要功能是在进行变化检测的推理前,匹配两期影像的位置,并将转换后的`im2`图像保存在原地址下,命名为`im2_M.tif`。使用代码如下: ` matcher`的主要功能是在进行变化检测的推理前,匹配两期影像的位置,并将转换后的`im2`图像保存在原地址下,命名为`im2_M.tif`。使用代码如下:

@ -12,6 +12,9 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Refer to
# https://github.com/GeoZcx/A-deeply-supervised-image-fusion-network-for-change-detection-in-remote-sensing-images .
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import paddle.nn.functional as F import paddle.nn.functional as F

@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Refer to https://github.com/likyoo/Siam-NestedUNet .
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import paddle.nn.functional as F import paddle.nn.functional as F

@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Transferred from https://github.com/rcdaudt/fully_convolutional_change_detection/blob/master/unet.py .
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import paddle.nn.functional as F import paddle.nn.functional as F

@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Transferred from https://github.com/rcdaudt/fully_convolutional_change_detection/blob/master/siamunet_conc.py .
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import paddle.nn.functional as F import paddle.nn.functional as F

@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Transferred from https://github.com/rcdaudt/fully_convolutional_change_detection/blob/master/siamunet_diff.py .
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import paddle.nn.functional as F import paddle.nn.functional as F

@ -5,7 +5,6 @@ opencv-contrib-python == 4.3.0.38
numba == 0.53.1 numba == 0.53.1
scikit-learn == 0.23.2 scikit-learn == 0.23.2
scikit-image >= 0.14.0 scikit-image >= 0.14.0
# numpy == 1.22.3
pandas pandas
scipy scipy
cython cython
@ -18,6 +17,7 @@ openpyxl
easydict easydict
munch munch
natsort natsort
geojson
# # Self installation # # Self installation
# GDAL >= 3.1.3 # GDAL >= 3.1.3

@ -0,0 +1,76 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import codecs
import cv2
import numpy as np
import argparse
import geojson
from geojson import Polygon, Feature, FeatureCollection
from utils import Raster, Timer
def _gt_convert(x, y, geotf):
x_geo = geotf[0] + x * geotf[1] + y * geotf[2]
y_geo = geotf[3] + x * geotf[4] + y * geotf[5]
return x_geo, y_geo
@Timer
def convert_data(mask_path, save_path, epsilon=0):
raster = Raster(mask_path)
img = raster.getArray()
geo_writer = codecs.open(save_path, "w", encoding="utf-8")
clas = np.unique(img)
cv2_v = (cv2.__version__.split(".")[0] == "3")
feats = []
if not isinstance(epsilon, (int, float)):
epsilon = 0
for iclas in range(1, len(clas)):
tmp = np.zeros_like(img).astype("uint8")
tmp[img == iclas] = 1
# TODO: Detect internal and external contour
results = cv2.findContours(tmp, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_TC89_KCOS)
contours = results[1] if cv2_v else results[0]
# hierarchys = results[2] if cv2_v else results[1]
if len(contours) == 0:
continue
for contour in contours:
contour = cv2.approxPolyDP(contour, epsilon, True)
polys = []
for point in contour:
x, y = point[0]
xg, yg = _gt_convert(x, y, raster.geot)
polys.append((xg, yg))
polys.append(polys[0])
feat = Feature(
geometry=Polygon([polys]), properties={"class": int(iclas)})
feats.append(feat)
gjs = FeatureCollection(feats)
geo_writer.write(geojson.dumps(gjs))
geo_writer.close()
parser = argparse.ArgumentParser(description="input parameters")
parser.add_argument("--mask_path", type=str, required=True, \
help="The path of mask tif.")
parser.add_argument("--save_path", type=str, required=True, \
help="The path to save the results, file suffix is `*.json`.")
parser.add_argument("--epsilon", type=float, default=0, \
help="The CV2 simplified parameters, `0` is the default.")
if __name__ == "__main__":
args = parser.parse_args()
convert_data(args.mask_path, args.save_path, args.epsilon)

@ -0,0 +1,3 @@
*.zip
*.tar.gz
sarship/

@ -0,0 +1,98 @@
#!/usr/bin/env python
# 目标检测模型Faster R-CNN训练示例脚本
# 执行此脚本前,请确认已正确安装PaddleRS库
import os
import paddlers as pdrs
from paddlers import transforms as T
# 下载文件存放目录
DOWNLOAD_DIR = './data/sarship/'
# 数据集存放目录
DATA_DIR = './data/sarship/sar_ship_1/'
# 训练集`file_list`文件路径
TRAIN_FILE_LIST_PATH = './data/sarship/sar_ship_1/train.txt'
# 验证集`file_list`文件路径
EVAL_FILE_LIST_PATH = './data/sarship/sar_ship_1/valid.txt'
# 数据集类别信息文件路径
LABEL_LIST_PATH = './data/sarship/sar_ship_1/labels.txt'
# 实验目录,保存输出的模型权重和结果
EXP_DIR = './output/faster_rcnn/'
# 下载和解压SAR影像舰船检测数据集
sarship_dataset = 'https://paddleseg.bj.bcebos.com/dataset/sar_ship_1.tar.gz'
if not os.path.exists(DATA_DIR):
pdrs.utils.download_and_decompress(sarship_dataset, path=DOWNLOAD_DIR)
# 定义训练和验证时使用的数据变换(数据增强、预处理等)
# 使用Compose组合多种变换方式。Compose中包含的变换将按顺序串行执行
# API说明:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/docs/apis/transforms.md
train_transforms = T.Compose([
# 对输入影像施加随机色彩扰动
T.RandomDistort(),
# 在影像边界进行随机padding
T.RandomExpand(),
# 随机裁剪,裁块大小在一定范围内变动
T.RandomCrop(),
# 随机水平翻转
T.RandomHorizontalFlip(),
# 对batch进行随机缩放,随机选择插值方式
T.BatchRandomResize(
target_sizes=[320, 352, 384, 416, 448, 480, 512, 544, 576, 608],
interp='RANDOM'),
# 影像归一化
T.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
eval_transforms = T.Compose([
# 使用双三次插值将输入影像缩放到固定大小
T.Resize(
target_size=608, interp='CUBIC'),
# 验证阶段与训练阶段的归一化方式必须相同
T.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# 分别构建训练和验证所用的数据集
train_dataset = pdrs.datasets.VOCDetection(
data_dir=DATA_DIR,
file_list=TRAIN_FILE_LIST_PATH,
label_list=LABEL_LIST_PATH,
transforms=train_transforms,
shuffle=True)
eval_dataset = pdrs.datasets.VOCDetection(
data_dir=DATA_DIR,
file_list=EVAL_FILE_LIST_PATH,
label_list=LABEL_LIST_PATH,
transforms=eval_transforms,
shuffle=False)
# 构建Faster R-CNN模型
# 目前已支持的模型请参考:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/docs/apis/model_zoo.md
# 模型输入参数请参考:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/paddlers/tasks/object_detector.py
model = pdrs.tasks.FasterRCNN(num_classes=len(train_dataset.labels))
# 执行模型训练
model.train(
num_epochs=10,
train_dataset=train_dataset,
train_batch_size=4,
eval_dataset=eval_dataset,
# 每多少个epoch存储一次检查点
save_interval_epochs=5,
# 每多少次迭代记录一次日志
log_interval_steps=4,
save_dir=EXP_DIR,
# 指定预训练权重
pretrain_weights='COCO',
# 初始学习率大小
learning_rate=0.005,
# 学习率预热(learning rate warm-up)步数与初始值
warmup_steps=0,
warmup_start_lr=0.0,
# 是否启用VisualDL日志功能
use_vdl=True)

@ -1,64 +0,0 @@
import os
import paddlers as pdrs
from paddlers import transforms as T
# download dataset
data_dir = 'sar_ship_1'
if not os.path.exists(data_dir):
dataset_url = 'https://paddleseg.bj.bcebos.com/dataset/sar_ship_1.tar.gz'
pdrs.utils.download_and_decompress(dataset_url, path='./')
# define transforms
train_transforms = T.Compose([
T.RandomDistort(),
T.RandomExpand(),
T.RandomCrop(),
T.RandomHorizontalFlip(),
T.BatchRandomResize(
target_sizes=[320, 352, 384, 416, 448, 480, 512, 544, 576, 608],
interp='RANDOM'),
T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
eval_transforms = T.Compose([
T.Resize(target_size=608, interp='CUBIC'),
T.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# define dataset
train_file_list = os.path.join(data_dir, 'train.txt')
val_file_list = os.path.join(data_dir, 'valid.txt')
label_file_list = os.path.join(data_dir, 'labels.txt')
train_dataset = pdrs.datasets.VOCDetection(
data_dir=data_dir,
file_list=train_file_list,
label_list=label_file_list,
transforms=train_transforms,
shuffle=True)
eval_dataset = pdrs.datasets.VOCDetection(
data_dir=data_dir,
file_list=train_file_list,
label_list=label_file_list,
transforms=eval_transforms,
shuffle=False)
# define models
num_classes = len(train_dataset.labels)
model = pdrs.tasks.FasterRCNN(num_classes=num_classes)
# train
model.train(
num_epochs=60,
train_dataset=train_dataset,
train_batch_size=2,
eval_dataset=eval_dataset,
pretrain_weights='COCO',
learning_rate=0.005 / 12,
warmup_steps=10,
warmup_start_lr=0.0,
save_interval_epochs=5,
lr_decay_epochs=[20, 40],
save_dir='output/faster_rcnn_sar_ship',
use_vdl=True)

@ -0,0 +1,99 @@
#!/usr/bin/env python
# 目标检测模型PP-YOLO训练示例脚本
# 执行此脚本前,请确认已正确安装PaddleRS库
import os
import paddlers as pdrs
from paddlers import transforms as T
# 下载文件存放目录
DOWNLOAD_DIR = './data/sarship/'
# 数据集存放目录
DATA_DIR = './data/sarship/sar_ship_1/'
# 训练集`file_list`文件路径
TRAIN_FILE_LIST_PATH = './data/sarship/sar_ship_1/train.txt'
# 验证集`file_list`文件路径
EVAL_FILE_LIST_PATH = './data/sarship/sar_ship_1/valid.txt'
# 数据集类别信息文件路径
LABEL_LIST_PATH = './data/sarship/sar_ship_1/labels.txt'
# 实验目录,保存输出的模型权重和结果
EXP_DIR = './output/ppyolo/'
# 下载和解压SAR影像舰船检测数据集
# 若目录已存在则不重复下载
sarship_dataset = 'https://paddleseg.bj.bcebos.com/dataset/sar_ship_1.tar.gz'
if not os.path.exists(DATA_DIR):
pdrs.utils.download_and_decompress(sarship_dataset, path=DOWNLOAD_DIR)
# 定义训练和验证时使用的数据变换(数据增强、预处理等)
# 使用Compose组合多种变换方式。Compose中包含的变换将按顺序串行执行
# API说明:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/docs/apis/transforms.md
train_transforms = T.Compose([
# 对输入影像施加随机色彩扰动
T.RandomDistort(),
# 在影像边界进行随机padding
T.RandomExpand(),
# 随机裁剪,裁块大小在一定范围内变动
T.RandomCrop(),
# 随机水平翻转
T.RandomHorizontalFlip(),
# 对batch进行随机缩放,随机选择插值方式
T.BatchRandomResize(
target_sizes=[320, 352, 384, 416, 448, 480, 512, 544, 576, 608],
interp='RANDOM'),
# 影像归一化
T.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
eval_transforms = T.Compose([
# 使用双三次插值将输入影像缩放到固定大小
T.Resize(
target_size=608, interp='CUBIC'),
# 验证阶段与训练阶段的归一化方式必须相同
T.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# 分别构建训练和验证所用的数据集
train_dataset = pdrs.datasets.VOCDetection(
data_dir=DATA_DIR,
file_list=TRAIN_FILE_LIST_PATH,
label_list=LABEL_LIST_PATH,
transforms=train_transforms,
shuffle=True)
eval_dataset = pdrs.datasets.VOCDetection(
data_dir=DATA_DIR,
file_list=EVAL_FILE_LIST_PATH,
label_list=LABEL_LIST_PATH,
transforms=eval_transforms,
shuffle=False)
# 构建PP-YOLO模型
# 目前已支持的模型请参考:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/docs/apis/model_zoo.md
# 模型输入参数请参考:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/paddlers/tasks/object_detector.py
model = pdrs.tasks.PPYOLO(num_classes=len(train_dataset.labels))
# 执行模型训练
model.train(
num_epochs=10,
train_dataset=train_dataset,
train_batch_size=4,
eval_dataset=eval_dataset,
# 每多少个epoch存储一次检查点
save_interval_epochs=10,
# 每多少次迭代记录一次日志
log_interval_steps=4,
save_dir=EXP_DIR,
# 指定预训练权重
pretrain_weights='COCO',
# 初始学习率大小
learning_rate=0.0005,
# 学习率预热(learning rate warm-up)步数与初始值
warmup_steps=0,
warmup_start_lr=0.0,
# 是否启用VisualDL日志功能
use_vdl=True)

@ -0,0 +1,99 @@
#!/usr/bin/env python
# 目标检测模型PP-YOLO Tiny训练示例脚本
# 执行此脚本前,请确认已正确安装PaddleRS库
import os
import paddlers as pdrs
from paddlers import transforms as T
# 下载文件存放目录
DOWNLOAD_DIR = './data/sarship/'
# 数据集存放目录
DATA_DIR = './data/sarship/sar_ship_1/'
# 训练集`file_list`文件路径
TRAIN_FILE_LIST_PATH = './data/sarship/sar_ship_1/train.txt'
# 验证集`file_list`文件路径
EVAL_FILE_LIST_PATH = './data/sarship/sar_ship_1/valid.txt'
# 数据集类别信息文件路径
LABEL_LIST_PATH = './data/sarship/sar_ship_1/labels.txt'
# 实验目录,保存输出的模型权重和结果
EXP_DIR = './output/ppyolotiny/'
# 下载和解压SAR影像舰船检测数据集
# 若目录已存在则不重复下载
sarship_dataset = 'https://paddleseg.bj.bcebos.com/dataset/sar_ship_1.tar.gz'
if not os.path.exists(DATA_DIR):
pdrs.utils.download_and_decompress(sarship_dataset, path=DOWNLOAD_DIR)
# 定义训练和验证时使用的数据变换(数据增强、预处理等)
# 使用Compose组合多种变换方式。Compose中包含的变换将按顺序串行执行
# API说明:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/docs/apis/transforms.md
train_transforms = T.Compose([
# 对输入影像施加随机色彩扰动
T.RandomDistort(),
# 在影像边界进行随机padding
T.RandomExpand(),
# 随机裁剪,裁块大小在一定范围内变动
T.RandomCrop(),
# 随机水平翻转
T.RandomHorizontalFlip(),
# 对batch进行随机缩放,随机选择插值方式
T.BatchRandomResize(
target_sizes=[320, 352, 384, 416, 448, 480, 512, 544, 576, 608],
interp='RANDOM'),
# 影像归一化
T.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
eval_transforms = T.Compose([
# 使用双三次插值将输入影像缩放到固定大小
T.Resize(
target_size=608, interp='CUBIC'),
# 验证阶段与训练阶段的归一化方式必须相同
T.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# 分别构建训练和验证所用的数据集
train_dataset = pdrs.datasets.VOCDetection(
data_dir=DATA_DIR,
file_list=TRAIN_FILE_LIST_PATH,
label_list=LABEL_LIST_PATH,
transforms=train_transforms,
shuffle=True)
eval_dataset = pdrs.datasets.VOCDetection(
data_dir=DATA_DIR,
file_list=EVAL_FILE_LIST_PATH,
label_list=LABEL_LIST_PATH,
transforms=eval_transforms,
shuffle=False)
# 构建PP-YOLO Tiny模型
# 目前已支持的模型请参考:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/docs/apis/model_zoo.md
# 模型输入参数请参考:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/paddlers/tasks/object_detector.py
model = pdrs.tasks.PPYOLOTiny(num_classes=len(train_dataset.labels))
# 执行模型训练
model.train(
num_epochs=10,
train_dataset=train_dataset,
train_batch_size=4,
eval_dataset=eval_dataset,
# 每多少个epoch存储一次检查点
save_interval_epochs=5,
# 每多少次迭代记录一次日志
log_interval_steps=4,
save_dir=EXP_DIR,
# 指定预训练权重
pretrain_weights='COCO',
# 初始学习率大小
learning_rate=0.0001,
# 学习率预热(learning rate warm-up)步数与初始值
warmup_steps=0,
warmup_start_lr=0.0,
# 是否启用VisualDL日志功能
use_vdl=True)

@ -0,0 +1,99 @@
#!/usr/bin/env python
# 目标检测模型PP-YOLOv2训练示例脚本
# 执行此脚本前,请确认已正确安装PaddleRS库
import os
import paddlers as pdrs
from paddlers import transforms as T
# 下载文件存放目录
DOWNLOAD_DIR = './data/sarship/'
# 数据集存放目录
DATA_DIR = './data/sarship/sar_ship_1/'
# 训练集`file_list`文件路径
TRAIN_FILE_LIST_PATH = './data/sarship/sar_ship_1/train.txt'
# 验证集`file_list`文件路径
EVAL_FILE_LIST_PATH = './data/sarship/sar_ship_1/valid.txt'
# 数据集类别信息文件路径
LABEL_LIST_PATH = './data/sarship/sar_ship_1/labels.txt'
# 实验目录,保存输出的模型权重和结果
EXP_DIR = './output/ppyolov2/'
# 下载和解压SAR影像舰船检测数据集
# 若目录已存在则不重复下载
sarship_dataset = 'https://paddleseg.bj.bcebos.com/dataset/sar_ship_1.tar.gz'
if not os.path.exists(DATA_DIR):
pdrs.utils.download_and_decompress(sarship_dataset, path=DOWNLOAD_DIR)
# 定义训练和验证时使用的数据变换(数据增强、预处理等)
# 使用Compose组合多种变换方式。Compose中包含的变换将按顺序串行执行
# API说明:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/docs/apis/transforms.md
train_transforms = T.Compose([
# 对输入影像施加随机色彩扰动
T.RandomDistort(),
# 在影像边界进行随机padding
T.RandomExpand(),
# 随机裁剪,裁块大小在一定范围内变动
T.RandomCrop(),
# 随机水平翻转
T.RandomHorizontalFlip(),
# 对batch进行随机缩放,随机选择插值方式
T.BatchRandomResize(
target_sizes=[320, 352, 384, 416, 448, 480, 512, 544, 576, 608],
interp='RANDOM'),
# 影像归一化
T.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
eval_transforms = T.Compose([
# 使用双三次插值将输入影像缩放到固定大小
T.Resize(
target_size=608, interp='CUBIC'),
# 验证阶段与训练阶段的归一化方式必须相同
T.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# 分别构建训练和验证所用的数据集
train_dataset = pdrs.datasets.VOCDetection(
data_dir=DATA_DIR,
file_list=TRAIN_FILE_LIST_PATH,
label_list=LABEL_LIST_PATH,
transforms=train_transforms,
shuffle=True)
eval_dataset = pdrs.datasets.VOCDetection(
data_dir=DATA_DIR,
file_list=EVAL_FILE_LIST_PATH,
label_list=LABEL_LIST_PATH,
transforms=eval_transforms,
shuffle=False)
# 构建PP-YOLOv2模型
# 目前已支持的模型请参考:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/docs/apis/model_zoo.md
# 模型输入参数请参考:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/paddlers/tasks/object_detector.py
model = pdrs.tasks.PPYOLOv2(num_classes=len(train_dataset.labels))
# 执行模型训练
model.train(
num_epochs=10,
train_dataset=train_dataset,
train_batch_size=4,
eval_dataset=eval_dataset,
# 每多少个epoch存储一次检查点
save_interval_epochs=5,
# 每多少次迭代记录一次日志
log_interval_steps=4,
save_dir=EXP_DIR,
# 指定预训练权重
pretrain_weights='COCO',
# 初始学习率大小
learning_rate=0.0001,
# 学习率预热(learning rate warm-up)步数与初始值
warmup_steps=0,
warmup_start_lr=0.0,
# 是否启用VisualDL日志功能
use_vdl=True)

@ -1,28 +0,0 @@
The detection training demo:
* dataset: AIR-SARShip-1.0
* target: ship
* model: faster_rcnn
Run the demo:
1. Install PaddleRS
```
git clone https://github.com/PaddleCV-SIG/PaddleRS.git
cd PaddleRS
pip install -r requirements.txt
python setup.py install
```
2. Run the demo
```
cd tutorials/train/detection/
# run training on single GPU
export CUDA_VISIBLE_DEVICES=0
python faster_rcnn_sar_ship.py
# run traing on multi gpu
export CUDA_VISIBLE_DEVICES=0,1
python -m paddle.distributed.launch faster_rcnn_sar_ship.py
```

@ -0,0 +1,98 @@
#!/usr/bin/env python
# 目标检测模型YOLOv3训练示例脚本
# 执行此脚本前,请确认已正确安装PaddleRS库
import os
import paddlers as pdrs
from paddlers import transforms as T
# 下载文件存放目录
DOWNLOAD_DIR = './data/sarship/'
# 数据集存放目录
DATA_DIR = './data/sarship/sar_ship_1/'
# 训练集`file_list`文件路径
TRAIN_FILE_LIST_PATH = './data/sarship/sar_ship_1/train.txt'
# 验证集`file_list`文件路径
EVAL_FILE_LIST_PATH = './data/sarship/sar_ship_1/valid.txt'
# 数据集类别信息文件路径
LABEL_LIST_PATH = './data/sarship/sar_ship_1/labels.txt'
# 实验目录,保存输出的模型权重和结果
EXP_DIR = './output/yolov3/'
# 下载和解压SAR影像舰船检测数据集
# 若目录已存在则不重复下载
sarship_dataset = 'https://paddleseg.bj.bcebos.com/dataset/sar_ship_1.tar.gz'
if not os.path.exists(DATA_DIR):
pdrs.utils.download_and_decompress(sarship_dataset, path=DOWNLOAD_DIR)
# 定义训练和验证时使用的数据变换(数据增强、预处理等)
# 使用Compose组合多种变换方式。Compose中包含的变换将按顺序串行执行
# API说明:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/docs/apis/transforms.md
train_transforms = T.Compose([
# 对输入影像施加随机色彩扰动
T.RandomDistort(),
# 在影像边界进行随机padding
T.RandomExpand(),
# 随机裁剪,裁块大小在一定范围内变动
T.RandomCrop(),
# 随机水平翻转
T.RandomHorizontalFlip(),
# 对batch进行随机缩放,随机选择插值方式
T.BatchRandomResize(
target_sizes=[320, 352, 384, 416, 448, 480, 512, 544, 576, 608],
interp='RANDOM'),
# 影像归一化
T.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
eval_transforms = T.Compose([
# 使用双三次插值将输入影像缩放到固定大小
T.Resize(
target_size=608, interp='CUBIC'),
# 验证阶段与训练阶段的归一化方式必须相同
T.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
# 分别构建训练和验证所用的数据集
train_dataset = pdrs.datasets.VOCDetection(
data_dir=DATA_DIR,
file_list=TRAIN_FILE_LIST_PATH,
label_list=LABEL_LIST_PATH,
transforms=train_transforms,
shuffle=True)
eval_dataset = pdrs.datasets.VOCDetection(
data_dir=DATA_DIR,
file_list=EVAL_FILE_LIST_PATH,
label_list=LABEL_LIST_PATH,
transforms=eval_transforms,
shuffle=False)
# 构建YOLOv3模型,使用DarkNet53作为backbone
# 目前已支持的模型请参考:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/docs/apis/model_zoo.md
# 模型输入参数请参考:https://github.com/PaddleCV-SIG/PaddleRS/blob/develop/paddlers/tasks/object_detector.py
model = pdrs.tasks.YOLOv3(
num_classes=len(train_dataset.labels), backbone='DarkNet53')
# 执行模型训练
model.train(
num_epochs=10,
train_dataset=train_dataset,
train_batch_size=4,
eval_dataset=eval_dataset,
# 每多少个epoch存储一次检查点
save_interval_epochs=5,
# 每多少次迭代记录一次日志
log_interval_steps=4,
save_dir=EXP_DIR,
# 初始学习率大小
learning_rate=0.0001,
# 学习率预热(learning rate warm-up)步数与初始值
warmup_steps=0,
warmup_start_lr=0.0,
# 是否启用VisualDL日志功能
use_vdl=True)
Loading…
Cancel
Save