forked from mindspore-Ecosystem/mindspore
!18320 FaceRecognitionOnTracking add 310 infer
Merge pull request !18320 from chenweitao_295/FaceRecognitionOnTracking
This commit is contained in:
commit
74cb4c4c47
|
@ -73,6 +73,7 @@ The entire code structure is as following:
|
||||||
.
|
.
|
||||||
└─ Face Recognition For Tracking
|
└─ Face Recognition For Tracking
|
||||||
├─ README.md
|
├─ README.md
|
||||||
|
├─ ascend310_infer # application for 310 inference
|
||||||
├─ scripts
|
├─ scripts
|
||||||
├─ run_standalone_train.sh # launch standalone training(1p) in ascend
|
├─ run_standalone_train.sh # launch standalone training(1p) in ascend
|
||||||
├─ run_distribute_train.sh # launch distributed training(8p) in ascend
|
├─ run_distribute_train.sh # launch distributed training(8p) in ascend
|
||||||
|
@ -84,6 +85,7 @@ The entire code structure is as following:
|
||||||
├─ run_export_gpu.sh # launch exporting mindir model in gpu
|
├─ run_export_gpu.sh # launch exporting mindir model in gpu
|
||||||
├─ run_train_cpu.sh # launch standalone training in cpu
|
├─ run_train_cpu.sh # launch standalone training in cpu
|
||||||
├─ run_eval_cpu.sh # launch evaluating in cpu
|
├─ run_eval_cpu.sh # launch evaluating in cpu
|
||||||
|
├─ run_infer_310.sh # launch inference on Ascend310
|
||||||
└─ run_export_cpu.sh # launch exporting mindir model in cpu
|
└─ run_export_cpu.sh # launch exporting mindir model in cpu
|
||||||
├─ src
|
├─ src
|
||||||
├─ config.py # parameter configuration
|
├─ config.py # parameter configuration
|
||||||
|
@ -95,6 +97,8 @@ The entire code structure is as following:
|
||||||
└─ me_init.py # network initialization
|
└─ me_init.py # network initialization
|
||||||
├─ train.py # training scripts
|
├─ train.py # training scripts
|
||||||
├─ eval.py # evaluation scripts
|
├─ eval.py # evaluation scripts
|
||||||
|
├─ postprocess.py # postprocess script
|
||||||
|
├─ preprocess.py # preprocess script
|
||||||
└─ export.py # export air/mindir model
|
└─ export.py # export air/mindir model
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -251,9 +255,11 @@ You will get the result as following in "./scripts/device0/eval.log" or txt file
|
||||||
1e-05: 0.035770748447963394@0.5053771466191392
|
1e-05: 0.035770748447963394@0.5053771466191392
|
||||||
```
|
```
|
||||||
|
|
||||||
### Convert model
|
### Inference process
|
||||||
|
|
||||||
If you want to infer the network on Ascend 310, you should convert the model to AIR:
|
#### Convert model
|
||||||
|
|
||||||
|
If you want to infer the network on Ascend 310, you should convert the model to MINDIR or AIR:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
Ascend:
|
Ascend:
|
||||||
|
@ -278,6 +284,42 @@ cd ./scripts
|
||||||
sh run_export_cpu.sh [PRETRAINED_BACKBONE] [BATCH_SIZE] [FILE_NAME](optional)
|
sh run_export_cpu.sh [PRETRAINED_BACKBONE] [BATCH_SIZE] [FILE_NAME](optional)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Export MINDIR:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# Ascend310 inference
|
||||||
|
python export.py --pretrained [PRETRAIN] --batch_size [BATCH_SIZE] --file_format [EXPORT_FORMAT]
|
||||||
|
```
|
||||||
|
|
||||||
|
The pretrained parameter is required.
|
||||||
|
`EXPORT_FORMAT` should be in ["AIR", "MINDIR"]
|
||||||
|
Current batch_size can only be set to 1.
|
||||||
|
|
||||||
|
#### Infer on Ascend310
|
||||||
|
|
||||||
|
Before performing inference, the mindir file must be exported by `export.py` script. We only provide an example of inference using MINDIR model.
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# Ascend310 inference
|
||||||
|
bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DEVICE_ID]
|
||||||
|
```
|
||||||
|
|
||||||
|
- `DEVICE_ID` is optional, default value is 0.
|
||||||
|
|
||||||
|
#### result
|
||||||
|
|
||||||
|
Inference result is saved in current path, you can find result like this in recall.log file.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
0.5: 0.9096926774720119@0.012683006512816064
|
||||||
|
0.3: 0.8121103841852932@0.06735802651382983
|
||||||
|
0.1: 0.5893883112042262@0.147308789767686
|
||||||
|
0.01: 0.25512525545944137@0.2586851498649049754
|
||||||
|
0.001: 0.10664387347206335@0.341498649049754
|
||||||
|
0.0001: 0.054125268312746624@0.41116268460973515
|
||||||
|
1e-05: 0.03846994254572563@0.47234829963417724
|
||||||
|
```
|
||||||
|
|
||||||
# [Model Description](#contents)
|
# [Model Description](#contents)
|
||||||
|
|
||||||
## [Performance](#contents)
|
## [Performance](#contents)
|
||||||
|
@ -313,6 +355,20 @@ sh run_export_cpu.sh [PRETRAINED_BACKBONE] [BATCH_SIZE] [FILE_NAME](optional)
|
||||||
| Recall | 0.62(FAR=0.1) | 0.62(FAR=0.1) | 0.62(FAR=0.1) |
|
| Recall | 0.62(FAR=0.1) | 0.62(FAR=0.1) | 0.62(FAR=0.1) |
|
||||||
| Model for inference | 17M (.ckpt file) | 17M (.ckpt file) | 17M (.ckpt file) |
|
| Model for inference | 17M (.ckpt file) | 17M (.ckpt file) | 17M (.ckpt file) |
|
||||||
|
|
||||||
|
### Inference Performance
|
||||||
|
|
||||||
|
| Parameters | Ascend |
|
||||||
|
| ------------------- | --------------------------- |
|
||||||
|
| Model Version | FaceRecognitionForTracking |
|
||||||
|
| Resource | Ascend 310; Euler2.8 |
|
||||||
|
| Uploaded Date | 11/06/2021 (month/day/year) |
|
||||||
|
| MindSpore Version | 1.2.0 |
|
||||||
|
| Dataset | 2K images |
|
||||||
|
| batch_size | 1 |
|
||||||
|
| outputs | recall |
|
||||||
|
| Recall | 0.589(FAR=0.1) |
|
||||||
|
| Model for inference | 17M(.ckpt file) |
|
||||||
|
|
||||||
# [ModelZoo Homepage](#contents)
|
# [ModelZoo Homepage](#contents)
|
||||||
|
|
||||||
Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).
|
Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
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})
|
||||||
|
find_library(MS_LIB libmindspore.so ${MINDSPORE_PATH}/lib)
|
||||||
|
file(GLOB_RECURSE MD_LIB ${MINDSPORE_PATH}/_c_dataengine*)
|
||||||
|
|
||||||
|
add_executable(main src/main.cc src/utils.cc)
|
||||||
|
target_link_libraries(main ${MS_LIB} ${MD_LIB} gflags)
|
|
@ -0,0 +1,23 @@
|
||||||
|
#!/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
|
||||||
|
mkdir out
|
||||||
|
fi
|
||||||
|
cd out || exit
|
||||||
|
cmake .. \
|
||||||
|
-DMINDSPORE_PATH="`pip show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`"
|
||||||
|
make
|
|
@ -0,0 +1,32 @@
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MINDSPORE_INFERENCE_UTILS_H_
|
||||||
|
#define MINDSPORE_INFERENCE_UTILS_H_
|
||||||
|
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
|
#include "include/api/types.h"
|
||||||
|
|
||||||
|
std::vector<std::string> GetAllFiles(std::string_view dirName);
|
||||||
|
DIR *OpenDir(std::string_view dirName);
|
||||||
|
std::string RealPath(std::string_view path);
|
||||||
|
mindspore::MSTensor ReadFileToTensor(const std::string &file);
|
||||||
|
int WriteResult(const std::string& imageFile, const std::vector<mindspore::MSTensor> &outputs);
|
||||||
|
#endif
|
|
@ -0,0 +1,127 @@
|
||||||
|
/**
|
||||||
|
* 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 <sys/time.h>
|
||||||
|
#include <gflags/gflags.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iosfwd>
|
||||||
|
#include <vector>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include "include/api/model.h"
|
||||||
|
#include "include/api/context.h"
|
||||||
|
#include "include/api/types.h"
|
||||||
|
#include "include/api/serialization.h"
|
||||||
|
#include "inc/utils.h"
|
||||||
|
|
||||||
|
using mindspore::Context;
|
||||||
|
using mindspore::Serialization;
|
||||||
|
using mindspore::Model;
|
||||||
|
using mindspore::Status;
|
||||||
|
using mindspore::ModelType;
|
||||||
|
using mindspore::GraphCell;
|
||||||
|
using mindspore::kSuccess;
|
||||||
|
using mindspore::MSTensor;
|
||||||
|
|
||||||
|
DEFINE_string(mindir_path, "", "mindir path");
|
||||||
|
DEFINE_string(input0_path, ".", "input0 path");
|
||||||
|
DEFINE_int32(device_id, 0, "device id");
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
gflags::ParseCommandLineFlags(&argc, &argv, true);
|
||||||
|
if (RealPath(FLAGS_mindir_path).empty()) {
|
||||||
|
std::cout << "Invalid mindir" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto context = std::make_shared<Context>();
|
||||||
|
auto ascend310 = std::make_shared<mindspore::Ascend310DeviceInfo>();
|
||||||
|
ascend310->SetDeviceID(FLAGS_device_id);
|
||||||
|
context->MutableDeviceInfo().push_back(ascend310);
|
||||||
|
mindspore::Graph graph;
|
||||||
|
Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph);
|
||||||
|
|
||||||
|
Model model;
|
||||||
|
Status ret = model.Build(GraphCell(graph), context);
|
||||||
|
if (ret != kSuccess) {
|
||||||
|
std::cout << "ERROR: Build failed." << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<MSTensor> 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<double, double> 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<MSTensor> inputs;
|
||||||
|
std::vector<MSTensor> 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<double, double>(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 = 0.0;
|
||||||
|
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;
|
||||||
|
}
|
|
@ -0,0 +1,130 @@
|
||||||
|
/**
|
||||||
|
* 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 "inc/utils.h"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using mindspore::MSTensor;
|
||||||
|
using mindspore::DataType;
|
||||||
|
|
||||||
|
std::vector<std::string> GetAllFiles(std::string_view dirName) {
|
||||||
|
struct dirent *filename;
|
||||||
|
DIR *dir = OpenDir(dirName);
|
||||||
|
if (dir == nullptr) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
std::vector<std::string> res;
|
||||||
|
while ((filename = readdir(dir)) != nullptr) {
|
||||||
|
std::string dName = std::string(filename->d_name);
|
||||||
|
if (dName == "." || dName == ".." || filename->d_type != DT_REG) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
res.emplace_back(std::string(dirName) + "/" + filename->d_name);
|
||||||
|
}
|
||||||
|
std::sort(res.begin(), res.end());
|
||||||
|
for (auto &f : res) {
|
||||||
|
std::cout << "image file: " << f << std::endl;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int WriteResult(const std::string& imageFile, const std::vector<MSTensor> &outputs) {
|
||||||
|
std::string homePath = "./result_Files";
|
||||||
|
for (size_t i = 0; i < outputs.size(); ++i) {
|
||||||
|
size_t outputSize;
|
||||||
|
std::shared_ptr<const void> netOutput;
|
||||||
|
netOutput = outputs[i].Data();
|
||||||
|
outputSize = outputs[i].DataSize();
|
||||||
|
int pos = imageFile.rfind('/');
|
||||||
|
std::string fileName(imageFile, pos + 1);
|
||||||
|
fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin");
|
||||||
|
std::string outFileName = homePath + "/" + fileName;
|
||||||
|
FILE * outputFile = fopen(outFileName.c_str(), "wb");
|
||||||
|
fwrite(netOutput.get(), outputSize, sizeof(char), outputFile);
|
||||||
|
fclose(outputFile);
|
||||||
|
outputFile = nullptr;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
mindspore::MSTensor ReadFileToTensor(const std::string &file) {
|
||||||
|
if (file.empty()) {
|
||||||
|
std::cout << "Pointer file is nullptr" << std::endl;
|
||||||
|
return mindspore::MSTensor();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ifstream ifs(file);
|
||||||
|
if (!ifs.good()) {
|
||||||
|
std::cout << "File: " << file << " is not exist" << std::endl;
|
||||||
|
return mindspore::MSTensor();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ifs.is_open()) {
|
||||||
|
std::cout << "File: " << file << "open failed" << std::endl;
|
||||||
|
return mindspore::MSTensor();
|
||||||
|
}
|
||||||
|
|
||||||
|
ifs.seekg(0, std::ios::end);
|
||||||
|
size_t size = ifs.tellg();
|
||||||
|
mindspore::MSTensor buffer(file, mindspore::DataType::kNumberTypeUInt8, {static_cast<int64_t>(size)}, nullptr, size);
|
||||||
|
|
||||||
|
ifs.seekg(0, std::ios::beg);
|
||||||
|
ifs.read(reinterpret_cast<char *>(buffer.MutableData()), size);
|
||||||
|
ifs.close();
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DIR *OpenDir(std::string_view dirName) {
|
||||||
|
if (dirName.empty()) {
|
||||||
|
std::cout << " dirName is null ! " << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
std::string realPath = RealPath(dirName);
|
||||||
|
struct stat s;
|
||||||
|
lstat(realPath.c_str(), &s);
|
||||||
|
if (!S_ISDIR(s.st_mode)) {
|
||||||
|
std::cout << "dirName is not a valid directory !" << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
DIR *dir;
|
||||||
|
dir = opendir(realPath.c_str());
|
||||||
|
if (dir == nullptr) {
|
||||||
|
std::cout << "Can not open dir " << dirName << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
std::cout << "Successfully opened the dir " << dirName << std::endl;
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string RealPath(std::string_view path) {
|
||||||
|
char realPathMem[PATH_MAX] = {0};
|
||||||
|
char *realPathRet = nullptr;
|
||||||
|
realPathRet = realpath(path.data(), realPathMem);
|
||||||
|
|
||||||
|
if (realPathRet == nullptr) {
|
||||||
|
std::cout << "File: " << path << " is not exist.";
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string realPath(realPathMem);
|
||||||
|
std::cout << path << " realpath is: " << realPath << std::endl;
|
||||||
|
return realPath;
|
||||||
|
}
|
|
@ -63,12 +63,12 @@ if __name__ == "__main__":
|
||||||
parser.add_argument('--device_target', type=str, choices=['Ascend', 'GPU', 'CPU'], default='Ascend',
|
parser.add_argument('--device_target', type=str, choices=['Ascend', 'GPU', 'CPU'], default='Ascend',
|
||||||
help='device_target')
|
help='device_target')
|
||||||
parser.add_argument('--file_name', type=str, default='FaceRecognitionForTracking', help='output file name')
|
parser.add_argument('--file_name', type=str, default='FaceRecognitionForTracking', help='output file name')
|
||||||
parser.add_argument('--file_format', type=str, choices=['AIR', 'ONNX', 'MINDIR'], default='AIR', help='file format')
|
parser.add_argument('--file_format', type=str, choices=['AIR', 'MINDIR'], default='AIR', help='file format')
|
||||||
|
|
||||||
arg = parser.parse_args()
|
arg = parser.parse_args()
|
||||||
|
|
||||||
if arg.device_target == 'Ascend':
|
if arg.device_target == 'Ascend':
|
||||||
devid = int(os.getenv('DEVICE_ID'))
|
devid = int(os.getenv('DEVICE_ID', '0'))
|
||||||
context.set_context(device_id=devid)
|
context.set_context(device_id=devid)
|
||||||
|
|
||||||
context.set_context(mode=context.GRAPH_MODE, device_target=arg.device_target)
|
context.set_context(mode=context.GRAPH_MODE, device_target=arg.device_target)
|
||||||
|
|
|
@ -0,0 +1,128 @@
|
||||||
|
# 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.
|
||||||
|
# ============================================================================
|
||||||
|
"""post process for 310 inference"""
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import warnings
|
||||||
|
import argparse
|
||||||
|
import numpy as np
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
warnings.filterwarnings('ignore')
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description='FaceRecognitionForTracking calcul Recall')
|
||||||
|
parser.add_argument("--result_path", type=str, required=True, default='', help="result file path")
|
||||||
|
parser.add_argument("--data_dir", type=str, required=True, default='', help="data dir")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def inclass_likehood(ims_info, types='cos'):
|
||||||
|
'''Inclass likehood.'''
|
||||||
|
obj_feas = {}
|
||||||
|
likehoods = []
|
||||||
|
for name, _, fea in ims_info:
|
||||||
|
if re.split('_\\d\\d\\d\\d', name)[0] not in obj_feas:
|
||||||
|
obj_feas[re.split('_\\d\\d\\d\\d', name)[0]] = []
|
||||||
|
obj_feas[re.split('_\\d\\d\\d\\d', name)[0]].append(fea) # pylint: "_\d\d\d\d" -> "_\\d\\d\\d\\d"
|
||||||
|
for _, feas in tqdm(obj_feas.items()):
|
||||||
|
feas = np.array(feas)
|
||||||
|
if types == 'cos':
|
||||||
|
likehood_mat = np.dot(feas, np.transpose(feas)).tolist()
|
||||||
|
for row in likehood_mat:
|
||||||
|
likehoods += row
|
||||||
|
else:
|
||||||
|
for fea in feas.tolist():
|
||||||
|
likehoods += np.sum(-(fea - feas) ** 2, axis=1).tolist()
|
||||||
|
|
||||||
|
likehoods = np.array(likehoods)
|
||||||
|
return likehoods
|
||||||
|
|
||||||
|
|
||||||
|
def btclass_likehood(ims_info, types='cos'):
|
||||||
|
'''Btclass likehood.'''
|
||||||
|
likehoods = []
|
||||||
|
count = 0
|
||||||
|
for name1, _, fea1 in tqdm(ims_info):
|
||||||
|
count += 1
|
||||||
|
# pylint: "_\d\d\d\d" -> "_\\d\\d\\d\\d"
|
||||||
|
frame_id1, _ = re.split('_\\d\\d\\d\\d', name1)[0], name1.split('_')[-1]
|
||||||
|
fea1 = np.array(fea1)
|
||||||
|
for name2, _, fea2 in ims_info:
|
||||||
|
# pylint: "_\d\d\d\d" -> "_\\d\\d\\d\\d"
|
||||||
|
frame_id2, _ = re.split('_\\d\\d\\d\\d', name2)[0], name2.split('_')[-1]
|
||||||
|
if frame_id1 == frame_id2:
|
||||||
|
continue
|
||||||
|
fea2 = np.array(fea2)
|
||||||
|
if types == 'cos':
|
||||||
|
likehoods.append(np.sum(fea1 * fea2))
|
||||||
|
else:
|
||||||
|
likehoods.append(np.sum(-(fea1 - fea2) ** 2))
|
||||||
|
|
||||||
|
likehoods = np.array(likehoods)
|
||||||
|
return likehoods
|
||||||
|
|
||||||
|
|
||||||
|
def tar_at_far(inlikehoods, btlikehoods):
|
||||||
|
test_point = [0.5, 0.3, 0.1, 0.01, 0.001, 0.0001, 0.00001]
|
||||||
|
tar_far = []
|
||||||
|
for point in test_point:
|
||||||
|
thre = btlikehoods[int(btlikehoods.size * point)]
|
||||||
|
n_ta = np.sum(inlikehoods > thre)
|
||||||
|
tar_far.append((point, float(n_ta) / inlikehoods.size, thre))
|
||||||
|
|
||||||
|
return tar_far
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
with open("result.txt", 'a+') as result_fw:
|
||||||
|
root_path = args.data_dir
|
||||||
|
root_file_list = os.listdir(root_path)
|
||||||
|
ims_info = []
|
||||||
|
for sub_path in root_file_list:
|
||||||
|
for im_path in os.listdir(os.path.join(root_path, sub_path)):
|
||||||
|
ims_info.append((im_path.split('.')[0], os.path.join(root_path, sub_path, im_path)))
|
||||||
|
|
||||||
|
paths = [path for name, path in ims_info]
|
||||||
|
names = [name for name, path in ims_info]
|
||||||
|
print("exact feature...")
|
||||||
|
result_shape = (1, 128)
|
||||||
|
result_path = args.result_path
|
||||||
|
l_t = []
|
||||||
|
for file in [name + "_0.bin" for name in names]:
|
||||||
|
full_file_path = os.path.join(result_path, file)
|
||||||
|
if os.path.isfile(full_file_path):
|
||||||
|
result = np.fromfile(full_file_path, dtype=np.float32).reshape(result_shape).astype(np.float16)
|
||||||
|
l_t.append(result)
|
||||||
|
feas = np.concatenate(l_t, axis=0)
|
||||||
|
ims_info = list(zip(names, paths, feas.tolist()))
|
||||||
|
|
||||||
|
print("exact inclass likehood...")
|
||||||
|
inlikehoods = inclass_likehood(ims_info)
|
||||||
|
inlikehoods[::-1].sort()
|
||||||
|
|
||||||
|
print("exact btclass likehood...")
|
||||||
|
btlikehoods = btclass_likehood(ims_info)
|
||||||
|
btlikehoods[::-1].sort()
|
||||||
|
tar_far = tar_at_far(inlikehoods, btlikehoods)
|
||||||
|
|
||||||
|
for far, tar, thre in tar_far:
|
||||||
|
print('---{}: {}@{}'.format(far, tar, thre))
|
||||||
|
|
||||||
|
for far, tar, thre in tar_far:
|
||||||
|
result_fw.write('{}: {}@{} \n'.format(far, tar, thre))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
|
@ -0,0 +1,73 @@
|
||||||
|
# Copyright 2020-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.
|
||||||
|
# ============================================================================
|
||||||
|
"""pre process for 310 inference"""
|
||||||
|
import os
|
||||||
|
import argparse
|
||||||
|
import numpy as np
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
import mindspore.dataset.vision.py_transforms as V
|
||||||
|
import mindspore.dataset.transforms.py_transforms as T
|
||||||
|
|
||||||
|
|
||||||
|
def load_images(paths, batch_size=1):
|
||||||
|
'''Load images.'''
|
||||||
|
ll = []
|
||||||
|
resize = V.Resize((96, 64))
|
||||||
|
transform = T.Compose([
|
||||||
|
V.ToTensor(),
|
||||||
|
V.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
|
||||||
|
for i, _ in enumerate(paths):
|
||||||
|
im = Image.open(paths[i])
|
||||||
|
im = resize(im)
|
||||||
|
img = np.array(im)
|
||||||
|
ts = transform(img)
|
||||||
|
ll.append(ts[0])
|
||||||
|
if len(ll) == batch_size:
|
||||||
|
yield np.stack(ll, axis=0)
|
||||||
|
ll.clear()
|
||||||
|
if ll:
|
||||||
|
yield np.stack(ll, axis=0)
|
||||||
|
|
||||||
|
|
||||||
|
def preprocess_data(args):
|
||||||
|
""" preprocess data"""
|
||||||
|
root_path = args.data_dir
|
||||||
|
root_file_list = os.listdir(root_path)
|
||||||
|
ims_info = []
|
||||||
|
for sub_path in root_file_list:
|
||||||
|
for im_path in os.listdir(os.path.join(root_path, sub_path)):
|
||||||
|
ims_info.append((im_path.split('.')[0], os.path.join(root_path, sub_path, im_path)))
|
||||||
|
|
||||||
|
paths = [path for name, path in ims_info]
|
||||||
|
names = [name for name, path in ims_info]
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
for img in load_images(paths):
|
||||||
|
img = img.astype(np.float32)
|
||||||
|
file_name = names[i] + ".bin"
|
||||||
|
file_path = os.path.join(args.output_path, file_name)
|
||||||
|
img.tofile(file_path)
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description='preprocess data bin')
|
||||||
|
parser.add_argument('--data_dir', type=str, default='', help='data dir, e.g. /home/test')
|
||||||
|
parser.add_argument('--output_path', type=str, default='', help='output image path, e.g. /home/output')
|
||||||
|
|
||||||
|
arg = parser.parse_args()
|
||||||
|
|
||||||
|
preprocess_data(arg)
|
|
@ -0,0 +1,110 @@
|
||||||
|
#!/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 2 || $# -gt 3 ]]; then
|
||||||
|
echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DEVICE_ID]
|
||||||
|
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)
|
||||||
|
data_path=$(get_real_path $2)
|
||||||
|
|
||||||
|
device_id=0
|
||||||
|
if [ $# == 3 ]; then
|
||||||
|
device_id=$3
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "mindir name: "$model
|
||||||
|
echo "dataset path: "$data_path
|
||||||
|
echo "device id: "$device_id
|
||||||
|
|
||||||
|
export ASCEND_HOME=/usr/local/Ascend/
|
||||||
|
if [ -d ${ASCEND_HOME}/ascend-toolkit ]; then
|
||||||
|
export PATH=$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH
|
||||||
|
export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/ascend-toolkit/latest/atc/lib64:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH
|
||||||
|
export TBE_IMPL_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe
|
||||||
|
export PYTHONPATH=${TBE_IMPL_PATH}:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/python/site-packages:$PYTHONPATH
|
||||||
|
export ASCEND_OPP_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp
|
||||||
|
else
|
||||||
|
export PATH=$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH
|
||||||
|
export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH
|
||||||
|
export PYTHONPATH=$ASCEND_HOME/atc/python/site-packages:$PYTHONPATH
|
||||||
|
export ASCEND_OPP_PATH=$ASCEND_HOME/opp
|
||||||
|
fi
|
||||||
|
|
||||||
|
function preprocess_data()
|
||||||
|
{
|
||||||
|
if [ -d preprocess_Result ]; then
|
||||||
|
rm -rf ./preprocess_Result
|
||||||
|
fi
|
||||||
|
mkdir preprocess_Result
|
||||||
|
python ../preprocess.py --output_path=./preprocess_Result --data_dir=$data_path &> preprocess.log
|
||||||
|
preprocess_data_path=./preprocess_Result
|
||||||
|
}
|
||||||
|
|
||||||
|
function compile_app()
|
||||||
|
{
|
||||||
|
cd ../ascend310_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
|
||||||
|
../ascend310_infer/out/main --mindir_path=$model --input0_path=$preprocess_data_path --device_id=$device_id &> infer.log
|
||||||
|
}
|
||||||
|
|
||||||
|
function cal_recall()
|
||||||
|
{
|
||||||
|
python3.7 ../postprocess.py --result_path=./result_Files --data_dir=$data_path &> recall.log &
|
||||||
|
}
|
||||||
|
|
||||||
|
preprocess_data
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "preprocess data failed"
|
||||||
|
exit 1
|
||||||
|
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_recall
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "calculate recall failed"
|
||||||
|
exit 1
|
||||||
|
fi
|
Loading…
Reference in New Issue