diff --git a/PyTorch/built-in/cv/semantic_segmentation/BiseNetV1_for_PyTorch/README.md b/PyTorch/built-in/cv/semantic_segmentation/BiseNetV1_for_PyTorch/README.md index 08a6a61914fadb5674c270ecf86785f29aec9df1..b131da2cfe8cbbbeb3df34d1f75f1f3983b6d7e2 100644 --- a/PyTorch/built-in/cv/semantic_segmentation/BiseNetV1_for_PyTorch/README.md +++ b/PyTorch/built-in/cv/semantic_segmentation/BiseNetV1_for_PyTorch/README.md @@ -1,129 +1,191 @@ # BiseNetV1 for Pytorch -This folder contains the implementation of training of the `BiseNetV1` on the `CityScapes` datasets based on `mmsegmentation` framework. +- [概述](#1) +- [准备训练环境](#2) +- [开始训练](#3) +- [训练结果展示](#4) +- [版本说明](#5) -## Usage -### Install -#### NPU-related components -- Install ASCEND-CANN, ASCEND-pytorch-1.8 and apex. -- Install `torchvison 0.9.1` and `pillow 9.3.0`. - ```bash - cd $BiseNetV1_for_PyTorch - pip install torchvision==0.9.1 - pip install pillow==9.3.0 +# 概述 + +## 简述 + +BiseNet是一种新的双向分割网络,设计了一个带有小步长的空间路径来保留空间位置信息生成高分辨率的特征图;同时设计了一个带有快速下采样率的语义路径来获取客观的感受野。在这两个模块之上引入一个新的特征融合模块将二者的特征图进行融合,实现速度和精度的平衡。 + ++ 参考实现: + + ``` + url=https://github.com/open-mmlab/mmdetection + commit_id=3e36d5cfd4fe7c550b4c3493360fd369b858b1dc ``` -#### mmcv-full -- Download `mmcv-full` v1.6.1 from github: - ```bash - cd $BiseNetV1_for_PyTorch - git clone -b v1.6.1 --depth=1 https://github.com/open-mmlab/mmcv.git ++ 适配昇腾 AI 处理器的实现: + + ``` + url=https://gitee.com/ascend/ModelZoo-PyTorch.git + code_path=PyTorch/built-in/cv/semantic_segmentation ``` -- Replace the MMCV file to adapt to the NPU: - ```bash - cd $BiseNetV1_for_PyTorch - /bin/cp -f mmcv_need/builder.py ${mmcv_path}/mmcv/runner/optimizer/ - /bin/cp -f mmcv_need/dist_utils.py ${mmcv_path}/mmcv/runner/ - /bin/cp -f mmcv_need/__init__utils.py ${mmcv_path}/mmcv/utils/__init__.py - /bin/cp -f mmcv_need/device_type.py ${mmcv_path}/mmcv/utils/ - /bin/cp -f mmcv_need/optimizer.py ${mmcv_path}/mmcv/runner/hooks/ - /bin/cp -f mmcv_need/__init__device.py ${mmcv_path}/mmcv/device/__init__.py - /bin/cp -rf mmcv_need/npu ${mmcv_path}/mmcv/device/ - /bin/cp -f mmcv_need/utils.py ${mmcv_path}/mmcv/device/ ++ 通过Git获取代码方法如下: + + ``` + git clone {url} # 克隆仓库的代码 + cd {code_path} # 切换到模型代码所在路径,若仓库下只有该模型,则无需切换 ``` -- Compiling and installing the MMCV (10 mins): - ```bash - cd $BiseNetV1_for_PyTorch/mmcv - pip install -r requirements/optional.txt - MMCV_WITH_OPS=1 FORCE_NPU=1 python setup.py install ++ 通过单击“立即下载”,下载源码包。 + +# 准备训练环境 + +## 准备环境 + +- 当前模型支持的固件与驱动、 CANN 以及 PyTorch 如下表所示。 + + **表 1** 版本配套表 + + | 配套 | 版本 | + | ---------- | ------------------------------------------------------------ | + | 硬件 | [1.0.17](https://www.hiascend.com/hardware/firmware-drivers?tag=commercial) | + | NPU固件与驱动 | [6.0.RC1](https://www.hiascend.com/hardware/firmware-drivers?tag=commercial) | + | CANN | [6.0.RC1](https://www.hiascend.com/software/cann/commercial?version=6.0.RC1) | + | PyTorch | [1.8.1](https://gitee.com/ascend/pytorch/tree/master/)| + +- 环境准备指导。 + + 请参考《[Pytorch框架训练环境准备](https://www.hiascend.com/document/detail/zh/ModelZoo/pytorchframework/ptes)》。 + +- 安装依赖。 + ``` - -#### mmsegmentation -- To install mmesegmentation, run: - ```bash - cd $BiseNetV1_for_PyTorch - pip install -v -e . - # "-v" means verbose, or more output - # "-e" means installing a project in editable mode, - # thus any local modifications made to the code will take effect without reinstallation. + pip install -r requirements.txt ``` -### Prepare Datasets -- The data can be found [here](https://www.cityscapes-dataset.com/downloads/) after registration. Download `gtFine_trainvaltest.zip` (241MB) and `leftImg8bit_trainvaltest.zip` (11GB). -- The compressed package should be stored in `$BiseNetV1_for_PyTorch/data`. -- To decompress the dataset, run: - ```shell - cd $BiseNetV1_for_PyTorch/data - apt install unzip - unzip leftImg8bit_trainvaltest.zip -d cityscapes - # when prompting whether to overwrite, enter 'A' - unzip gtFine_trainvaltest.zip -d cityscapes +- 安装MMCV库和mmsegmentation + + 执行命令: + ``` -- After decompression, the file structure should look like: - ```none - $ BiseNetV1_for_PyTorch - ├── mmseg - ├── tools - ├── configs - ├── data - │ └── cityscapes - │ ├── leftImg8bit - │ │ ├── train - │ │ ├── val - │ │ └── test - │ ├── gtFine - │ │ ├── train - │ │ ├── val - │ │ └── test - │ ├── train.txt - │ ├── val.txt - │ └── test.txt - └── ... + cd BiseNetV1_for_PyTorch + bash tools/download_mmcv.sh ``` -- By convention, `**labelTrainIds.png` are used for cityscapes training. To generate `**labelTrainIds.png`, run: - ```shell - cd $BiseNetV1_for_PyTorch - pip install cityscapesscripts - # --nproc means 8 process for conversion, which could be omitted as well. - python tools/convert_datasets/cityscapes.py data/cityscapes --nproc 8 ++ 建议使用`python` or `python3.7` 来运行程序,如果需要使用`python3` ,执行以下命令: + + ```bash + unlink /usr/bin/python3 + ln -s /usr/bin/python3.7 /usr/bin/python3 ``` + -### Full Test (for accuracy) -For full test on 8 NPU, run: -```bash -cd $BiseNetV1_for_PyTorch -bash ./test/train_full_8p.sh -``` -Defaultly, training auto-resumes checkpoint in work directory. Remove the `work_dirs` directory to train from begin. +## 准备数据集 + +cityscapes数据集可以在该地址下载: [here](https://www.cityscapes-dataset.com/downloads/) ,需要下载 `gtFine_trainvaltest.zip` (241MB) 和 `leftImg8bit_trainvaltest.zip` (11GB). -### Performance Test -For performance test on 1 NPU, run: +在BiseNetV1_for_PyTorch目录创建data文件夹,并解压数据集: -```bash -cd $BiseNetV1_for_PyTorch -bash ./test/train_performance_1p.sh + ``` +cd BiseNetV1_for_PyTorch +mkdir data +cd data +unzip leftImg8bit_trainvaltest.zip -d cityscapes +# 当提示是否要overwrite时, 键入'A' +unzip gtFine_trainvaltest.zip -d cityscapes + ``` + +解压后,数据集目录结构参考如下所示: + +``` +$ BiseNetV1_for_PyTorch + ├── mmseg + ├── tools + ├── configs + ├── data + │ └── cityscapes + │ ├── leftImg8bit + │ │ ├── train + │ │ ├── val + │ │ └── test + │ ├── gtFine + │ │ ├── train + │ │ ├── val + │ │ └── test + │ ├── train.txt + │ ├── val.txt + │ └── test.txt + └── ... ``` -For performance test on 8 NPU, run: +labelTrainIds.png用于cityscapes的训练,执行命令生成labelTrainIds.png: -```bash -cd $BiseNetV1_for_PyTorch -bash ./test/train_performance_8p.sh ``` +cd BiseNetV1_for_PyTorch +pip3 install cityscapesscripts +python3 tools/convert_datasets/cityscapes.py data/cityscapes --nproc 8 +``` + + + +# 开始训练 + +## 训练模型 + +1. 进入解压后的源码包根目录。 + + ``` + cd /${模型文件夹名称} + ``` + +2. 运行训练脚本。 + + 该模型支持单机单卡训练和单机8卡训练。 + + + 单机单卡训练 + + 启动单卡训练: + + ``` + bash test/train_performance_1p.sh # 1p性能 + ``` + + + 单机8卡训练 + + 启动8卡训练: + + ``` + bash test/train_full_8p.sh # 8p精度 + bash test/train_performance_8p.sh # 8p性能 + ``` + + 模型训练脚本参数说明如下: + + 公共参数: + --seed //随机种子 + --gpu-ids //训练使用的device卡id + --opt-level //混合精度级别 + + + + +# 训练结果展示 + +**表 2** 训练结果展示表 + +| NAME | mIoU | FPS | Steps | AMP_Type | +|--------|----------|--------------------:| ---------:| ----------| +| 1p-竞品 | - | 9 | 400 | O1 | +| 1p-NPU | - | 12 | 400 | O1 | +| 8p-竞品 | - | 62 | 400 | O1 | +| 8p-NPU | - | 88 | 400 | O1 | +| 8p-竞品 | 75.80 | 62 | 40000 | O1 | +| 8p-NPU | 76.03 | 88 | 40000 | O1 | + + + +# 版本说明 + +## 变更 -### Training result for `BiseNetV1` +2023.01.12:Readme整改。 -| mIoU | FPS | Npu_nums | Steps | AMP_Type | CPU | -|:----:|:---:|:--------:|:-----:|:--------:|:---:| -| - | - | 1 | 400 | O1 | ARM | -| - | - | 8 | 400 | O1 | ARM | -| - | - | 8 | 40000 | O1 | ARM | +## 已知问题 -### Notes -It is recommended to use `python` or `python3.7` to execute the model training process. If you need to use `python3`, run the following commands before using `python3` due to the `2to3` dependency. -```bash -unlink /usr/bin/python3 -ln -s /usr/bin/python3.7 /usr/bin/python3 -``` \ No newline at end of file +无。 \ No newline at end of file diff --git a/PyTorch/built-in/cv/semantic_segmentation/BiseNetV1_for_PyTorch/requirements.txt b/PyTorch/built-in/cv/semantic_segmentation/BiseNetV1_for_PyTorch/requirements.txt index 6da5adea757ffc79ac35e544d4afe85c5f44a90d..e002a80eb760342bc67c7ca1bc7c60181a32042d 100644 --- a/PyTorch/built-in/cv/semantic_segmentation/BiseNetV1_for_PyTorch/requirements.txt +++ b/PyTorch/built-in/cv/semantic_segmentation/BiseNetV1_for_PyTorch/requirements.txt @@ -1,3 +1,5 @@ +torchvision +pillow==9.3.0 -r requirements/optional.txt -r requirements/runtime.txt -r requirements/tests.txt diff --git a/PyTorch/built-in/cv/semantic_segmentation/BiseNetV1_for_PyTorch/tools/download_mmcv.sh b/PyTorch/built-in/cv/semantic_segmentation/BiseNetV1_for_PyTorch/tools/download_mmcv.sh new file mode 100644 index 0000000000000000000000000000000000000000..5c20108e0b466214d9a870d656ed8748343dd86c --- /dev/null +++ b/PyTorch/built-in/cv/semantic_segmentation/BiseNetV1_for_PyTorch/tools/download_mmcv.sh @@ -0,0 +1,22 @@ +# 从Github中下载 v1.6.1版本的MMCV库 +git clone -b v1.6.1 --depth=1 https://github.com/open-mmlab/mmcv.git + +# 使用mmcv_need文件夹中的文件替换MMCV库 +cp -rf mmcv_need/builder.py mmcv/mmcv/runner/optimizer/ +cp -rf mmcv_need/dist_utils.py mmcv/mmcv/runner/ +cp -rf mmcv_need/__init__utils.py mmcv/mmcv/utils/__init__.py +cp -rf mmcv_need/device_type.py mmcv/mmcv/utils/ +cp -rf mmcv_need/optimizer.py mmcv/mmcv/runner/hooks/ +cp -rf mmcv_need/__init__device.py mmcv/mmcv/device/__init__.py +cp -rf mmcv_need/npu mmcv/mmcv/device/ +cp -rf mmcv_need/utils.py mmcv/mmcv/device/ + +# 编译安装MMCV库 +cd mmcv +pip3 install -r requirements/optional.txt +source ../test/env_npu.sh +MMCV_WITH_OPS=1 FORCE_NPU=1 python3 setup.py install + +# 安装mmsegmentation +cd .. +pip3 install -v -e . \ No newline at end of file