From 835fff9e6039d9f54933c7b625e023bcdd38d28b Mon Sep 17 00:00:00 2001 From: jihaoqin <651149627@qq.com> Date: Thu, 16 Dec 2021 13:24:48 +0800 Subject: [PATCH] faceboxes 310 infer --- research/cv/faceboxes/README.md | 48 ++- .../faceboxes/ascend310_infer/CMakeLists.txt | 15 + .../cv/faceboxes/ascend310_infer/build.sh | 29 ++ .../cv/faceboxes/ascend310_infer/inc/utils.h | 35 ++ .../cv/faceboxes/ascend310_infer/src/main.cc | 153 ++++++++ .../cv/faceboxes/ascend310_infer/src/utils.cc | 197 ++++++++++ research/cv/faceboxes/postprocess310.py | 340 ++++++++++++++++++ research/cv/faceboxes/preprocess310.py | 58 +++ .../cv/faceboxes/scripts/run_infer_310.sh | 119 ++++++ 9 files changed, 989 insertions(+), 5 deletions(-) create mode 100644 research/cv/faceboxes/ascend310_infer/CMakeLists.txt create mode 100644 research/cv/faceboxes/ascend310_infer/build.sh create mode 100644 research/cv/faceboxes/ascend310_infer/inc/utils.h create mode 100644 research/cv/faceboxes/ascend310_infer/src/main.cc create mode 100644 research/cv/faceboxes/ascend310_infer/src/utils.cc create mode 100644 research/cv/faceboxes/postprocess310.py create mode 100644 research/cv/faceboxes/preprocess310.py create mode 100644 research/cv/faceboxes/scripts/run_infer_310.sh diff --git a/research/cv/faceboxes/README.md b/research/cv/faceboxes/README.md index b6bcb4a74..e94c56f11 100644 --- a/research/cv/faceboxes/README.md +++ b/research/cv/faceboxes/README.md @@ -242,13 +242,37 @@ Parameters for both training and evaluation can be set in config.py python eval.py ``` - The results will be shown after running the above python command: + The results will be shown after running the above python command. + +## [Inference Process](#contents) + +### Export MindIR + +```shell +python export.py --checkpoint_path [CKPT_PATH] +``` + +You will get the model of the format `.mindir`. + +### Infer on Ascend310 + +Before performing inference, the mindir file must be exported by `export.py` script. *Please note that we do the inference using fixed image shape `(1024 x 1024)`, so the performance is lower than that of FaceBoxes on Ascend 910.* + +```shell +# Ascend310 inference +cd scripts/ +bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DEVICE_ID] +``` + +### result + +Inference result is saved in current path, you can find result like this in acc.log file. ```text - # cat eval.log - Easy Val AP : 0.8510 - Medium Val AP : 0.7692 - Hard Val AP : 0.4032 + # cat acc.log + Easy Val AP : 0.7996 + Medium Val AP : 0.7187 + Hard Val AP : 0.4013 ``` # [Model Description](#contents) @@ -268,6 +292,7 @@ Parameters for both training and evaluation can be set in config.py | Optimizer | SGD | | Loss Function | MultiBoxLoss + Softmax Cross Entropy | | outputs | bounding box + confidence | +| Accuracy | Easy:0.8510;Medium:0.7692;Hard:0.4032 | | Loss | 2.780 | | Speed | 4pcs: 92 ms/step | | Total time | 4pcs: 7.6 hours | @@ -275,6 +300,19 @@ Parameters for both training and evaluation can be set in config.py | Checkpoint for Fine tuning | 13M (.ckpt file) | | Scripts | [faceboxes script](https://gitee.com/mindspore/models/tree/master/research/cv/faceboxes) | +### Inference Performance + +| Parameters | Ascend | +| ------------------- | --------------------------- | +| Model Version | FaceBoxes | +| Resource | Ascend 310 | +| Uploaded Date | 12/03/2021 (month/day/year) | +| Dataset | WiderFace | +| batch_size | 1 | +| outputs | bounding box + confidence | +| Accuracy | Easy:0.7996;Medium:0.7187;Hard:0.4013 | +| Model for inference | 4.1M(.mindir file) | + # [Description of Random Situation](#contents) In train.py, we set the seed with setup_seed function. diff --git a/research/cv/faceboxes/ascend310_infer/CMakeLists.txt b/research/cv/faceboxes/ascend310_infer/CMakeLists.txt new file mode 100644 index 000000000..df0434bd2 --- /dev/null +++ b/research/cv/faceboxes/ascend310_infer/CMakeLists.txt @@ -0,0 +1,15 @@ +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) +find_package(gflags REQUIRED) diff --git a/research/cv/faceboxes/ascend310_infer/build.sh b/research/cv/faceboxes/ascend310_infer/build.sh new file mode 100644 index 000000000..713d7f657 --- /dev/null +++ b/research/cv/faceboxes/ascend310_infer/build.sh @@ -0,0 +1,29 @@ +#!/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 + +cmake .. \ + -DMINDSPORE_PATH="`pip show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`" +make diff --git a/research/cv/faceboxes/ascend310_infer/inc/utils.h b/research/cv/faceboxes/ascend310_infer/inc/utils.h new file mode 100644 index 000000000..f8ae1e5b4 --- /dev/null +++ b/research/cv/faceboxes/ascend310_infer/inc/utils.h @@ -0,0 +1,35 @@ +/** + * 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 +#include +#include +#include +#include +#include "include/api/types.h" + +std::vector 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 &outputs); +std::vector GetAllFiles(std::string dir_name); +std::vector> GetAllInputData(std::string dir_name); + +#endif diff --git a/research/cv/faceboxes/ascend310_infer/src/main.cc b/research/cv/faceboxes/ascend310_infer/src/main.cc new file mode 100644 index 000000000..7d70606d7 --- /dev/null +++ b/research/cv/faceboxes/ascend310_infer/src/main.cc @@ -0,0 +1,153 @@ +/** + * 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 + +#include "include/api/model.h" +#include "include/api/context.h" +#include "include/api/types.h" +#include "include/api/serialization.h" +#include "include/dataset/vision_ascend.h" +#include "include/dataset/execute.h" +#include "include/dataset/transforms.h" +#include "include/dataset/vision.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; +using mindspore::dataset::Execute; +using mindspore::dataset::vision::Decode; +using mindspore::dataset::vision::Resize; +using mindspore::dataset::vision::CenterCrop; +using mindspore::dataset::vision::Normalize; +using mindspore::dataset::vision::HWC2CHW; + + +DEFINE_string(mindir_path, "", "mindir path"); +DEFINE_string(input0_path, ".", "input0 path"); +DEFINE_string(dataset_name, "widerface", "dataset name"); +DEFINE_int32(device_id, 0, "device id"); + +int load_model(Model *model, std::vector *model_inputs, std::string mindir_path, int device_id) { + if (RealPath(mindir_path).empty()) { + std::cout << "Invalid mindir" << std::endl; + return 1; + } + + auto context = std::make_shared(); + auto ascend310 = std::make_shared(); + ascend310->SetDeviceID(device_id); + context->MutableDeviceInfo().push_back(ascend310); + mindspore::Graph graph; + Serialization::Load(mindir_path, ModelType::kMindIR, &graph); + + Status ret = model->Build(GraphCell(graph), context); + if (ret != kSuccess) { + std::cout << "ERROR: Build failed." << std::endl; + return 1; + } + + *model_inputs = model->GetInputs(); + if (model_inputs->empty()) { + std::cout << "Invalid model, inputs is empty." << std::endl; + return 1; + } + return 0; +} + +int main(int argc, char **argv) { + gflags::ParseCommandLineFlags(&argc, &argv, true); + + Model model; + std::vector model_inputs; + load_model(&model, &model_inputs, FLAGS_mindir_path, FLAGS_device_id); + + std::map costTime_map; + struct timeval start = {0}; + struct timeval end = {0}; + + if (FLAGS_dataset_name == "widerface") { + auto input0_files = GetAllFiles(FLAGS_input0_path); + if (input0_files.empty()) { + std::cout << "ERROR: no input data." << std::endl; + return 1; + } + size_t size = input0_files.size(); + for (size_t i = 0; i < size; ++i) { + double startTimeMs; + double endTimeMs; + std::vector inputs; + std::vector outputs; + std::cout << "Start predict input files:" << input0_files[i] <(startTimeMs, endTimeMs)); + int rst = WriteResult(input0_files[i], outputs); + if (rst != 0) { + std::cout << "write result failed." << std::endl; + return rst; + } + } + } else { + std::cout << "ERROR: only support widetface dataset!" << std::endl; + return 1; + } + + 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; +} diff --git a/research/cv/faceboxes/ascend310_infer/src/utils.cc b/research/cv/faceboxes/ascend310_infer/src/utils.cc new file mode 100644 index 000000000..d545acd31 --- /dev/null +++ b/research/cv/faceboxes/ascend310_infer/src/utils.cc @@ -0,0 +1,197 @@ +/** + * 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 "inc/utils.h" + +using mindspore::MSTensor; +using mindspore::DataType; + +std::vector> GetAllInputData(std::string dir_name) { + std::vector> ret; + + DIR *dir = OpenDir(dir_name); + if (dir == nullptr) { + return {}; + } + struct dirent *filename; + /* read all the files in the dir ~ */ + std::vector sub_dirs; + while ((filename = readdir(dir)) != nullptr) { + std::string d_name = std::string(filename->d_name); + // get rid of "." and ".." + if (d_name == "." || d_name == ".." || d_name.empty()) { + continue; + } + std::string dir_path = RealPath(std::string(dir_name) + "/" + filename->d_name); + struct stat s; + lstat(dir_path.c_str(), &s); + if (!S_ISDIR(s.st_mode)) { + continue; + } + + sub_dirs.emplace_back(dir_path); + } + std::sort(sub_dirs.begin(), sub_dirs.end()); + + (void)std::transform(sub_dirs.begin(), sub_dirs.end(), std::back_inserter(ret), + [](const std::string &d) { return GetAllFiles(d); }); + + return ret; +} + + +std::vector GetAllFiles(std::string dir_name) { + struct dirent *filename; + DIR *dir = OpenDir(dir_name); + if (dir == nullptr) { + return {}; + } + + std::vector res; + while ((filename = readdir(dir)) != nullptr) { + std::string d_name = std::string(filename->d_name); + if (d_name == "." || d_name == ".." || d_name.size() <= 3) { + continue; + } + res.emplace_back(std::string(dir_name) + "/" + filename->d_name); + } + std::sort(res.begin(), res.end()); + + return res; +} + + +std::vector GetAllFiles(std::string_view dirName) { + struct dirent *filename; + DIR *dir = OpenDir(dirName); + if (dir == nullptr) { + return {}; + } + std::vector 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 &outputs) { + std::string homePath = "./result_Files"; + const int INVALID_POINTER = -1; + const int ERROR = -2; + for (size_t i = 0; i < outputs.size(); ++i) { + size_t outputSize; + std::shared_ptr 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"); + if (outputFile == nullptr) { + std::cout << "open result file " << outFileName << " failed" << std::endl; + return INVALID_POINTER; + } + size_t size = fwrite(netOutput.get(), sizeof(char), outputSize, outputFile); + if (size != outputSize) { + fclose(outputFile); + outputFile = nullptr; + std::cout << "write result file " << outFileName << " failed, write size[" << size << + "] is smaller than output size[" << outputSize << "], maybe the disk is full." << std::endl; + return ERROR; + } + 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(size)}, nullptr, size); + + ifs.seekg(0, std::ios::beg); + ifs.read(reinterpret_cast(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; +} diff --git a/research/cv/faceboxes/postprocess310.py b/research/cv/faceboxes/postprocess310.py new file mode 100644 index 000000000..4f21d9f50 --- /dev/null +++ b/research/cv/faceboxes/postprocess310.py @@ -0,0 +1,340 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +"""Eval FaceBoxes.""" +from __future__ import print_function +import argparse +import os +import time +import numpy as np +import cv2 + +from src.config import faceboxes_config +from src.utils import decode_bbox, prior_box + +class Timer(): + def __init__(self): + self.start_time = 0. + self.diff = 0. + + def start(self): + self.start_time = time.time() + + def end(self): + self.diff = time.time() - self.start_time + +class DetectionEngine: + """DetectionEngine""" + def __init__(self, cfg, arg): + self.results = {} + self.nms_thresh = cfg['val_nms_threshold'] + self.conf_thresh = cfg['val_confidence_threshold'] + self.iou_thresh = cfg['val_iou_threshold'] + self.var = cfg['variance'] + self.gt_dir = os.path.join(arg.val_dataset_folder, 'ground_truth') + + def _iou(self, a, b): + """iou""" + A = a.shape[0] + B = b.shape[0] + max_xy = np.minimum( + np.broadcast_to(np.expand_dims(a[:, 2:4], 1), [A, B, 2]), + np.broadcast_to(np.expand_dims(b[:, 2:4], 0), [A, B, 2])) + min_xy = np.maximum( + np.broadcast_to(np.expand_dims(a[:, 0:2], 1), [A, B, 2]), + np.broadcast_to(np.expand_dims(b[:, 0:2], 0), [A, B, 2])) + inter = np.maximum((max_xy - min_xy + 1), np.zeros_like(max_xy - min_xy)) + inter = inter[:, :, 0] * inter[:, :, 1] + + area_a = np.broadcast_to( + np.expand_dims( + (a[:, 2] - a[:, 0] + 1) * (a[:, 3] - a[:, 1] + 1), 1), + np.shape(inter)) + area_b = np.broadcast_to( + np.expand_dims( + (b[:, 2] - b[:, 0] + 1) * (b[:, 3] - b[:, 1] + 1), 0), + np.shape(inter)) + union = area_a + area_b - inter + return inter / union + + def _nms(self, boxes, threshold=0.5): + """nms""" + x1 = boxes[:, 0] + y1 = boxes[:, 1] + x2 = boxes[:, 2] + y2 = boxes[:, 3] + scores = boxes[:, 4] + + areas = (x2 - x1 + 1) * (y2 - y1 + 1) + order = scores.argsort()[::-1] + + reserved_boxes = [] + while order.size > 0: + i = order[0] + reserved_boxes.append(i) + max_x1 = np.maximum(x1[i], x1[order[1:]]) + max_y1 = np.maximum(y1[i], y1[order[1:]]) + min_x2 = np.minimum(x2[i], x2[order[1:]]) + min_y2 = np.minimum(y2[i], y2[order[1:]]) + + intersect_w = np.maximum(0.0, min_x2 - max_x1 + 1) + intersect_h = np.maximum(0.0, min_y2 - max_y1 + 1) + intersect_area = intersect_w * intersect_h + ovr = intersect_area / (areas[i] + areas[order[1:]] - intersect_area) + + indices = np.where(ovr <= threshold)[0] + order = order[indices + 1] + + return reserved_boxes + + def detect(self, boxes, confs, resize, scale, image_path, priors): + """detect""" + if boxes.shape[0] == 0: + # add to result + event_name, img_name = image_path.split('/') + self.results[event_name][img_name[:-4]] = {'img_path': image_path, + 'bboxes': []} + return + + boxes = decode_bbox(np.squeeze(boxes, 0), priors, self.var) + boxes = boxes * scale / resize + + scores = np.squeeze(confs, 0)[:, 1] + # ignore low scores + inds = np.where(scores > self.conf_thresh)[0] + boxes = boxes[inds] + scores = scores[inds] + + # keep top-K before NMS + order = scores.argsort()[::-1] + boxes = boxes[order] + scores = scores[order] + + # do NMS + dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False) + keep = self._nms(dets, self.nms_thresh) + dets = dets[keep, :] + + dets[:, 2:4] = (dets[:, 2:4].astype(np.int) - dets[:, 0:2].astype(np.int)).astype(np.float) # int + dets[:, 0:4] = dets[:, 0:4].astype(np.int).astype(np.float) # int + + + # add to result + event_name, img_name = image_path.split('/') + if event_name not in self.results.keys(): + self.results[event_name] = {} + self.results[event_name][img_name[:-4]] = {'img_path': image_path, + 'bboxes': dets[:, :5].astype(np.float).tolist()} + + def _get_gt_boxes(self): + """get gt boxes""" + from scipy.io import loadmat + gt = loadmat(os.path.join(self.gt_dir, 'wider_face_val.mat')) + hard = loadmat(os.path.join(self.gt_dir, 'wider_hard_val.mat')) + medium = loadmat(os.path.join(self.gt_dir, 'wider_medium_val.mat')) + easy = loadmat(os.path.join(self.gt_dir, 'wider_easy_val.mat')) + + faceboxes = gt['face_bbx_list'] + events = gt['event_list'] + files = gt['file_list'] + + hard_gt_list = hard['gt_list'] + medium_gt_list = medium['gt_list'] + easy_gt_list = easy['gt_list'] + + return faceboxes, events, files, hard_gt_list, medium_gt_list, easy_gt_list + + def _norm_pre_score(self): + """norm pre score""" + max_score = 0 + min_score = 1 + + for event in self.results: + for name in self.results[event].keys(): + bbox = np.array(self.results[event][name]['bboxes']).astype(np.float) + if bbox.shape[0] <= 0: + continue + max_score = max(max_score, np.max(bbox[:, -1])) + min_score = min(min_score, np.min(bbox[:, -1])) + + length = max_score - min_score + for event in self.results: + for name in self.results[event].keys(): + bbox = np.array(self.results[event][name]['bboxes']).astype(np.float) + if bbox.shape[0] <= 0: + continue + bbox[:, -1] -= min_score + bbox[:, -1] /= length + self.results[event][name]['bboxes'] = bbox.tolist() + + def _image_eval(self, predict, gt, keep, iou_thresh, section_num): + """image eval""" + _predict = predict.copy() + _gt = gt.copy() + + image_p_right = np.zeros(_predict.shape[0]) + image_gt_right = np.zeros(_gt.shape[0]) + proposal = np.ones(_predict.shape[0]) + + # x1y1wh -> x1y1x2y2 + _predict[:, 2:4] = _predict[:, 0:2] + _predict[:, 2:4] + _gt[:, 2:4] = _gt[:, 0:2] + _gt[:, 2:4] + + ious = self._iou(_predict[:, 0:4], _gt[:, 0:4]) + for i in range(_predict.shape[0]): + gt_ious = ious[i, :] + max_iou, max_index = gt_ious.max(), gt_ious.argmax() + if max_iou >= iou_thresh: + if keep[max_index] == 0: + image_gt_right[max_index] = -1 + proposal[i] = -1 + elif image_gt_right[max_index] == 0: + image_gt_right[max_index] = 1 + + right_index = np.where(image_gt_right == 1)[0] + image_p_right[i] = len(right_index) + + + + image_pr = np.zeros((section_num, 2), dtype=np.float) + for section in range(section_num): + _thresh = 1 - (section + 1)/section_num + over_score_index = np.where(predict[:, 4] >= _thresh)[0] + if over_score_index.shape[0] <= 0: + image_pr[section, 0] = 0 + image_pr[section, 1] = 0 + else: + index = over_score_index[-1] + p_num = len(np.where(proposal[0:(index+1)] == 1)[0]) + image_pr[section, 0] = p_num + image_pr[section, 1] = image_p_right[index] + + return image_pr + + def get_eval_result(self): + """get eval result""" + self._norm_pre_score() + facebox_list, event_list, file_list, hard_gt_list, medium_gt_list, easy_gt_list = self._get_gt_boxes() + section_num = 1000 + sets = ['easy', 'medium', 'hard'] + set_gts = [easy_gt_list, medium_gt_list, hard_gt_list] + ap_key_dict = {0: "Easy Val AP : ", 1: "Medium Val AP : ", 2: "Hard Val AP : ",} + ap_dict = {} + for _set in range(len(sets)): + gt_list = set_gts[_set] + count_gt = 0 + pr_curve = np.zeros((section_num, 2), dtype=np.float) + for i, _ in enumerate(event_list): + event = str(event_list[i][0][0]) + image_list = file_list[i][0] + event_predict_dict = self.results[event] + event_gt_index_list = gt_list[i][0] + event_gt_box_list = facebox_list[i][0] + + for j, _ in enumerate(image_list): + predict = np.array(event_predict_dict[str(image_list[j][0][0])]['bboxes']).astype(np.float) + gt_boxes = event_gt_box_list[j][0].astype('float') + keep_index = event_gt_index_list[j][0] + count_gt += len(keep_index) + + if gt_boxes.shape[0] <= 0 or predict.shape[0] <= 0: + continue + keep = np.zeros(gt_boxes.shape[0]) + if keep_index.shape[0] > 0: + keep[keep_index-1] = 1 + + image_pr = self._image_eval(predict, gt_boxes, keep, + iou_thresh=self.iou_thresh, + section_num=section_num) + pr_curve += image_pr + + precision = pr_curve[:, 1] / pr_curve[:, 0] + recall = pr_curve[:, 1] / count_gt + + precision = np.concatenate((np.array([0.]), precision, np.array([0.]))) + recall = np.concatenate((np.array([0.]), recall, np.array([1.]))) + for i in range(precision.shape[0]-1, 0, -1): + precision[i-1] = np.maximum(precision[i-1], precision[i]) + index = np.where(recall[1:] != recall[:-1])[0] + ap = np.sum((recall[index + 1] - recall[index]) * precision[index + 1]) + + + print(ap_key_dict[_set] + '{:.4f}'.format(ap)) + + return ap_dict + + +def softmax(raw, axis=None): + """simple softmax""" + raw -= raw.max(axis=axis, keepdims=True) + raw_exp = np.exp(raw) + return raw_exp / raw_exp.sum(axis=axis, keepdims=True) + + +def val(args): + """val""" + cfg = faceboxes_config + + # testing dataset + test_dataset = [] + with open(os.path.join(args.val_dataset_folder, 'val_img_list.txt'), 'r') as f: + lines = f.readlines() + for line in lines: + test_dataset.append(line.rstrip()) + + timers = {'forward_time': Timer(), 'misc': Timer()} + + max_size = 1024 + priors = prior_box(image_size=(max_size, max_size), + min_sizes=cfg['min_sizes'], + steps=cfg['steps'], clip=cfg['clip']) + + # init detection engine + detection = DetectionEngine(cfg, args) + + # testing begin + for i, img_name in enumerate(test_dataset): + image_path = os.path.join(args.val_dataset_folder, 'images', img_name) + + img_raw = cv2.imread(image_path, cv2.IMREAD_COLOR) + img = np.float32(img_raw) + ori_H, ori_W, _ = img.shape + + # testing scale + scale = np.array([1024, 1024, 1024, 1024], dtype=img.dtype) + resize = np.array([1024/ori_W, 1024/ori_H, 1024/ori_W, 1024/ori_H], dtype=img.dtype) + + boxes_name = os.path.join("./result_Files", "widerface_test" + "_" + str(i) + "_0.bin") + boxes = np.fromfile(boxes_name, np.float32) + boxes = boxes.reshape(1, -1, 4) + confs_name = os.path.join("./result_Files", "widerface_test" + "_" + str(i) + "_1.bin") + confs = np.fromfile(confs_name, np.float32) + confs = confs.reshape(1, -1, 2) + confs = softmax(confs, -1) + + timers['misc'].start() + detection.detect(boxes, confs, resize, scale, img_name, priors) + timers['misc'].end() + + print('============== Eval starting ==============') + detection.get_eval_result() + print('============== Eval done ==============') + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Process file') + parser.add_argument('--val_dataset_folder', type=str, default='/home/dataset/widerface/val', + help='val dataset folder.') + args_opt = parser.parse_args() + val(args_opt) diff --git a/research/cv/faceboxes/preprocess310.py b/research/cv/faceboxes/preprocess310.py new file mode 100644 index 000000000..08d9694a1 --- /dev/null +++ b/research/cv/faceboxes/preprocess310.py @@ -0,0 +1,58 @@ +# 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""" +from __future__ import print_function +import argparse +import os +import numpy as np +import cv2 + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description='Process file') + parser.add_argument('--val_dataset_folder', type=str, default='/home/dataset/widerface/val', + help='val dataset folder.') + args = parser.parse_args() + + # testing dataset + test_dataset = [] + with open(os.path.join(args.val_dataset_folder, 'val_img_list.txt'), 'r') as f: + lines = f.readlines() + for line in lines: + test_dataset.append(line.rstrip()) + + # transform data to bin_file + img_path = "./bin_file" + if os.path.exists(img_path): + os.system('rm -rf ' + img_path) + os.makedirs(img_path) + h_max, w_max = 1024, 1024 + for i, img_name in enumerate(test_dataset): + image_path = os.path.join(args.val_dataset_folder, 'images', img_name) + + img_raw = cv2.imread(image_path, cv2.IMREAD_COLOR) + img = np.float32(img_raw) + + # testing scale + img = cv2.resize(img, (1024, 1024)) + + img -= (104, 117, 123) + img = img.transpose(2, 0, 1) + img = np.expand_dims(img, 0) # [1, c, h, w] + + # save bin file + file_name = "widerface_test" + "_" + str(i) + ".bin" + file_path = os.path.join(img_path, file_name) + img.tofile(file_path) diff --git a/research/cv/faceboxes/scripts/run_infer_310.sh b/research/cv/faceboxes/scripts/run_infer_310.sh new file mode 100644 index 000000000..0c7328bdf --- /dev/null +++ b/research/cv/faceboxes/scripts/run_infer_310.sh @@ -0,0 +1,119 @@ +#!/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] [DATASET_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) + +dataset_path=$(get_real_path $2) + + +device_id=0 +if [ $# == 3 ]; then + device_id=$3 +fi + +echo "mindir name: "$model +echo "dataset path: "$dataset_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 ../preprocess310.py --val_dataset_folder=$dataset_path +} + +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=./bin_file --device_id=$device_id &> infer.log +} + +function cal_acc() +{ + python ../postprocess310.py --val_dataset_folder=$dataset_path &> acc.log +} + +echo "preprocess data... (~9 min)" +preprocess_data +if [ $? -ne 0 ]; then + echo "preprocess dataset failed" + exit 1 +fi + +echo "compile app... (~10 sec)" +compile_app +if [ $? -ne 0 ]; then + echo "compile app code failed" + exit 1 +fi + +echo "inference... (~3 min)" +infer +if [ $? -ne 0 ]; then + echo " execute inference failed" + exit 1 +fi + +echo "postprocess... (~3 min)" +cal_acc +if [ $? -ne 0 ]; then + echo "calculate accuracy failed" + exit 1 +fi -- Gitee