Skip to content

Commit

Permalink
Merge pull request PaddlePaddle#2 from Bobholamovic/unittest
Browse files Browse the repository at this point in the history
[Test] Add Unittests
  • Loading branch information
juncaipeng authored Aug 1, 2022
2 parents 05e1eee + 286bf20 commit dce23e4
Show file tree
Hide file tree
Showing 222 changed files with 6,709 additions and 3,896 deletions.
81 changes: 81 additions & 0 deletions .github/workflows/build_and_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: build and test

on:
push:
branches:
- develop
- "tests/**"
paths-ignore:
- "docs/**"
- "README.md"
pull_request:
branches:
- develop

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build_and_test_cpu:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ["3.7", "3.8"]
include:
- python-version: "3.7"
os: windows-latest
gdal-whl-url: https://download.lfd.uci.edu/pythonlibs/archived/cp37/GDAL-3.3.3-cp37-cp37m-win_amd64.whl
- python-version: "3.7"
os: ubuntu-latest
gdal-whl-url: https://versaweb.dl.sourceforge.net/project/gdal-wheels-for-linux/GDAL-3.4.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
- python-version: "3.8"
os: windows-latest
gdal-whl-url: https://download.lfd.uci.edu/pythonlibs/archived/GDAL-3.3.3-cp38-cp38-win_amd64.whl
- python-version: "3.8"
os: ubuntu-latest
gdal-whl-url: https://versaweb.dl.sourceforge.net/project/gdal-wheels-for-linux/GDAL-3.4.1-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
fail-fast: false
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Upgrade pip
run: python -m pip install pip --upgrade --user
- name: Install PaddlePaddle
run: python -m pip install paddlepaddle==2.3.1 -i https://mirror.baidu.com/pypi/simple
- name: Install PaddleRS
run: |
python -m pip install -r requirements.txt
python -m pip install -e .
- name: Install GDAL
run: python -m pip install ${{ matrix.gdal-whl-url }}
- name: Run unittests
run: |
cd tests
bash run_fast_tests.sh
shell: bash

build_and_test_cuda102:
runs-on: ubuntu-18.04
container:
image: registry.baidubce.com/paddlepaddle/paddle:2.3.1-gpu-cuda10.2-cudnn7
steps:
- uses: actions/checkout@v3
- name: Upgrade pip
run: python3.7 -m pip install pip --upgrade --user
- name: Install PaddleRS
run: |
python3.7 -m pip install -r requirements.txt
python3.7 -m pip install -e .
- name: Install GDAL
run: python3.7 -m pip install https://versaweb.dl.sourceforge.net/project/gdal-wheels-for-linux/GDAL-3.4.1-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
# Do not run unittests, because there is NO GPU in the machine.
# - name: Run unittests
# run: |
# cd tests
# bash run_fast_tests.sh
# shell: bash
25 changes: 25 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: lint

on: [push, pull_request]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.7
uses: actions/setup-python@v4
with:
python-version: 3.7
- name: Upgrade pip
run: python -m pip install pip --upgrade --user
- name: Install pre-commit hooks
run: |
pip install pre-commit
pre-commit install
- name: Lint
run: pre-commit run --all-files
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

**基于飞桨框架开发的高性能遥感图像处理开发套件,端到端地完成从训练到部署的全流程遥感深度学习应用。**

