diff --git a/research/cv/ghostnet_d/README.md b/research/cv/ghostnet_d/README.md new file mode 100644 index 0000000000000000000000000000000000000000..89293ac60dc70d4e2f56eb570cb55f9167bab68f --- /dev/null +++ b/research/cv/ghostnet_d/README.md @@ -0,0 +1,191 @@ +# Contents + +- [Contents](#contents) + - [Algorithm Introduction](#algorithm-introduction) + - [Algorithm Principles](#algorithm-principles) + - [Configuring the training process](#configuring-the-training-process) + - [Dataset](#dataset) + - [Requirements](#requirements) + - [Hardware (Ascend)](#hardware-ascend) + - [Framework](#framework) + - [For more information, please check the resources below](#for-more-information-please-check-the-resources-below) + - [Script Description](#script-description) + - [Scripts and Sample Code](#scripts-and-sample-code) + - [Script Parameter](#script-parameter) + - [Training Process](#training-process) + - [For training](#for-training) + - [Evaluation](#evaluation) + - [Evaluation Process](#evaluation-process) + - [Evaluation Result](#evaluation-result) + - [ModeZoo Homepage](#modelzoo-homepage) + +## Algorithm Introduction + +GhostNeXt is an efficient network architecture, which can obtain high performance on GPUs in terms of both accuracy and latency. Current networks usually use depth-wise convolution to reduce latency, but a GPU-efficient network architecture should not contain too much trivial computation in a building stage. Therefore, we present to utilize the stage-wise feature redundancy to formulate GhostX stage structure. The new GhostX stage can be well embedded into any mainstream CNN architectures. + +## Algorithm Principles + +The features will be split into two parts where the first part is processed using the original block with fewer output channels for generating intrinsic features, and the other are generated using cheap operations by exploiting stage-wise redundancy. To further enhance their representation capacity, we embed the information of intermediate intrinsic features into the cheap operation for compensation. The following figure shows the proposed GhostX stage: + +![G1](image/ghostnetD_1.png) + +To complement the lacked information, we propose to utilize the intermediate features in the complicated path to enhance the representation capacity of the cheap operations. Mix operation for aggregating the intermediate information into the ghost features. + +![G2](image/ghostnetD_2.png) + +We apply a global average pooling to obtain the aggregated feature, then transform the aggregated feature into the same domain as the origin output. + +For Davincit chips, the slicing operation will introduce a larger latency so we apply 1x1 convolution to replace it. In this work, we apply the proposed GhostX stage on ResNets to obtain GhostNets_D. + +### Configuring the training process + +For details, see the configuration file src/ghostnet_d.yml in the sample code. + +```yaml + +general: + backend: mindspore + parallel_fully_train: True + +pipeline: [fully_train] + +fully_train: + pipe_step: + type: TrainPipeStep + + model: + model_desc: + type: ./src/ghostnet_d.ghostnet101 + + trainer: + type: Trainer + epochs: 120 + mixup: True + optimizer: + type: SGD + params: + lr: 0.001 + momentum: 0.9 + weight_decay: 0.0001 + lr_scheduler: + type: CosineAnnealingLR + by_epoch: True + params: + T_max: 120 + loss: + type: CrossEntropyLoss + params: + sparse: True +``` + +## Dataset + +The benchmark datasets can be downloaded as follows: + +[ImageNet2012](https://image-net.org/challenges/LSVRC/2012/). + +After downloaded the correspond dataset to the target place, You can configure and use the dataset separately for train and test. + +Dataset configuration parameters in src/ghostnext_d.yml: + +```yaml + type: Imagenet + common: + data_path: "/cache/datasets/ILSVRC/Data/CLS-LOC" + batch_size: 128 + n_class: 1000 +``` + +## Requirements + +### Hardware (Ascend) + +> Prepare hardware environment with Ascend. + +### Framework + +> [MindSpore](https://www.mindspore.cn/install/en) + +### For more information, please check the resources below + +[MindSpore Tutorials](https://www.mindspore.cn/tutorials/en/r1.3/index.html) + +[MindSpore Python API](https://www.mindspore.cn/docs/api/en/r1.3/index.html) + +## Script Description + +### Scripts and Sample Code + +```bash +ghostnet_d +├── eval.py # inference entry +├── train.py # pre-training entry +├── README.md # Readme +├── scripts +│ ├── run_standalone.sh # shell script for standalone train on ascend +│ ├── run_distributed.sh # shell script for distributed train on ascend +└── src + └── ghostnet_d.yml # options/hyper-parameters of ghostnet_d +``` + +### Script Parameter + +> For details about hyperparameters, see src/ghostnet_d.yml. + +## Training Process + +### For training + +- Standalone Ascend Training: + +```bash +sh scripts/run_standalone.sh +``` + +- Distributed Ascend Training: + +```bash +sh scripts/run_distributed.sh [RANK_TABLE_FILE] +``` + + For distributed training, a hccl configuration file with JSON format needs to be created in advance. + + Please follow the instructions in the link below: + + . + +`$RANK_TABLE_FILE` is needed when you are running a distribute task on ascend. + +> Or one can run following script for all tasks. + +```bash +python3 train.py +``` + +## Evaluation + +### Evaluation Process + +> Inference example: + +Modify src/eval.yml: + +```bash +models_folder: [CHECKPOINT_PATH] +``` + +```bash +python3 eval.py +``` + +### Evaluation Result + +The result are evaluated by the value of accuracy and parameters. + +```bash +INFO Best values: [{'worker_id': '0', 'performance': {'accuracy_top1': 0.7735977564102564,'flops': 0.0, 'params': 38216.872}}] +``` + +## ModelZoo Homepage + +Please check the official [homepage](https://gitee.com/mindspore/models). \ No newline at end of file diff --git a/research/cv/ghostnet_d/eval.py b/research/cv/ghostnet_d/eval.py new file mode 100644 index 0000000000000000000000000000000000000000..643b5c4963d87da47f337d41357235c223a13f54 --- /dev/null +++ b/research/cv/ghostnet_d/eval.py @@ -0,0 +1,20 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +"""eval script""" +import vega + + +if __name__ == '__main__': + vega.run('./src/eval.yml') diff --git a/research/cv/ghostnet_d/image/ghostnetD_1.png b/research/cv/ghostnet_d/image/ghostnetD_1.png new file mode 100644 index 0000000000000000000000000000000000000000..a079e35f5d6ef66a75d5d91f8e16d394d346a66e Binary files /dev/null and b/research/cv/ghostnet_d/image/ghostnetD_1.png differ diff --git a/research/cv/ghostnet_d/image/ghostnetD_2.png b/research/cv/ghostnet_d/image/ghostnetD_2.png new file mode 100644 index 0000000000000000000000000000000000000000..f2838697244a0f3eaba588b38027f6fc215bb1f2 Binary files /dev/null and b/research/cv/ghostnet_d/image/ghostnetD_2.png differ diff --git a/research/cv/ghostnet_d/requirements.txt b/research/cv/ghostnet_d/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..d912a2afb6d7478b41be2bb4844dedbb67f8817b --- /dev/null +++ b/research/cv/ghostnet_d/requirements.txt @@ -0,0 +1 @@ +noah-vega==1.8.1 \ No newline at end of file diff --git a/research/cv/ghostnet_d/scripts/run_distributed.sh b/research/cv/ghostnet_d/scripts/run_distributed.sh new file mode 100644 index 0000000000000000000000000000000000000000..165445163526eae1056f0f11ca3b9d96052dd6cb --- /dev/null +++ b/research/cv/ghostnet_d/scripts/run_distributed.sh @@ -0,0 +1,37 @@ +#!/bin/bash +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ + +ulimit -u unlimited +export DEVICE_NUM=8 +export RANK_SIZE=8 + +if [ $# != 1 ] +then + echo "Usage: sh run_distribution.sh [RANK_TABLE_FILE]" +exit 1 +fi + +if [ ! -f $1 ] +then + echo "error: RANK_TABLE_FILE=$1 is not a file" +exit 1 +fi + +RANK_TABLE_FILE=$(realpath $1) +export RANK_TABLE_FILE + +python3 -m vega.tools.run_pipeline ../src/ghostnet_d.yml -b m -d NPU \ +> train.log 2>&1 & diff --git a/research/cv/ghostnet_d/scripts/run_standalone.sh b/research/cv/ghostnet_d/scripts/run_standalone.sh new file mode 100644 index 0000000000000000000000000000000000000000..63f870d5868658963e9e2e521d83f081d90d4fa4 --- /dev/null +++ b/research/cv/ghostnet_d/scripts/run_standalone.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ + +ulimit -u unlimited +export DEVICE_NUM=1 +export RANK_SIZE=1 + +if [ $# != 0 ] +then + echo "Usage: sh run_standalone.sh" +exit 1 +fi + +python3 -m vega.tools.run_pipeline ../src/ghostnet_d.yml -b m -d NPU \ +> train.log 2>&1 & diff --git a/research/cv/ghostnet_d/src/eval.yml b/research/cv/ghostnet_d/src/eval.yml new file mode 100644 index 0000000000000000000000000000000000000000..daba876bc41b93dd9e0411046d29a7d603110a1f --- /dev/null +++ b/research/cv/ghostnet_d/src/eval.yml @@ -0,0 +1,35 @@ +general: + backend: mindspore + +pipeline: [eval] + +eval: + pipe_step: + type: TrainPipeStep + models_folder: ~ + + dataset: + type: Imagenet + common: + data_path: "/cache/datasets/ILSVRC/Data/CLS-LOC" + batch_size: 128 + + trainer: + with_train: False + type: Trainer + epochs: 120 + optimizer: + type: SGD + params: + lr: 0.001 + momentum: 0.9 + weight_decay: 0.0001 + lr_scheduler: + type: CosineAnnealingLR + by_epoch: True + params: + T_max: 120 + loss: + type: CrossEntropyLoss + params: + sparse: True diff --git a/research/cv/ghostnet_d/src/ghostnet_d.yml b/research/cv/ghostnet_d/src/ghostnet_d.yml new file mode 100644 index 0000000000000000000000000000000000000000..cd58bda1719e145cb26fdea76f644c6e179689b7 --- /dev/null +++ b/research/cv/ghostnet_d/src/ghostnet_d.yml @@ -0,0 +1,41 @@ +general: + backend: mindspore + device_category: NPU + parallel_fully_train: True + +pipeline: [fully_train] + +fully_train: + pipe_step: + type: TrainPipeStep + + model: + model_desc: + type: ghostnet_d.ghostnet101 + + trainer: + type: Trainer + epochs: 120 + mixup: True + optimizer: + type: SGD + params: + lr: 0.001 + momentum: 0.9 + weight_decay: 0.0001 + lr_scheduler: + type: CosineAnnealingLR + by_epoch: True + params: + T_max: 120 + loss: + type: CrossEntropyLoss + params: + sparse: True + + dataset: + type: Imagenet + common: + data_path: "/cache/datasets/ILSVRC/Data/CLS-LOC" + batch_size: 128 + n_class: 1000 \ No newline at end of file diff --git a/research/cv/ghostnet_d/train.py b/research/cv/ghostnet_d/train.py new file mode 100644 index 0000000000000000000000000000000000000000..d24ac56a744b1aca5dbc13a546717d74d8eebab5 --- /dev/null +++ b/research/cv/ghostnet_d/train.py @@ -0,0 +1,20 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +"""train""" +import vega + + +if __name__ == '__main__': + vega.run('./src/ghostnet_d.yml')