diff --git a/official/cv/Arcface/Dockerfile b/official/cv/Arcface/Dockerfile deleted file mode 100644 index 360861ede17fb0ab697fbcac190acde7c1e29fef..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -ARG FROM_IMAGE_NAME -FROM ${FROM_IMAGE_NAME} - -COPY requirements.txt . -RUN pip3.7 install -r requirements.txt diff --git a/official/cv/Arcface/README.md b/official/cv/Arcface/README.md deleted file mode 100644 index a76102757383164bdbb61ccb168a6b54b21b81f0..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/README.md +++ /dev/null @@ -1,430 +0,0 @@ -Contents - - - -- [Arcface Overview](#arcface-overview) -- [Datasets](#datasets) -- [Environment Requirements](#environment-requirements) -- [Quick Start](#quick-start) - - [Script Description](#script-description) - - [Script and Sample Code](#script-and-sample-code) - - [Script Parameters](#script-parameters) - - [Training Process](#training-process) - - [Distributed Training](#distributed-training) - - [Evaluation Process](#evaluation-process) - - [Evaluation](#evaluation) - - [ONNX Evaluation](#onnx-evaluation) - - [Exporting a MindIR Model](#exporting-a-mindir-model) -- [Inference Process](#inference-process) - - [Usage](#usage) - - [Results](#results) -- [Model Description](#model-description) - - [Performance](#performance) - - [Training Performance](#training-performance) - - [Evaluation Performance](#evaluation-performance) -- [Random Seed Description](#random-seed-description) -- [ModelZoo Home Page](#modelzoo-home-page) - - - -# Arcface Overview - -One of the main challenges in feature learning using Deep Convolutional Neural Networks (DCNNs) for large-scale face recognition is the design of appropriate loss functions that enhance discriminative power. ArcFace outperforms SoftmaxLoss, Center Loss, A-Softmax Loss and Cosine Margin Loss in face recognition. ArcFace is improved in contrast to the conventional softmax. ArcFace has a clear geometric interpretation due to the exact correspondence to the geodesic distance on the hypersphere Based on experimental evaluation of all the recent state-of-the-art face recognition methods on over 10 benchmarks. It is proved that ArcFace consistently outperforms the state-of-the-art and can be easily implemented with negligible computational overhead. - -[Paper](https://arxiv.org/pdf/1801.07698v3.pdf): Deng J , Guo J , Zafeiriou S . ArcFace: Additive Angular Margin Loss for Deep Face Recognition[J]. 2018. - -# Datasets - -Training set: [MS1MV2](https://github.com/deepinsight/insightface/wiki/Dataset-Zoo) - -Validation set: LFW, CFP-FP, AGEDB, CPLFW, CALFW, IJB-B, and IJB-C - -Training set: 5,822,653 images of 85742 classes - -```shell -#Convert the REC data format to JPG. -python src/rec2jpg_dataset.py --include rec/dataset/path --output output/path -``` - -Note: In the Arm environment, the [MXNet](https://mxnet.apache.org/versions/1.9.0/get_started/build_from_source.html) can run properly only after being built by the source code. - -# Environment Requirements - -- Hardware: Ascend AI Processors - - Set up the hardware environment with Ascend AI Processors. - -- Framework - - [MindSpore](https://www.mindspore.cn/install/en) -- For more information, please check the following resources: - - [MindSpore Tutorials](https://www.mindspore.cn/tutorials/en/master/index.html) - - [MindSpore Python API](https://www.mindspore.cn/docs/en/master/api_python/mindspore.html) - -# Quick Start - -After installing MindSpore from the official website, you can perform the following steps for training and evaluation: - -- On Ascend AI Processors - - ```python - # Example of distributed training - bash scripts/run_distribute_train.sh rank_size /path/dataset - - # Example of standalone training - bash scripts/run_standalone_train.sh /path/dataset device_id - - # Evaluation example - bash scripts/run_eval.sh /path/evalset /path/ckpt - ``` - -- On GPUs - - ```python - # Example of distributed training - bash scripts/run_distribute_train_gpu.sh /path/dataset rank_size - - # Example of standalone training - bash scripts/run_standalone_train_gpu.sh /path/dataset - - # Evaluation example - bash scripts/run_eval_gpu.sh /path/evalset /path/ckpt - ``` - -## Script Description - -## Script and Sample Code - -```path -└── Arcface - ├── README.md // Arcface description - ├── ascend310_infer // Inference on Ascend 310 AI Processors - ├── inc - ├── utils.h - ├── src - ├── main.cc - ├── utils.cc - ├── build.sh - └── CMakeLists.txt - ├── scripts - ├── run_310_infer.sh // Shell script for inference on Ascend 310 AI Processors - ├── run_distribute_train.sh // Shell script for distributed training - ├── run_standalone_train.sh // Shell script for standalone training - ├── run_eval_ijbc.sh // Shell script for evaluation on the IJB-C dataset - ├── run_eval.sh // Shell script for evaluation - ├── run_eval_ijbc_onnx.sh // Shell script for ONNX evaluation on the IJB-C dataset - └── run_eval_onnx.sh // Shell script for ONNX evaluation - ├──src - ├── loss.py // Loss function - ├── dataset.py // Dataset creation - ├── iresnet.py // ResNet architecture - ├── rec2jpg_dataset.py // Converting data from REC to JPG - ├── val.py // Test script - ├── train.py // Training script - ├── export.py - ├── requirements.txt - ├── preprocess.py // Preprocessing inference data on Ascend 310 AI Processors - ├── postprocess.py // Post-processing inference data on Ascend 310 AI Processors - ├── eval_onnx.py // ONNX evaluation - └── eval_ijbc_onnx.py // ONNX evaluation on the IJB-C dataset - -``` - -## Script Parameters - -```python -Key parameters in train.py and val.py are as follows: - --- modelarts: Specifies whether to use the ModelArts platform for training. The options are True or False. The default value is False. --- device_id: ID of the device used for dataset training or evaluation. When train.sh is used for distributed training, ignore this parameter. --- device_num: Number of devices used for distributed training. --- train_url: Output path of the checkpoint file. --- data_url: Training dataset path. --- ckpt_url: Checkpoint path. --- eval_url: Validation dataset path. - -``` - -## Training Process - -### Distributed Training - -- On Ascend AI Processors - - ```bash - bash scripts/run_distribute_train.sh rank_size /path/dataset - ``` - - The preceding shell script will run distributed training in the backend. You can view the result in the `device[X]/train.log` file. - The following methods are used to achieve the loss value: - - ```log - epoch: 2 step: 11372, loss is 12.807039 - epoch time: 1104549.619 ms, per step time: 97.129 ms - epoch: 3 step: 11372, loss is 9.13787 - ... - epoch: 21 step: 11372, loss is 1.5028578 - epoch time: 1104673.362 ms, per step time: 97.140 ms - epoch: 22 step: 11372, loss is 0.8846929 - epoch time: 1104929.793 ms, per step time: 97.162 ms - ``` - -- On GPUs - - ```bash - bash scripts/run_distribute_train_gpu.sh /path/dataset rank_size - ``` - - The preceding shell script will run distributed training in the backend. You can view the result in the `train_parallel/train.log` file. - The following methods are used to achieve the loss value: - - ```log - epoch: 2 step: 11372, loss is 10.572094 - epoch time: 1104549.619 ms, per step time: 991.390 ms - epoch: 3 step: 11372, loss is 7.442794 - ... - epoch: 21 step: 11372, loss is 0.8472798 - epoch time: 1104673.362 ms, per step time: 989.479 ms - epoch: 22 step: 11372, loss is 0.5226351 - epoch time: 1104929.793 ms, per step time: 989.548 ms - ``` - -## Evaluation Process - -### Evaluation - -- Evaluate the LFW, CFP-FP, AgeDB-30, CALFW, and CPLFW datasets on the Ascend AI Processors. - - Check the checkpoint path for evaluation before running the following command. Set the checkpoint path to an absolute full path, for example, `username/arcface/arcface-11372-1.ckpt`. - - ```bash - bash scripts/run_eval.sh /path/evalset /path/ckpt - ``` - - The preceding Python command is executed in the backend. You can view the result in the **eval.log** file. The accuracy of the test set is as follows: - - ```bash - [lfw]Accuracy-Flip: 0.99817+-0.00273 - [cfp_fp]Accuracy-Flip: 0.98000+-0.00586 - [agedb_30]Accuracy-Flip: 0.98100+-0.00642 - [calfw]Accuracy-Flip: 0.96150+-0.01099 - [cplfw]Accuracy-Flip: 0.92583+-0.01367 - ``` - -- Evaluate the IJB-B and IJB-C datasets on the Ascend AI Processors. - - Check the checkpoint path for evaluation before running the following command. Set the checkpoint path to an absolute full path, for example, `username/arcface/arcface-11372-1.ckpt`. - - Ensure that the input validation set path is `IJB_release/IJBB/` or `IJB_release/IJBC/`. - - ```bash - bash scripts/run_eval_ijbc.sh /path/evalset /path/ckpt - ``` - - The preceding Python command is executed in the backend. You can view the result in the **eval.log** file. The accuracy of the test set is as follows: - - ```bash - +-----------+-------+-------+--------+-------+-------+-------+ - | Methods | 1e-06 | 1e-05 | 0.0001 | 0.001 | 0.01 | 0.1 | - +-----------+-------+-------+--------+-------+-------+-------+ - | ijbb-IJBB | 40.01 | 87.91 | 94.36 | 96.48 | 97.72 | 98.70 | - +-----------+-------+-------+--------+-------+-------+-------+ - - +-----------+-------+-------+--------+-------+-------+-------+ - | Methods | 1e-06 | 1e-05 | 0.0001 | 0.001 | 0.01 | 0.1 | - +-----------+-------+-------+--------+-------+-------+-------+ - | ijbc-IJBC | 82.08 | 93.37 | 95.87 | 97.40 | 98.40 | 99.05 | - +-----------+-------+-------+--------+-------+-------+-------+ - ``` - -- Evaluate the LFW, CFP-FP, AgeDB-30, CALFW, and CPLFW datasets on GPUs. - - Check the checkpoint path for evaluation before running the following command. Set the checkpoint path to an absolute full path, for example, `username/arcface/arcface-11372-1.ckpt`. - - ```bash - bash scripts/run_eval_gpu.sh /path/evalset /path/ckpt - ``` - - The preceding Python command is executed in the backend. You can view the result in the **eval.log** file. The accuracy of the test set is as follows: - - ```bash - [lfw]Accuracy-Flip: 0.99767+-0.00271 - [cfp_fp]Accuracy-Flip: 0.98414+-0.00659 - [agedb_30]Accuracy-Flip: 0.98033+-0.00878 - [calfw]Accuracy-Flip: 0.95983+-0.01141 - [cplfw]Accuracy-Flip: 0.92817+-0.01279 - ``` - -- Evaluate the IJB-B and IJB-C datasets on GPUs. - - Check the checkpoint path for evaluation before running the following command. Set the checkpoint path to an absolute full path, for example, `username/arcface/arcface-11372-1.ckpt`. - - Ensure that the input validation set path is `IJB_release/IJBB/` or `IJB_release/IJBC/`. - - ```bash - bash scripts/run_eval_ijbc_gpu.sh /path/evalset /path/ckpt - ``` - - The preceding Python command is executed in the backend. You can view the result in the **eval.log** file. The accuracy of the test set is as follows: - - ```bash - +-----------+-------+-------+--------+-------+-------+-------+ - | Methods | 1e-06 | 1e-05 | 0.0001 | 0.001 | 0.01 | 0.1 | - +-----------+-------+-------+--------+-------+-------+-------+ - | ijbb-IJBB | 42.46 | 89.76 | 94.81 | 96.58 | 97.73 | 98.78 | - +-----------+-------+-------+--------+-------+-------+-------+ - - +-----------+-------+-------+--------+-------+-------+-------+ - | Methods | 1e-06 | 1e-05 | 0.0001 | 0.001 | 0.01 | 0.1 | - +-----------+-------+-------+--------+-------+-------+-------+ - | ijbc-IJBC | 86.67 | 94.35 | 96.19 | 97.55 | 98.38 | 99.10 | - +-----------+-------+-------+--------+-------+-------+-------+ - ``` - -### ONNX Evaluation - -Click [here](https://www.mindspore.cn/resources/hub/details?MindSpore/1.6/arcface_ms1mv2) to obtain the CKPT file required for evaluation. - -Before performing evaluation, you need to export the ONNX file using **export.py**. - -'''python -python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --batch_size [BATCH_SIZE] --file_format ONNX -''' - -- Use the ONNX file to evaluate the LFW, CFP-FP, AgeDB-30, CALFW, and CPLFW datasets on GPUs. - - ```bash - bash run_eval_onnx.sh /path/evalset /path/onnx - - # Example of ONNX evaluation: - python export.py --batch_size 64 --ckpt_file ./arcface_ascend_v160_ms1mv2_research_cv_IJBB97.67_IJBC98.36.ckpt --file_name onnx_64 --file_format ONNX --device_target GPU - cd ./scripts/ - bash run_eval_onnx.sh ../dataset/ ../onnx_64.onnx - ``` - - The preceding Python command is executed in the backend. You can view the result in the **eval_onnx.log** file. The accuracy of the test set is as follows: - - ```bash - [lfw]Accuracy-Flip: 0.99750+-0.00291 - [cfp_fp]Accuracy-Flip: 0.98500+-0.00643 - [agedb_30]Accuracy-Flip: 0.98050+-0.00778 - [calfw]Accuracy-Flip: 0.96117+-0.01118 - [cplfw]Accuracy-Flip: 0.92900+-0.01250 - ``` - -- Use th ONNX file to evaluate the IJB-B dataset on GPUs. - - Check the checkpoint file path used for evaluation before running the command below. Ensure that the input validation set path is `IJB_release/IJBB/`. - - ```bash - bash run_eval_ijbc_onnx.sh /path/evalset path/onnx_bs path/onnx_rs IJBB - - # Example of ONNX evaluation: - python export.py --batch_size 256 --ckpt_file ./arcface_ascend_v160_ms1mv2_research_cv_IJBB97.67_IJBC98.36.ckpt --file_name onnx_256 --file_format ONNX --device_target GPU - python export.py --batch_size 92 --ckpt_file ./arcface_ascend_v160_ms1mv2_research_cv_IJBB97.67_IJBC98.36.ckpt --file_name onnx_92 --file_format ONNX --device_target GPU - cd ./scripts/ - bash run_eval_ijbc_onnx.sh ../dataset/IJB_release/IJBB ../onnx_256.onnx ../onnx_92.onnx IJBB - ``` - - The preceding Python command is executed in the backend. You can view the result in the **eval_onnx_IJBB.log** file. The accuracy of the test set is as follows: - - ```bash - +-----------+-------+-------+--------+-------+-------+-------+ - | Methods | 1e-06 | 1e-05 | 0.0001 | 0.001 | 0.01 | 0.1 | - +-----------+-------+-------+--------+-------+-------+-------+ - | ijbb-IJBB | 46.81 | 89.89 | 94.81 | 96.54 | 97.68 | 98.70 | - +-----------+-------+-------+--------+-------+-------+-------+ - ``` - -- Use th ONNX file to evaluate the IJB-C dataset on GPUs. - - Check the checkpoint file path used for evaluation before running the command below. Ensure that the input validation set path is `IJB_release/IJBC/`. - - ```bash - bash run_eval_ijbc_onnx.sh /path/evalset path/onnx_bs path/onnx_rs IJBC - - # Example of ONNX evaluation: - python export.py --batch_size 256 --ckpt_file ./arcface_ascend_v160_ms1mv2_research_cv_IJBB97.67_IJBC98.36.ckpt --file_name onnx_256 --file_format ONNX --device_target GPU - python export.py --batch_size 254 --ckpt_file ./arcface_ascend_v160_ms1mv2_research_cv_IJBB97.67_IJBC98.36.ckpt --file_name onnx_254 --file_format ONNX --device_target GPU - cd ./scripts/ - bash run_eval_ijbc_onnx.sh ../dataset/IJB_release/IJBC ../onnx_256.onnx ../onnx_254.onnx IJBC - ``` - - The preceding Python command is executed in the backend. You can view the result in the **eval_onnx_IJBC.log** file. The accuracy of the test set is as follows: - - ```bash - +-----------+-------+-------+--------+-------+-------+-------+ - | Methods | 1e-06 | 1e-05 | 0.0001 | 0.001 | 0.01 | 0.1 | - +-----------+-------+-------+--------+-------+-------+-------+ - | ijbc-IJBC | 88.76 | 94.39 | 96.28 | 97.52 | 98.36 | 99.12 | - +-----------+-------+-------+--------+-------+-------+-------+ - ``` - -## Exporting a MindIR Model - -```python -python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] -``` - -The `ckpt_file` parameter is mandatory, and the value range of `FILE_FORMAT` is ["AIR", "MINDIR"]. - -# Inference Process - -**Set environment variables before inference by referring to [MindSpore C++ Inference Deployment Guide](https://gitee.com/mindspore/models/blob/master/utils/cpp_infer/README.md).** - -## Usage - -Before performing inference, you need to export the MINDIR file using **export.py**. - -```bash -# Inference on Ascend 310 AI Processors -bash run_310_infer.sh [MINDIR_PATH] [DATASET_PATH] [NEED_PREPROCESS] [DEVICE_TARGET] [DEVICE_ID] -``` - -The value range of `DEVICE_TARGET` is ['GPU', 'CPU', 'Ascend']. `NEED_PREPROCESS` specifies whether data needs to be preprocessed. The value can be **'y'** or **'n'**. In this example, **'y'** is set. `DEVICE_ID` is optional and the default value is **0**. - -### Results - -The inference result is saved in the current path. You can view the final accuracy result in **acc.log**. - -# Model Description - -## Performance - -### Training Performance - -| Parameter | Arcface | GPU -| ------------- | ------------------------------------------------------------ | ------------------------------------------- | -| Model version | arcface | arcface | -| Resources | Ascend 910 AI Processor; 2.60 GHz CPU with 192 cores; memory: 755 GB | GeForce RTX 3090; 2.90 GHz CPU with 64 cores; 755 GB memory | -| Upload date | 2021-05-30 | 2021-11-12 | -| MindSpore version| 1.2.0-c77-python3.7-aarch64 | 1.5.0 | -| Dataset | MS1MV2 | MS1MV2 | -| Training parameters | lr=0.08; gamma=0.1 | lr=0.08; gamma=0.1 | -| Optimizer | SGD | SGD | -| Loss function | Arcface | Arcface | -| Output | Probability | Probability | -| Loss | 0.6 | 0.7 | -| Speed | Single device: 108 ms/step; 8 devices: 97 ms/step | 8 devices: 990 ms/step | -| Total duration | Single device: 65 hours; 8 devices: 8.5 hours | 8 devices: 75 hours | -| Parameters (M) | 85.2 | 85.2 | -| Finetuned checkpoint | 1249 MB (.ckpt file) | 1249 MB (.ckpt file) | -| Script | [Link](https://gitee.com/mindspore/models/tree/master/official/cv/Arcface)| [Link](https://gitee.com/mindspore/models/tree/master/official/cv/Arcface)| - -### Evaluation Performance - -| Parameter | Ascend | GPU | -| ------------- | ------------------------ | ------------------------- | -| Model version | arcface | arcface | -| Resources | Ascend 910 | GeForce RTX 3090 | -| Upload date | 2021/05/30 | 2021/11/12 | -| MindSpore version| 1.2.0-c77-python3.7-aarch64 | 1.5.0 | -| Datasets | IJB-C, IJB-B, LFW, CFP-FP, AgeDB-30, CALFW, and CPLFW| IJB-C, IJB-B, LFW, CFP-FP, AgeDB-30, CALFW, and CPLFW| -| Output | Probability | Probability | -| Accuracy | lfw:0.998 cfp_fp:0.98 agedb_30:0.981 calfw:0.961 cplfw:0.926 IJB-B:0.943 IJB-C:0.958 | lfw:0.998 cfp_fp:0.984 agedb_30:0.9803 calfw:0.9598 cplfw:0.928 IJB-B:0.943 IJB-C:0.958 | - -# Random Seed Description - -The initial parameters of the network are initialized randomly. - -# ModelZoo Home Page - -For details, please go to the [official website](https://gitee.com/mindspore/models). diff --git a/official/cv/Arcface/README_CN.md b/official/cv/Arcface/README_CN.md deleted file mode 100644 index 4714861476fa1d167e3fd3c09f8e795fe9c5947f..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/README_CN.md +++ /dev/null @@ -1,429 +0,0 @@ -目录 - -- [Arcface概述](#arcface概述) -- [数据集](#数据集) -- [环境要求](#环境要求) -- [快速入门](#快速入门) - - [脚本说明](#脚本说明) - - [脚本和样例代码](#脚本和样例代码) - - [脚本参数](#脚本参数) - - [训练过程](#训练过程) - - [分布式训练](#分布式训练) - - [评估过程](#评估过程) - - [评估](#评估) - - [ONNX评估](#onnx评估) - - [导出mindir模型](#导出mindir模型) -- [推理过程](#推理过程) - - [用法](#用法) - - [结果](#结果) -- [模型描述](#模型描述) - - [性能](#性能) - - [训练性能](#训练性能) - - [评估性能](#评估性能) -- [随机情况说明](#随机情况说明) -- [ModelZoo主页](#modelzoo主页) - - - -# Arcface概述 - -使用深度卷积神经网络进行大规模人脸识别的特征学习中的主要挑战之一是设计适当的损失函数以增强判别能力。继SoftmaxLoss、Center Loss、A-Softmax Loss、Cosine Margin Loss之后,Arcface在人脸识别中具有更加良好的表现。Arcface是传统softmax的改进, 将类之间的距离映射到超球面的间距,论文给出了对此的清晰几何解释。同时,基于10多个人脸识别基准的实验评估,证明了Arcface优于现有的技术,并且可以轻松实现。 - -[论文](https://arxiv.org/pdf/1801.07698v3.pdf): Deng J , Guo J , Zafeiriou S . ArcFace: Additive Angular Margin Loss for Deep Face Recognition[J]. 2018. - -# 数据集 - -使用的训练数据集:[MS1MV2](https://github.com/deepinsight/insightface/wiki/Dataset-Zoo) - -验证数据集:lfw,cfp-fp,agedb,cplfw,calfw,IJB-B,IJB-C - -训练集:5,822,653张图片,85742个类 - -```shell -#将rec数据格式转换成jpg -python src/rec2jpg_dataset.py --include rec/dataset/path --output output/path -``` - -注意:在arm环境中,需源码编译[mxnet](https://mxnet.apache.org/versions/1.9.0/get_started/build_from_source.html)才能正常运行。 - -# 环境要求 - -- 硬件:昇腾处理器(Ascend) - - 使用Ascend处理器来搭建硬件环境。 - -- 框架 - - [MindSpore](https://www.mindspore.cn/install) -- 如需查看详情,请参见如下资源: - - [MindSpore教程](https://www.mindspore.cn/tutorials/zh-CN/master/index.html) - - [MindSpore Python API](https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore.html) - -# 快速入门 - -通过官方网站安装MindSpore后,您可以按照如下步骤进行训练和评估: - -- Ascend处理器环境运行 - - ```python - # 分布式训练运行示例 - bash scripts/run_distribute_train.sh rank_size /path/dataset - - # 单机训练运行示例 - bash scripts/run_standalone_train.sh /path/dataset device_id - - # 运行评估示例 - bash scripts/run_eval.sh /path/evalset /path/ckpt - ``` - -- GPU处理器环境运行 - - ```python - # 分布式训练运行示例 - bash scripts/run_distribute_train_gpu.sh /path/dataset rank_size - - # 单机训练运行示例 - bash scripts/run_standalone_train_gpu.sh /path/dataset 0 - - # 运行评估示例 - bash scripts/run_eval_gpu.sh /path/evalset /path/ckpt - ``` - -## 脚本说明 - -## 脚本和样例代码 - -```path -└── Arcface - ├── README.md // Arcface相关描述 - ├── ascend310_infer //310推理 - ├── inc - ├── utils.h - ├── src - ├── main.cc - ├── utils.cc - ├── build.sh - └── CMakeLists.txt - ├── scripts - ├── run_310_infer.sh // 用于310推理的shell脚本 - ├── run_distribute_train.sh // 用于分布式训练的shell脚本 - ├── run_standalone_train.sh // 用于单机训练的shell脚本 - ├── run_eval_ijbc.sh // 用于IJBC数据集评估的shell脚本 - ├── run_eval.sh // 用于评估的shell脚本 - ├── run_eval_ijbc_onnx.sh // 用于IJBC数据集ONNX评估的shell脚本 - └── run_eval_onnx.sh // 用于ONNX评估的shell脚本 - ├──src - ├── loss.py //损失函数 - ├── dataset.py // 创建数据集 - ├── iresnet.py // ResNet架构 - ├── rec2jpg_dataset.py // 将rec数据格式转换成jpg - ├── val.py // 测试脚本 - ├── train.py // 训练脚本 - ├── export.py - ├── requirements.txt - ├── preprocess.py // 310推理数据预处理 - ├── preprocess.py // 310推理数据后处理 - ├── eval_onnx.py // ONNX评估 - └── eval_ijbc_onnx.py // IJBC数据集的ONNX评估 - -``` - -## 脚本参数 - -```python -train.py和val.py中主要参数如下: - --- modelarts:是否使用modelarts平台训练。可选值为True、False。默认为False。 --- device_id:用于训练或评估数据集的设备ID。当使用train.sh进行分布式训练时,忽略此参数。 --- device_num:使用tra进行分布式训练时使用的设备数。 --- train_url:checkpoint的输出路径。 --- data_url:训练集路径。 --- ckpt_url:checkpoint路径。 --- eval_url:验证集路径。 - -``` - -## 训练过程 - -### 分布式训练 - -- Ascend处理器环境运行 - - ```bash - bash scripts/run_distribute_train.sh rank_size /path/dataset - ``` - - 上述shell脚本将在后台运行分布训练。可以通过`device[X]/train.log`文件查看结果。 - 采用以下方式达到损失值: - - ```log - epoch: 2 step: 11372, loss is 12.807039 - epoch time: 1104549.619 ms, per step time: 97.129 ms - epoch: 3 step: 11372, loss is 9.13787 - ... - epoch: 21 step: 11372, loss is 1.5028578 - epoch time: 1104673.362 ms, per step time: 97.140 ms - epoch: 22 step: 11372, loss is 0.8846929 - epoch time: 1104929.793 ms, per step time: 97.162 ms - ``` - -- GPU处理器环境运行 - - ```bash - bash scripts/run_distribute_train_gpu.sh /path/dataset rank_size - ``` - - 上述shell脚本将在后台运行分布训练。可以通过`train_parallel/train.log`文件查看结果。 - 采用以下方式达到损失值: - - ```log - epoch: 2 step: 11372, loss is 10.572094 - epoch time: 1104549.619 ms, per step time: 991.390 ms - epoch: 3 step: 11372, loss is 7.442794 - ... - epoch: 21 step: 11372, loss is 0.8472798 - epoch time: 1104673.362 ms, per step time: 989.479 ms - epoch: 22 step: 11372, loss is 0.5226351 - epoch time: 1104929.793 ms, per step time: 989.548 ms - ``` - -## 评估过程 - -### 评估 - -- 在Ascend环境运行时评估lfw、cfp_fp、agedb_30、calfw、cplfw数据集 - - 在运行以下命令之前,请检查用于评估的检查点路径。请将检查点路径设置为绝对全路径,例如“username/arcface/arcface-11372-1.ckpt”。 - - ```bash - bash scripts/run_eval.sh /path/evalset /path/ckpt - ``` - - 上述python命令将在后台运行,您可以通过eval.log文件查看结果。测试数据集的准确性如下: - - ```bash - [lfw]Accuracy-Flip: 0.99817+-0.00273 - [cfp_fp]Accuracy-Flip: 0.98000+-0.00586 - [agedb_30]Accuracy-Flip: 0.98100+-0.00642 - [calfw]Accuracy-Flip: 0.96150+-0.01099 - [cplfw]Accuracy-Flip: 0.92583+-0.01367 - ``` - -- 在Ascend环境运行时评估IJB-B、IJB-C数据集 - - 在运行以下命令之前,请检查用于评估的检查点路径。请将检查点路径设置为绝对全路径,例如“username/arcface/arcface-11372-1.ckpt”。 - - 同时,情确保传入的评估数据集路径为“IJB_release/IJBB/”或“IJB_release/IJBC/”。 - - ```bash - bash scripts/run_eval_ijbc.sh /path/evalset /path/ckpt - ``` - - 上述python命令将在后台运行,您可以通过eval.log文件查看结果。测试数据集的准确性如下: - - ```bash - +-----------+-------+-------+--------+-------+-------+-------+ - | Methods | 1e-06 | 1e-05 | 0.0001 | 0.001 | 0.01 | 0.1 | - +-----------+-------+-------+--------+-------+-------+-------+ - | ijbb-IJBB | 40.01 | 87.91 | 94.36 | 96.48 | 97.72 | 98.70 | - +-----------+-------+-------+--------+-------+-------+-------+ - - +-----------+-------+-------+--------+-------+-------+-------+ - | Methods | 1e-06 | 1e-05 | 0.0001 | 0.001 | 0.01 | 0.1 | - +-----------+-------+-------+--------+-------+-------+-------+ - | ijbc-IJBC | 82.08 | 93.37 | 95.87 | 97.40 | 98.40 | 99.05 | - +-----------+-------+-------+--------+-------+-------+-------+ - ``` - -- 在GPU环境运行时评估lfw、cfp_fp、agedb_30、calfw、cplfw数据集 - - 在运行以下命令之前,请检查用于评估的检查点路径。请将检查点路径设置为绝对全路径,例如“username/arcface/arcface-11372-1.ckpt”。 - - ```bash - bash scripts/run_eval_gpu.sh /path/evalset /path/ckpt - ``` - - 上述python命令将在后台运行,您可以通过eval.log文件查看结果。测试数据集的准确性如下: - - ```bash - [lfw]Accuracy-Flip: 0.99767+-0.00271 - [cfp_fp]Accuracy-Flip: 0.98414+-0.00659 - [agedb_30]Accuracy-Flip: 0.98033+-0.00878 - [calfw]Accuracy-Flip: 0.95983+-0.01141 - [cplfw]Accuracy-Flip: 0.92817+-0.01279 - ``` - -- 在GPU环境运行时评估IJB-B、IJB-C数据集 - - 在运行以下命令之前,请检查用于评估的检查点路径。请将检查点路径设置为绝对全路径,例如“username/arcface/arcface-11372-1.ckpt”。 - - 同时,情确保传入的评估数据集路径为“IJB_release/IJBB/”或“IJB_release/IJBC/”。 - - ```bash - bash scripts/run_eval_ijbc_gpu.sh /path/evalset /path/ckpt - ``` - - 上述python命令将在后台运行,您可以通过eval.log文件查看结果。测试数据集的准确性如下: - - ```bash - +-----------+-------+-------+--------+-------+-------+-------+ - | Methods | 1e-06 | 1e-05 | 0.0001 | 0.001 | 0.01 | 0.1 | - +-----------+-------+-------+--------+-------+-------+-------+ - | ijbb-IJBB | 42.46 | 89.76 | 94.81 | 96.58 | 97.73 | 98.78 | - +-----------+-------+-------+--------+-------+-------+-------+ - - +-----------+-------+-------+--------+-------+-------+-------+ - | Methods | 1e-06 | 1e-05 | 0.0001 | 0.001 | 0.01 | 0.1 | - +-----------+-------+-------+--------+-------+-------+-------+ - | ijbc-IJBC | 86.67 | 94.35 | 96.19 | 97.55 | 98.38 | 99.10 | - +-----------+-------+-------+--------+-------+-------+-------+ - ``` - -### ONNX评估 - -评估所需ckpt获取地址:[获取地址](https://www.mindspore.cn/resources/hub/details?MindSpore/1.6/arcface_ms1mv2) - -在执行评估之前,需要通过export.py导出onnx文件。 - -'''python -python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --batch_size [BATCH_SIZE] --file_format ONNX -''' - -- 在GPU环境中运行ONNX评估lfw、cfp_fp、agedb_30、calfw、cplfw数据集 - - ```bash - bash run_eval_onnx.sh /path/evalset /path/onnx - - # 使用onnx评估的示例: - python export.py --batch_size 64 --ckpt_file ./arcface_ascend_v160_ms1mv2_research_cv_IJBB97.67_IJBC98.36.ckpt --file_name onnx_64 --file_format ONNX --device_target GPU - cd ./scripts/ - bash run_eval_onnx.sh ../dataset/ ../onnx_64.onnx - ``` - - 上述python命令将在后台运行,您可以通过eval_onnx.log文件查看结果。测试数据集的准确性如下: - - ```bash - [lfw]Accuracy-Flip: 0.99750+-0.00291 - [cfp_fp]Accuracy-Flip: 0.98500+-0.00643 - [agedb_30]Accuracy-Flip: 0.98050+-0.00778 - [calfw]Accuracy-Flip: 0.96117+-0.01118 - [cplfw]Accuracy-Flip: 0.92900+-0.01250 - ``` - -- 在GPU环境运行ONNX评估IJB-B数据集 - - 在运行以下命令之前,请检查用于评估的检查点路径。请确保传入的评估数据集路径为“IJB_release/IJBB/”。 - - ```bash - bash run_eval_ijbc_onnx.sh /path/evalset path/onnx_bs path/onnx_rs IJBB - - # 使用onnx评估的示例: - python export.py --batch_size 256 --ckpt_file ./arcface_ascend_v160_ms1mv2_research_cv_IJBB97.67_IJBC98.36.ckpt --file_name onnx_256 --file_format ONNX --device_target GPU - python export.py --batch_size 92 --ckpt_file ./arcface_ascend_v160_ms1mv2_research_cv_IJBB97.67_IJBC98.36.ckpt --file_name onnx_92 --file_format ONNX --device_target GPU - cd ./scripts/ - bash run_eval_ijbc_onnx.sh ../dataset/IJB_release/IJBB ../onnx_256.onnx ../onnx_92.onnx IJBB - ``` - - 上述python命令将在后台运行,您可以通过eval_onnx_IJBB.log文件查看结果。测试数据集的准确性如下: - - ```bash - +-----------+-------+-------+--------+-------+-------+-------+ - | Methods | 1e-06 | 1e-05 | 0.0001 | 0.001 | 0.01 | 0.1 | - +-----------+-------+-------+--------+-------+-------+-------+ - | ijbb-IJBB | 46.81 | 89.89 | 94.81 | 96.54 | 97.68 | 98.70 | - +-----------+-------+-------+--------+-------+-------+-------+ - ``` - -- 在GPU环境运行ONNX评估IJB-C数据集 - - 在运行以下命令之前,请检查用于评估的检查点路径。请确保传入的评估数据集路径为“IJB_release/IJBC/”。 - - ```bash - bash run_eval_ijbc_onnx.sh /path/evalset path/onnx_bs path/onnx_rs IJBC - - # 使用onnx评估的示例: - python export.py --batch_size 256 --ckpt_file ./arcface_ascend_v160_ms1mv2_research_cv_IJBB97.67_IJBC98.36.ckpt --file_name onnx_256 --file_format ONNX --device_target GPU - python export.py --batch_size 254 --ckpt_file ./arcface_ascend_v160_ms1mv2_research_cv_IJBB97.67_IJBC98.36.ckpt --file_name onnx_254 --file_format ONNX --device_target GPU - cd ./scripts/ - bash run_eval_ijbc_onnx.sh ../dataset/IJB_release/IJBC ../onnx_256.onnx ../onnx_254.onnx IJBC - ``` - - 上述python命令将在后台运行,您可以通过eval_onnx_IJBC.log文件查看结果。测试数据集的准确性如下: - - ```bash - +-----------+-------+-------+--------+-------+-------+-------+ - | Methods | 1e-06 | 1e-05 | 0.0001 | 0.001 | 0.01 | 0.1 | - +-----------+-------+-------+--------+-------+-------+-------+ - | ijbc-IJBC | 88.76 | 94.39 | 96.28 | 97.52 | 98.36 | 99.12 | - +-----------+-------+-------+--------+-------+-------+-------+ - ``` - -## 导出mindir模型 - -```python -python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] -``` - -参数`ckpt_file` 是必需的,`FILE_FORMAT` 必须在 ["AIR", "MINDIR"]中进行选择。 - -# 推理过程 - -**推理前需参照 [MindSpore C++推理部署指南](https://gitee.com/mindspore/models/blob/master/utils/cpp_infer/README_CN.md) 进行环境变量设置。** - -## 用法 - -在执行推理之前,需要通过export.py导出mindir文件。 - -```bash -bash run_infer_cpp.sh [MINDIR_PATH] [DATASET_PATH] [NEED_PREPROCESS] [DEVICE_TYPE] [DEVICE_ID] -``` - -`DEVICE_TYPE` 可选值范围为:['GPU', 'CPU', 'Ascend'] -`NEED_PREPROCESS` 表示数据是否需要预处理,可选值范围为:'y' 或者 'n',这里直接选择‘y’ -`DEVICE_ID` 可选, 默认值为0。 - -### 结果 - -推理结果保存在当前路径,可在acc.log中看到最终精度结果。 - -# 模型描述 - -## 性能 - -### 训练性能 - -| 参数 | Arcface | GPU -| ------------- | ------------------------------------------------------------ | ------------------------------------------- | -| 模型版本 | arcface | arcface | -| 资源 | Ascend 910; CPU: 2.60GHz,192内核;内存,755G | GeForce RTX 3090; CPU: 2.90GHz,64内核;内存,755G | -| 上传日期 | 2021-05-30 | 2021-11-12 | -| MindSpore版本 | 1.2.0-c77-python3.7-aarch64 | 1.5.0 | -| 数据集 | MS1MV2 | MS1MV2 | -| 训练参数 | lr=0.08; gamma=0.1 | lr=0.08; gamma=0.1 | -| 优化器 | SGD | SGD | -| 损失函数 | Arcface | Arcface | -| 输出 | 概率 | 概率 | -| 损失 | 0.6 | 0.7 | -| 速度 | 1卡:108毫秒/步;8卡:97毫秒/步 | 8卡:990毫秒/步 | -| 总时间 | 1卡:65小时;8卡:8.5小时 | 8卡:75小时 | -| 参数(M) | 85.2 | 85.2 | -| 微调检查点 | 1249M (.ckpt file) | 1249M (.ckpt file) | -| 脚本 | [脚本路径](https://gitee.com/mindspore/models/tree/master/official/cv/Arcface) | [脚本路径](https://gitee.com/mindspore/models/tree/master/official/cv/Arcface) | - -### 评估性能 - -| 参数 | Ascend | GPU | -| ------------- | ------------------------ | ------------------------- | -| 模型版本 | arcface | arcface | -| 资源 | Ascend 910 | GeForce RTX 3090 | -| 上传日期 | 2021/05/30 | 2021/11/12 | -| MindSpore版本 | 1.2.0-c77-python3.7-aarch64 | 1.5.0 | -| 数据集 | IJBC、IJBB、lfw、cfp_fp、agedb_30、calfw、cplfw | IJBC、IJBB、lfw、cfp_fp、agedb_30、calfw、cplfw | -| 输出 | 概率 | 概率 | -| 准确性 | lfw:0.998 cfp_fp:0.98 agedb_30:0.981 calfw:0.961 cplfw:0.926 IJB-B:0.943 IJB-C:0.958 | lfw:0.998 cfp_fp:0.984 agedb_30:0.9803 calfw:0.9598 cplfw:0.928 IJB-B:0.943 IJB-C:0.958 | - -# 随机情况说明 - -网络的初始参数均为随即初始化。 - -# ModelZoo主页 - -请浏览官网[主页](https://gitee.com/mindspore/models)。 diff --git a/official/cv/Arcface/cpp_infer/CMakeLists.txt b/official/cv/Arcface/cpp_infer/CMakeLists.txt deleted file mode 100644 index b3a2f8e0735b0703ca8d28c65d00f7f56d56021f..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/cpp_infer/CMakeLists.txt +++ /dev/null @@ -1,28 +0,0 @@ -cmake_minimum_required(VERSION 3.14.1) -project(Ascend310Infer) -add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -std=c++17 -Werror -Wall -fPIE -Wl,--allow-shlib-undefined") -set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) -option(MINDSPORE_PATH "mindspore install path" "") -include_directories(${MINDSPORE_PATH}) -include_directories(${MINDSPORE_PATH}/include) -include_directories(${PROJECT_SRC_ROOT}) - -set(TOP_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../..) -include_directories(${TOP_DIR}/utils/cpp_infer/example/) # common_inc in top dir -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/) # common_inc in local dir -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../) # common_inc in local dir - -if(EXISTS ${MINDSPORE_PATH}/lib/libmindspore-lite.so) - message(--------------- Compile-with-MindSpore-Lite ----------------) - set(MS_LIB ${MINDSPORE_PATH}/lib/libmindspore-lite.so) - set(MD_LIB ${MINDSPORE_PATH}/lib/libminddata-lite.so) - add_compile_definitions(ENABLE_LITE) -else() - message(--------------- Compile-with-MindSpore ----------------) - set(MS_LIB ${MINDSPORE_PATH}/lib/libmindspore.so) - file(GLOB_RECURSE MD_LIB ${MINDSPORE_PATH}/_c_dataengine*) -endif() - -add_executable(main src/main.cc) -target_link_libraries(main ${MS_LIB} ${MD_LIB}) diff --git a/official/cv/Arcface/cpp_infer/build.sh b/official/cv/Arcface/cpp_infer/build.sh deleted file mode 100644 index f0f4581ea5b5bab677fe0b597309368e80d56251..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/cpp_infer/build.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/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. -# ============================================================================ -if [ -d out ]; then - rm -rf out -fi - -mkdir out -cd out || exit - -if [ -f "Makefile" ]; then - make clean -fi - -if [ $MS_LITE_HOME ];then - MINDSPORE_PATH=$MS_LITE_HOME/runtime -else - MINDSPORE_PATH="`pip show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`" - if [[ ! $MINDSPORE_PATH ]];then - MINDSPORE_PATH="`pip show mindspore | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`" - fi -fi -cmake .. -DMINDSPORE_PATH=$MINDSPORE_PATH -make diff --git a/official/cv/Arcface/cpp_infer/src/main.cc b/official/cv/Arcface/cpp_infer/src/main.cc deleted file mode 100644 index 28b09a52e909866888dc9157e5d4ce6dacbe1570..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/cpp_infer/src/main.cc +++ /dev/null @@ -1,111 +0,0 @@ -/** - * 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. - */ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "common_inc/infer.h" - -DEFINE_string(mindir_path, "", "mindir path"); -DEFINE_string(input0_path, ".", "input0 path"); -DEFINE_int32(device_id, 0, "device id"); -DEFINE_string(device_type, "CPU", "device type"); - -int main(int argc, char **argv) { - if (!ParseCommandLineFlags(argc, argv)) { - std::cout << "Failed to parse args" << std::endl; - return 1; - } - if (RealPath(FLAGS_mindir_path).empty()) { - std::cout << "Invalid mindir" << std::endl; - return 1; - } - - Model model; - if (!LoadModel(FLAGS_mindir_path, FLAGS_device_type, FLAGS_device_id, &model)) { - std::cout << "Failed to load model " << FLAGS_mindir_path << ", device id: " << FLAGS_device_id - << ", device type: " << FLAGS_device_type; - return 1; - } - Status ret; - - std::vector model_inputs = model.GetInputs(); - if (model_inputs.empty()) { - std::cout << "Invalid model, inputs is empty." << std::endl; - return 1; - } - - auto input0_files = GetAllFiles(FLAGS_input0_path); - - if (input0_files.empty()) { - std::cout << "ERROR: input data empty." << std::endl; - return 1; - } - - std::map costTime_map; - size_t size = input0_files.size(); - - for (size_t i = 0; i < size; ++i) { - struct timeval start = {0}; - struct timeval end = {0}; - double startTimeMs; - double endTimeMs; - std::vector inputs; - std::vector outputs; - std::cout << "Start predict input files:" << input0_files[i] << std::endl; - - auto input0 = ReadFileToTensor(input0_files[i]); - - inputs.emplace_back(model_inputs[0].Name(), model_inputs[0].DataType(), model_inputs[0].Shape(), - input0.Data().get(), input0.DataSize()); - - gettimeofday(&start, nullptr); - ret = model.Predict(inputs, &outputs); - gettimeofday(&end, nullptr); - if (ret != kSuccess) { - std::cout << "Predict " << input0_files[i] << " failed." << std::endl; - return 1; - } - startTimeMs = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000; - endTimeMs = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000; - costTime_map.insert(std::pair(startTimeMs, endTimeMs)); - WriteResult(input0_files[i], outputs); - } - double average = 0.0; - int inferCount = 0; - - for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) { - double diff = iter->second - iter->first; - average += diff; - inferCount++; - } - average = average / inferCount; - std::stringstream timeCost; - timeCost << "NN inference cost average time: "<< average << " ms of infer_count " << inferCount << std::endl; - std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << inferCount << std::endl; - std::string fileName = "./time_Result" + std::string("/test_perform_static.txt"); - std::ofstream fileStream(fileName.c_str(), std::ios::trunc); - fileStream << timeCost.str(); - fileStream.close(); - costTime_map.clear(); - return 0; -} diff --git a/official/cv/Arcface/eval_ijbc.py b/official/cv/Arcface/eval_ijbc.py deleted file mode 100644 index 493da95e9686fc6862782f49415d9c36c5a602bd..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/eval_ijbc.py +++ /dev/null @@ -1,525 +0,0 @@ -# coding: utf-8 -# 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. -# ============================================================================ -''' -evaluation of IJBB or IJBC -''' - -import os -import pickle -from pathlib import Path -import timeit -import argparse -import warnings -import sys - -import matplotlib -import matplotlib.pyplot as plt -import pandas as pd -import numpy as np - -import sklearn -from sklearn.metrics import roc_curve, auc - -from menpo.visualize.viewmatplotlib import sample_colours_from_colourmap -from prettytable import PrettyTable - -import cv2 - -from skimage import transform as trans - -import mindspore -from mindspore.train.serialization import load_checkpoint, load_param_into_net -from mindspore import Tensor -from mindspore import dtype as mstype -import mindspore.ops as ops -import mindspore.nn as nn -from src.iresnet import iresnet100 - -matplotlib.use('Agg') - -sys.path.insert(0, "../") -warnings.filterwarnings("ignore") - -parser = argparse.ArgumentParser(description='do ijb test') -# general -parser.add_argument('--model-prefix', default='', help='path to load model.') -parser.add_argument('--image-path', default='', type=str, help='') -parser.add_argument('--result-dir', default='.', type=str, help='') -parser.add_argument('--batch-size', default=128, type=int, help='') -parser.add_argument('--network', default='iresnet50', type=str, help='') -parser.add_argument('--job', default='insightface', type=str, help='job name') -parser.add_argument('--target', default='IJBC', type=str, - help='target, set to IJBC or IJBB') -args = parser.parse_args() - -target = args.target -model_path = args.model_prefix -image_path = args.image_path -result_dir = args.result_dir -gpu_id = None -use_norm_score = True # if Ture, TestMode(N1) -use_detector_score = True # if Ture, TestMode(D1) -use_flip_test = True # if Ture, TestMode(F1) -job = args.job - - -class Embedding(nn.Cell): - '''Embedding - ''' - def __init__(self, prefix, data_shape, batch_size=1): - super().__init__() - image_size = (112, 112) - self.image_size = image_size - resnet = iresnet100() - param_dict = load_checkpoint(args.model_prefix) - load_param_into_net(resnet, param_dict) - self.model = resnet - src = np.array([ - [30.2946, 51.6963], - [65.5318, 51.5014], - [48.0252, 71.7366], - [33.5493, 92.3655], - [62.7299, 92.2041]], dtype=np.float32) - src[:, 0] += 8.0 - self.src = src - self.batch_size = batch_size - self.data_shape = data_shape - self.reshape = ops.Reshape() - self.div = ops.Div() - self.sub = ops.Sub() - self.shape = ops.Shape() - self.print = ops.Print() - - def get(self, rimg, landmark): - '''get - ''' - assert landmark.shape[0] == 68 or landmark.shape[0] == 5 - assert landmark.shape[1] == 2 - if landmark.shape[0] == 68: - landmark5 = np.zeros((5, 2), dtype=np.float32) - landmark5[0] = (landmark[36] + landmark[39]) / 2 - landmark5[1] = (landmark[42] + landmark[45]) / 2 - landmark5[2] = landmark[30] - landmark5[3] = landmark[48] - landmark5[4] = landmark[54] - else: - landmark5 = landmark - tform = trans.SimilarityTransform() - tform.estimate(landmark5, self.src) - M = tform.params[0:2, :] - img = cv2.warpAffine(rimg, - M, (self.image_size[1], self.image_size[0]), - borderValue=0.0) - img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) - img_flip = np.fliplr(img) - img = np.transpose(img, (2, 0, 1)) # 3*112*112, RGB - img_flip = np.transpose(img_flip, (2, 0, 1)) - input_blob = np.zeros( - (2, 3, self.image_size[1], self.image_size[0]), dtype=np.uint8) - input_blob[0] = img - input_blob[1] = img_flip - return input_blob - - # @torch.no_grad() - def forward_db(self, batch_data): - '''forward_db - ''' - imgs = Tensor(batch_data, mstype.float32) - imgs = self.div(imgs, 255) - imgs = self.sub(imgs, 0.5) - imgs = self.div(imgs, 0.5) - feat = self.model(imgs) - shape = self.shape(feat) - feat = self.reshape(feat, (self.batch_size, 2 * shape[1])) - return feat.asnumpy() - - -# 将一个list尽量均分成n份,限制len(list)==n,份数大于原list内元素个数则分配空list[] -def divideIntoNstrand(listTemp, n): - twoList = [[] for i in range(n)] - for i, e in enumerate(listTemp): - twoList[i % n].append(e) - return twoList - - -def read_template_media_list(path): - ijb_meta = pd.read_csv(path, sep=' ', header=None).values - templates = ijb_meta[:, 1].astype(np.int_) - media = ijb_meta[:, 2].astype(np.int_) - return templates, media - - -# In[ ]: - - -def read_template_pair_list(path): - pairs = pd.read_csv(path, sep=' ', header=None).values - t1 = pairs[:, 0].astype(np.int_) - t2 = pairs[:, 1].astype(np.int_) - label = pairs[:, 2].astype(np.int_) - return t1, t2, label - - -# In[ ]: - - -def read_image_feature(path): - with open(path, 'rb') as fid: - img_feats = pickle.load(fid) - return img_feats - - -# In[ ]: - - -def get_image_feature(img_path, files_list, _model_path, epoch): - '''get_image_feature - ''' - batch_size = args.batch_size - data_shape = (3, 112, 112) - - files = files_list - print('files:', len(files)) - rare_size = len(files) % batch_size - faceness_scores = [] - batch = 0 - img_feats = np.empty((len(files), 1024), dtype=np.float32) - - batch_data = np.empty((2 * batch_size, 3, 112, 112)) - embedding = Embedding(_model_path, data_shape, batch_size) - for img_index, each_line in enumerate(files[:len(files) - rare_size]): - name_lmk_score = each_line.strip().split(' ') - img_name = os.path.join(img_path, name_lmk_score[0]) - img = cv2.imread(img_name) - lmk = np.array([float(x) for x in name_lmk_score[1:-1]], - dtype=np.float32) - lmk = lmk.reshape((5, 2)) - input_blob = embedding.get(img, lmk) - - batch_data[2 * (img_index - batch * batch_size)][:] = input_blob[0] - batch_data[2 * (img_index - batch * batch_size) + 1][:] = input_blob[1] - if (img_index + 1) % batch_size == 0: - print('batch', batch) - img_feats[batch * batch_size:batch * batch_size + - batch_size][:] = embedding.forward_db(batch_data) - batch += 1 - faceness_scores.append(name_lmk_score[-1]) - - batch_data = np.empty((2 * rare_size, 3, 112, 112)) - embedding = Embedding(_model_path, data_shape, rare_size) - for img_index, each_line in enumerate(files[len(files) - rare_size:]): - name_lmk_score = each_line.strip().split(' ') - img_name = os.path.join(img_path, name_lmk_score[0]) - img = cv2.imread(img_name) - lmk = np.array([float(x) for x in name_lmk_score[1:-1]], - dtype=np.float32) - lmk = lmk.reshape((5, 2)) - input_blob = embedding.get(img, lmk) - batch_data[2 * img_index][:] = input_blob[0] - batch_data[2 * img_index + 1][:] = input_blob[1] - if (img_index + 1) % rare_size == 0: - print('batch', batch) - img_feats[len(files) - - rare_size:][:] = embedding.forward_db(batch_data) - batch += 1 - faceness_scores.append(name_lmk_score[-1]) - faceness_scores = np.array(faceness_scores).astype(np.float32) - return img_feats, faceness_scores - - -# In[ ]: - - -def image2template_feature(img_feats=None, templates=None, media=None): - '''image2template_feature - ''' - # ========================================================== - # 1. image feature l2 normalization. img_feats:[number_image x feats_dim] - # 2. compute media feature. - # 3. compute template feature. - # ========================================================== - unique_templates = np.unique(templates) - template_feats = np.zeros((len(unique_templates), img_feats.shape[1])) - - for count_template, uqt in enumerate(unique_templates): - - (ind_t,) = np.where(templates == uqt) - face_norm_feats = img_feats[ind_t] - face_media = media[ind_t] - unique_media, unique_media_counts = np.unique(face_media, - return_counts=True) - media_norm_feats = [] - for u, ct in zip(unique_media, unique_media_counts): - (ind_m,) = np.where(face_media == u) - if ct == 1: - media_norm_feats += [face_norm_feats[ind_m]] - else: # image features from the same video will be aggregated into one feature - media_norm_feats += [ - np.mean(face_norm_feats[ind_m], axis=0, keepdims=True) - ] - media_norm_feats = np.array(media_norm_feats) - template_feats[count_template] = np.sum(media_norm_feats, axis=0) - if count_template % 2000 == 0: - print('Finish Calculating {} template features.'.format( - count_template)) - template_norm_feats = sklearn.preprocessing.normalize(template_feats) - return template_norm_feats, unique_templates - - -# In[ ]: - - -def verification(template_norm_feats=None, - unique_templates=None, - p1=None, - p2=None): - '''verification - ''' - # ========================================================== - # Compute set-to-set Similarity Score. - # ========================================================== - template2id = np.zeros((max(unique_templates) + 1, 1), dtype=int) - for count_template, uqt in enumerate(unique_templates): - template2id[uqt] = count_template - - score = np.zeros((len(p1),)) # save cosine distance between pairs - - total_pairs = np.array(range(len(p1))) - # small batchsize instead of all pairs in one batch due to the memory limiation - batchsize = 100000 - sublists = [ - total_pairs[i:i + batchsize] for i in range(0, len(p1), batchsize) - ] - total_sublists = len(sublists) - for c, s in enumerate(sublists): - feat1 = template_norm_feats[template2id[p1[s]]] - feat2 = template_norm_feats[template2id[p2[s]]] - similarity_score = np.sum(feat1 * feat2, -1) - score[s] = similarity_score.flatten() - if c % 10 == 0: - print('Finish {}/{} pairs.'.format(c, total_sublists)) - return score - - -# In[ ]: -def verification2(template_norm_feats=None, - unique_templates=None, - p1=None, - p2=None): - '''verification2 - ''' - template2id = np.zeros((max(unique_templates) + 1, 1), dtype=int) - for count_template, uqt in enumerate(unique_templates): - template2id[uqt] = count_template - score = np.zeros((len(p1),)) # save cosine distance between pairs - total_pairs = np.array(range(len(p1))) - # small batchsize instead of all pairs in one batch due to the memory limiation - batchsize = 100000 - sublists = [ - total_pairs[i:i + batchsize] for i in range(0, len(p1), batchsize) - ] - total_sublists = len(sublists) - for c, s in enumerate(sublists): - feat1 = template_norm_feats[template2id[p1[s]]] - feat2 = template_norm_feats[template2id[p2[s]]] - similarity_score = np.sum(feat1 * feat2, -1) - score[s] = similarity_score.flatten() - if c % 10 == 0: - print('Finish {}/{} pairs.'.format(c, total_sublists)) - return score - - -def read_score(path): - with open(path, 'rb') as fid: - img_feats = pickle.load(fid) - return img_feats - - -def main(): - mindspore.set_context(mode=0, device_id=0) - # # Step1: Load Meta Data - - # In[ ]: - - assert target in ('IJBC', 'IJBB') - - # ============================================================= - # load image and template relationships for template feature embedding - # tid --> template id, mid --> media id - # format: - # image_name tid mid - # ============================================================= - start = timeit.default_timer() - templates, media = read_template_media_list( - os.path.join('%s/meta' % image_path, - '%s_face_tid_mid.txt' % target.lower())) - stop = timeit.default_timer() - print('Time: %.2f s. ' % (stop - start)) - - # In[ ]: - - # ============================================================= - # load template pairs for template-to-template verification - # tid : template id, label : 1/0 - # format: - # tid_1 tid_2 label - # ============================================================= - start = timeit.default_timer() - p1, p2, label = read_template_pair_list( - os.path.join('%s/meta' % image_path, - '%s_template_pair_label.txt' % target.lower())) - stop = timeit.default_timer() - print('Time: %.2f s. ' % (stop - start)) - - # # Step 2: Get Image Features - - # In[ ]: - - # ============================================================= - # load image features - # format: - # img_feats: [image_num x feats_dim] (227630, 512) - # ============================================================= - start = timeit.default_timer() - img_path = '%s/loose_crop' % image_path - img_list_path = '%s/meta/%s_name_5pts_score.txt' % (image_path, target.lower()) - img_list = open(img_list_path) - files = img_list.readlines() - files_list = files - - # img_feats - img_feats, faceness_scores = get_image_feature(img_path, files_list, - model_path, 0) - stop = timeit.default_timer() - print('Time: %.2f s. ' % (stop - start)) - print('Feature Shape: ({} , {}) .'.format(img_feats.shape[0], - img_feats.shape[1])) - - # # Step3: Get Template Features - - # In[ ]: - - # ============================================================= - # compute template features from image features. - # ============================================================= - start = timeit.default_timer() - # ========================================================== - # Norm feature before aggregation into template feature? - # Feature norm from embedding network and faceness score are able to decrease weights for noise samples (not face). - # ========================================================== - # 1. FaceScore (Feature Norm) - # 2. FaceScore (Detector) - - if use_flip_test: - # concat --- F1 - # img_input_feats = img_feats - # add --- F2 - img_input_feats = img_feats[:, 0:img_feats.shape[1] // - 2] + img_feats[:, img_feats.shape[1] // 2:] - else: - img_input_feats = img_feats[:, 0:img_feats.shape[1] // 2] - - if use_norm_score: - img_input_feats = img_input_feats - else: - # normalise features to remove norm information - img_input_feats = img_input_feats / np.sqrt( - np.sum(img_input_feats ** 2, -1, keepdims=True)) - - if use_detector_score: - print(img_input_feats.shape, faceness_scores.shape) - img_input_feats = img_input_feats * faceness_scores[:, np.newaxis] - else: - img_input_feats = img_input_feats - - template_norm_feats, unique_templates = image2template_feature( - img_input_feats, templates, media) - stop = timeit.default_timer() - print('Time: %.2f s. ' % (stop - start)) - - # # Step 4: Get Template Similarity Scores - - # In[ ]: - - # ============================================================= - # compute verification scores between template pairs. - # ============================================================= - start = timeit.default_timer() - score = verification(template_norm_feats, unique_templates, p1, p2) - stop = timeit.default_timer() - print('Time: %.2f s. ' % (stop - start)) - - # In[ ]: - save_path = os.path.join(result_dir, args.job) - - if not os.path.exists(save_path): - os.makedirs(save_path) - - score_save_file = os.path.join(save_path, "%s.npy" % target.lower()) - np.save(score_save_file, score) - - # # Step 5: Get ROC Curves and TPR@FPR Table - - # In[ ]: - - files = [score_save_file] - methods = [] - scores = [] - for file in files: - methods.append(Path(file).stem) - scores.append(np.load(file)) - - methods = np.array(methods) - scores = dict(zip(methods, scores)) - colours = dict( - zip(methods, sample_colours_from_colourmap(methods.shape[0], 'Set2'))) - x_labels = [10 ** -6, 10 ** -5, 10 ** -4, 10 ** -3, 10 ** -2, 10 ** -1] - tpr_fpr_table = PrettyTable(['Methods'] + [str(x) for x in x_labels]) - fig = plt.figure() - for method in methods: - fpr, tpr, _ = roc_curve(label, scores[method]) - roc_auc = auc(fpr, tpr) - fpr = np.flipud(fpr) - tpr = np.flipud(tpr) # select largest tpr at same fpr - plt.plot(fpr, - tpr, - color=colours[method], - lw=1, - label=('[%s (AUC = %0.4f %%)]' % - (method.split('-')[-1], roc_auc * 100))) - tpr_fpr_row = [] - tpr_fpr_row.append("%s-%s" % (method, target)) - for fpr_iter in np.arange(len(x_labels)): - _, min_index = min( - list(zip(abs(fpr - x_labels[fpr_iter]), range(len(fpr))))) - tpr_fpr_row.append('%.2f' % (tpr[min_index] * 100)) - tpr_fpr_table.add_row(tpr_fpr_row) - plt.xlim([10 ** -6, 0.1]) - plt.ylim([0.3, 1.0]) - plt.grid(linestyle='--', linewidth=1) - plt.xticks(x_labels) - plt.yticks(np.linspace(0.3, 1.0, 8, endpoint=True)) - plt.xscale('log') - plt.xlabel('False Positive Rate') - plt.ylabel('True Positive Rate') - plt.title('ROC on IJB') - plt.legend(loc="lower right") - fig.savefig(os.path.join(save_path, '%s.pdf' % target.lower())) - print(tpr_fpr_table) - - -if __name__ == '__main__': - main() diff --git a/official/cv/Arcface/eval_ijbc_onnx.py b/official/cv/Arcface/eval_ijbc_onnx.py deleted file mode 100644 index b6ba4fbb18126cb95f70080ada08596bd01d6625..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/eval_ijbc_onnx.py +++ /dev/null @@ -1,503 +0,0 @@ -# coding: utf-8 -# Copyright 2022 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. -# ============================================================================ -''' -evaluation of IJBB or IJBC -''' - -import os -import pickle -from pathlib import Path -import timeit -import argparse -import warnings -import sys - -import matplotlib -import matplotlib.pyplot as plt -import pandas as pd -import numpy as np -import onnxruntime as ort - -import sklearn -from sklearn.metrics import roc_curve, auc - -from menpo.visualize.viewmatplotlib import sample_colours_from_colourmap -from prettytable import PrettyTable - -import cv2 - -from skimage import transform as trans - -matplotlib.use('Agg') - -sys.path.insert(0, "../") -warnings.filterwarnings("ignore") - -parser = argparse.ArgumentParser(description='do ijb test') -# general -parser.add_argument('--onnx_bs_dir', default='/path/onnx_bs.onnx', help='path to load onnx.') -parser.add_argument('--onnx_rs_dir', default='/path/onnx_rs.onnx', help='path to load onnx.') -parser.add_argument('--image-path', default='./dataset/IJB_release/IJBC', type=str, help='') -parser.add_argument('--result-dir', default='result', type=str, help='') -parser.add_argument('--batch-size', default=128, type=int, help='batch size') -parser.add_argument('--job', default='onnx_result_ijbc', type=str, help='job name') -parser.add_argument('--device_target', default='GPU', type=str, help='device target') -parser.add_argument('--target', default='IJBC', type=str, help='target, set to IJBC or IJBB') -args = parser.parse_args() - -target = args.target -device_target = args.device_target -onnx_bs_dir = args.onnx_bs_dir -onnx_rs_dir = args.onnx_rs_dir -image_path = args.image_path -result_dir = args.result_dir -use_norm_score = True # if Ture, TestMode(N1) -use_detector_score = True # if Ture, TestMode(D1) -use_flip_test = True # if Ture, TestMode(F1) -job = args.job - -class Embedding(): - ''' - Embedding - ''' - def __init__(self, session, input_name, data_shape, batch_size=1): - super().__init__() - image_size = (112, 112) - self.image_size = image_size - - src = np.array([ - [30.2946, 51.6963], - [65.5318, 51.5014], - [48.0252, 71.7366], - [33.5493, 92.3655], - [62.7299, 92.2041]], dtype=np.float32) - src[:, 0] += 8.0 - self.src = src - self.batch_size = batch_size - self.data_shape = data_shape - self.session = session - self.input_name = input_name - - def get(self, rimg, landmark): - ''' - get - ''' - assert landmark.shape[0] == 68 or landmark.shape[0] == 5 - assert landmark.shape[1] == 2 - if landmark.shape[0] == 68: - landmark5 = np.zeros((5, 2), dtype=np.float32) - landmark5[0] = (landmark[36] + landmark[39]) / 2 - landmark5[1] = (landmark[42] + landmark[45]) / 2 - landmark5[2] = landmark[30] - landmark5[3] = landmark[48] - landmark5[4] = landmark[54] - else: - landmark5 = landmark - tform = trans.SimilarityTransform() - tform.estimate(landmark5, self.src) - M = tform.params[0:2, :] - img = cv2.warpAffine(rimg, - M, (self.image_size[1], self.image_size[0]), - borderValue=0.0) - img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) - img_flip = np.fliplr(img) - img = np.transpose(img, (2, 0, 1)) # 3*112*112, RGB - img_flip = np.transpose(img_flip, (2, 0, 1)) - input_blob = np.zeros((2, 3, self.image_size[1], self.image_size[0]), dtype=np.uint8) - input_blob[0] = img - input_blob[1] = img_flip - - return input_blob - - # @torch.no_grad() - def forward_db(self, batch_data): - ''' - forward_db - ''' - imgs = np.divide(batch_data, 255) - imgs = np.subtract(imgs, 0.5) - imgs = np.divide(imgs, 0.5) - - feat = self.session.run(None, {self.input_name: imgs})[0] - - shape = feat.shape - feat = feat.reshape((self.batch_size, 2 * shape[1])) - - return feat - -# Divide a list into n parts as much as possible, limit len(list)==n. -def divideIntoNstrand(listTemp, n): - twoList = [[] for i in range(n)] - for i, e in enumerate(listTemp): - twoList[i % n].append(e) - - return twoList - -def read_template_media_list(path): - ijb_meta = pd.read_csv(path, sep=' ', header=None).values - templates = ijb_meta[:, 1].astype(np.int_) - media = ijb_meta[:, 2].astype(np.int_) - - return templates, media - -def read_template_pair_list(path): - pairs = pd.read_csv(path, sep=' ', header=None).values - t1 = pairs[:, 0].astype(np.int_) - t2 = pairs[:, 1].astype(np.int_) - label = pairs[:, 2].astype(np.int_) - - return t1, t2, label - -def read_image_feature(path): - with open(path, 'rb') as fid: - img_feats = pickle.load(fid) - - return img_feats - -def get_image_feature(img_path, files_list, epoch, _onnx_bs_dir, _onnx_rs_dir): - ''' - get_image_feature - ''' - session_bs, input_name_bs = create_session(_onnx_bs_dir, device_target) - session_rs, input_name_rs = create_session(_onnx_rs_dir, device_target) - - batch_size = args.batch_size - data_shape = (3, 112, 112) - - files = files_list - print('files:', len(files)) - rare_size = len(files) % batch_size - faceness_scores = [] - batch = 0 - img_feats = np.empty((len(files), 1024), dtype=np.float32) - - batch_data = np.empty((2 * batch_size, 3, 112, 112)) - embedding = Embedding(session_bs, input_name_bs, data_shape, batch_size) - for img_index, each_line in enumerate(files[:len(files) - rare_size]): - name_lmk_score = each_line.strip().split(' ') - img_name = os.path.join(img_path, name_lmk_score[0]) - img = cv2.imread(img_name) - lmk = np.array([float(x) for x in name_lmk_score[1:-1]], dtype=np.float32) - lmk = lmk.reshape((5, 2)) - input_blob = embedding.get(img, lmk) - - batch_data[2 * (img_index - batch * batch_size)][:] = input_blob[0] - batch_data[2 * (img_index - batch * batch_size) + 1][:] = input_blob[1] - batch_data = np.array(batch_data, np.float32) - if (img_index + 1) % batch_size == 0: - print('batch', batch) - img_feats[batch * batch_size:batch * batch_size + batch_size][:] = embedding.forward_db(batch_data) - batch += 1 - faceness_scores.append(name_lmk_score[-1]) - - batch_data = np.empty((2 * rare_size, 3, 112, 112)) - embedding = Embedding(session_rs, input_name_rs, data_shape, rare_size) - print(len(files) - rare_size) - for img_index, each_line in enumerate(files[len(files) - rare_size:]): - name_lmk_score = each_line.strip().split(' ') - img_name = os.path.join(img_path, name_lmk_score[0]) - img = cv2.imread(img_name) - lmk = np.array([float(x) for x in name_lmk_score[1:-1]], dtype=np.float32) - lmk = lmk.reshape((5, 2)) - input_blob = embedding.get(img, lmk) - batch_data[2 * img_index][:] = input_blob[0] - batch_data[2 * img_index + 1][:] = input_blob[1] - batch_data = np.array(batch_data, np.float32) - if (img_index + 1) % rare_size == 0: - print('batch', batch) - img_feats[len(files) - rare_size:][:] = embedding.forward_db(batch_data) - batch += 1 - faceness_scores.append(name_lmk_score[-1]) - faceness_scores = np.array(faceness_scores).astype(np.float32) - - return img_feats, faceness_scores - -def image2template_feature(img_feats=None, templates=None, media=None): - ''' - image2template_feature - ''' - # ========================================================== - # 1. image feature l2 normalization. img_feats:[number_image x feats_dim] - # 2. compute media feature. - # 3. compute template feature. - # ========================================================== - unique_templates = np.unique(templates) - template_feats = np.zeros((len(unique_templates), img_feats.shape[1])) - - for count_template, uqt in enumerate(unique_templates): - (ind_t,) = np.where(templates == uqt) - face_norm_feats = img_feats[ind_t] - face_media = media[ind_t] - unique_media, unique_media_counts = np.unique(face_media, return_counts=True) - media_norm_feats = [] - for u, ct in zip(unique_media, unique_media_counts): - (ind_m,) = np.where(face_media == u) - if ct == 1: - media_norm_feats += [face_norm_feats[ind_m]] - else: # image features from the same video will be aggregated into one feature - media_norm_feats += [ - np.mean(face_norm_feats[ind_m], axis=0, keepdims=True) - ] - media_norm_feats = np.array(media_norm_feats) - template_feats[count_template] = np.sum(media_norm_feats, axis=0) - if count_template % 2000 == 0: - print('Finish Calculating {} template features.'.format(count_template)) - template_norm_feats = sklearn.preprocessing.normalize(template_feats) - - return template_norm_feats, unique_templates - -def verification(template_norm_feats=None, - unique_templates=None, - p1=None, - p2=None): - ''' - verification - ''' - # ========================================================== - # Compute set-to-set Similarity Score. - # ========================================================== - template2id = np.zeros((max(unique_templates) + 1, 1), dtype=int) - for count_template, uqt in enumerate(unique_templates): - template2id[uqt] = count_template - - score = np.zeros((len(p1),)) # save cosine distance between pairs - - total_pairs = np.array(range(len(p1))) - # small batchsize instead of all pairs in one batch due to the memory limiation - batchsize = 100000 - sublists = [ - total_pairs[i:i + batchsize] for i in range(0, len(p1), batchsize) - ] - total_sublists = len(sublists) - for c, s in enumerate(sublists): - feat1 = template_norm_feats[template2id[p1[s]]] - feat2 = template_norm_feats[template2id[p2[s]]] - similarity_score = np.sum(feat1 * feat2, -1) - score[s] = similarity_score.flatten() - if c % 10 == 0: - print('Finish {}/{} pairs.'.format(c, total_sublists)) - - return score - -def verification2(template_norm_feats=None, - unique_templates=None, - p1=None, - p2=None): - ''' - verification2 - ''' - template2id = np.zeros((max(unique_templates) + 1, 1), dtype=int) - for count_template, uqt in enumerate(unique_templates): - template2id[uqt] = count_template - score = np.zeros((len(p1),)) # save cosine distance between pairs - total_pairs = np.array(range(len(p1))) - # small batchsize instead of all pairs in one batch due to the memory limiation - batchsize = 100000 - sublists = [ - total_pairs[i:i + batchsize] for i in range(0, len(p1), batchsize) - ] - total_sublists = len(sublists) - for c, s in enumerate(sublists): - feat1 = template_norm_feats[template2id[p1[s]]] - feat2 = template_norm_feats[template2id[p2[s]]] - similarity_score = np.sum(feat1 * feat2, -1) - score[s] = similarity_score.flatten() - if c % 10 == 0: - print('Finish {}/{} pairs.'.format(c, total_sublists)) - - return score - -def read_score(path): - with open(path, 'rb') as fid: - img_feats = pickle.load(fid) - - return img_feats - -def create_session(checkpoint_path, target_device): - ''' - create onnx session - ''' - if target_device == 'GPU': - providers = ['CUDAExecutionProvider'] - elif target_device == 'CPU': - providers = ['CPUExecutionProvider'] - else: - raise ValueError(f'Unsupported target device {target_device!r}. Expected one of: "CPU", "GPU"') - session = ort.InferenceSession(checkpoint_path, providers=providers) - input_name = session.get_inputs()[0].name - - return session, input_name - -def main(): - assert target in ('IJBC', 'IJBB') - - # ============================================================= - # load image and template relationships for template feature embedding - # tid --> template id, mid --> media id - # format: - # image_name tid mid - # ============================================================= - start = timeit.default_timer() - templates, media = read_template_media_list( - os.path.join('%s/meta' % image_path, - '%s_face_tid_mid.txt' % target.lower())) - stop = timeit.default_timer() - print('Time: %.2f s. ' % (stop - start)) - - # ============================================================= - # load template pairs for template-to-template verification - # tid : template id, label : 1/0 - # format: - # tid_1 tid_2 label - # ============================================================= - start = timeit.default_timer() - p1, p2, label = read_template_pair_list( - os.path.join('%s/meta' % image_path, - '%s_template_pair_label.txt' % target.lower())) - stop = timeit.default_timer() - print('Time: %.2f s. ' % (stop - start)) - - # # Step 2: Get Image Features - # ============================================================= - # load image features - # format: - # img_feats: [image_num x feats_dim] (227630, 512) - # ============================================================= - start = timeit.default_timer() - img_path = '%s/loose_crop' % image_path - img_list_path = '%s/meta/%s_name_5pts_score.txt' % (image_path, target.lower()) - img_list = open(img_list_path) - files = img_list.readlines() - files_list = files - - # img_feats - img_feats, faceness_scores = get_image_feature(img_path, files_list, 0, onnx_bs_dir, onnx_rs_dir) - stop = timeit.default_timer() - print('Time: %.2f s. ' % (stop - start)) - print('Feature Shape: ({} , {}) .'.format(img_feats.shape[0], - img_feats.shape[1])) - - # # Step3: Get Template Features - # ============================================================= - # compute template features from image features. - # ============================================================= - start = timeit.default_timer() - # ========================================================== - # Norm feature before aggregation into template feature? - # Feature norm from embedding network and faceness score are able to decrease weights for noise samples (not face). - # ========================================================== - # 1. FaceScore (Feature Norm) - # 2. FaceScore (Detector) - - if use_flip_test: - # concat --- F1 - # img_input_feats = img_feats - # add --- F2 - img_input_feats = img_feats[:, 0:img_feats.shape[1] // - 2] + img_feats[:, img_feats.shape[1] // 2:] - else: - img_input_feats = img_feats[:, 0:img_feats.shape[1] // 2] - - if use_norm_score: - img_input_feats = img_input_feats - else: - # normalise features to remove norm information - img_input_feats = img_input_feats / np.sqrt( - np.sum(img_input_feats ** 2, -1, keepdims=True)) - - if use_detector_score: - print(img_input_feats.shape, faceness_scores.shape) - img_input_feats = img_input_feats * faceness_scores[:, np.newaxis] - else: - img_input_feats = img_input_feats - - template_norm_feats, unique_templates = image2template_feature( - img_input_feats, templates, media) - stop = timeit.default_timer() - print('Time: %.2f s. ' % (stop - start)) - - # # Step 4: Get Template Similarity Scores - - # In[ ]: - - # ============================================================= - # compute verification scores between template pairs. - # ============================================================= - start = timeit.default_timer() - score = verification(template_norm_feats, unique_templates, p1, p2) - stop = timeit.default_timer() - print('Time: %.2f s. ' % (stop - start)) - - # In[ ]: - save_path = os.path.join(result_dir, args.job) - - if not os.path.exists(save_path): - os.makedirs(save_path) - - score_save_file = os.path.join(save_path, "%s.npy" % target.lower()) - np.save(score_save_file, score) - - # # Step 5: Get ROC Curves and TPR@FPR Table - - # In[ ]: - - files = [score_save_file] - methods = [] - scores = [] - for file in files: - methods.append(Path(file).stem) - scores.append(np.load(file)) - - methods = np.array(methods) - scores = dict(zip(methods, scores)) - colours = dict( - zip(methods, sample_colours_from_colourmap(methods.shape[0], 'Set2'))) - x_labels = [10 ** -6, 10 ** -5, 10 ** -4, 10 ** -3, 10 ** -2, 10 ** -1] - tpr_fpr_table = PrettyTable(['Methods'] + [str(x) for x in x_labels]) - fig = plt.figure() - for method in methods: - fpr, tpr, _ = roc_curve(label, scores[method]) - roc_auc = auc(fpr, tpr) - fpr = np.flipud(fpr) - tpr = np.flipud(tpr) # select largest tpr at same fpr - plt.plot(fpr, - tpr, - color=colours[method], - lw=1, - label=('[%s (AUC = %0.4f %%)]' % - (method.split('-')[-1], roc_auc * 100))) - tpr_fpr_row = [] - tpr_fpr_row.append("%s-%s" % (method, target)) - for fpr_iter in np.arange(len(x_labels)): - _, min_index = min( - list(zip(abs(fpr - x_labels[fpr_iter]), range(len(fpr))))) - tpr_fpr_row.append('%.2f' % (tpr[min_index] * 100)) - tpr_fpr_table.add_row(tpr_fpr_row) - plt.xlim([10 ** -6, 0.1]) - plt.ylim([0.3, 1.0]) - plt.grid(linestyle='--', linewidth=1) - plt.xticks(x_labels) - plt.yticks(np.linspace(0.3, 1.0, 8, endpoint=True)) - plt.xscale('log') - plt.xlabel('False Positive Rate') - plt.ylabel('True Positive Rate') - plt.title('ROC on IJB') - plt.legend(loc="lower right") - fig.savefig(os.path.join(save_path, '%s.pdf' % target.lower())) - print(tpr_fpr_table) - -if __name__ == '__main__': - main() diff --git a/official/cv/Arcface/eval_onnx.py b/official/cv/Arcface/eval_onnx.py deleted file mode 100644 index c54f72a4e4e68d344998a993569b2172881db215..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/eval_onnx.py +++ /dev/null @@ -1,343 +0,0 @@ -# Copyright 2022 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. -# ============================================================================ -''' -evaluation of lfw, calfw, cfp_fp, agedb_30, cplfw -''' - -import datetime -import os -import pickle -import argparse -from io import BytesIO - -import numpy as np -import sklearn -from sklearn.decomposition import PCA -from sklearn.model_selection import KFold -import matplotlib.pyplot as plt -from scipy import interpolate -import onnxruntime as ort - -class LFold: - ''' - LFold - ''' - def __init__(self, n_splits=2, shuffle=False): - self.n_splits = n_splits - if self.n_splits > 1: - self.k_fold = KFold(n_splits=n_splits, shuffle=shuffle) - - def split(self, indices): - if self.n_splits > 1: - return self.k_fold.split(indices) - - return [(indices, indices)] - -def calculate_roc(thresholds, - embeddings1, - embeddings2, - actual_issame, - nrof_folds=10, - pca=0): - ''' - calculate_roc - ''' - assert embeddings1.shape[0] == embeddings2.shape[0] - assert embeddings1.shape[1] == embeddings2.shape[1] - nrof_pairs = min(len(actual_issame), embeddings1.shape[0]) - nrof_thresholds = len(thresholds) - k_fold = LFold(n_splits=nrof_folds, shuffle=False) - - tprs = np.zeros((nrof_folds, nrof_thresholds)) - fprs = np.zeros((nrof_folds, nrof_thresholds)) - accuracy = np.zeros((nrof_folds)) - indices = np.arange(nrof_pairs) - - if pca == 0: - diff = np.subtract(embeddings1, embeddings2) - dist = np.sum(np.square(diff), 1) - - for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)): - if pca > 0: - print('doing pca on', fold_idx) - embed1_train = embeddings1[train_set] - embed2_train = embeddings2[train_set] - _embed_train = np.concatenate((embed1_train, embed2_train), axis=0) - pca_model = PCA(n_components=pca) - pca_model.fit(_embed_train) - embed1 = pca_model.transform(embeddings1) - embed2 = pca_model.transform(embeddings2) - embed1 = sklearn.preprocessing.normalize(embed1) - embed2 = sklearn.preprocessing.normalize(embed2) - diff = np.subtract(embed1, embed2) - dist = np.sum(np.square(diff), 1) - - # Find the best threshold for the fold - acc_train = np.zeros((nrof_thresholds)) - for threshold_idx, threshold in enumerate(thresholds): - _, _, acc_train[threshold_idx] = calculate_accuracy( - threshold, dist[train_set], actual_issame[train_set]) - best_threshold_index = np.argmax(acc_train) - for threshold_idx, threshold in enumerate(thresholds): - tprs[fold_idx, threshold_idx], fprs[fold_idx, threshold_idx], _ = calculate_accuracy( - threshold, dist[test_set], - actual_issame[test_set]) - _, _, accuracy[fold_idx] = calculate_accuracy( - thresholds[best_threshold_index], dist[test_set], - actual_issame[test_set]) - - tpr = np.mean(tprs, 0) - fpr = np.mean(fprs, 0) - - return tpr, fpr, accuracy - -def calculate_accuracy(threshold, dist, actual_issame): - ''' - calculate_acc - ''' - predict_issame = np.less(dist, threshold) - tp = np.sum(np.logical_and(predict_issame, actual_issame)) - fp = np.sum(np.logical_and(predict_issame, np.logical_not(actual_issame))) - tn = np.sum( - np.logical_and(np.logical_not(predict_issame), - np.logical_not(actual_issame))) - fn = np.sum(np.logical_and(np.logical_not(predict_issame), actual_issame)) - - tpr = 0 if (tp + fn == 0) else float(tp) / float(tp + fn) - fpr = 0 if (fp + tn == 0) else float(fp) / float(fp + tn) - acc = float(tp + tn) / dist.size - - return tpr, fpr, acc - -def calculate_val(thresholds, - embeddings1, - embeddings2, - actual_issame, - far_target, - nrof_folds=10): - ''' - calculate_val - ''' - assert embeddings1.shape[0] == embeddings2.shape[0] - assert embeddings1.shape[1] == embeddings2.shape[1] - nrof_pairs = min(len(actual_issame), embeddings1.shape[0]) - nrof_thresholds = len(thresholds) - k_fold = LFold(n_splits=nrof_folds, shuffle=False) - - val = np.zeros(nrof_folds) - far = np.zeros(nrof_folds) - - diff = np.subtract(embeddings1, embeddings2) - dist = np.sum(np.square(diff), 1) - indices = np.arange(nrof_pairs) - - for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)): - - # Find the threshold that gives FAR = far_target - far_train = np.zeros(nrof_thresholds) - for threshold_idx, threshold in enumerate(thresholds): - _, far_train[threshold_idx] = calculate_val_far( - threshold, dist[train_set], actual_issame[train_set]) - if np.max(far_train) >= far_target: - f = interpolate.interp1d(far_train, thresholds, kind='slinear') - threshold = f(far_target) - else: - threshold = 0.0 - - val[fold_idx], far[fold_idx] = calculate_val_far( - threshold, dist[test_set], actual_issame[test_set]) - - val_mean = np.mean(val) - far_mean = np.mean(far) - val_std = np.std(val) - - return val_mean, val_std, far_mean - -def calculate_val_far(threshold, dist, actual_issame): - ''' - calculate_val_far - ''' - predict_issame = np.less(dist, threshold) - true_accept = np.sum(np.logical_and(predict_issame, actual_issame)) - false_accept = np.sum( - np.logical_and(predict_issame, np.logical_not(actual_issame))) - n_same = np.sum(actual_issame) - n_diff = np.sum(np.logical_not(actual_issame)) - val = float(true_accept) / float(n_same) - far = float(false_accept) / float(n_diff) - - return val, far - -def evaluate(embeddings, actual_issame, nrof_folds=10, pca=0): - ''' - evaluate - ''' - # Calculate evaluation metrics - thresholds = np.arange(0, 4, 0.01) - embeddings1 = embeddings[0::2] - embeddings2 = embeddings[1::2] - tpr, fpr, accuracy = calculate_roc(thresholds, - embeddings1, - embeddings2, - np.asarray(actual_issame), - nrof_folds=nrof_folds, - pca=pca) - thresholds = np.arange(0, 4, 0.001) - val, val_std, far = calculate_val(thresholds, - embeddings1, - embeddings2, - np.asarray(actual_issame), - 1e-3, - nrof_folds=nrof_folds) - - return tpr, fpr, accuracy, val, val_std, far - -def load_bin(path, image_size): - ''' - load evalset of .bin - ''' - try: - with open(path, 'rb') as f: - bins, issame_list = pickle.load(f) # py2 - except UnicodeDecodeError as _: - with open(path, 'rb') as f: - bins, issame_list = pickle.load(f, encoding='bytes') # py3 - data_list = [] - for _ in [0, 1]: - data = np.zeros( - (len(issame_list) * 2, 3, image_size[0], image_size[1])) - data_list.append(data) - for idx in range(len(issame_list) * 2): - _bin = bins[idx] - img = plt.imread(BytesIO(_bin), "jpg") - if img.shape[1] != image_size[0]: - img = mx.image.resize_short(img, image_size[0]) - img = np.transpose(img, axes=(2, 0, 1)) - for flip in [0, 1]: - if flip == 1: - img = np.flip(img, axis=2) - data_list[flip][idx][:] = img - return data_list, issame_list - - -def test(data_set, session, input_name, batch_size, nfolds=10): - ''' - test - ''' - print('testing verification..') - data_list = data_set[0] - issame_list = data_set[1] - embeddings_list = [] - time_consumed = 0.0 - for data in data_list: - embeddings = None - ba = 0 - while ba < data.shape[0]: - bb = min(ba + batch_size, data.shape[0]) - count = bb - ba - _data = data[bb - batch_size: bb] - - time0 = datetime.datetime.now() - img = ((_data / 255) - 0.5) / 0.5 - net_out = session.run(None, {input_name: np.array(img, np.float32)})[0] - _embeddings = net_out - time_now = datetime.datetime.now() - diff = time_now - time0 - time_consumed += diff.total_seconds() - if embeddings is None: - embeddings = np.zeros((data.shape[0], _embeddings.shape[1])) - embeddings[ba:bb, :] = _embeddings[(batch_size - count):, :] - ba = bb - embeddings_list.append(embeddings) - _xnorm = 0.0 - _xnorm_cnt = 0 - for embed in embeddings_list: - for i in range(embed.shape[0]): - _em = embed[i] - _norm = np.linalg.norm(_em) - _xnorm += _norm - _xnorm_cnt += 1 - _xnorm /= _xnorm_cnt - - embeddings = embeddings_list[0].copy() - embeddings = sklearn.preprocessing.normalize(embeddings) - _, _, acc, _, _, _ = evaluate(embeddings, issame_list, nrof_folds=nfolds) - acc1 = np.mean(acc) - std1 = np.std(acc) - embeddings = embeddings_list[0] + embeddings_list[1] - embeddings = sklearn.preprocessing.normalize(embeddings) - print(embeddings.shape) - print('infer time', time_consumed) - _, _, accuracy, _, _, _ = evaluate( - embeddings, issame_list, nrof_folds=nfolds) - acc2, std2 = np.mean(accuracy), np.std(accuracy) - return acc1, std1, acc2, std2, _xnorm, embeddings_list - -def create_session(checkpoint_path, target_device): - ''' - create onnx session - ''' - if target_device == 'GPU': - providers = ['CUDAExecutionProvider'] - elif target_device == 'CPU': - providers = ['CPUExecutionProvider'] - else: - raise ValueError(f'Unsupported target device {target_device!r}. Expected one of: "CPU", "GPU"') - session = ort.InferenceSession(checkpoint_path, providers=providers) - input_name = session.get_inputs()[0].name - return session, input_name - -def main(): - '''r - main function - ''' - parser = argparse.ArgumentParser(description='do onnx') - # general - parser.add_argument('--eval_url', default='/data/arcface/', help='') - parser.add_argument('--target', - default='lfw,cfp_fp,agedb_30', - help='test targets.') - parser.add_argument('--nfolds', default=10, type=int, help='') - parser.add_argument('--device_target', default="GPU", type=str, help='device target') - parser.add_argument('--onnx_url', default='', type=str, help='onnx path') - parser.add_argument('--batch_size', default=64, type=int, help='batch size') - args = parser.parse_args() - - # Create inference session - session, input_name = create_session(args.onnx_url, args.device_target) - - # Create dataset - ver_list = [] - ver_name_list = [] - image_size = [112, 112] - for name in args.target.split(','): - path = os.path.join(args.eval_url, name + ".bin") - if os.path.exists(path): - print('loading.. ', name) - data_set = load_bin(path, image_size) - ver_list.append(data_set) - ver_name_list.append(name) - - length = len(ver_list) - for i in range(length): - acc1, std1, acc2, std2, xnorm, _ = test( - ver_list[i], session, input_name, args.batch_size, args.nfolds) - print('[%s]XNorm: %f' % (ver_name_list[i], xnorm)) - print('[%s]Accuracy: %1.5f+-%1.5f' % (ver_name_list[i], acc1, std1)) - print('[%s]Accuracy-Flip: %1.5f+-%1.5f' % - (ver_name_list[i], acc2, std2)) - -if __name__ == '__main__': - main() diff --git a/official/cv/Arcface/export.py b/official/cv/Arcface/export.py deleted file mode 100644 index f6bd1a0ee220eb3d5c83a951ca0fecf98de25362..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/export.py +++ /dev/null @@ -1,55 +0,0 @@ -# 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. -# ============================================================================ -""" -##############export checkpoint file into air, onnx, mindir models################# -python export.py -""" -import argparse -import numpy as np - -import mindspore -from mindspore import dtype as mstype -from mindspore import Tensor, load_checkpoint, load_param_into_net, export - -from src.iresnet import iresnet100 - -parser = argparse.ArgumentParser(description='Classification') -parser.add_argument("--device_id", type=int, default=0, help="Device id") -parser.add_argument("--batch_size", type=int, default=64, help="batch size") -parser.add_argument("--ckpt_file", type=str, required=True, help="Checkpoint file path.") -parser.add_argument("--file_name", type=str, default="arcface", help="output file name.") -parser.add_argument('--file_format', type=str, choices=["AIR", "ONNX", "MINDIR"], default='MINDIR', help='file format') -parser.add_argument("--device_target", type=str, choices=["Ascend", "GPU", "CPU"], default="Ascend", - help="device target") -parser.add_argument('--dataset_name', type=str, default='MS1MV2', choices=['MS1MV2'], - help='dataset name.') -args = parser.parse_args() - -mindspore.set_context(mode=0, device_target=args.device_target) -if args.device_target == "Ascend": - mindspore.set_context(device_id=args.device_id) - -if __name__ == '__main__': - if args.dataset_name != 'MS1MV2': - raise ValueError("dataset is not supported.") - - net = iresnet100() - - assert args.ckpt_file is not None, "args.ckpt_file is None." - param_dict = load_checkpoint(args.ckpt_file) - load_param_into_net(net, param_dict) - - input_arr = Tensor(np.ones([args.batch_size, 3, 112, 112]), mstype.float32) - export(net, input_arr, file_name=args.file_name, file_format=args.file_format) diff --git a/official/cv/Arcface/infer/Dockerfile b/official/cv/Arcface/infer/Dockerfile deleted file mode 100644 index 360861ede17fb0ab697fbcac190acde7c1e29fef..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/infer/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -ARG FROM_IMAGE_NAME -FROM ${FROM_IMAGE_NAME} - -COPY requirements.txt . -RUN pip3.7 install -r requirements.txt diff --git a/official/cv/Arcface/infer/convert/convert_om.sh b/official/cv/Arcface/infer/convert/convert_om.sh deleted file mode 100644 index 2e953e25faf0651663b24023047627f1638fffe8..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/infer/convert/convert_om.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/bash - -# Copyright (c) 2021. Huawei Technologies Co., Ltd. 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. - - -if [ $# -ne 2 ] -then - echo "Wrong parameter format." - echo "Usage:" - echo " bash $0 [INPUT_AIR_PATH] [AIPP_PATH] [OUTPUT_OM_PATH_NAME]" - echo "Example: " - echo " bash convert_om.sh xxx.air xx" - - exit 1 -fi - -model_path=$1 -output_model_name=$2 - -atc \ ---model=$model_path \ ---framework=1 \ ---output=$output_model_name \ ---input_format=NCHW \ ---input_shape="x:1,3,112,112" \ ---enable_small_channel=1 \ ---log=error \ ---soc_version=Ascend310 - -#--insert_op_conf=$aipp_cfg_file \ No newline at end of file diff --git a/official/cv/Arcface/infer/data/config/arcface.pipeline b/official/cv/Arcface/infer/data/config/arcface.pipeline deleted file mode 100644 index 1576651f5453d3d65879d3d49920859ca8910bdd..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/infer/data/config/arcface.pipeline +++ /dev/null @@ -1,61 +0,0 @@ -{ - "im_arcface": { - "stream_config": { - "deviceId": "0" - }, - "appsrc0": { - "props": { - "blocksize": "409600" - }, - "factory": "appsrc", - "next": "mxpi_imagedecoder0" - }, - "mxpi_imagedecoder0": { - "props": { - "handleMethod": "opencv" - }, - "factory": "mxpi_imagedecoder", - "next": "mxpi_imagenormalize0" - }, - "mxpi_imagenormalize0":{ - "props": { - "alpha":"127.5,127.5,127.5", - "beta":"127.5,127.5,127.5", - "dataType":"FLOAT32" - }, - "factory": "mxpi_imagenormalize", - "next": "mxpi_imageresize0" - }, - "mxpi_imageresize0": { - "props": { - "handleMethod": "opencv", - "resizeType": "Resizer_Stretch", - "resizeHeight": "112", - "resizeWidth": "112" - }, - "factory": "mxpi_imageresize", - "next": "mxpi_tensorinfer0" - }, - "mxpi_tensorinfer0": { - "props": { - "dataSource": "mxpi_imageresize0", - "modelPath": "../data/model/ArcFace.om" - }, - "factory": "mxpi_tensorinfer", - "next": "mxpi_dataserialize0" - }, - "mxpi_dataserialize0": { - "props": { - "outputDataKeys": "mxpi_tensorinfer0" - }, - "factory": "mxpi_dataserialize", - "next": "appsink0" - }, - "appsink0": { - "props": { - "blocksize": "4096000" - }, - "factory": "appsink" - } - } -} diff --git a/official/cv/Arcface/infer/data/input/gendata.py b/official/cv/Arcface/infer/data/input/gendata.py deleted file mode 100644 index 55fecb0b4e3e21940b910c63283336b003be31b1..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/infer/data/input/gendata.py +++ /dev/null @@ -1,57 +0,0 @@ -# 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. -# ============================================================================ -""" -python gendata.py -""" -import io -import os -import argparse -import pickle -import numpy as np -import cv2 -import matplotlib.pyplot as plt - - -def gendata(read_path, save_dir): - bins, _ = pickle.load(open(read_path, 'rb'), encoding='bytes') - cnt = 0 - for bini in bins: - data = plt.imread(io.BytesIO(bini), "jpg") - cv2.imwrite(os.path.join(save_dir, str(cnt) + '_.jpg'), data) - cv2.imwrite(os.path.join(save_dir, 'f_' + str(cnt) + '_.jpg'), np.fliplr(data)) - cnt += 1 - if cnt % 100 == 0: - print('%d/%d' % (cnt, len(bins))) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser(description='') - # Datasets - parser.add_argument('--eval_url', default='./data/', type=str, - help='output path') - parser.add_argument('--result_url', default='./data/', type=str) - parser.add_argument('--target', - default='agedb_30,lfw,cfp_fp,calfw,cplfw', - help='test targets.') - args = parser.parse_args() - ver_list = [] - ver_name_list = [] - for name in args.target.split(','): - data_path = os.path.join(args.eval_url, name + ".bin") - save_path = os.path.join(args.result_url, name) - if os.path.exists(data_path): - if not os.path.exists(save_path): - os.mkdir(save_path) - gendata(data_path, save_path) diff --git a/official/cv/Arcface/infer/docker_start_infer.sh b/official/cv/Arcface/infer/docker_start_infer.sh deleted file mode 100644 index 8840fe9d2755cb954ba0b4185394975d0b015722..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/infer/docker_start_infer.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash - -# Copyright (c) 2021. Huawei Technologies Co., Ltd. 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. - -docker_image=$1 -model_dir=$2 - -if [ -z "${docker_image}" ]; then - echo "please input docker_image" - exit 1 -fi - -if [ ! -d "${model_dir}" ]; then - echo "please input model_dir" - exit 1 -fi - -docker run -it \ - --device=/dev/davinci0 \ - --device=/dev/davinci_manager \ - --device=/dev/devmm_svm \ - --device=/dev/hisi_hdc \ - -v /usr/local/Ascend/driver:/usr/local/Ascend/driver \ - -v ${model_dir}:${model_dir} \ - ${docker_image} \ - /bin/bash \ No newline at end of file diff --git a/official/cv/Arcface/infer/mxbase/CMakeLists.txt b/official/cv/Arcface/infer/mxbase/CMakeLists.txt deleted file mode 100644 index 1c43c6cfba981f228312a83c42d3ab47b8c55fa0..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/infer/mxbase/CMakeLists.txt +++ /dev/null @@ -1,59 +0,0 @@ -cmake_minimum_required(VERSION 3.14.0) -project(arcface) -set(TARGET arcface) - -add_definitions(-DENABLE_DVPP_INTERFACE) -add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) -add_definitions(-Dgoogle=mindxsdk_private) -add_compile_options(-std=c++11 -fPIE -fstack-protector-all -fPIC -Wall) -add_link_options(-Wl,-z,relro,-z,now,-z,noexecstack -pie) - -# Check environment variable -if(NOT DEFINED ENV{ASCEND_HOME}) - message(FATAL_ERROR "please define environment variable:ASCEND_HOME") -endif() -if(NOT DEFINED ENV{ASCEND_VERSION}) - message(WARNING "please define environment variable:ASCEND_VERSION") -endif() -if(NOT DEFINED ENV{ARCH_PATTERN}) - message(WARNING "please define environment variable:ARCH_PATTERN") -endif() -set(ACL_INC_DIR $ENV{ASCEND_HOME}/$ENV{ASCEND_VERSION}/$ENV{ARCH_PATTERN}/acllib/include) -set(ACL_LIB_DIR $ENV{ASCEND_HOME}/$ENV{ASCEND_VERSION}/$ENV{ARCH_PATTERN}/acllib/lib64) - -set(MXBASE_INC $ENV{MX_SDK_HOME}/include) -set(MXBASE_LIB_DIR $ENV{MX_SDK_HOME}/lib) -set(MXBASE_POST_LIB_DIR $ENV{MX_SDK_HOME}/lib/modelpostprocessors) -set(MXBASE_POST_PROCESS_DIR $ENV{MX_SDK_HOME}//include/MxBase/postprocess/include/) - -if(DEFINED ENV{MXSDK_OPENSOURCE_DIR}) - set(OPENSOURCE_DIR $ENV{MXSDK_OPENSOURCE_DIR}) -else() - set(OPENSOURCE_DIR $ENV{MX_SDK_HOME}/opensource) -endif() - -include_directories(${ACL_INC_DIR}) -include_directories(${MXBASE_INC}) -include_directories(${MXBASE_POST_PROCESS_DIR}) -include_directories(${OPENSOURCE_DIR}/include) -include_directories(${OPENSOURCE_DIR}/include/opencv4) -message(WARNING "${OPENSOURCE_DIR}/include/opencv4/opencv2") - - -link_directories(${ACL_LIB_DIR}) -link_directories(${MXBASE_LIB_DIR}) -link_directories(${MXBASE_POST_LIB_DIR}) -link_directories(${OPENSOURCE_DIR}/lib) - -set(ACL_LIB_PATH $ENV{ASCEND_HOME}/nnrt/latest/acllib) -message(WARNING "ACL_LIB_PATH:${ACL_LIB_PATH}/lib64/.") -#include_directories(${CMAKE_CURRENT_BINARY_DIR}) -include_directories(${ACL_LIB_PATH}/include) -link_directories(${ACL_LIB_PATH}/lib64/) - - -add_executable(${TARGET} src/main_opencv.cpp src/ArcFaceOpencv.cpp) - -target_link_libraries(${TARGET} glog cpprest mxbase resnet50postprocess opencv_world) - -install(TARGETS ${TARGET} RUNTIME DESTINATION ${PROJECT_SOURCE_DIR}/) diff --git a/official/cv/Arcface/infer/mxbase/build.sh b/official/cv/Arcface/infer/mxbase/build.sh deleted file mode 100644 index f96a57ad6b436f7fda845bff24db12bea2a9bc98..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/infer/mxbase/build.sh +++ /dev/null @@ -1,56 +0,0 @@ -#!/bin/bash - -# Copyright (c) 2021. Huawei Technologies Co., Ltd. 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. - -path_cur=$(dirname $0) -function check_env() -{ - echo "-------------------------" - echo "--${OPENSOURCE_DIR}--" - # set ASCEND_VERSION to ascend-toolkit/latest when it was not specified by user - if [ ! "${ASCEND_VERSION}" ]; then - export ASCEND_VERSION=ascend-toolkit/latest - echo "Set ASCEND_VERSION to the default value: ${ASCEND_VERSION}" - else - echo "ASCEND_VERSION is set to ${ASCEND_VERSION} by user" - fi - - if [ ! "${ARCH_PATTERN}" ]; then - # set ARCH_PATTERN to ./ when it was not specified by user - export ARCH_PATTERN=./ - echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}" - else - echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user" - fi -} - -function build_arcface() -{ - cd $path_cur - rm -rf build - mkdir -p build - cd build - cmake .. - make - ret=$? - if [ ${ret} -ne 0 ]; then - echo "Failed to build arcface." - exit ${ret} - fi - make install -} - -check_env -build_arcface \ No newline at end of file diff --git a/official/cv/Arcface/infer/mxbase/src/ArcFaceOpencv.cpp b/official/cv/Arcface/infer/mxbase/src/ArcFaceOpencv.cpp deleted file mode 100644 index f8a5f203cc2cf98d04b2079eb96b74990388a3ab..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/infer/mxbase/src/ArcFaceOpencv.cpp +++ /dev/null @@ -1,255 +0,0 @@ -/* - * Copyright (c) 2021. Huawei Technologies Co., Ltd. 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. - */ - -#include "ArcFaceOpencv.h" - -#include "MxBase/DeviceManager/DeviceManager.h" -#include "MxBase/Log/Log.h" - -void getfilename(std::string *filename, const std::string &imgpath); -APP_ERROR ArcFaceOpencv::Init(const InitParam &initParam) { - deviceId_ = initParam.deviceId; - APP_ERROR ret = MxBase::DeviceManager::GetInstance()->InitDevices(); - if (ret != APP_ERR_OK) { - LogError << "Init devices failed, ret=" << ret << "."; - return ret; - } - ret = MxBase::TensorContext::GetInstance()->SetContext(initParam.deviceId); - if (ret != APP_ERR_OK) { - LogError << "Set context failed, ret=" << ret << "."; - return ret; - } - dvppWrapper_ = std::make_shared(); - ret = dvppWrapper_->Init(); - if (ret != APP_ERR_OK) { - LogError << "DvppWrapper init failed, ret=" << ret << "."; - return ret; - } - model_ = std::make_shared(); - ret = model_->Init(initParam.modelPath, modelDesc_); - if (ret != APP_ERR_OK) { - LogError << "ModelInferenceProcessor init failed, ret=" << ret << "."; - return ret; - } - return APP_ERR_OK; -} - -APP_ERROR ArcFaceOpencv::DeInit() { - dvppWrapper_->DeInit(); - model_->DeInit(); - MxBase::DeviceManager::GetInstance()->DestroyDevices(); - return APP_ERR_OK; -} - -APP_ERROR ArcFaceOpencv::ReadImage(const std::string &imgPath, - cv::Mat *imageMat) { - *imageMat = cv::imread(imgPath, cv::IMREAD_COLOR); - return APP_ERR_OK; -} - -APP_ERROR ArcFaceOpencv::ResizeImage(const cv::Mat &srcImageMat, - uint8_t (&imageArray)[IMG_H][IMG_W][IMG_C]) { - // HWC->CHW - uint8_t image_chw[IMG_C][IMG_H][IMG_W]; - for (int i = 0; i < IMG_H; i++) { - for (int j = 0; j < IMG_W; j++) { - cv::Vec3b nums = srcImageMat.at(i, j); - image_chw[0][i][j] = nums[0]; - image_chw[1][i][j] = nums[1]; - image_chw[2][i][j] = nums[2]; - } - } - // reshape - uint32_t cnt = 0; - for (int i = 0; i < IMG_C; i++) { - for (int j = 0; j < IMG_H; j++) { - for (int k = 0; k < IMG_W; k++) { - imageArray[cnt / (IMG_W * 3)][(cnt / 3) % IMG_W][cnt % 3] = - image_chw[i][j][k]; - cnt++; - } - } - } - // array to mat - cv::Mat imgMat(IMG_H, IMG_W, CV_8UC3, imageArray); - // encode - std::vector inImage; - cv::imencode(".jpg", imgMat, inImage); - - cv::Mat img_decode = cv::imdecode(inImage, cv::IMREAD_COLOR); - for (int i = 0; i < IMG_H; i++) { - for (int j = 0; j < IMG_W; j++) { - cv::Vec3b nums = img_decode.at(i, j); - imageArray[i][j][0] = nums[0]; - imageArray[i][j][1] = nums[1]; - imageArray[i][j][2] = nums[2]; - } - } - - return APP_ERR_OK; -} - -APP_ERROR ArcFaceOpencv::NormalizeImage( - uint8_t (&imageArray)[IMG_H][IMG_W][IMG_C], - float (&imageArrayNormal)[IMG_H][IMG_W][IMG_C]) { - for (int i = 0; i < IMG_H; i++) { - for (int j = 0; j < IMG_W; j++) { - for (int k = 0; k < IMG_C; k++) { - imageArrayNormal[i][j][k] = (imageArray[i][j][k] - 127.5) / 127.5; - } - } - } - return APP_ERR_OK; -} - -APP_ERROR ArcFaceOpencv::ArrayToTensorBase(float (&imageArray)[IMG_H][IMG_W][IMG_C], - MxBase::TensorBase *tensorBase) { - const uint32_t dataSize = IMG_H * IMG_W * IMG_C * sizeof(float); - MxBase::MemoryData memoryDataDst(dataSize, MxBase::MemoryData::MEMORY_DEVICE, - deviceId_); - MxBase::MemoryData memoryDataSrc(imageArray, dataSize, - MxBase::MemoryData::MEMORY_HOST_MALLOC); - - APP_ERROR ret = - MxBase::MemoryHelper::MxbsMallocAndCopy(memoryDataDst, memoryDataSrc); - if (ret != APP_ERR_OK) { - LogError << GetError(ret) << "Memory malloc failed."; - return ret; - } - - std::vector shape = {IMG_C, IMG_H, IMG_W}; - *tensorBase = MxBase::TensorBase(memoryDataDst, false, shape, - MxBase::TENSOR_DTYPE_FLOAT32); - return APP_ERR_OK; -} - -APP_ERROR ArcFaceOpencv::Inference( - const std::vector &inputs, - std::vector *outputs) { - auto dtypes = model_->GetOutputDataType(); - for (size_t i = 0; i < modelDesc_.outputTensors.size(); ++i) { - std::vector shape = {}; - for (size_t j = 0; j < modelDesc_.outputTensors[i].tensorDims.size(); ++j) { - shape.push_back((uint32_t)modelDesc_.outputTensors[i].tensorDims[j]); - } - MxBase::TensorBase tensor(shape, dtypes[i], - MxBase::MemoryData::MemoryType::MEMORY_DEVICE, - deviceId_); - APP_ERROR ret = MxBase::TensorBase::TensorBaseMalloc(tensor); - if (ret != APP_ERR_OK) { - LogError << "TensorBaseMalloc failed, ret=" << ret << "."; - return ret; - } - (*outputs).push_back(tensor); - } - MxBase::DynamicInfo dynamicInfo = {}; - dynamicInfo.dynamicType = MxBase::DynamicType::STATIC_BATCH; - auto startTime = std::chrono::high_resolution_clock::now(); - APP_ERROR ret = model_->ModelInference(inputs, *outputs, dynamicInfo); - auto endTime = std::chrono::high_resolution_clock::now(); - double costMs = std::chrono::duration(endTime - startTime) - .count(); // save time - inferCostTimeMilliSec += costMs; - if (ret != APP_ERR_OK) { - LogError << "ModelInference failed, ret=" << ret << "."; - return ret; - } - return APP_ERR_OK; -} - -APP_ERROR ArcFaceOpencv::SaveResult(MxBase::TensorBase *tensor, - const std::string &resultpath) { - std::ofstream outfile(resultpath); - APP_ERROR ret = (*tensor).ToHost(); - if (ret != APP_ERR_OK) { - LogError << "ToHost failed"; - return ret; - } - - if (outfile.fail()) { - LogError << "Failed to open result file: "; - return APP_ERR_COMM_FAILURE; - } - - float *result = reinterpret_cast((*tensor).GetBuffer()); - for (int i = 0; i < FEATURE_NUM; i++) { - outfile << float(result[i]) << " "; - } - outfile << std::endl; - outfile.close(); - return APP_ERR_OK; -} - -APP_ERROR ArcFaceOpencv::Process(const std::string &imgPath, - const std::string &resultPath) { - cv::Mat imageMat; - APP_ERROR ret = ReadImage(imgPath, &imageMat); - if (ret != APP_ERR_OK) { - LogError << "ReadImage failed, ret=" << ret << "."; - return ret; - } - uint8_t img_array[IMG_H][IMG_W][IMG_C]; - ResizeImage(imageMat, img_array); - float img_normalize[IMG_H][IMG_W][IMG_C]; - NormalizeImage(img_array, img_normalize); - - std::vector inputs = {}; - std::vector outputs = {}; - - MxBase::TensorBase tensorBase; - ret = ArrayToTensorBase(img_normalize, &tensorBase); - if (ret != APP_ERR_OK) { - LogError << "ArrayToTensorBase failed, ret=" << ret << "."; - return ret; - } - - inputs.push_back(tensorBase); - - auto startTime = std::chrono::high_resolution_clock::now(); - ret = Inference(inputs, &outputs); - auto endTime = std::chrono::high_resolution_clock::now(); - double costMs = std::chrono::duration(endTime - startTime) - .count(); // save time - inferCostTimeMilliSec += costMs; - if (ret != APP_ERR_OK) { - LogError << "Inference failed, ret=" << ret << "."; - return ret; - } - - std::string filename = ""; - getfilename(&filename, imgPath); - std::string resultpath = resultPath + "/" + filename + ".txt"; - ret = SaveResult(&outputs[0], resultpath); - if (ret != APP_ERR_OK) { - LogError << "SaveResult failed, ret=" << ret << "."; - return ret; - } - return APP_ERR_OK; -} - -void getfilename(std::string *filename, const std::string &imgpath) { - int i; - for (i = imgpath.length() - 1; i >= 0; i--) { - // '/' is the delimiter between the file naem and the parent directory in imgpath - if (imgpath[i] == '/') { - break; - } - } - // '.' is the delimiter between the file name and the file suffix - while (imgpath[++i] != '.') { - *filename += imgpath[i]; - } -} diff --git a/official/cv/Arcface/infer/mxbase/src/ArcFaceOpencv.h b/official/cv/Arcface/infer/mxbase/src/ArcFaceOpencv.h deleted file mode 100644 index f142075ad8b3f4d2f6ee8dd912ac51fa6a3c3260..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/infer/mxbase/src/ArcFaceOpencv.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2021. Huawei Technologies Co., Ltd. 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. - */ -#ifndef ARCFACE_MINSPORE_PORT_H -#define ARCFACE_MINSPORE_PORT_H -#include -#include -#include -#include -#include -#include - - -#include "MxBase/CV/Core/DataType.h" -#include "MxBase/DvppWrapper/DvppWrapper.h" -#include "MxBase/ErrorCode/ErrorCode.h" -#include "MxBase/ModelInfer/ModelInferenceProcessor.h" -#include "MxBase/PostProcessBases/ObjectPostProcessBase.h" -#include "MxBase/Tensor/TensorContext/TensorContext.h" - -struct InitParam { - uint32_t deviceId; - bool softmax; - bool checkTensor; - std::string modelPath; -}; - -class ArcFaceOpencv { - public: - static const int IMG_C = 3; - static const int IMG_H = 112; - static const int IMG_W = 112; - static const int FEATURE_NUM = 512; - APP_ERROR Init(const InitParam &initParam); - APP_ERROR DeInit(); - APP_ERROR ReadImage(const std::string &imgPath, cv::Mat *imageMat); - APP_ERROR ResizeImage(const cv::Mat &srcImageMat, - uint8_t (&imageArray)[IMG_H][IMG_W][IMG_C]); - APP_ERROR NormalizeImage(uint8_t (&imageArray)[IMG_H][IMG_W][IMG_C], - float (&imageArrayNormal)[IMG_H][IMG_W][IMG_C]); - APP_ERROR ArrayToTensorBase(float (&imageArray)[IMG_H][IMG_W][IMG_C], - MxBase::TensorBase *tensorBase); - APP_ERROR Inference(const std::vector &inputs, - std::vector *outputs); - APP_ERROR Process(const std::string &imgPath, const std::string &resultPath); - // get infer time - double GetInferCostMilliSec() const { return inferCostTimeMilliSec; } - - private: - APP_ERROR SaveResult(MxBase::TensorBase *tensor, - const std::string &resultpath); - - private: - std::shared_ptr dvppWrapper_; - std::shared_ptr model_; - MxBase::ModelDesc modelDesc_; - uint32_t deviceId_ = 0; - // infer time - double inferCostTimeMilliSec = 0.0; -}; - -#endif diff --git a/official/cv/Arcface/infer/mxbase/src/main_opencv.cpp b/official/cv/Arcface/infer/mxbase/src/main_opencv.cpp deleted file mode 100644 index 1dfde36b382342a0258258bfa6243260aba9255a..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/infer/mxbase/src/main_opencv.cpp +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (c) 2021. Huawei Technologies Co., Ltd. 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. - */ - -#include -#include - -#include "ArcFaceOpencv.h" -#include "MxBase/Log/Log.h" -void getdataname(std::string *filename, const std::string &imgpath); -APP_ERROR ScanImages(const std::string &path, - std::vector *imgFiles) { - DIR *dirPtr = opendir(path.c_str()); - if (dirPtr == nullptr) { - LogError << "opendir failed. dir:" << path; - return APP_ERR_INTERNAL_ERROR; - } - dirent *direntPtr = nullptr; - while ((direntPtr = readdir(dirPtr)) != nullptr) { - std::string fileName = direntPtr->d_name; - if (fileName == "." || fileName == "..") { - continue; - } - - (*imgFiles).emplace_back(path + "/" + fileName); - } - closedir(dirPtr); - return APP_ERR_OK; -} - -int main(int argc, char *argv[]) { - if (argc <= 1) { - LogWarn << "Please input image path, such as './arcface image_dir'."; - return APP_ERR_OK; - } - - InitParam initParam = {}; - initParam.deviceId = 0; - initParam.checkTensor = true; - initParam.modelPath = "../data/model/ArcFace.om"; - std::string imgPath = argv[1]; - std::string dataname = ""; - getdataname(&dataname, imgPath); - std::string resultPath = "../data/mxbase_out/" + dataname; - auto arcface = std::make_shared(); - APP_ERROR ret = arcface->Init(initParam); - if (ret != APP_ERR_OK) { - LogError << "ArcFace init failed, ret=" << ret << "."; - return ret; - } - - std::vector imgFilePaths; - ret = ScanImages(imgPath, &imgFilePaths); - if (ret != APP_ERR_OK) { - return ret; - } - auto startTime = std::chrono::high_resolution_clock::now(); - int cnt = 0; - - if (access(resultPath.c_str(), 0) == -1) { - std::string command = "mkdir -p " + resultPath; - LogError << command.c_str() << std::endl; - system(command.c_str()); - } - for (auto &imgFile : imgFilePaths) { - ret = arcface->Process(imgFile, resultPath); - if (ret != APP_ERR_OK) { - LogError << "Arcface process failed, ret=" << ret << "."; - arcface->DeInit(); - return ret; - } - if (cnt++ % 1000 == 0) { - LogError << cnt << std::endl; - } - } - auto endTime = std::chrono::high_resolution_clock::now(); - arcface->DeInit(); - double costMilliSecs = - std::chrono::duration(endTime - startTime).count(); - double fps = 1000.0 * imgFilePaths.size() / arcface->GetInferCostMilliSec(); - LogInfo << "[Process Delay] cost: " << costMilliSecs << " ms\tfps: " << fps - << " imgs/sec"; - return APP_ERR_OK; -} - -void getdataname(std::string *filename, const std::string &imgpath) { - int i; - for (i = imgpath.length() - 1; i >= 0; i--) { - if (imgpath[i] == '/') { - break; - } - } - while (imgpath[++i] != '\0') { - *filename += imgpath[i]; - } -} diff --git a/official/cv/Arcface/infer/requirements.txt b/official/cv/Arcface/infer/requirements.txt deleted file mode 100644 index 044bdd758636edf2ee49b03917cb416d76a71de4..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/infer/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -sklearn \ No newline at end of file diff --git a/official/cv/Arcface/infer/sdk/build.sh b/official/cv/Arcface/infer/sdk/build.sh deleted file mode 100644 index 60d9d9fc6dd69eb20cb5a1dbd15b757dcd2b75df..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/infer/sdk/build.sh +++ /dev/null @@ -1,78 +0,0 @@ -#!/bin/bash - -# Copyright (c) 2021. Huawei Technologies Co., Ltd. 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. - -# The number of parameters must be 2. -if [ $# -ne 4 ] -then - echo "Wrong parameter format." - echo "Usage:" - echo " bash $0 [INPUT_PATH] [PIPELINE_PATH] [OUTPUT_PATH] [DATASET_NAME]" - echo "Example: " - echo " bash build.sh ../data/input/data/ ../data/config/arcface.pipeline ../data/sdk_out/ lfw" - - exit 1 -fi - -# The path of a folder containing eval images. -image_path=$1 -# The path of pipeline file. -pipeline_dir=$2 -# The path of a folder used to store all results. -result_dir=$3 -# The name of pipeline eval dataset. -dataset_name=$4 - -if [ ! -d $image_path ] -then - echo "Please input the correct directory containing images." - exit -fi - -if [ ! -d $result_dir ] -then - mkdir -p $result_dir -fi - -set -e - -CUR_PATH=$(cd "$(dirname "$0")" || { warn "Failed to check path/to/run.sh" ; exit ; } ; pwd) -echo "enter $CUR_PATH" - -# Simple log helper functions -info() { echo -e "\033[1;34m[INFO ][MxStream] $1\033[1;37m" ; } -warn() { echo >&2 -e "\033[1;31m[WARN ][MxStream] $1\033[1;37m" ; } - -export LD_LIBRARY_PATH=${MX_SDK_HOME}/lib:${MX_SDK_HOME}/opensource/lib:${MX_SDK_HOME}/opensource/lib64:/usr/local/Ascend/ascend-toolkit/latest/acllib/lib64:${LD_LIBRARY_PATH} - -#to set PYTHONPATH, import the StreamManagerApi.py -export PYTHONPATH=$PYTHONPATH:${MX_SDK_HOME}/python - -if [ ! "${MX_SDK_HOME}" ] -then -export GST_PLUGIN_SCANNER=${MX_SDK_HOME}/opensource/libexec/gstreamer-1.0/gst-plugin-scanner -fi - -if [ ! "${MX_SDK_HOME}" ] -then -export GST_PLUGIN_PATH=${MX_SDK_HOME}/opensource/lib/gstreamer-1.0:${MX_SDK_HOME}/lib/plugins -fi - -python3.7 main_sdk_jpg.py --eval_url=$image_path \ - --PL_PATH=$pipeline_dir \ - --result_url=$result_dir \ - --target=$dataset_name - -exit 0 \ No newline at end of file diff --git a/official/cv/Arcface/infer/sdk/main_sdk_jpg.py b/official/cv/Arcface/infer/sdk/main_sdk_jpg.py deleted file mode 100644 index d960770f87cbb724ea1dff425b1536e10e26d203..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/infer/sdk/main_sdk_jpg.py +++ /dev/null @@ -1,327 +0,0 @@ -# coding=utf-8 -""" -Copyright (c) 2021. Huawei Technologies Co., Ltd. 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 datetime -import json -import os -import argparse -import pickle -import numpy as np -import cv2 -import sklearn -from sklearn.decomposition import PCA -from sklearn.model_selection import KFold -from scipy import interpolate -import MxpiDataType_pb2 as MxpiDataType -from StreamManagerApi import StreamManagerApi, StringVector, MxDataInput - - -class LFold: - ''' - LFold - ''' - - def __init__(self, n_splits=2, shuffle=False): - self.n_splits = n_splits - if self.n_splits > 1: - self.k_fold = KFold(n_splits=n_splits, shuffle=shuffle) - - def split(self, indices): - if self.n_splits > 1: - return self.k_fold.split(indices) - return [(indices, indices)] - - -def calculate_roc(thresholds, - embeddings1, - embeddings2, - actual_issame, - nrof_folds=10, - pca=0): - ''' - calculate_roc - ''' - assert embeddings1.shape[0] == embeddings2.shape[0] - assert embeddings1.shape[1] == embeddings2.shape[1] - nrof_pairs = min(len(actual_issame), embeddings1.shape[0]) - nrof_thresholds = len(thresholds) - k_fold = LFold(n_splits=nrof_folds, shuffle=False) - - tprs = np.zeros((nrof_folds, nrof_thresholds)) - fprs = np.zeros((nrof_folds, nrof_thresholds)) - accuracy = np.zeros((nrof_folds)) - indices = np.arange(nrof_pairs) - - if pca == 0: - diff = np.subtract(embeddings1, embeddings2) - dist = np.sum(np.square(diff), 1) - - for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)): - if pca > 0: - print('doing pca on', fold_idx) - embed1_train = embeddings1[train_set] - embed2_train = embeddings2[train_set] - _embed_train = np.concatenate((embed1_train, embed2_train), axis=0) - pca_model = PCA(n_components=pca) - pca_model.fit(_embed_train) - embed1 = pca_model.transform(embeddings1) - embed2 = pca_model.transform(embeddings2) - embed1 = sklearn.preprocessing.normalize(embed1) - embed2 = sklearn.preprocessing.normalize(embed2) - diff = np.subtract(embed1, embed2) - dist = np.sum(np.square(diff), 1) - - # Find the best threshold for the fold - acc_train = np.zeros((nrof_thresholds)) - for threshold_idx, threshold in enumerate(thresholds): - _, _, acc_train[threshold_idx] = calculate_accuracy( - threshold, dist[train_set], actual_issame[train_set]) - best_threshold_index = np.argmax(acc_train) - for threshold_idx, threshold in enumerate(thresholds): - tprs[fold_idx, threshold_idx], fprs[fold_idx, threshold_idx], _ = calculate_accuracy( - threshold, dist[test_set], - actual_issame[test_set]) - _, _, accuracy[fold_idx] = calculate_accuracy( - thresholds[best_threshold_index], dist[test_set], - actual_issame[test_set]) - - tpr = np.mean(tprs, 0) - fpr = np.mean(fprs, 0) - return tpr, fpr, accuracy - - -def calculate_accuracy(threshold, dist, actual_issame): - '''calculate_acc - ''' - predict_issame = np.less(dist, threshold) - tp = np.sum(np.logical_and(predict_issame, actual_issame)) - fp = np.sum(np.logical_and(predict_issame, np.logical_not(actual_issame))) - tn = np.sum( - np.logical_and(np.logical_not(predict_issame), - np.logical_not(actual_issame))) - fn = np.sum(np.logical_and(np.logical_not(predict_issame), actual_issame)) - - tpr = 0 if (tp + fn == 0) else float(tp) / float(tp + fn) - fpr = 0 if (fp + tn == 0) else float(fp) / float(fp + tn) - acc = float(tp + tn) / dist.size - return tpr, fpr, acc - - -def calculate_val(thresholds, - embeddings1, - embeddings2, - actual_issame, - far_target, - nrof_folds=10): - ''' - calculate_val - ''' - assert embeddings1.shape[0] == embeddings2.shape[0] - assert embeddings1.shape[1] == embeddings2.shape[1] - nrof_pairs = min(len(actual_issame), embeddings1.shape[0]) - nrof_thresholds = len(thresholds) - k_fold = LFold(n_splits=nrof_folds, shuffle=False) - - val = np.zeros(nrof_folds) - far = np.zeros(nrof_folds) - - diff = np.subtract(embeddings1, embeddings2) - dist = np.sum(np.square(diff), 1) - indices = np.arange(nrof_pairs) - - for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)): - - # Find the threshold that gives FAR = far_target - far_train = np.zeros(nrof_thresholds) - for threshold_idx, threshold in enumerate(thresholds): - _, far_train[threshold_idx] = calculate_val_far( - threshold, dist[train_set], actual_issame[train_set]) - if np.max(far_train) >= far_target: - f = interpolate.interp1d(far_train, thresholds, kind='slinear') - threshold = f(far_target) - else: - threshold = 0.0 - - val[fold_idx], far[fold_idx] = calculate_val_far( - threshold, dist[test_set], actual_issame[test_set]) - - val_mean = np.mean(val) - far_mean = np.mean(far) - val_std = np.std(val) - return val_mean, val_std, far_mean - - -def calculate_val_far(threshold, dist, actual_issame): - ''' - calculate_val_far - ''' - predict_issame = np.less(dist, threshold) - true_accept = np.sum(np.logical_and(predict_issame, actual_issame)) - false_accept = np.sum( - np.logical_and(predict_issame, np.logical_not(actual_issame))) - n_same = np.sum(actual_issame) - n_diff = np.sum(np.logical_not(actual_issame)) - val = float(true_accept) / float(n_same) - far = float(false_accept) / float(n_diff) - return val, far - - -def evaluate(embeddings, actual_issame, nrof_folds=10, pca=0): - '''evaluate - ''' - # Calculate evaluation metrics - thresholds = np.arange(0, 4, 0.01) - embeddings1 = embeddings[0::2] - embeddings2 = embeddings[1::2] - tpr, fpr, accuracy = calculate_roc(thresholds, - embeddings1, - embeddings2, - np.asarray(actual_issame), - nrof_folds=nrof_folds, - pca=pca) - thresholds = np.arange(0, 4, 0.001) - val, val_std, far = calculate_val(thresholds, - embeddings1, - embeddings2, - np.asarray(actual_issame), - 1e-3, - nrof_folds=nrof_folds) - return tpr, fpr, accuracy, val, val_std, far - - -def inference(dir_name, res_dir_name, PL_PATH): - '''inference - ''' - _, issame_list = pickle.load(open(data_path + '.bin', 'rb'), encoding='bytes') - stream_manager_api = StreamManagerApi() - ret = stream_manager_api.InitManager() - if ret != 0: - print("Failed to init Stream manager, ret=%s" % str(ret)) - exit() - - # create streams by pipeline config file - with open(PL_PATH, 'rb') as f: - pipelineStr = f.read() - ret = stream_manager_api.CreateMultipleStreams(pipelineStr) - - if ret != 0: - print("Failed to create Stream, ret=%s" % str(ret)) - exit() - # Construct the input of the stream - data_input = MxDataInput() - file_list = os.listdir(dir_name) - if not os.path.exists(res_dir_name): - os.makedirs(res_dir_name) - cnt = 0 - leng = int(len(file_list) / 2) - embeding = np.zeros(shape=[leng, 512], dtype=np.float32) - embeding_f = np.zeros(shape=[leng, 512], dtype=np.float32) - - for file_name in file_list: - stream_name = b'im_arcface' - in_plugin_id = 0 - file_path = os.path.join(dir_name, file_name) - img_decode = cv2.imread(file_path) - img_decode = np.transpose(img_decode, axes=(2, 0, 1)) - img_decode = img_decode.reshape([112, 112, 3]) - - _, encoded_image = cv2.imencode(".jpg", img_decode) - img_bytes = encoded_image.tobytes() - data_input.data = img_bytes - unique_id = stream_manager_api.SendData(stream_name, in_plugin_id, data_input) - if unique_id < 0: - print("Failed to send data to stream.") - exit() - keys = [b"mxpi_tensorinfer0"] - keyVec = StringVector() - for key in keys: - keyVec.push_back(key) - start_time = datetime.datetime.now() - infer_result = stream_manager_api.GetProtobuf(stream_name, in_plugin_id, keyVec) - end_time = datetime.datetime.now() - if infer_result.size() == 0: - print("infer_result is null") - exit() - - if infer_result[0].errorCode != 0: - print("GetProtobuf error. errorCode=%d, errorMsg=%s" % ( - infer_result[0].errorCode, infer_result[0].data.decode())) - exit() - resultList = MxpiDataType.MxpiTensorPackageList() - resultList.ParseFromString(infer_result[0].messageBuf) - output = np.frombuffer(resultList.tensorPackageVec[0].tensorVec[0].dataStr, dtype=' 1: - device_id = int(os.getenv('DEVICE_ID')) - mindspore.set_context(device_id=device_id) - else: - mindspore.set_context(device_id=args.device_id) - if args.device_num > 1: - mindspore.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL, - gradients_mean=True, - ) - cost_model_context.set_cost_model_context(device_memory_capacity=32.0 * 1024.0 * 1024.0 * 1024.0, - costmodel_gamma=0.001, - costmodel_beta=280.0) - set_algo_parameters(elementwise_op_strategy_follow=True) - init() - ckpt_save_path = CKPT_PATH + "ckpt_" + str(get_rank()) + "/" - print("ckpt_save_path finish copy to %s" % ckpt_save_path) - if args.modelarts: - import moxing as mox - - if not os.path.exists(DATA_PATH): - os.makedirs(DATA_PATH, 0o755) - mox.file.copy_parallel(src_url=args.data_url, dst_url=DATA_PATH) - print("training data finish copy to %s" % DATA_PATH) - - mox.file.copy_parallel( - src_url=args.data_url, dst_url=DATA_PATH + os.getenv('DEVICE_ID')) - zip_command = "unzip -o -q " + DATA_PATH + os.getenv('DEVICE_ID') + "/MS1M.zip -d " + DATA_PATH + os.getenv( - 'DEVICE_ID') - os.system(zip_command) - train_dataset = create_dataset(dataset_path=DATA_PATH + os.getenv('DEVICE_ID') + '/MS1M/', - do_train=True, - repeat_num=1, batch_size=args.batch_size, target=target) - else: - train_dataset = create_dataset(dataset_path=args.data_url, do_train=True, - repeat_num=1, batch_size=args.batch_size, target=target) - step = train_dataset.get_dataset_size() - lr = lr_generator(args.lr, train_epoch, steps_per_epoch=step) - net = iresnet100() - train_net = MyNetWithLoss(net, args) - optimizer = nn.SGD(params=train_net.trainable_params(), learning_rate=lr / 512 * args.batch_size * args.device_num, - momentum=args.momentum, weight_decay=args.weight_decay) - model = Model(train_net, optimizer=optimizer) - - time_cb = TimeMonitor(data_size=train_dataset.get_dataset_size()) - loss_cb = LossMonitor() - cb = [time_cb, loss_cb] - config_ck = CheckpointConfig( - save_checkpoint_steps=60, keep_checkpoint_max=5) - prefix = "ArcFace" - if args.modelarts: - ckpt_cb = ModelCheckpoint(prefix="ArcFace", config=config_ck, - directory=ckpt_save_path) - cb.append(ckpt_cb) - else: - if args.device_num == 8 and MutiDev.get_rank() % 8 == 0: - ckpt_cb = ModelCheckpoint(prefix="ArcFace", config=config_ck, - directory=args.train_url) - cb.append(ckpt_cb) - if args.device_num == 1: - ckpt_cb = ModelCheckpoint(prefix="ArcFace", config=config_ck, - directory=args.train_url) - cb.append(ckpt_cb) - model.train(train_epoch, train_dataset, callbacks=cb, dataset_sink_mode=True) - - if not os.path.isdir(CKPT_PATH): - os.makedirs(CKPT_PATH) - ckpt_list = glob.glob(CKPT_PATH + prefix + "*.ckpt") - if not ckpt_list: - print("ckpt file not generated.") - - ckpt_list.sort(key=os.path.getmtime) - ckpt_model = ckpt_list[-1] - net = iresnet100() - frozen_to_air_args = {'ckpt_file': ckpt_model, - 'batch_size': args.batch_size, - 'height': 112, - 'width': 112, - 'file_name': (CKPT_PATH + prefix), - 'file_format': 'AIR'} - frozen_to_air(net, frozen_to_air_args) - - if args.modelarts: - mox.file.copy_parallel(src_url=CKPT_PATH, dst_url=args.train_url) diff --git a/official/cv/Arcface/postprocess.py b/official/cv/Arcface/postprocess.py deleted file mode 100644 index de12ba0222f1fdca907561cb54f46426a96879f7..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/postprocess.py +++ /dev/null @@ -1,328 +0,0 @@ -# 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. -# ============================================================================ -''' -evaluation of lfw, calfw, cfp_fp, agedb_30, cplfw -''' - -import datetime -import os -import pickle -import argparse -from io import BytesIO - -import numpy as np -import sklearn -from sklearn.decomposition import PCA -from sklearn.model_selection import KFold -import matplotlib.pyplot as plt -from scipy import interpolate - - -class LFold: - ''' - LFold - ''' - def __init__(self, n_splits=2, shuffle=False): - self.n_splits = n_splits - if self.n_splits > 1: - self.k_fold = KFold(n_splits=n_splits, shuffle=shuffle) - - def split(self, indices): - if self.n_splits > 1: - return self.k_fold.split(indices) - return [(indices, indices)] - - -def calculate_roc(thresholds, - embeddings1, - embeddings2, - actual_issame, - nrof_folds=10, - pca=0): - ''' - calculate_roc - - # noqa: DAR201 - ''' - assert embeddings1.shape[0] == embeddings2.shape[0] - assert embeddings1.shape[1] == embeddings2.shape[1] - nrof_pairs = min(len(actual_issame), embeddings1.shape[0]) - nrof_thresholds = len(thresholds) - k_fold = LFold(n_splits=nrof_folds, shuffle=False) - - tprs = np.zeros((nrof_folds, nrof_thresholds)) - fprs = np.zeros((nrof_folds, nrof_thresholds)) - accuracy = np.zeros((nrof_folds)) - indices = np.arange(nrof_pairs) - - if pca == 0: - diff = np.subtract(embeddings1, embeddings2) - dist = np.sum(np.square(diff), 1) - - for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)): - if pca > 0: - print('doing pca on', fold_idx) - embed1_train = embeddings1[train_set] - embed2_train = embeddings2[train_set] - _embed_train = np.concatenate((embed1_train, embed2_train), axis=0) - pca_model = PCA(n_components=pca) - pca_model.fit(_embed_train) - embed1 = pca_model.transform(embeddings1) - embed2 = pca_model.transform(embeddings2) - embed1 = sklearn.preprocessing.normalize(embed1) - embed2 = sklearn.preprocessing.normalize(embed2) - diff = np.subtract(embed1, embed2) - dist = np.sum(np.square(diff), 1) - - # Find the best threshold for the fold - acc_train = np.zeros((nrof_thresholds)) - for threshold_idx, threshold in enumerate(thresholds): - _, _, acc_train[threshold_idx] = calculate_accuracy( - threshold, dist[train_set], actual_issame[train_set]) - best_threshold_index = np.argmax(acc_train) - for threshold_idx, threshold in enumerate(thresholds): - tprs[fold_idx, threshold_idx], fprs[fold_idx, threshold_idx], _ = calculate_accuracy( - threshold, dist[test_set], - actual_issame[test_set]) - _, _, accuracy[fold_idx] = calculate_accuracy( - thresholds[best_threshold_index], dist[test_set], - actual_issame[test_set]) - - tpr = np.mean(tprs, 0) - fpr = np.mean(fprs, 0) - return tpr, fpr, accuracy - - -def calculate_accuracy(threshold, dist, actual_issame): - '''calculate_acc - ''' - predict_issame = np.less(dist, threshold) - tp = np.sum(np.logical_and(predict_issame, actual_issame)) - fp = np.sum(np.logical_and(predict_issame, np.logical_not(actual_issame))) - tn = np.sum( - np.logical_and(np.logical_not(predict_issame), - np.logical_not(actual_issame))) - fn = np.sum(np.logical_and(np.logical_not(predict_issame), actual_issame)) - - tpr = 0 if (tp + fn == 0) else float(tp) / float(tp + fn) - fpr = 0 if (fp + tn == 0) else float(fp) / float(fp + tn) - acc = float(tp + tn) / dist.size - return tpr, fpr, acc - - -def calculate_val(thresholds, - embeddings1, - embeddings2, - actual_issame, - far_target, - nrof_folds=10): - ''' - calculate_val - - # noqa: DAR201 - ''' - assert embeddings1.shape[0] == embeddings2.shape[0] - assert embeddings1.shape[1] == embeddings2.shape[1] - nrof_pairs = min(len(actual_issame), embeddings1.shape[0]) - nrof_thresholds = len(thresholds) - k_fold = LFold(n_splits=nrof_folds, shuffle=False) - - val = np.zeros(nrof_folds) - far = np.zeros(nrof_folds) - - diff = np.subtract(embeddings1, embeddings2) - dist = np.sum(np.square(diff), 1) - indices = np.arange(nrof_pairs) - - for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)): - - # Find the threshold that gives FAR = far_target - far_train = np.zeros(nrof_thresholds) - for threshold_idx, threshold in enumerate(thresholds): - _, far_train[threshold_idx] = calculate_val_far( - threshold, dist[train_set], actual_issame[train_set]) - if np.max(far_train) >= far_target: - f = interpolate.interp1d(far_train, thresholds, kind='slinear') - threshold = f(far_target) - else: - threshold = 0.0 - - val[fold_idx], far[fold_idx] = calculate_val_far( - threshold, dist[test_set], actual_issame[test_set]) - - val_mean = np.mean(val) - far_mean = np.mean(far) - val_std = np.std(val) - return val_mean, val_std, far_mean - - -def calculate_val_far(threshold, dist, actual_issame): - '''calculate_val_far - ''' - predict_issame = np.less(dist, threshold) - true_accept = np.sum(np.logical_and(predict_issame, actual_issame)) - false_accept = np.sum( - np.logical_and(predict_issame, np.logical_not(actual_issame))) - n_same = np.sum(actual_issame) - n_diff = np.sum(np.logical_not(actual_issame)) - val = float(true_accept) / float(n_same) - far = float(false_accept) / float(n_diff) - return val, far - - -def evaluate(embeddings, actual_issame, nrof_folds=10, pca=0): - '''evaluate - ''' - # Calculate evaluation metrics - thresholds = np.arange(0, 4, 0.01) - embeddings1 = embeddings[0::2] - embeddings2 = embeddings[1::2] - tpr, fpr, accuracy = calculate_roc(thresholds, - embeddings1, - embeddings2, - np.asarray(actual_issame), - nrof_folds=nrof_folds, - pca=pca) - thresholds = np.arange(0, 4, 0.001) - val, val_std, far = calculate_val(thresholds, - embeddings1, - embeddings2, - np.asarray(actual_issame), - 1e-3, - nrof_folds=nrof_folds) - return tpr, fpr, accuracy, val, val_std, far - - -def load_bin(path, image_size): - '''load evalset of .bin - ''' - try: - with open(path, 'rb') as f: - bins, issame_list = pickle.load(f) # py2 - except UnicodeDecodeError as _: - with open(path, 'rb') as f: - bins, issame_list = pickle.load(f, encoding='bytes') # py3 - data_list = [] - for _ in [0, 1]: - data = np.zeros( - (len(issame_list) * 2, 3, image_size[0], image_size[1])) - data_list.append(data) - for idx in range(len(issame_list) * 2): - _bin = bins[idx] - img = plt.imread(BytesIO(_bin), "jpg") - if img.shape[1] != image_size[0]: - img = mx.image.resize_short(img, image_size[0]) - img = np.transpose(img, axes=(2, 0, 1)) - for flip in [0, 1]: - if flip == 1: - img = np.flip(img, axis=2) - data_list[flip][idx][:] = img - return data_list, issame_list - - -def test(data_set, batch_size, args): - '''test - ''' - print('testing verification..') - data_list = data_set[0] - issame_list = data_set[1] - embeddings_list = [] - time_consumed = 0.0 - i = 0 - for data in data_list: - embeddings = None - ba = 0 - while ba < data.shape[0]: - bb = min(ba + batch_size, data.shape[0]) - count = bb - ba - - time0 = datetime.datetime.now() - file_name = os.path.join(args.result_dir, 'lfw_' + str(i) + '_0.bin') - net_out = np.fromfile(file_name, np.float32).reshape(batch_size, 512) - _embeddings = net_out - time_now = datetime.datetime.now() - diff = time_now - time0 - time_consumed += diff.total_seconds() - if embeddings is None: - embeddings = np.zeros((data.shape[0], _embeddings.shape[1])) - embeddings[ba:bb, :] = _embeddings[(batch_size - count):, :] - ba = bb - i = i + 1 - embeddings_list.append(embeddings) - _xnorm = 0.0 - _xnorm_cnt = 0 - for embed in embeddings_list: - for i in range(embed.shape[0]): - _em = embed[i] - _norm = np.linalg.norm(_em) - _xnorm += _norm - _xnorm_cnt += 1 - _xnorm /= _xnorm_cnt - - embeddings = embeddings_list[0].copy() - embeddings = sklearn.preprocessing.normalize(embeddings) - _, _, acc, _, _, _ = evaluate(embeddings, issame_list, nrof_folds=args.nfolds) - acc1 = np.mean(acc) - std1 = np.std(acc) - embeddings = embeddings_list[0] + embeddings_list[1] - embeddings = sklearn.preprocessing.normalize(embeddings) - print(embeddings.shape) - print('infer time', time_consumed) - _, _, accuracy, _, _, _ = evaluate( - embeddings, issame_list, nrof_folds=args.nfolds) - acc2, std2 = np.mean(accuracy), np.std(accuracy) - return acc1, std1, acc2, std2, _xnorm, embeddings_list - - -def main(): - '''r - main function - ''' - parser = argparse.ArgumentParser(description='do verification') - # general - parser.add_argument('--target', - default='lfw', - help='test targets.') - parser.add_argument('--batch_size', default=64, type=int, help='') - parser.add_argument('--nfolds', default=10, type=int, help='') - parser.add_argument('--result_dir', type=str, help='') - parser.add_argument('--label_dir', type=str, help='') - parser.add_argument('--data_set', type=str, help='') - args = parser.parse_args() - - image_size = [112, 112] - ver_list = [] - ver_name_list = [] - for name in args.target.split(','): - path = os.path.join(args.data_set) - print("loading...") - if os.path.exists(path): - print('loading.. ', name) - data_set = load_bin(path, image_size) - ver_list.append(data_set) - ver_name_list.append(name) - - length = len(ver_list) - for i in range(length): - acc1, std1, acc2, std2, xnorm, _ = test( - ver_list[i], args.batch_size, args) - print('[%s]XNorm: %f' % (ver_name_list[i], xnorm)) - print('[%s]Accuracy: %1.5f+-%1.5f' % (ver_name_list[i], acc1, std1)) - print('[%s]Accuracy-Flip: %1.5f+-%1.5f' % - (ver_name_list[i], acc2, std2)) - - -if __name__ == '__main__': - main() diff --git a/official/cv/Arcface/preprocess.py b/official/cv/Arcface/preprocess.py deleted file mode 100644 index 81fe629c6c7861b4527f35dd5441a7534d247d15..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/preprocess.py +++ /dev/null @@ -1,87 +0,0 @@ -# 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. -# ============================================================================ -""" -##############preprocess################# -""" -import os -import pickle -import argparse -from io import BytesIO -import numpy as np -import matplotlib.pyplot as plt - - -def load_bin(path, image_size): - '''load evalset of .bin - ''' - try: - with open(path, 'rb') as f: - bins, issame_list = pickle.load(f) # py2 - except UnicodeDecodeError as _: - with open(path, 'rb') as f: - bins, issame_list = pickle.load(f, encoding='bytes') # py3 - data_list = [] - for _ in [0, 1]: - data = np.zeros( - (len(issame_list) * 2, 3, image_size[0], image_size[1])) - data_list.append(data) - for idx in range(len(issame_list) * 2): - _bin = bins[idx] - img = plt.imread(BytesIO(_bin), "jpg") - img = np.transpose(img, axes=(2, 0, 1)) - for flip in [0, 1]: - if flip == 1: - img = np.flip(img, axis=2) - data_list[flip][idx][:] = img - print("data_list:", len(data_list)) - return data_list, issame_list - - -def test(data_set, batch_size, label_dir): - '''test - ''' - print('testing preprocess') - data_list = data_set[0] - issame_list = data_set[1] - np.save(os.path.join(label_dir, "issame_list.npy"), issame_list) - i = 0 - print(len(data_list)) - print(len(issame_list)) - for data in data_list: - ba = 0 - while ba < data.shape[0]: - bb = min(ba + batch_size, data.shape[0]) - _data = data[bb - batch_size: bb] - - img = ((_data / 255) - 0.5) / 0.5 - ba = bb - img = img.astype(np.float32) - file_path = os.path.join(img_path, "lfw" + '_' + str(i) + '.bin') - i = i + 1 - img.tofile(file_path) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser(description='do preprocess') - parser.add_argument("--result_dir", type=str, help="") - parser.add_argument("--label_dir", type=str, help="") - parser.add_argument('--batch_size', default=64, type=int, help='') - parser.add_argument("--dataset_path", type=str, help="") - args = parser.parse_args() - img_path = os.path.join(args.result_dir) - img_size = [112, 112] - dataset = load_bin(args.dataset_path, img_size) - test(dataset, args.batch_size, args.label_dir) - print("="*20, "export bin files finished", "="*20) diff --git a/official/cv/Arcface/requirements.txt b/official/cv/Arcface/requirements.txt deleted file mode 100644 index f3816aa5f10536c6a61ba5af3301ccc6b9b767e6..0000000000000000000000000000000000000000 Binary files a/official/cv/Arcface/requirements.txt and /dev/null differ diff --git a/official/cv/Arcface/scripts/docker_start.sh b/official/cv/Arcface/scripts/docker_start.sh deleted file mode 100644 index be414f2f6acb062b27f3087f1b02f516677ee858..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/scripts/docker_start.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/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. -# ============================================================================ - -docker_image=$1 -data_dir=$2 -model_dir=$3 - -docker run -it --ipc=host \ - --device=/dev/davinci0 \ - --device=/dev/davinci1 \ - --device=/dev/davinci2 \ - --device=/dev/davinci3 \ - --device=/dev/davinci4 \ - --device=/dev/davinci5 \ - --device=/dev/davinci6 \ - --device=/dev/davinci7 \ - --device=/dev/davinci_manager \ - --device=/dev/devmm_svm \ - --device=/dev/hisi_hdc \ - --privileged \ - -v /usr/local/Ascend/driver:/usr/local/Ascend/driver \ - -v /usr/local/Ascend/add-ons/:/usr/local/Ascend/add-ons \ - -v ${data_dir}:${data_dir} \ - -v ${model_dir}:${model_dir} \ - -v /var/log/npu/conf/slog/slog.conf:/var/log/npu/conf/slog/slog.conf \ - -v /var/log/npu/slog/:/var/log/npu/slog/ \ - -v /var/log/npu/profiling/:/var/log/npu/profiling \ - -v /var/log/npu/dump/:/var/log/npu/dump \ - -v /var/log/npu/:/usr/slog ${docker_image} \ - /bin/bash diff --git a/official/cv/Arcface/scripts/run_cpp_infer.sh b/official/cv/Arcface/scripts/run_cpp_infer.sh deleted file mode 100644 index 4a7b777e1d84880a2ee0bc51c08b2971d9bdf22c..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/scripts/run_cpp_infer.sh +++ /dev/null @@ -1,136 +0,0 @@ -#!/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. -# ============================================================================ - -if [[ $# -lt 4 || $# -gt 5 ]]; then - echo "Usage: bash run_infer_cpp.sh [MINDIR_PATH] [DATASET_PATH] [NEED_PREPROCESS] [DEVICE_TYPE] [DEVICE_ID] - DEVICE_TYPE can choose from [Ascend, GPU, CPU] - NEED_PREPROCESS means weather need preprocess or not, it's value is 'y' or 'n'. - DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero" -exit 1 -fi - -get_real_path(){ - if [ "${1:0:1}" == "/" ]; then - echo "$1" - else - echo "$(realpath -m $PWD/$1)" - fi -} -model=$(get_real_path $1) -dataset_path=$(get_real_path $2) - -if [ "$3" == "y" ] || [ "$3" == "n" ];then - need_preprocess=$3 -else - echo "weather need preprocess or not, it's value must be in [y, n]" - exit 1 -fi - -device_id=0 -if [ $# == 5 ]; then - device_id=$5 -fi - -if [ $4 == 'GPU' ]; then - if [ $CUDA_VISIABLE_DEVICES ]; then - device_id=$CUDA_VISIABLE_DEVICES - fi -fi - -echo "mindir name: "$model -echo "dataset path: "$dataset_path -echo "need preprocess: "$need_preprocess -echo "device id: "$device_id - -if [ $4 == 'Ascend' ] || [ $4 == 'GPU' ] || [ $4 == 'CPU' ]; then - device_type=$4 -else - echo "DEVICE_TYPE can choose from [Ascend, GPU, CPU]" - exit 1 -fi -echo "device type: "$device_type - -if [ $MS_LITE_HOME ]; then - RUNTIME_HOME=$MS_LITE_HOME/runtime - TOOLS_HOME=$MS_LITE_HOME/tools - RUNTIME_LIBS=$RUNTIME_HOME/lib:$RUNTIME_HOME/third_party/glog/:$RUNTIME_HOME/third_party/libjpeg-turbo/lib - RUNTIME_LIBS=$RUNTIME_LIBS:$RUNTIME_HOME/third_party/dnnl/ - export LD_LIBRARY_PATH=$RUNTIME_LIBS:$TOOLS_HOME/converter/lib:$LD_LIBRARY_PATH - echo "Insert LD_LIBRARY_PATH the MindSpore Lite runtime libs path: $RUNTIME_LIBS $TOOLS_HOME/converter/lib" -fi - -function preprocess_data() -{ - if [ -d preprocess_Result ]; then - rm -rf ./preprocess_Result - fi - mkdir preprocess_Result - if [ -d label ]; then - rm -rf ./label - fi - mkdir label - python ../preprocess.py --dataset_path=$dataset_path --result_dir=./preprocess_Result/ --label_dir=./label/ -} - -function compile_app() -{ - cd ../cpp_infer || exit - bash build.sh &> build.log -} - -function infer() -{ - cd - || exit - if [ -d result_Files ]; then - rm -rf ./result_Files - fi - if [ -d time_Result ]; then - rm -rf ./time_Result - fi - mkdir result_Files - mkdir time_Result - - ../cpp_infer/out/main --device_type=$device_type --mindir_path=$model --input0_path=./preprocess_Result --device_id=$device_id &> infer.log - -} - -function cal_acc() -{ - python ../postprocess.py --data_set=$dataset_path --result_dir=./result_Files --label_dir=./label/issame_list.npy &> acc.log -} - -if [ $need_preprocess == "y" ]; then - preprocess_data - if [ $? -ne 0 ]; then - echo "preprocess dataset failed" - exit 1 - fi -fi -compile_app -if [ $? -ne 0 ]; then - echo "compile app code failed" - exit 1 -fi -infer -if [ $? -ne 0 ]; then - echo " execute inference failed" - exit 1 -fi -cal_acc -if [ $? -ne 0 ]; then - echo "calculate accuracy failed" - exit 1 -fi diff --git a/official/cv/Arcface/scripts/run_distribute_train.sh b/official/cv/Arcface/scripts/run_distribute_train.sh deleted file mode 100644 index c5e025b968cc51a7cb94ad6e89ab579d1b12484b..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/scripts/run_distribute_train.sh +++ /dev/null @@ -1,63 +0,0 @@ -#!/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. -# ============================================================================ - -echo "==============================================================================================================" -echo "Please run the script as: " -echo "bash run.sh RANK_SIZE DATA_PATH" -echo "For example: bash run.sh 8 path/dataset" -echo "It is better to use the absolute path." -echo "==============================================================================================================" - -RANK_SIZE=$1 -DATA_PATH=$2 - -EXEC_PATH=$(pwd) -echo "$EXEC_PATH" - -test_dist_8pcs() -{ - export RANK_TABLE_FILE=${EXEC_PATH}/rank_table_8pcs.json - export RANK_SIZE=8 -} - -test_dist_2pcs() -{ - export RANK_TABLE_FILE=${EXEC_PATH}/rank_table_2pcs.json - echo "$RANK_TABLE_FILE" - export RANK_SIZE=2 -} - -test_dist_${RANK_SIZE}pcs - -for((i=0;i env$i.log - python train.py \ - --data_url $DATA_PATH \ - --device_num $RANK_SIZE \ - > train.log$i 2>&1 & - cd ../ -done -echo "finish" -cd ../ diff --git a/official/cv/Arcface/scripts/run_distribute_train_gpu.sh b/official/cv/Arcface/scripts/run_distribute_train_gpu.sh deleted file mode 100644 index ab30ccd389f8416e6bf70960eb4f256ce8c8415a..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/scripts/run_distribute_train_gpu.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/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. -# ============================================================================ - -echo "==============================================================================================================" -echo "Please run the script as: " -echo "bash run.sh DATA_PATH RANK_SIZE" -echo "For example: bash run.sh /path/dataset 8" -echo "It is better to use the absolute path." -echo "==============================================================================================================" -set -e - -export DEVICE_NUM=$2 -export RANK_SIZE=$2 -export DATASET_NAME=$1 -export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python - -rm -rf ./train_parallel -mkdir ./train_parallel -cp -r ./src/ ./train_parallel -# shellcheck disable=SC2035 -cp *.py ./train_parallel -cd ./train_parallel -env > env.log -echo "start training" - mpirun -n $2 --allow-run-as-root --output-filename log_output --merge-stderr-to-stdout \ - python train.py --device_num $2 --device_target GPU --data_url $1 \ - > train.log 2>&1 & diff --git a/official/cv/Arcface/scripts/run_eval.sh b/official/cv/Arcface/scripts/run_eval.sh deleted file mode 100644 index ad4c8100c27980797385679be80b8ba4464bd91a..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/scripts/run_eval.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/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. -# ============================================================================ - -echo "==============================================================================================================" -echo "Please run the script as: " -echo "bash run.sh EVAL_PATH CKPT_PATH" -echo "For example: bash run.sh path/evalset path/ckpt" -echo "It is better to use the absolute path." -echo "==============================================================================================================" - -EVAL_PATH=$1 -CKPT_PATH=$2 - -python val.py \ ---ckpt_url "$CKPT_PATH" \ ---device_id 1 \ ---eval_url "$EVAL_PATH" \ ---target lfw,cfp_fp,agedb_30,calfw,cplfw \ -> eval.log 2>&1 & diff --git a/official/cv/Arcface/scripts/run_eval_gpu.sh b/official/cv/Arcface/scripts/run_eval_gpu.sh deleted file mode 100644 index c2a42d36ab8d20150bc37a7403f01f5d0c2a47c5..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/scripts/run_eval_gpu.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/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. -# ============================================================================ - -echo "==============================================================================================================" -echo "Please run the script as: " -echo "bash run.sh EVAL_PATH CKPT_PATH" -echo "For example: bash run.sh path/evalset path/ckpt" -echo "It is better to use the absolute path." -echo "==============================================================================================================" - -EVAL_PATH=$1 -CKPT_PATH=$2 - -python val.py \ ---ckpt_url "$CKPT_PATH" \ ---device_id 1 \ ---eval_url "$EVAL_PATH" \ ---device_target "GPU" \ ---target lfw,cfp_fp,agedb_30,calfw,cplfw \ -> eval.log 2>&1 & diff --git a/official/cv/Arcface/scripts/run_eval_ijbc.sh b/official/cv/Arcface/scripts/run_eval_ijbc.sh deleted file mode 100644 index c38d2d2b9ae09e097feba558d074150e07dc6403..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/scripts/run_eval_ijbc.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/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. -# ============================================================================ -echo "==============================================================================================================" -echo "Please run the script as: " -echo "bash run.sh EVAL_PATH CKPT_PATH" -echo "For example: bash run.sh path/evalset path/ckpt" -echo "It is better to use the absolute path." -echo "==============================================================================================================" - -EVAL_PATH=$1 -CKPT_PATH=$2 - -python eval_ijbc.py \ ---model-prefix "$CKPT_PATH" \ ---image-path "$EVAL_PATH" \ ---result-dir ms1mv2_arcface_r100 \ ---batch-size 128 \ ---job ms1mv2_arcface_r100 \ ---target IJBC \ ---network iresnet100 \ -> eval.log 2>&1 & diff --git a/official/cv/Arcface/scripts/run_eval_ijbc_gpu.sh b/official/cv/Arcface/scripts/run_eval_ijbc_gpu.sh deleted file mode 100644 index c38d2d2b9ae09e097feba558d074150e07dc6403..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/scripts/run_eval_ijbc_gpu.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/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. -# ============================================================================ -echo "==============================================================================================================" -echo "Please run the script as: " -echo "bash run.sh EVAL_PATH CKPT_PATH" -echo "For example: bash run.sh path/evalset path/ckpt" -echo "It is better to use the absolute path." -echo "==============================================================================================================" - -EVAL_PATH=$1 -CKPT_PATH=$2 - -python eval_ijbc.py \ ---model-prefix "$CKPT_PATH" \ ---image-path "$EVAL_PATH" \ ---result-dir ms1mv2_arcface_r100 \ ---batch-size 128 \ ---job ms1mv2_arcface_r100 \ ---target IJBC \ ---network iresnet100 \ -> eval.log 2>&1 & diff --git a/official/cv/Arcface/scripts/run_eval_ijbc_onnx.sh b/official/cv/Arcface/scripts/run_eval_ijbc_onnx.sh deleted file mode 100644 index 26194892ada9f844d40bda358f8807d7f68b0d96..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/scripts/run_eval_ijbc_onnx.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/bash -# Copyright 2022 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. -# ============================================================================ - -if [ $# -ne 4 ] -then - echo "==============================================================================================================" - echo "Please run the script as: " - echo "bash run_eval_ijbc_onnx.sh EVAL_PATH ONNX_BS_PATH ONNX_RS_PATH TARGET" - echo "For example: bash run_eval_ijbc_onnx.sh path/evalset path/onnx_bs path/onnx_rs IJBC" - echo "It is better to use the absolute path." - echo "==============================================================================================================" -exit 1 -fi - -EVAL_PATH=$1 -ONNX_BS_PATH=$2 -ONNX_RS_PATH=$3 -TARGET=$4 - -nohup python ../eval_ijbc_onnx.py \ ---image-path "$EVAL_PATH" \ ---onnx_bs_dir "$ONNX_BS_PATH" \ ---onnx_rs_dir "$ONNX_RS_PATH" \ ---batch-size 128 \ ---result-dir onnx_ms1mv2_arcface_r100 \ ---job onnx_ms1mv2_arcface_r100 \ ---target $TARGET \ -> eval_onnx_$TARGET.log 2>&1 & diff --git a/official/cv/Arcface/scripts/run_eval_onnx.sh b/official/cv/Arcface/scripts/run_eval_onnx.sh deleted file mode 100644 index eac873d610ab69a3798e127abdb31cef4ebc3892..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/scripts/run_eval_onnx.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash -# Copyright 2022 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. -# ============================================================================ - -if [ $# -ne 2 ] -then - echo "==============================================================================================================" - echo "Please run the script as: " - echo "bash run_eval_onnx.sh EVAL_PATH ONNX_PATH" - echo "For example: bash run_eval_onnx.sh path/evalset path/onnx" - echo "It is better to use the absolute path." - echo "==============================================================================================================" -exit 1 -fi - -EVAL_PATH=$1 -ONNX_PATH=$2 - -nohup python ../eval_onnx.py \ ---onnx_url "$ONNX_PATH" \ ---eval_url "$EVAL_PATH" \ ---target lfw,cfp_fp,agedb_30,calfw,cplfw \ -> eval_onnx.log 2>&1 & diff --git a/official/cv/Arcface/scripts/run_standalone_train.sh b/official/cv/Arcface/scripts/run_standalone_train.sh deleted file mode 100644 index dde96e714ab0dbcdef66494659979687f0c1aa11..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/scripts/run_standalone_train.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/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. -# ============================================================================ - -echo "==============================================================================================================" -echo "Please run the script as: " -echo "bash run.sh DATA_PATH" -echo "For example: bash run.sh path/MS1M DEVICE_ID" -echo "It is better to use the absolute path." -echo "==============================================================================================================" - -# shellcheck disable=SC2034 -DATA_PATH=$1 -export DEVICE_ID=$2 -python train.py \ ---data_url $DATA_PATH \ ---device_num 1 \ -> train.log 2>&1 & \ No newline at end of file diff --git a/official/cv/Arcface/scripts/run_standalone_train_gpu.sh b/official/cv/Arcface/scripts/run_standalone_train_gpu.sh deleted file mode 100644 index 3910e238d3956559eefc6cdd7defbe126f24c4a3..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/scripts/run_standalone_train_gpu.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/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. -# ============================================================================ - -echo "==============================================================================================================" -echo "Please run the script as: " -echo "bash run.sh DATA_PATH" -echo "For example: bash run.sh /path/dataset 0" -echo "It is better to use the absolute path." -echo "==============================================================================================================" -EXE_PATH=$(pwd) -DATA_PATH=$1 -export DEVICE_ID=$2 - -python train.py \ - --epochs 25 \ - --train_url "$EXE_PATH" \ - --data_url "$DATA_PATH" \ - --device_target "GPU" \ - --device_num 1 \ - > train.log 2>&1 & -echo "start training" -cd ../ diff --git a/official/cv/Arcface/src/dataset.py b/official/cv/Arcface/src/dataset.py deleted file mode 100644 index 20c870a4d4b753378f6e1ac2ccf6c0d8c03f5ae2..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/src/dataset.py +++ /dev/null @@ -1,99 +0,0 @@ -# Copyright 2021-2022 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. -# ============================================================================ -""" -python dataset.py -""" -import os -import mindspore.common.dtype as mstype -import mindspore.dataset.engine as de -import mindspore.dataset.vision as C -import mindspore.dataset.transforms as C2 - - -def create_dataset(dataset_path, do_train, repeat_num=1, batch_size=32, target="Ascend"): - """ - create a train dataset - - Args: - dataset_path(string): the path of dataset. - do_train(bool): whether dataset is used for train or eval. - repeat_num(int): the repeat times of dataset. Default: 1 - batch_size(int): the batch size of dataset. Default: 32 - target(str): the device target. Default: Ascend - - Returns: - dataset - """ - device_num, rank_id = _get_rank_info() - - if device_num == 1: - ds = de.ImageFolderDataset( - dataset_path, num_parallel_workers=8, shuffle=True) - else: - ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=8, shuffle=True, - num_shards=device_num, shard_id=rank_id) - - image_size = 112 - mean = [0.5 * 255, 0.5 * 255, 0.5 * 255] - std = [0.5 * 255, 0.5 * 255, 0.5 * 255] - - # define map operations - if do_train: - trans = [ - # C.RandomCropDecodeResize(image_size, scale=(0.08, 1.0), ratio=(0.75, 1.333)), - C.Decode(), - C.RandomHorizontalFlip(prob=0.5), - C.Normalize(mean=mean, std=std), - C.HWC2CHW() - ] - else: - trans = [ - C.Decode(), - C.Resize(256), - C.CenterCrop(image_size), - C.Normalize(mean=mean, std=std), - C.HWC2CHW() - ] - - type_cast_op = C2.TypeCast(mstype.int32) - - ds = ds.map(input_columns="image", - num_parallel_workers=8, operations=trans) - ds = ds.map(input_columns="label", num_parallel_workers=8, - operations=type_cast_op) - - # apply batch operations - ds = ds.batch(batch_size, drop_remainder=True) - - # apply dataset repeat operation - ds = ds.repeat(repeat_num) - - return ds - - -def _get_rank_info(): - """ - get rank size and rank id - """ - rank_size = int(os.environ.get("RANK_SIZE", 1)) - - if rank_size > 1: - rank_size = int(os.environ.get("RANK_SIZE")) - rank_id = int(os.environ.get("RANK_ID")) - else: - rank_size = 1 - rank_id = 0 - - return rank_size, rank_id diff --git a/official/cv/Arcface/src/iresnet.py b/official/cv/Arcface/src/iresnet.py deleted file mode 100644 index 99b8587d3a5b508d59e41953cdb68a38887ee2ea..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/src/iresnet.py +++ /dev/null @@ -1,233 +0,0 @@ -# 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. -# ============================================================================ -""" -python iresnet.py -""" -from mindspore import nn -import mindspore.ops as ops - -__all__ = ['iresnet18', 'iresnet34', 'iresnet50', 'iresnet100'] - - -def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1): - """ - 3x3 convolution with padding - - # noqa: DAR201 - """ - return nn.Conv2d(in_planes, - out_planes, - kernel_size=3, - stride=stride, - padding=dilation, - pad_mode='pad', - group=groups, - has_bias=False, - dilation=dilation) - - -def conv1x1(in_planes, out_planes, stride=1): - """ - 1x1 convolution - - # noqa: DAR201 - """ - return nn.Conv2d(in_planes, - out_planes, - kernel_size=1, - stride=stride, - has_bias=False) - - -class IBasicBlock(nn.Cell): - '''IBasicBlock - ''' - expansion = 1 - - def __init__(self, inplanes, planes, stride=1, downsample=None, - groups=1, base_width=64, dilation=1): - super(IBasicBlock, self).__init__() - if groups != 1 or base_width != 64: - raise ValueError( - 'BasicBlock only supports groups=1 and base_width=64') - if dilation > 1: - raise NotImplementedError( - "Dilation > 1 not supported in BasicBlock") - - self.bn1 = nn.BatchNorm2d( - inplanes, - eps=1e-05, - ) - self.conv1 = conv3x3(inplanes, planes) - self.bn2 = nn.BatchNorm2d( - planes, - eps=1e-05, - ) - self.prelu = nn.PReLU(planes) - self.conv2 = conv3x3(planes, planes, stride) - self.bn3 = nn.BatchNorm2d( - planes, - eps=1e-05, - ) - self.downsample = downsample - self.stride = stride - - def construct(self, x): - '''construct - - # noqa: DAR201 - ''' - identity = x - - out = self.bn1(x) - out = self.conv1(out) - out = self.bn2(out) - out = self.prelu(out) - out = self.conv2(out) - out = self.bn3(out) - - if self.downsample is not None: - identity = self.downsample(x) - - out += identity - - return out - - -class IResNet(nn.Cell): - '''IResNet - ''' - fc_scale = 7 * 7 - - def __init__(self, - block, layers, dropout=0, num_features=512, zero_init_residual=False, - groups=1, width_per_group=64, replace_stride_with_dilation=None): - super(IResNet, self).__init__() - self.inplanes = 64 - self.dilation = 1 - if replace_stride_with_dilation is None: - replace_stride_with_dilation = [False, False, False] - if len(replace_stride_with_dilation) != 3: - raise ValueError("replace_stride_with_dilation should be None " - "or a 3-element tuple, got {}".format(replace_stride_with_dilation)) - self.groups = groups - self.base_width = width_per_group - self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=3, - stride=1, padding=1, pad_mode='pad', has_bias=False) - self.bn1 = nn.BatchNorm2d(self.inplanes, eps=1e-05) - self.prelu = nn.PReLU(self.inplanes) - self.layer1 = self._make_layer(block, 64, layers[0], stride=2) - self.layer2 = self._make_layer(block, - 128, - layers[1], - stride=2, - dilate=replace_stride_with_dilation[0]) - self.layer3 = self._make_layer(block, - 256, - layers[2], - stride=2, - dilate=replace_stride_with_dilation[1]) - self.layer4 = self._make_layer(block, - 512, - layers[3], - stride=2, - dilate=replace_stride_with_dilation[2]) - self.bn2 = nn.BatchNorm2d(512 * block.expansion, eps=1e-05,) - self.dropout = nn.Dropout(p=dropout) - self.fc = nn.Dense(512 * block.expansion * self.fc_scale, - num_features) - self.features = nn.BatchNorm1d(num_features, eps=1e-05) - self.features.gamma.requires_grad = False - self.reshape = ops.Reshape() - self.flatten = ops.Flatten() - - def _make_layer(self, block, planes, blocks, stride=1, dilate=False): - '''make_layer - ''' - downsample = None - previous_dilation = self.dilation - if dilate: - self.dilation *= stride - stride = 1 - if stride != 1 or self.inplanes != planes * block.expansion: - downsample = nn.SequentialCell([ - conv1x1(self.inplanes, planes * block.expansion, stride), - nn.BatchNorm2d(planes * block.expansion, eps=1e-05) - ]) - layers = [] - layers.append( - block(self.inplanes, planes, stride, downsample, self.groups, - self.base_width, previous_dilation)) - self.inplanes = planes * block.expansion - for _ in range(1, blocks): - layers.append( - block(self.inplanes, - planes, - groups=self.groups, - base_width=self.base_width, - dilation=self.dilation)) - - return nn.SequentialCell(layers) - - def construct(self, x): - '''construct - - # noqa: DAR201 - ''' - x = self.conv1(x) - x = self.bn1(x) - x = self.prelu(x) - - x = self.layer1(x) - x = self.layer2(x) - x = self.layer3(x) - x = self.layer4(x) - - x = self.bn2(x) - x = self.flatten(x) - # b, c, _, _ = x.shape - # x = self.reshape(x, (b, -1)) - x = self.dropout(x) - x = self.fc(x) - x = self.features(x) - - return x - - -def _iresnet(arch, block, layers, pretrained, progress, **kwargs): - model = IResNet(block, layers, **kwargs) - if pretrained: - raise ValueError() - return model - - -def iresnet18(pretrained=False, progress=True, **kwargs): - return _iresnet('iresnet18', IBasicBlock, [2, 2, 2, 2], pretrained, - progress, **kwargs) - - -def iresnet34(pretrained=False, progress=True, **kwargs): - return _iresnet('iresnet34', IBasicBlock, [3, 4, 6, 3], pretrained, - progress, **kwargs) - - -def iresnet50(pretrained=False, progress=True, **kwargs): - return _iresnet('iresnet50', IBasicBlock, [3, 4, 14, 3], pretrained, - progress, **kwargs) - - -def iresnet100(pretrained=False, progress=True, **kwargs): - return _iresnet('iresnet100', IBasicBlock, [3, 13, 30, 3], pretrained, - progress, **kwargs) diff --git a/official/cv/Arcface/src/loss.py b/official/cv/Arcface/src/loss.py deleted file mode 100644 index 99e72113158c2f1ec550cc9810c9d1c1906905c9..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/src/loss.py +++ /dev/null @@ -1,123 +0,0 @@ -# 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. -# ============================================================================ -""" -python loss.py -""" -from mindspore import Tensor -import mindspore.nn as nn -from mindspore import Parameter -import mindspore.ops as ops -from mindspore import dtype as mstype -from mindspore.ops import functional as F -from mindspore.common.initializer import initializer - - -class ArcFace(nn.Cell): - ''' - Arcface loss - ''' - def __init__(self, world_size, s=64.0, m=0.5): - super(ArcFace, self).__init__() - self.s = s - self.shape = ops.Shape() - self.mul = ops.Mul() - self.cos = ops.Cos() - self.acos = ops.ACos() - self.onehot = ops.OneHot().shard(((1, world_size), (), ())) - # self.tile = ops.Tile().shard(((8, 1),)) - self.on_value = Tensor(m, mstype.float32) - self.off_value = Tensor(0.0, mstype.float32) - - def construct(self, cosine, label): - m_hot = self.onehot(label, self.shape( - cosine)[1], self.on_value, self.off_value) - - cosine = self.acos(cosine) - cosine += m_hot - cosine = self.cos(cosine) - cosine = self.mul(cosine, self.s) - return cosine - - -class SoftMaxCE(nn.Cell): - ''' - softmax cross entrophy - ''' - def __init__(self, world_size): - super(SoftMaxCE, self).__init__() - self.max = ops.ReduceMax(keep_dims=True) - self.sum = ops.ReduceSum(keep_dims=True) - self.mean = ops.ReduceMean(keep_dims=False) - self.exp = ops.Exp() - self.div = ops.Div() - self.onehot = ops.OneHot().shard(((1, world_size), (), ())) - self.mul = ops.Mul() - self.log = ops.Log() - self.onvalue = Tensor(1.0, mstype.float32) - self.offvalue = Tensor(0.0, mstype.float32) - self.eps = Tensor(1e-30, mstype.float32) - - def construct(self, logits, total_label): - '''construct - ''' - max_fc = self.max(logits, 1) - - logits_exp = self.exp(logits - max_fc) - logits_sum_exp = self.sum(logits_exp, 1) - - logits_exp = self.div(logits_exp, logits_sum_exp) - - label = self.onehot(total_label, F.shape( - logits)[1], self.onvalue, self.offvalue) - - softmax_result_log = self.log(logits_exp + self.eps) - loss = self.sum((self.mul(softmax_result_log, label)), -1) - loss = self.mul(ops.scalar_to_tensor(-1.0), loss) - loss_v = self.mean(loss, 0) - - return loss_v - - -class PartialFC(nn.Cell): - '''partialFC - ''' - def __init__(self, num_classes, world_size): - super(PartialFC, self).__init__() - self.L2Norm = ops.L2Normalize(axis=1) - self.weight = Parameter(initializer( - "normal", (num_classes, 512)), name="mp_weight") - self.sub_weight = self.weight - self.linear = ops.MatMul(transpose_b=True).shard( - ((1, 1), (world_size, 1))) - self.margin_softmax = ArcFace(world_size=world_size) - self.loss = SoftMaxCE(world_size=world_size) - - def construct(self, features, label): - total_label, norm_weight = self.prepare(label) - total_features = self.L2Norm(features) - logits = self.forward(total_features, norm_weight) - logits = self.margin_softmax(logits, total_label) - loss_v = self.loss(logits, total_label) - return loss_v - - def forward(self, total_features, norm_weight): - logits = self.linear(F.cast(total_features, mstype.float16), F.cast( - norm_weight, mstype.float16)) - return F.cast(logits, mstype.float32) - - def prepare(self, label): - total_label = label - norm_weight = self.L2Norm(self.sub_weight) - return total_label, norm_weight diff --git a/official/cv/Arcface/src/rec2jpg_dataset.py b/official/cv/Arcface/src/rec2jpg_dataset.py deleted file mode 100644 index 51cb8a0dfe99e6a7b95fed33c96a93abbffde3bc..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/src/rec2jpg_dataset.py +++ /dev/null @@ -1,57 +0,0 @@ -# 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. -# ============================================================================ -""" -rec format to jpg -""" -import os -import argparse -from skimage import io -import mxnet as mx -from mxnet import recordio -from tqdm import tqdm - - -def main(dataset_path, output_dir): - path_imgrec = os.path.join(dataset_path, 'train.rec') - path_imgidx = os.path.join(dataset_path, 'train.idx') - if not os.path.exists(output_dir): - os.mkdir(output_dir) - - imgrec = recordio.MXIndexedRecordIO(path_imgidx, path_imgrec, 'r') - img_info = imgrec.read_idx(0) - header, _ = mx.recordio.unpack(img_info) - max_idx = int(header.label[0]) - print('max_idx:', max_idx) - for i in tqdm(range(max_idx)): - header, s = recordio.unpack(imgrec.read_idx(i + 1)) - img = mx.image.imdecode(s).asnumpy() - label = str(header.label) - ids = str(i) - - label_dir = os.path.join(output_dir, label) - if not os.path.exists(label_dir): - os.mkdir(label_dir) - fname = 'Figure_{}.png'.format(ids) - fpath = os.path.join(label_dir, fname) - io.imsave(fpath, img) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser(description='do dataset merge') - # general - parser.add_argument('--include', default='', type=str, help='') - parser.add_argument('--output', default='', type=str, help='') - args = parser.parse_args() - main(args.include, args.output) diff --git a/official/cv/Arcface/train.py b/official/cv/Arcface/train.py deleted file mode 100644 index 0dda876a6be0b23bd5068782b2132d0889537564..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/train.py +++ /dev/null @@ -1,172 +0,0 @@ -# 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. -# ============================================================================ -""" -python train.py -""" -import argparse -import os -import numpy as np - -import mindspore -import mindspore.nn as nn -from mindspore import Tensor -import mindspore.ops as ops -from mindspore.train.model import Model, ParallelMode -from mindspore import dtype as mstype -from mindspore.train.callback import ModelCheckpoint, CheckpointConfig, LossMonitor, TimeMonitor -from mindspore.communication.management import init, get_rank -from mindspore.parallel import _cost_model_context as cost_model_context -from mindspore.parallel import set_algo_parameters - -from src.dataset import create_dataset -from src.iresnet import iresnet100 -from src.loss import PartialFC -mindspore.common.set_seed(1024) -parser = argparse.ArgumentParser(description='Training') - -# Datasets -parser.add_argument('--train_url', default='.', type=str, - help='output path') -parser.add_argument('--data_url', default='data path', type=str) -# Optimization options -parser.add_argument('--epochs', default=25, type=int, metavar='N', - help='number of total epochs to run') -parser.add_argument('--num_classes', default=85742, type=int, metavar='N', - help='num of classes') -parser.add_argument('--batch_size', default=64, type=int, metavar='N', - help='train batchsize (default: 256)') -parser.add_argument('--lr', '--learning-rate', default=0.08, type=float, - metavar='LR', help='initial learning rate') -parser.add_argument('--schedule', type=int, nargs='+', default=[10, 16, 21], - help='Decrease learning rate at these epochs.') -parser.add_argument('--gamma', type=float, default=0.1, - help='LR is multiplied by gamma on schedule.') -parser.add_argument('--momentum', default=0.9, type=float, metavar='M', - help='momentum') -parser.add_argument('--weight-decay', '--wd', default=1e-4, type=float, - metavar='W', help='weight decay (default: 1e-4)') -# Device options -parser.add_argument('--device_target', type=str, - default='Ascend', choices=['GPU', 'Ascend']) -parser.add_argument('--device_num', type=int, default=8) -parser.add_argument('--device_id', type=int, default=0) -parser.add_argument('--modelarts', action="store_true", help="using modelarts") - -args = parser.parse_args() - - -def lr_generator(lr_init, total_epochs, steps_per_epoch): - """lr_generator - """ - lr_each_step = [] - for i in range(total_epochs): - if i in args.schedule: - lr_init *= args.gamma - for _ in range(steps_per_epoch): - lr_each_step.append(lr_init) - lr_each_step = np.array(lr_each_step).astype(np.float32) - return Tensor(lr_each_step) - - -class MyNetWithLoss(nn.Cell): - """ - WithLossCell - """ - def __init__(self, backbone, cfg): - super(MyNetWithLoss, self).__init__(auto_prefix=False) - self._backbone = backbone.to_float(mstype.float16) - self._loss_fn = PartialFC(num_classes=cfg.num_classes, - world_size=cfg.device_num).to_float(mstype.float32) - self.L2Norm = ops.L2Normalize(axis=1) - - def construct(self, data, label): - out = self._backbone(data) - loss = self._loss_fn(out, label) - return loss - - -if __name__ == "__main__": - train_epoch = args.epochs - target = args.device_target - mindspore.set_context(mode=0, - device_target=target, save_graphs=False) - device_id = args.device_id - if args.device_num > 1: - if target == 'Ascend': - device_id = int(os.getenv('DEVICE_ID')) - mindspore.set_context(device_id=device_id) - mindspore.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL, - gradients_mean=True, - ) - cost_model_context.set_cost_model_context(device_memory_capacity=32.0 * 1024.0 * 1024.0 * 1024.0, - costmodel_gamma=0.001, - costmodel_beta=280.0) - set_algo_parameters(elementwise_op_strategy_follow=True) - init() - elif target == 'GPU': - init() - mindspore.set_auto_parallel_context(device_num=args.device_num, - parallel_mode=ParallelMode.DATA_PARALLEL, - gradients_mean=True, - auto_parallel_search_mode="recursive_programming") - else: - device_id = int(os.getenv('DEVICE_ID')) - - if args.modelarts: - import moxing as mox - - mox.file.copy_parallel( - src_url=args.data_url, dst_url='/cache/data_path_' + os.getenv('DEVICE_ID')) - zip_command = "unzip -o -q /cache/data_path_" + os.getenv('DEVICE_ID') \ - + "/MS1M.zip -d /cache/data_path_" + \ - os.getenv('DEVICE_ID') - os.system(zip_command) - train_dataset = create_dataset(dataset_path='/cache/data_path_' + os.getenv('DEVICE_ID') + '/MS1M/', - do_train=True, - repeat_num=1, batch_size=args.batch_size, target=target) - else: - train_dataset = create_dataset(dataset_path=args.data_url, do_train=True, - repeat_num=1, batch_size=args.batch_size, target=target) - step = train_dataset.get_dataset_size() - lr = lr_generator(args.lr, train_epoch, steps_per_epoch=step) - net = iresnet100() - train_net = MyNetWithLoss(net, args) - optimizer = nn.SGD(params=train_net.trainable_params(), learning_rate=lr / 512 * args.batch_size * args.device_num, - momentum=args.momentum, weight_decay=args.weight_decay) - - model = Model(train_net, optimizer=optimizer) - - config_ck = CheckpointConfig( - save_checkpoint_steps=60, keep_checkpoint_max=20) - if args.modelarts: - ckpt_cb = ModelCheckpoint(prefix="ArcFace-", config=config_ck, - directory='/cache/train_output/') - else: - ckpt_cb = ModelCheckpoint(prefix="ArcFace-", config=config_ck, - directory=args.train_url) - time_cb = TimeMonitor(data_size=train_dataset.get_dataset_size()) - loss_cb = LossMonitor() - cb = [ckpt_cb, time_cb, loss_cb] - if args.device_num == 1: - model.train(train_epoch, train_dataset, - callbacks=cb, dataset_sink_mode=True) - elif args.device_num > 1 and get_rank() % 8 == 0: - model.train(train_epoch, train_dataset, - callbacks=cb, dataset_sink_mode=True) - else: - model.train(train_epoch, train_dataset, dataset_sink_mode=True) - if args.modelarts: - mox.file.copy_parallel( - src_url='/cache/train_output', dst_url=args.train_url) diff --git a/official/cv/Arcface/val.py b/official/cv/Arcface/val.py deleted file mode 100644 index f2d4b010b85294702aa8ef1584986200be9d37b0..0000000000000000000000000000000000000000 --- a/official/cv/Arcface/val.py +++ /dev/null @@ -1,339 +0,0 @@ -# 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. -# ============================================================================ -''' -evaluation of lfw, calfw, cfp_fp, agedb_30, cplfw -''' - -import datetime -import os -import pickle -import argparse -from io import BytesIO - -import numpy as np -import sklearn -from sklearn.decomposition import PCA -from sklearn.model_selection import KFold -import matplotlib.pyplot as plt -from scipy import interpolate -import mindspore -from mindspore.train.serialization import load_checkpoint, load_param_into_net - -from src.iresnet import iresnet100 - - -class LFold: - ''' - LFold - ''' - def __init__(self, n_splits=2, shuffle=False): - self.n_splits = n_splits - if self.n_splits > 1: - self.k_fold = KFold(n_splits=n_splits, shuffle=shuffle) - - def split(self, indices): - if self.n_splits > 1: - return self.k_fold.split(indices) - return [(indices, indices)] - - -def calculate_roc(thresholds, - embeddings1, - embeddings2, - actual_issame, - nrof_folds=10, - pca=0): - ''' - calculate_roc - ''' - assert embeddings1.shape[0] == embeddings2.shape[0] - assert embeddings1.shape[1] == embeddings2.shape[1] - nrof_pairs = min(len(actual_issame), embeddings1.shape[0]) - nrof_thresholds = len(thresholds) - k_fold = LFold(n_splits=nrof_folds, shuffle=False) - - tprs = np.zeros((nrof_folds, nrof_thresholds)) - fprs = np.zeros((nrof_folds, nrof_thresholds)) - accuracy = np.zeros((nrof_folds)) - indices = np.arange(nrof_pairs) - - if pca == 0: - diff = np.subtract(embeddings1, embeddings2) - dist = np.sum(np.square(diff), 1) - - for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)): - if pca > 0: - print('doing pca on', fold_idx) - embed1_train = embeddings1[train_set] - embed2_train = embeddings2[train_set] - _embed_train = np.concatenate((embed1_train, embed2_train), axis=0) - pca_model = PCA(n_components=pca) - pca_model.fit(_embed_train) - embed1 = pca_model.transform(embeddings1) - embed2 = pca_model.transform(embeddings2) - embed1 = sklearn.preprocessing.normalize(embed1) - embed2 = sklearn.preprocessing.normalize(embed2) - diff = np.subtract(embed1, embed2) - dist = np.sum(np.square(diff), 1) - - # Find the best threshold for the fold - acc_train = np.zeros((nrof_thresholds)) - for threshold_idx, threshold in enumerate(thresholds): - _, _, acc_train[threshold_idx] = calculate_accuracy( - threshold, dist[train_set], actual_issame[train_set]) - best_threshold_index = np.argmax(acc_train) - for threshold_idx, threshold in enumerate(thresholds): - tprs[fold_idx, threshold_idx], fprs[fold_idx, threshold_idx], _ = calculate_accuracy( - threshold, dist[test_set], - actual_issame[test_set]) - _, _, accuracy[fold_idx] = calculate_accuracy( - thresholds[best_threshold_index], dist[test_set], - actual_issame[test_set]) - - tpr = np.mean(tprs, 0) - fpr = np.mean(fprs, 0) - return tpr, fpr, accuracy - - -def calculate_accuracy(threshold, dist, actual_issame): - '''calculate_acc - ''' - predict_issame = np.less(dist, threshold) - tp = np.sum(np.logical_and(predict_issame, actual_issame)) - fp = np.sum(np.logical_and(predict_issame, np.logical_not(actual_issame))) - tn = np.sum( - np.logical_and(np.logical_not(predict_issame), - np.logical_not(actual_issame))) - fn = np.sum(np.logical_and(np.logical_not(predict_issame), actual_issame)) - - tpr = 0 if (tp + fn == 0) else float(tp) / float(tp + fn) - fpr = 0 if (fp + tn == 0) else float(fp) / float(fp + tn) - acc = float(tp + tn) / dist.size - return tpr, fpr, acc - - -def calculate_val(thresholds, - embeddings1, - embeddings2, - actual_issame, - far_target, - nrof_folds=10): - ''' - calculate_val - ''' - assert embeddings1.shape[0] == embeddings2.shape[0] - assert embeddings1.shape[1] == embeddings2.shape[1] - nrof_pairs = min(len(actual_issame), embeddings1.shape[0]) - nrof_thresholds = len(thresholds) - k_fold = LFold(n_splits=nrof_folds, shuffle=False) - - val = np.zeros(nrof_folds) - far = np.zeros(nrof_folds) - - diff = np.subtract(embeddings1, embeddings2) - dist = np.sum(np.square(diff), 1) - indices = np.arange(nrof_pairs) - - for fold_idx, (train_set, test_set) in enumerate(k_fold.split(indices)): - - # Find the threshold that gives FAR = far_target - far_train = np.zeros(nrof_thresholds) - for threshold_idx, threshold in enumerate(thresholds): - _, far_train[threshold_idx] = calculate_val_far( - threshold, dist[train_set], actual_issame[train_set]) - if np.max(far_train) >= far_target: - f = interpolate.interp1d(far_train, thresholds, kind='slinear') - threshold = f(far_target) - else: - threshold = 0.0 - - val[fold_idx], far[fold_idx] = calculate_val_far( - threshold, dist[test_set], actual_issame[test_set]) - - val_mean = np.mean(val) - far_mean = np.mean(far) - val_std = np.std(val) - return val_mean, val_std, far_mean - - -def calculate_val_far(threshold, dist, actual_issame): - '''calculate_val_far - ''' - predict_issame = np.less(dist, threshold) - true_accept = np.sum(np.logical_and(predict_issame, actual_issame)) - false_accept = np.sum( - np.logical_and(predict_issame, np.logical_not(actual_issame))) - n_same = np.sum(actual_issame) - n_diff = np.sum(np.logical_not(actual_issame)) - val = float(true_accept) / float(n_same) - far = float(false_accept) / float(n_diff) - return val, far - - -def evaluate(embeddings, actual_issame, nrof_folds=10, pca=0): - '''evaluate - ''' - # Calculate evaluation metrics - thresholds = np.arange(0, 4, 0.01) - embeddings1 = embeddings[0::2] - embeddings2 = embeddings[1::2] - tpr, fpr, accuracy = calculate_roc(thresholds, - embeddings1, - embeddings2, - np.asarray(actual_issame), - nrof_folds=nrof_folds, - pca=pca) - thresholds = np.arange(0, 4, 0.001) - val, val_std, far = calculate_val(thresholds, - embeddings1, - embeddings2, - np.asarray(actual_issame), - 1e-3, - nrof_folds=nrof_folds) - return tpr, fpr, accuracy, val, val_std, far - - -def load_bin(path, image_size): - '''load evalset of .bin - ''' - try: - with open(path, 'rb') as f: - bins, issame_list = pickle.load(f) # py2 - except UnicodeDecodeError as _: - with open(path, 'rb') as f: - bins, issame_list = pickle.load(f, encoding='bytes') # py3 - data_list = [] - for _ in [0, 1]: - data = np.zeros( - (len(issame_list) * 2, 3, image_size[0], image_size[1])) - data_list.append(data) - for idx in range(len(issame_list) * 2): - _bin = bins[idx] - img = plt.imread(BytesIO(_bin), "jpg") - if img.shape[1] != image_size[0]: - img = mx.image.resize_short(img, image_size[0]) - img = np.transpose(img, axes=(2, 0, 1)) - for flip in [0, 1]: - if flip == 1: - img = np.flip(img, axis=2) - data_list[flip][idx][:] = img - return data_list, issame_list - - -def test(data_set, backbone, batch_size, nfolds=10): - '''test - ''' - print('testing verification..') - data_list = data_set[0] - issame_list = data_set[1] - embeddings_list = [] - time_consumed = 0.0 - for data in data_list: - embeddings = None - ba = 0 - while ba < data.shape[0]: - bb = min(ba + batch_size, data.shape[0]) - count = bb - ba - _data = data[bb - batch_size: bb] - - time0 = datetime.datetime.now() - img = ((_data / 255) - 0.5) / 0.5 - net_out = backbone(mindspore.Tensor(img, mindspore.float32)) - _embeddings = net_out.asnumpy() - time_now = datetime.datetime.now() - diff = time_now - time0 - time_consumed += diff.total_seconds() - if embeddings is None: - embeddings = np.zeros((data.shape[0], _embeddings.shape[1])) - embeddings[ba:bb, :] = _embeddings[(batch_size - count):, :] - ba = bb - embeddings_list.append(embeddings) - _xnorm = 0.0 - _xnorm_cnt = 0 - for embed in embeddings_list: - for i in range(embed.shape[0]): - _em = embed[i] - _norm = np.linalg.norm(_em) - _xnorm += _norm - _xnorm_cnt += 1 - _xnorm /= _xnorm_cnt - - embeddings = embeddings_list[0].copy() - embeddings = sklearn.preprocessing.normalize(embeddings) - _, _, acc, _, _, _ = evaluate(embeddings, issame_list, nrof_folds=nfolds) - acc1 = np.mean(acc) - std1 = np.std(acc) - embeddings = embeddings_list[0] + embeddings_list[1] - embeddings = sklearn.preprocessing.normalize(embeddings) - print(embeddings.shape) - print('infer time', time_consumed) - _, _, accuracy, _, _, _ = evaluate( - embeddings, issame_list, nrof_folds=nfolds) - acc2, std2 = np.mean(accuracy), np.std(accuracy) - return acc1, std1, acc2, std2, _xnorm, embeddings_list - - -def main(): - '''r - main function - ''' - parser = argparse.ArgumentParser(description='do verification') - # general - parser.add_argument( - '--eval_url', default='/data/arcface/', help='') - parser.add_argument('--device_id', default=0, type=int, help='device id') - parser.add_argument('--target', - default='lfw,cfp_fp,agedb_30', - help='test targets.') - parser.add_argument( - '--ckpt_url', default="/cache/ArcFace--25_11372.ckpt", type=str, help='ckpt path') - parser.add_argument('--device_target', default="Ascend", type=str, help='device target') - parser.add_argument('--batch-size', default=64, type=int, help='') - parser.add_argument('--max', default='', type=str, help='') - parser.add_argument('--nfolds', default=10, type=int, help='') - args = parser.parse_args() - mindspore.set_context(device_id=args.device_id, mode=0, - device_target=args.device_target) - image_size = [112, 112] - time0 = datetime.datetime.now() - model = iresnet100() - param_dict = load_checkpoint(args.ckpt_url) - load_param_into_net(model, param_dict) - time_now = datetime.datetime.now() - diff = time_now - time0 - print('model loading time', diff.total_seconds()) - - ver_list = [] - ver_name_list = [] - for name in args.target.split(','): - path = os.path.join(args.eval_url, name + ".bin") - if os.path.exists(path): - print('loading.. ', name) - data_set = load_bin(path, image_size) - ver_list.append(data_set) - ver_name_list.append(name) - - length = len(ver_list) - for i in range(length): - acc1, std1, acc2, std2, xnorm, _ = test( - ver_list[i], model, args.batch_size, args.nfolds) - print('[%s]XNorm: %f' % (ver_name_list[i], xnorm)) - print('[%s]Accuracy: %1.5f+-%1.5f' % (ver_name_list[i], acc1, std1)) - print('[%s]Accuracy-Flip: %1.5f+-%1.5f' % - (ver_name_list[i], acc2, std2)) - - -if __name__ == '__main__': - main()