<!-- [![Build Status](https://travis-ci.org/PaddleCV-SIG/PaddleRS.svg?branch=release/0.1)](https://travis-ci.org/PaddleCV-SIG/PaddleRS) -->
<!-- [![Version](https://img.shields.io/github/release/PaddleCV-SIG/PaddleRS.svg)](https://github.com/PaddleCV-SIG/PaddleRS/releases) -->
[![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](LICENSE)
<!-- [![version](https://img.shields.io/github/release/PaddleCV-SIG/PaddleRS.svg)](https://github.com/PaddleCV-SIG/PaddleRS/releases) -->
[![license](https://img.shields.io/badge/license-Apache%202-blue.svg)](LICENSE)
[![build status](https://github.com/PaddleCV-SIG/PaddleRS/workflows/build_and_test.yaml/badge.svg?branch=develop)](https://github.com/PaddleCV-SIG/PaddleRS/actions)
![python version](https://img.shields.io/badge/python-3.7+-orange.svg)
![support os](https://img.shields.io/badge/os-linux%2C%20win%2C%20mac-yellow.svg)
</div>
Expand Down Expand Up @@ -92,21 +92,21 @@ PaddleRS是遥感科研院所、相关高校共同基于飞桨开发的遥感处
<li>ResizeByShort</li>
<li>RandomResizeByShort</li>
<li>ResizeByLong</li>
<li>RandomFlipOrRotation</li>
<li>RandomFlipOrRotate</li>
<li>RandomHorizontalFlip</li>
<li>RandomVerticalFlip</li>
<li>Normalize</li>
<li>CenterCrop</li>
<li>RandomCrop</li>
<li>RandomScaleAspect</li>
<li>RandomExpand</li>
<li>Padding</li>
<li>Pad</li>
<li>MixupImage</li>
<li>RandomDistort</li>
<li>RandomBlur</li>
<li>Defogging</li>
<li>DimReducing</li>
<li>BandSelecting</li>
<li>Dehaze</li>
<li>ReduceDim</li>
<li>SelectBand</li>
<li>RandomSwap</li>
</ul>
</td>
Expand Down Expand Up @@ -223,4 +223,3 @@ PaddleRS是遥感科研院所、相关高校共同基于飞桨开发的遥感处
year={2022}
}
```

1 change: 0 additions & 1 deletion deploy/export/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,3 @@ python deploy/export_model.py --model_dir=./output/deeplabv3p/best_model/ --save
- 对于检测模型中的YOLO/PPYOLO系列模型,请保证输入影像的`w``h`有相同取值、且均为32的倍数;指定`--fixed_input_shape`时,R-CNN模型的`w``h`也均需为32的倍数。
- 指定`[w,h]`时,请使用半角逗号(`,`)分隔`w``h`,二者之间不允许存在空格等其它字符。
-`w``h`设得越大,则模型在推理过程中的耗时和内存/显存占用越高。不过,如果`w``h`过小,则可能对模型的精度存在较大负面影响。
- 对于变化检测模型BIT,请保证指定`--fixed_input_shape`,并且数值不包含负数,因为BIT用到空间注意力,需要从tensor中获取`b,c,h,w`的属性,若为负数则报错。
36 changes: 27 additions & 9 deletions deploy/export/export_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,23 @@

def get_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--model_dir', '-m', type=str, default=None, help='model directory path')
parser.add_argument('--save_dir', '-s', type=str, default=None, help='path to save inference model')
parser.add_argument('--fixed_input_shape', '-fs', type=str, default=None,
parser.add_argument(
'--model_dir',
'-m',
type=str,
default=None,
help='model directory path')
parser.add_argument(
'--save_dir',
'-s',
type=str,
default=None,
help='path to save inference model')
parser.add_argument(
'--fixed_input_shape',
'-fs',
type=str,
default=None,
help="export inference model with fixed input shape: [w,h] or [n,c,w,h]")
return parser

Expand All @@ -39,13 +53,17 @@ def get_parser():
fixed_input_shape = literal_eval(args.fixed_input_shape)
# Check validaty
if not isinstance(fixed_input_shape, list):
raise ValueError("fixed_input_shape should be of None or list type.")
raise ValueError(
"fixed_input_shape should be of None or list type.")
if len(fixed_input_shape) not in (2, 4):
raise ValueError("fixed_input_shape contains an incorrect number of elements.")
raise ValueError(
"fixed_input_shape contains an incorrect number of elements.")
if fixed_input_shape[-1] <= 0 or fixed_input_shape[-2] <= 0:
raise ValueError("the input width and height must be positive integers.")
if len(fixed_input_shape)==4 and fixed_input_shape[1] <= 0:
raise ValueError("the number of input channels must be a positive integer.")
raise ValueError(
"Input width and height must be positive integers.")
if len(fixed_input_shape) == 4 and fixed_input_shape[1] <= 0:
raise ValueError(
"The number of input channels must be a positive integer.")

# Set environment variables
os.environ['PADDLEX_EXPORT_STAGE'] = 'True'
Expand All @@ -56,4 +74,4 @@ def get_parser():

# Do dynamic-to-static cast
# XXX: Invoke a protected (single underscore) method outside of subclasses.
model._export_inference_model(args.save_dir, fixed_input_shape)
model._export_inference_model(args.save_dir, fixed_input_shape)
4 changes: 1 addition & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
PaddleSeg commit fec42fd869b6f796c74cd510671595e3512bc8e9

# 开发规范
请注意,paddlers/models/ppxxx系列除了修改import路径和支持多通道模型外,不要增删改任何代码。
新增的模型需放在paddlers/models/下的seg、det、cls、cd目录下。
新增的模型需放在paddlers/models/下的seg、det、cls、cd目录下。
12 changes: 6 additions & 6 deletions docs/apis/model_zoo.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ PaddleRS的基础模型库来自[PaddleClas](https://github.com/PaddlePaddle/Pad

## 自定义模型库

| 模型名称 | 用途 |
| --------------- | -------- |
| 模型名称 | 用途 |
| --------------- | -------- |
| FarSeg | 语义分割 |
| BIT | 变化检测 |
| CDNet | 变化检测 |
| DSIFN | 变化检测 |
| STANet | 变化检测 |
| SNUNet | 变化检测 |
| SNUNet | 变化检测 |
| DSAMNet | 变化检测 |
| FCEarlyFusion | 变化检测 |
| FCSiamConc | 变化检测 |
| FCSiamDiff | 变化检测 |
| FCEarlyFusion | 变化检测 |
| FCSiamConc | 变化检测 |
| FCSiamDiff | 变化检测 |


## 如何导入
Expand Down
10 changes: 5 additions & 5 deletions docs/apis/transforms.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 数据增强

PaddleRS将多种任务需要的数据增强进行了有机整合,均通过`Compose`进行使用,数据读取方面通过`ImgDecoder`可以对不只三通道RGB图像进行读取,还可以对SAR以及多通道图像进行读取,提供有转为`uint8`的选项。此外提供以下数据增强的方法。
PaddleRS将多种任务需要的数据增强进行了有机整合,均通过`Compose`进行使用,数据读取方面通过`DecodeImg`可以对不只三通道RGB图像进行读取,还可以对SAR以及多通道图像进行读取,提供有转为`uint8`的选项。此外提供以下数据增强的方法。

| 数据增强名称 | 用途 | 任务 | ... |
| -------------------- | ----------------------------------------------- | -------- | ---- |
Expand All @@ -16,13 +16,13 @@ PaddleRS将多种任务需要的数据增强进行了有机整合,均通过`Co
| RandomCrop | 对输入进行随机中心裁剪 | 所有 | ... |
| RandomScaleAspect | 裁剪输入并重新调整大小至原始大小 | 所有 | ... |
| RandomExpand | 通过根据随机偏移填充来随机扩展输入 | 所有 | ... |
| Padding | 将输入填充到指定的大小 | 所有 | ... |
| Pad | 将输入填充到指定的大小 | 所有 | ... |
| MixupImage | 将两张图片和它们的`gt_bbbox/gt_score`混合在一起 | 目标检测 | ... |
| RandomDistort | 对输入进行随机色彩变换 | 所有 | ... |
| RandomBlur | 对输入进行随机模糊 | 所有 | ... |
| Defogging | 对输入图像进行去雾 | 所有 | ... |
| DimReducing | 对输入图像进行降维 | 所有 | ... |
| BandSelecting | 选择输入图像的波段 | 所有 | ... |
| Dehaze | 对输入图像进行去雾 | 所有 | ... |
| ReduceDim | 对输入图像进行降维 | 所有 | ... |
| SelectBand | 选择输入图像的波段 | 所有 | ... |
| RandomSwap | 随机交换两个输入图像 | 变化检测 | ... |
| ... | ... | | ... |

Expand Down
4 changes: 2 additions & 2 deletions docs/data/coco_tools_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Args_show = True
------------------------------------------------Info------------------------------------------------
json read...
json keys: dict_keys(['info', 'licenses', 'images', 'annotations', 'categories'])
json keys: dict_keys(['info', 'licenses', 'images', 'annotations', 'categories'])
***********************info***********************
Content Type: dict
Expand Down Expand Up @@ -409,7 +409,7 @@ Args_show = True
------------------------------------------------Info------------------------------------------------
json read...
json keys: dict_keys(['images', 'categories'])
json keys: dict_keys(['images', 'categories'])
**********************images**********************
Content Type: list
Expand Down
2 changes: 1 addition & 1 deletion docs/data/dataset_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,4 @@
| [9-5](https://aistudio.baidu.com/aistudio/datasetdetail/136567) | [WHU TCL SatMVS 1.0](http://gpcv.whu.edu.cn/data/whu_tlc.html) | 图像生成 | 5120 * 5120 | 1 | 300 | __ | tif, jpg | __ | 2.1m, 2.5m | __ | 卫星影像 | ZY3 | 2021 | 武汉大学 | http://gpcv.whu.edu.cn/data/whu_tlc.html | https://aistudio.baidu.com/aistudio/datasetdetail/136567 |
| [9-6](https://aistudio.baidu.com/aistudio/datasetdetail/136567) | [WHU TCL SatMVS 2.0](http://gpcv.whu.edu.cn/data/whu_tlc.html) | 图像生成 | 768 * 384 | 1 | 5011 | __ | tif | __ | 2.1m, 2.5m | __ | 卫星影像 | ZY3 | 2021 | 武汉大学 | http://gpcv.whu.edu.cn/data/whu_tlc.html | https://aistudio.baidu.com/aistudio/datasetdetail/136567 |
| 9-7 | [DLR-ACD](https://www.dlr.de/eoc/en/desktopdefault.aspx/tabid-12760/22294_read-58354/) | 图像生成 | 3619 * 5226 | 3 | 33 | 1 | __ | __ | 0.045~ 0.15m | __ | 航拍影像 | 航拍影像 | 2019 | German Aerospace Center | https://www.dlr.de/eoc/en/desktopdefault.aspx/tabid-12760/22294_read-58354/ | |
| 9-8 | [SEN12MS-CR](https://mediatum.ub.tum.de/1554803) | 图像生成 | 256 * 256 | 13, 2 | 122218 | __ | __ | __ | __ | __ | 卫星影像 | Sentinel1, Sentinel2 | 2020 | TUM | https://mediatum.ub.tum.de/1554803 | |
| 9-8 | [SEN12MS-CR](https://mediatum.ub.tum.de/1554803) | 图像生成 | 256 * 256 | 13, 2 | 122218 | __ | __ | __ | __ | __ | 卫星影像 | Sentinel1, Sentinel2 | 2020 | TUM | https://mediatum.ub.tum.de/1554803 | |
2 changes: 1 addition & 1 deletion docs/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ python -m paddle.distributed.launch --gpus 0,1 tutorials/train/semantic_segmenta
visualdl --logdir output/deeplabv3p_resnet50_multi_channel/vdl_log --port 8001
```

服务启动后,使用浏览器打开 https://0.0.0.0:8001https://localhost:8001
服务启动后,使用浏览器打开 https://0.0.0.0:8001https://localhost:8001
2 changes: 1 addition & 1 deletion paddlers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@

log_level = 2

from . import tasks, datasets, transforms, utils, tools, models, deploy
from . import tasks, datasets, transforms, utils, tools, models, deploy
2 changes: 1 addition & 1 deletion paddlers/custom_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from . import cls, det, seg, gan, cd
from . import cls, det, seg, gan, cd
2 changes: 1 addition & 1 deletion paddlers/custom_models/cd/backbones/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
# 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.
# limitations under the License.
24 changes: 18 additions & 6 deletions paddlers/custom_models/cd/bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
from .param_init import KaimingInitMixin


def calc_product(*args):
if len(args) < 1:
raise ValueError
ret = args[0]
for arg in args[1:]:
ret *= arg
return ret


class BIT(nn.Layer):
"""
The BIT implementation based on PaddlePaddle.
Expand Down Expand Up @@ -131,9 +140,10 @@ def __init__(self,
def _get_semantic_tokens(self, x):
b, c = x.shape[:2]
att_map = self.conv_att(x)
att_map = att_map.reshape((b, self.token_len, 1, -1))
att_map = att_map.reshape(
(b, self.token_len, 1, calc_product(*att_map.shape[2:])))
att_map = F.softmax(att_map, axis=-1)
x = x.reshape((b, 1, c, -1))
x = x.reshape((b, 1, c, att_map.shape[-1]))
tokens = (x * att_map).sum(-1)
return tokens

Expand Down Expand Up @@ -172,7 +182,7 @@ def forward(self, t1, t2):
else:
token1 = self._get_reshaped_tokens(x1)
token2 = self._get_reshaped_tokens(x2)

# Transformer encoder forward
token = paddle.concat([token1, token2], axis=1)
token = self.encode(token)
Expand Down Expand Up @@ -253,6 +263,7 @@ def __init__(self,

inner_dim = head_dim * n_heads
self.n_heads = n_heads
self.head_dim = head_dim
self.scale = dim**-0.5

self.apply_softmax = apply_softmax
Expand All @@ -272,9 +283,10 @@ def forward(self, x, ref):
k = self.fc_k(ref)
v = self.fc_v(ref)

q = q.reshape((b, n, h, -1)).transpose((0, 2, 1, 3))
k = k.reshape((b, paddle.shape(ref)[1], h, -1)).transpose((0, 2, 1, 3))
v = v.reshape((b, paddle.shape(ref)[1], h, -1)).transpose((0, 2, 1, 3))
q = q.reshape((b, n, h, self.head_dim)).transpose((0, 2, 1, 3))
rn = ref.shape[1]
k = k.reshape((b, rn, h, self.head_dim)).transpose((0, 2, 1, 3))
v = v.reshape((b, rn, h, self.head_dim)).transpose((0, 2, 1, 3))

mult = paddle.matmul(q, k, transpose_y=True) * self.scale

Expand Down
Loading

0 comments on commit dce23e4

Please sign in to comment.