diff --git a/official/cv/crnn/README.md b/official/cv/crnn/README.md index 237142ab15a570bfb6efcc26846e14cd9f6c541a..02226ad55614c09f9e7c90105f0365459b19abe5 100644 --- a/official/cv/crnn/README.md +++ b/official/cv/crnn/README.md @@ -24,7 +24,6 @@ - [Export MindIR](#export-mindir) - [Infer on Ascend310](#infer-on-ascend310) - [result](#result) - - [Post Training Quantization](#post-training-quantization) - [Model Description](#model-description) - [Performance](#performance) - [Training Performance](#training-performance) @@ -422,41 +421,6 @@ correct num: 2042 , total num: 3000 result CRNNAccuracy is: 0.806666666666 ``` -### [Post Training Quantization](#contents) - -Relative executing script files reside in the directory "ascend310_quant_infer". Please implement following steps sequentially to complete post quantization. -Current quantization project bases on IIIT5K dataset. - -1. Generate data of .bin format required for AIR model inference at Ascend310 platform. - -```shell -python export_bin.py --eval_dataset [DATASET NAME] --eval_dataset_path [DATA PATH] -``` - -2. Export quantized AIR model. - -Post quantization of model requires special toolkits for exporting quantized AIR model. Please refer to [official website](https://www.hiascend.com/software/cann/community). - -```shell -python post_quant.py --eval_dataset [DATASET NAME] --eval_dataset_path [DATA PATH] --ckpt_file [CKPT_PATH] -``` - -The quantized AIR file will be stored as "./results/crnn_quant.air". - -3. Implement inference at Ascend310 platform. - -```shell -# Ascend310 quant inference -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH] -``` - -Inference result is saved in current path, you can find result like this in acc.log file. - -```bash -correct num: 2398 , total num: 3000 -result CRNNAccuracy is: 0.7933333333333 -``` - ## [Model Description](#contents) ### [Performance](#contents) diff --git a/official/cv/crnn/ascend310_quant_infer/acc.py b/official/cv/crnn/ascend310_quant_infer/acc.py deleted file mode 100644 index 6438f9d970856861c9757da2b6c9ebf8b85b3c4c..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/acc.py +++ /dev/null @@ -1,137 +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. -# ============================================================================ -"""postprocess for 310 inference""" -import os -import argparse -import numpy as np -import Levenshtein -from mindspore import nn - - -label_dict = "abcdefghijklmnopqrstuvwxyz0123456789" - -parser = argparse.ArgumentParser("crnn quant postprocess") -parser.add_argument("--result_path", type=str, required=True, help="path to inference results.") -parser.add_argument("--label_path", type=str, required=True, help="path to label.npy.") -parser.add_argument("--num_step", type=int, default=24, help="num step for LSTM layer") - -args, _ = parser.parse_known_args() - - -class CRNNAccuracy(nn.Metric): - """ - Define accuracy metric for warpctc network. - """ - - def __init__(self, print_flag=True): - super(CRNNAccuracy, self).__init__() - self._correct_num = 0 - self._total_num = 0 - self.blank = len(label_dict) - self.print_flag = print_flag - - def clear(self): - self._correct_num = 0 - self._total_num = 0 - - def update(self, *inputs): - if len(inputs) != 2: - raise ValueError('CRNNAccuracy need 2 inputs (y_pred, y), but got {}'.format(len(inputs))) - y_pred = self._convert_data(inputs[0]) - str_pred = self._ctc_greedy_decoder(y_pred) - if isinstance(inputs[1], list) and isinstance(inputs[1][0], str): - str_label = [x.lower() for x in inputs[1]] - else: - y = self._convert_data(inputs[1]) - str_label = self._convert_labels(y) - - for pred, label in zip(str_pred, str_label): - if self.print_flag: - print(pred, " :: ", label) - edit_distance = Levenshtein.distance(pred, label) - self._total_num += 1 - if edit_distance == 0: - self._correct_num += 1 - - def eval(self): - if self._total_num == 0: - raise RuntimeError('Accuary can not be calculated, because the number of samples is 0.') - print('correct num: ', self._correct_num, ', total num: ', self._total_num) - sequence_accurancy = self._correct_num / self._total_num - return sequence_accurancy - - def _arr2char(self, inputs): - string = "" - for i in inputs: - if i < self.blank: - string += label_dict[i] - return string - - def _convert_labels(self, inputs): - str_list = [] - for label in inputs: - str_temp = self._arr2char(label) - str_list.append(str_temp) - return str_list - - def _ctc_greedy_decoder(self, y_pred): - """ - parse predict result to labels - """ - indices = [] - seq_len, batch_size, _ = y_pred.shape - indices = y_pred.argmax(axis=2) - lens = [seq_len] * batch_size - pred_labels = [] - for i in range(batch_size): - idx = indices[:, i] - last_idx = self.blank - pred_label = [] - for j in range(lens[i]): - cur_idx = idx[j] - if cur_idx not in [last_idx, self.blank]: - pred_label.append(cur_idx) - last_idx = cur_idx - pred_labels.append(pred_label) - str_results = [] - for i in pred_labels: - str_results.append(self._arr2char(i)) - return str_results - - -def calculate_acc(result_path, label_path): - """ - Calculate accuracy according to the annotation file and result file. - """ - metrics = CRNNAccuracy() - label_list = np.load(label_path) - prefix = "crnn_data_bs_1_" - for i in range(len(os.listdir(result_path))): - result_file = os.path.join(result_path, prefix + str(i) + "_output_0.bin") - class_num = len(label_dict) + 1 - pred_y = np.fromfile(result_file, dtype=np.float16).reshape(args.num_step, -1, class_num) - label = label_list[i] - label_str = str() - for index in label[0]: - if int(index) < len(label_dict): - label_str += label_dict[int(index)] - metrics.update(pred_y, [label_str]) - - print("result CRNNAccuracy is: ", metrics.eval()) - metrics.clear() - - -if __name__ == '__main__': - calculate_acc(args.result_path, args.label_path) diff --git a/official/cv/crnn/ascend310_quant_infer/export_bin.py b/official/cv/crnn/ascend310_quant_infer/export_bin.py deleted file mode 100644 index 85ea3b1ddfcf06fbfa522be478d19cb38d3fd0e3..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/export_bin.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. -# ============================================================================ -"""generate data and label needed for AIR model inference""" -import os -import sys -import shutil -import numpy as np -from mindspore import context - - -def generate_data(): - """ - Generate data and label needed for AIR model inference at Ascend310 platform. - """ - context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") - result_path = "./data" - if os.path.exists(result_path): - shutil.rmtree(result_path) - os.makedirs(result_path) - - data_path = os.path.join(result_path, "00_input") - os.makedirs(data_path) - - dataset = create_dataset(name=config.eval_dataset, - dataset_path=config.eval_dataset_path, - batch_size=1, - is_training=False, - config=config) - labels_list = [] - prefix = "crnn_data_bs_1_" - for i, data in enumerate(dataset): - file_path = os.path.join(data_path, prefix + str(i) + ".bin") - data[0].asnumpy().tofile(file_path) - labels_list.append(data[1].asnumpy()) - np.save(os.path.join(result_path, "label.npy"), labels_list) - - -if __name__ == "__main__": - sys.path.append("..") - from src.dataset import create_dataset - from src.model_utils.config import config - - generate_data() diff --git a/official/cv/crnn/ascend310_quant_infer/inc/model_process.h b/official/cv/crnn/ascend310_quant_infer/inc/model_process.h deleted file mode 100644 index 79e19833c7a1b3e428458c23aaba953dc40b1f01..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/inc/model_process.h +++ /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. - */ - -#pragma once -#include <iostream> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* ModelProcess -*/ -class ModelProcess { - public: - /** - * @brief Constructor - */ - ModelProcess(); - - /** - * @brief Destructor - */ - ~ModelProcess(); - - /** - * @brief load model from file with mem - * @param [in] modelPath: model path - * @return result - */ - Result LoadModelFromFileWithMem(const char *modelPath); - - /** - * @brief unload model - */ - void Unload(); - - /** - * @brief create model desc - * @return result - */ - Result CreateDesc(); - - /** - * @brief destroy desc - */ - void DestroyDesc(); - - /** - * @brief create model input - * @param [in] inputDataBuffer: input buffer - * @param [in] bufferSize: input buffer size - * @return result - */ - Result CreateInput(void *inputDataBuffer, size_t bufferSize); - - /** - * @brief destroy input resource - */ - void DestroyInput(); - - /** - * @brief create output buffer - * @return result - */ - Result CreateOutput(); - - /** - * @brief destroy output resource - */ - void DestroyOutput(); - - /** - * @brief model execute - * @return result - */ - Result Execute(); - - /** - * @brief dump model output result to file - */ - void DumpModelOutputResult(char *output_name); - - /** - * @brief get model output result - */ - void OutputModelResult(); - - private: - uint32_t modelId_; - size_t modelMemSize_; - size_t modelWeightSize_; - void *modelMemPtr_; - void *modelWeightPtr_; - bool loadFlag_; // model load flag - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; -}; - diff --git a/official/cv/crnn/ascend310_quant_infer/inc/sample_process.h b/official/cv/crnn/ascend310_quant_infer/inc/sample_process.h deleted file mode 100644 index 24d6ea01e59925673a548a7873ab310623235549..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/inc/sample_process.h +++ /dev/null @@ -1,58 +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. - */ - -#pragma once -#include <string> -#include <vector> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* SampleProcess -*/ -class SampleProcess { - public: - /** - * @brief Constructor - */ - SampleProcess(); - - /** - * @brief Destructor - */ - ~SampleProcess(); - - /** - * @brief init reousce - * @return result - */ - Result InitResource(); - - /** - * @brief sample process - * @return result - */ - Result Process(char *om_path, char *input_folder); - - void GetAllFiles(std::string path, std::vector<std::string> *files); - - private: - void DestroyResource(); - - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; -}; diff --git a/official/cv/crnn/ascend310_quant_infer/inc/utils.h b/official/cv/crnn/ascend310_quant_infer/inc/utils.h deleted file mode 100644 index 3ae2a571b8e8ba51c01b02e23f36dfad5a7b18f1..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/inc/utils.h +++ /dev/null @@ -1,52 +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. - */ - -#pragma once -#include <iostream> -#include <string> - -#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) -#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args) -#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) - -typedef enum Result { - SUCCESS = 0, - FAILED = 1 -} Result; - -/** -* Utils -*/ -class Utils { - public: - /** - * @brief create device buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return device buffer of file - */ - static void *GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize); - - /** - * @brief create buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return buffer of pic - */ - static void* ReadBinFile(std::string fileName, uint32_t *fileSize); -}; - -#pragma once diff --git a/official/cv/crnn/ascend310_quant_infer/post_quant.py b/official/cv/crnn/ascend310_quant_infer/post_quant.py deleted file mode 100644 index b7b28717da618e5b7a783cc730e7b1459c2d6082..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/post_quant.py +++ /dev/null @@ -1,78 +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. -# ============================================================================ -"""do post training quantization for Ascend310""" -import sys -import numpy as np - -from amct_mindspore.quantize_tool import create_quant_config -from amct_mindspore.quantize_tool import quantize_model -from amct_mindspore.quantize_tool import save_model -import mindspore as ms -from mindspore import Tensor, context, load_checkpoint - - -def quant_crnn(network, dataset, input_data): - """ - Export post training quantization model of AIR format. - - Args: - network: the origin network for inference. - dataset: the data for inference. - input_data: the data used for constructing network. The shape and format of input data should be the same as - actual data for inference. - """ - - # step2: create the quant config json file - create_quant_config("./config.json", network, input_data) - - # step3: do some network modification and return the modified network - calibration_network = quantize_model("./config.json", network, input_data) - calibration_network.set_train(False) - - # step4: perform the evaluation of network to do activation calibration - for data in dataset.create_dict_iterator(num_epochs=1): - _ = calibration_network(data["image"]) - - # step5: export the air file - save_model("results/crnn_quant", calibration_network, input_data) - print("[INFO] the quantized AIR file has been stored at: \n {}".format("results/crnn_quant.air")) - - -def model_export(): - context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", device_id=get_device_id()) - config.batch_size = 1 - net = crnn(config) - load_checkpoint(config.ckpt_file, net=net) - net.set_train(False) - - input_data = Tensor(np.zeros([config.batch_size, 3, config.image_height, config.image_width]), ms.float32) - - ds = create_dataset(name=config.eval_dataset, - dataset_path=config.eval_dataset_path, - batch_size=config.batch_size, - is_training=False, - config=config) - dataset = ds.take(1) - quant_crnn(net, dataset, input_data) - - -if __name__ == '__main__': - sys.path.append("..") - from src.crnn import crnn - from src.model_utils.config import config - from src.model_utils.device_adapter import get_device_id - from src.dataset import create_dataset - - model_export() diff --git a/official/cv/crnn/ascend310_quant_infer/run_quant_infer.sh b/official/cv/crnn/ascend310_quant_infer/run_quant_infer.sh deleted file mode 100644 index b034e5b293a114a7b6b05904b4880bc5e2e0cc9b..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/run_quant_infer.sh +++ /dev/null @@ -1,104 +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 3 ]; then - echo "Usage: bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH]" - echo "Example: bash run_quant_infer.sh ./crnn_quant.air ./00_data ./label.npy" -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) -label_path=$(get_real_path $3) - -echo "air name: "$model -echo "dataset path: "$data_path -echo "label path: "$label_path - -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 ASCEND_HOME=/usr/local/Ascend/latest/ - export PATH=$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/fwkacllib/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/atc/lib64:$LD_LIBRARY_PATH - export TBE_IMPL_PATH=$ASCEND_HOME/opp/op_impl/built-in/ai_core/tbe - export PYTHONPATH=${TBE_IMPL_PATH}:$PYTHONPATH - export ASCEND_OPP_PATH=$ASCEND_HOME/opp -fi - -function air_to_om() -{ - atc --input_format=NCHW --framework=1 --model=$model --output=crnn_quant --soc_version=Ascend310 &> atc.log -} - -function compile_app() -{ - bash ./src/build.sh &> build.log -} - -function infer() -{ - if [ -d result ]; then - rm -rf ./result - fi - mkdir result - ./out/main ./crnn_quant.om $data_path &> infer.log -} - -function cal_acc() -{ - python3.7 ./acc.py --result_path=./result --label_path=$label_path &> acc.log -} - -echo "start atc================================================" -air_to_om -if [ $? -ne 0 ]; then - echo "air to om code failed" - exit 1 -fi - -echo "start compile============================================" -compile_app -if [ $? -ne 0 ]; then - echo "compile app code failed" - exit 1 -fi - -echo "start infer==============================================" -infer -if [ $? -ne 0 ]; then - echo " execute inference failed" - exit 1 -fi - -echo "start calculate acc======================================" -cal_acc -if [ $? -ne 0 ]; then - echo "calculate accuracy failed" - exit 1 -fi \ No newline at end of file diff --git a/official/cv/crnn/ascend310_quant_infer/src/CMakeLists.txt b/official/cv/crnn/ascend310_quant_infer/src/CMakeLists.txt deleted file mode 100644 index 655026d7d91612267a287e83e886ba2ce1304d18..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/src/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. - -# CMake lowest version requirement -cmake_minimum_required(VERSION 3.5.1) -# project information -project(InferClassification) -# Check environment variable -if(NOT DEFINED ENV{ASCEND_HOME}) - message(FATAL_ERROR "please define environment variable:ASCEND_HOME") -endif() - -# Compile options -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") - -# Skip build rpath -set(CMAKE_SKIP_BUILD_RPATH True) - -# Set output directory -set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SRC_ROOT}/../out) - -# Set include directory and library directory -set(FWKACL_LIB_DIR $ENV{ASCEND_HOME}/fwkacllib) -set(ACL_LIB_DIR $ENV{ASCEND_HOME}/acllib) -set(ATLAS_ACL_LIB_DIR $ENV{ASCEND_HOME}/ascend-toolkit/latest/acllib) - -# Header path -include_directories(${ACL_LIB_DIR}/include/) -include_directories(${FWKACL_LIB_DIR}/include/) -include_directories(${ATLAS_ACL_LIB_DIR}/include/) -include_directories(${PROJECT_SRC_ROOT}/../inc) - -# add host lib path -link_directories(${ACL_LIB_DIR} ${FWKACL_LIB_DIR}) -find_library(acl libascendcl.so ${ACL_LIB_DIR}/lib64 ${FWKACL_LIB_DIR}/lib64 ${ATLAS_ACL_LIB_DIR}/lib64) - -add_executable(main utils.cpp - sample_process.cpp - model_process.cpp - main.cpp) - -target_link_libraries(main ${acl} gflags pthread) diff --git a/official/cv/crnn/ascend310_quant_infer/src/acl.json b/official/cv/crnn/ascend310_quant_infer/src/acl.json deleted file mode 100644 index 0967ef424bce6791893e9a57bb952f80fd536e93..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/src/acl.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/official/cv/crnn/ascend310_quant_infer/src/build.sh b/official/cv/crnn/ascend310_quant_infer/src/build.sh deleted file mode 100644 index b5979b68e60b673f763a3cac98c5e147e7085291..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/src/build.sh +++ /dev/null @@ -1,55 +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. -# ============================================================================ -path_cur=$(cd "`dirname $0`" || exit; pwd) - -function preparePath() { - rm -rf $1 - mkdir -p $1 - cd $1 || exit -} - -function buildA300() { - if [ ! "${ARCH_PATTERN}" ]; then - # set ARCH_PATTERN to acllib when it was not specified by user - export ARCH_PATTERN=acllib - echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}" - else - echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user, reset it to ${ARCH_PATTERN}/acllib" - export ARCH_PATTERN=${ARCH_PATTERN}/acllib - fi - - path_build=$path_cur/build - preparePath $path_build - cmake .. - make -j - ret=$? - cd .. - return ${ret} -} - -# 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 - -buildA300 - -if [ $? -ne 0 ]; then - exit 1 -fi diff --git a/official/cv/crnn/ascend310_quant_infer/src/main.cpp b/official/cv/crnn/ascend310_quant_infer/src/main.cpp deleted file mode 100644 index 80165505f447d418e0f107b76d04ffae59b89a73..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/src/main.cpp +++ /dev/null @@ -1,42 +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 <iostream> -#include "../inc/sample_process.h" -#include "../inc/utils.h" -bool g_is_device = false; - -int main(int argc, char **argv) { - if (argc != 3) { - ERROR_LOG("usage:./main path_of_om path_of_inputFolder"); - return FAILED; - } - SampleProcess processSample; - Result ret = processSample.InitResource(); - if (ret != SUCCESS) { - ERROR_LOG("sample init resource failed"); - return FAILED; - } - - ret = processSample.Process(argv[1], argv[2]); - if (ret != SUCCESS) { - ERROR_LOG("sample process failed"); - return FAILED; - } - - INFO_LOG("execute sample success"); - return SUCCESS; -} diff --git a/official/cv/crnn/ascend310_quant_infer/src/model_process.cpp b/official/cv/crnn/ascend310_quant_infer/src/model_process.cpp deleted file mode 100644 index 04e6a42ab43cbc41720fe6b9e30bf919323c9f9e..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/src/model_process.cpp +++ /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. - */ - -#include "../inc/model_process.h" -#include <iostream> -#include <map> -#include <sstream> -#include <algorithm> -#include "../inc/utils.h" -extern bool g_is_device; - -ModelProcess::ModelProcess() :modelId_(0), modelMemSize_(0), modelWeightSize_(0), modelMemPtr_(nullptr), -modelWeightPtr_(nullptr), loadFlag_(false), modelDesc_(nullptr), input_(nullptr), output_(nullptr) { -} - -ModelProcess::~ModelProcess() { - Unload(); - DestroyDesc(); - DestroyInput(); - DestroyOutput(); -} - -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { - if (loadFlag_) { - ERROR_LOG("has already loaded a model"); - return FAILED; - } - - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("query model failed, model file is %s", modelPath); - return FAILED; - } - - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); - return FAILED; - } - - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); - return FAILED; - } - - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("load model from file failed, model file is %s", modelPath); - return FAILED; - } - - loadFlag_ = true; - INFO_LOG("load model %s success", modelPath); - return SUCCESS; -} - -Result ModelProcess::CreateDesc() { - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { - ERROR_LOG("create model description failed"); - return FAILED; - } - - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("get model description failed"); - return FAILED; - } - - INFO_LOG("create model description success"); - - return SUCCESS; -} - -void ModelProcess::DestroyDesc() { - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } -} - -Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { - ERROR_LOG("can't create dataset, create input failed"); - return FAILED; - } - - aclDataBuffer* inputData = aclCreateDataBuffer(inputDataBuffer, bufferSize); - if (inputData == nullptr) { - ERROR_LOG("can't create data buffer, create input failed"); - return FAILED; - } - - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("add input dataset buffer failed"); - aclDestroyDataBuffer(inputData); - inputData = nullptr; - return FAILED; - } - - return SUCCESS; -} - -void ModelProcess::DestroyInput() { - if (input_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); - aclDestroyDataBuffer(dataBuffer); - } - aclmdlDestroyDataset(input_); - input_ = nullptr; -} - -Result ModelProcess::CreateOutput() { - if (modelDesc_ == nullptr) { - ERROR_LOG("no model description, create output failed"); - return FAILED; - } - - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { - ERROR_LOG("can't create dataset, create output failed"); - return FAILED; - } - - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); - for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); - - void *outputBuffer = nullptr; - aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't malloc buffer, size is %zu, create output failed", buffer_size); - return FAILED; - } - - aclDataBuffer* outputData = aclCreateDataBuffer(outputBuffer, buffer_size); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't create data buffer, create output failed"); - aclrtFree(outputBuffer); - return FAILED; - } - - ret = aclmdlAddDatasetBuffer(output_, outputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't add data buffer, create output failed"); - aclrtFree(outputBuffer); - aclDestroyDataBuffer(outputData); - return FAILED; - } - } - - INFO_LOG("create model output success"); - return SUCCESS; -} - -void ModelProcess::DumpModelOutputResult(char *output_name) { - size_t outputNum = aclmdlGetDatasetNumBuffers(output_); - - for (size_t i = 0; i < outputNum; ++i) { - std::stringstream ss; - ss << "result/" << output_name << "_output_" << i << ".bin"; - std::string outputFileName = ss.str(); - FILE *outputFile = fopen(outputFileName.c_str(), "wb"); - if (outputFile != nullptr) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void* outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - (void)aclrtFreeHost(outHostData); - return; - } - - fwrite(outHostData, len, sizeof(char), outputFile); - - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } else { - fwrite(data, len, sizeof(char), outputFile); - } - fclose(outputFile); - outputFile = nullptr; - } else { - ERROR_LOG("create output file [%s] failed", outputFileName.c_str()); - return; - } - } - - INFO_LOG("dump data success"); - return; -} - -void ModelProcess::OutputModelResult() { - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void *outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - float *outData = NULL; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - return; - } - - outData = reinterpret_cast<float*>(outHostData); - } else { - outData = reinterpret_cast<float*>(data); - } - std::map<float, unsigned int, std::greater<float> > resultMap; - for (unsigned int j = 0; j < len / sizeof(float); ++j) { - resultMap[*outData] = j; - outData++; - } - - int cnt = 0; - for (auto it = resultMap.begin(); it != resultMap.end(); ++it) { - // print top 5 - if (++cnt > 5) { - break; - } - - INFO_LOG("top %d: index[%d] value[%lf]", cnt, it->second, it->first); - } - if (!g_is_device) { - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } - } - - INFO_LOG("output data success"); - return; -} - -void ModelProcess::DestroyOutput() { - if (output_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - (void)aclrtFree(data); - (void)aclDestroyDataBuffer(dataBuffer); - } - - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; -} - -Result ModelProcess::Execute() { - aclError ret = aclmdlExecute(modelId_, input_, output_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); - return FAILED; - } - - INFO_LOG("model execute success"); - return SUCCESS; -} - -void ModelProcess::Unload() { - if (!loadFlag_) { - WARN_LOG("no model had been loaded, unload failed"); - return; - } - - aclError ret = aclmdlUnload(modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); - } - - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } - - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; - } - - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; - } - - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); -} diff --git a/official/cv/crnn/ascend310_quant_infer/src/sample_process.cpp b/official/cv/crnn/ascend310_quant_infer/src/sample_process.cpp deleted file mode 100644 index 5f8f20f297bf6101d3a68ffd65bd0e6ed69d9486..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/src/sample_process.cpp +++ /dev/null @@ -1,252 +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 "../inc/sample_process.h" -#include <sys/time.h> -#include <sys/types.h> -#include <dirent.h> -#include <string.h> -#include <iostream> -#include <fstream> -#include "../inc/model_process.h" -#include "acl/acl.h" -#include "../inc/utils.h" -extern bool g_is_device; -using std::string; -using std::vector; - -SampleProcess::SampleProcess() :deviceId_(0), context_(nullptr), stream_(nullptr) { -} - -SampleProcess::~SampleProcess() { - DestroyResource(); -} - -Result SampleProcess::InitResource() { - // ACL init - - const char *aclConfigPath = "./src/acl.json"; - aclError ret = aclInit(aclConfigPath); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl init failed"); - return FAILED; - } - INFO_LOG("acl init success"); - - // open device - ret = aclrtSetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl open device %d failed", deviceId_); - return FAILED; - } - INFO_LOG("open device %d success", deviceId_); - - // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create context failed"); - return FAILED; - } - INFO_LOG("create context success"); - - // create stream - ret = aclrtCreateStream(&stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create stream failed"); - return FAILED; - } - INFO_LOG("create stream success"); - - // get run mode - aclrtRunMode runMode; - ret = aclrtGetRunMode(&runMode); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl get run mode failed"); - return FAILED; - } - g_is_device = (runMode == ACL_DEVICE); - INFO_LOG("get run mode success"); - return SUCCESS; -} - -void SampleProcess::GetAllFiles(std::string path, std::vector<string> *files) { - DIR *pDir = NULL; - struct dirent* ptr = nullptr; - if (!(pDir = opendir(path.c_str()))) { - return; - } - while ((ptr = readdir(pDir)) != 0) { - if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { - files->push_back(path + "/" + ptr->d_name); - } - } - closedir(pDir); -} - -Result SampleProcess::Process(char *om_path, char *input_folder) { - // model init - const double second_to_millisecond = 1000; - const double second_to_microsecond = 1000000; - - double whole_cost_time = 0.0; - struct timeval start_global = {0}; - struct timeval end_global = {0}; - double startTimeMs_global = 0.0; - double endTimeMs_global = 0.0; - - gettimeofday(&start_global, nullptr); - - ModelProcess processModel; - const char* omModelPath = om_path; - - Result ret = processModel.LoadModelFromFileWithMem(omModelPath); - if (ret != SUCCESS) { - ERROR_LOG("execute LoadModelFromFileWithMem failed"); - return FAILED; - } - - ret = processModel.CreateDesc(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateDesc failed"); - return FAILED; - } - - ret = processModel.CreateOutput(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateOutput failed"); - return FAILED; - } - - std::vector<string> testFile; - GetAllFiles(input_folder, &testFile); - - if (testFile.size() == 0) { - WARN_LOG("no input data under folder"); - } - - double model_cost_time = 0.0; - double edge_to_edge_model_cost_time = 0.0; - - for (size_t index = 0; index < testFile.size(); ++index) { - INFO_LOG("start to process file:%s", testFile[index].c_str()); - // model process - - struct timeval time_init = {0}; - double timeval_init = 0.0; - gettimeofday(&time_init, nullptr); - timeval_init = (time_init.tv_sec * second_to_microsecond + time_init.tv_usec) / second_to_millisecond; - - uint32_t devBufferSize; - void *picDevBuffer = Utils::GetDeviceBufferOfFile(testFile[index], &devBufferSize); - if (picDevBuffer == nullptr) { - ERROR_LOG("get pic device buffer failed,index is %zu", index); - return FAILED; - } - ret = processModel.CreateInput(picDevBuffer, devBufferSize); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateInput failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - struct timeval start = {0}; - struct timeval end = {0}; - double startTimeMs = 0.0; - double endTimeMs = 0.0; - gettimeofday(&start, nullptr); - startTimeMs = (start.tv_sec * second_to_microsecond + start.tv_usec) / second_to_millisecond; - - ret = processModel.Execute(); - - gettimeofday(&end, nullptr); - endTimeMs = (end.tv_sec * second_to_microsecond + end.tv_usec) / second_to_millisecond; - - double cost_time = endTimeMs - startTimeMs; - INFO_LOG("model infer time: %lf ms", cost_time); - - model_cost_time += cost_time; - - double edge_to_edge_cost_time = endTimeMs - timeval_init; - edge_to_edge_model_cost_time += edge_to_edge_cost_time; - - if (ret != SUCCESS) { - ERROR_LOG("execute inference failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - int pos = testFile[index].find_last_of('/'); - std::string name = testFile[index].substr(pos+1); - std::string outputname = name.substr(0, name.rfind(".")); - - // dump output result to file in the current directory - processModel.DumpModelOutputResult(const_cast<char *>(outputname.c_str())); - - // release model input buffer - aclrtFree(picDevBuffer); - processModel.DestroyInput(); - } - double test_file_size = 0.0; - test_file_size = testFile.size(); - INFO_LOG("infer dataset size:%lf", test_file_size); - - gettimeofday(&end_global, nullptr); - startTimeMs_global = (start_global.tv_sec * second_to_microsecond + start_global.tv_usec) / second_to_millisecond; - endTimeMs_global = (end_global.tv_sec * second_to_microsecond + end_global.tv_usec) / second_to_millisecond; - whole_cost_time = (endTimeMs_global - startTimeMs_global) / test_file_size; - - model_cost_time /= test_file_size; - INFO_LOG("model cost time per sample: %lf ms", model_cost_time); - edge_to_edge_model_cost_time /= test_file_size; - INFO_LOG("edge-to-edge model cost time per sample:%lf ms", edge_to_edge_model_cost_time); - INFO_LOG("whole cost time per sample: %lf ms", whole_cost_time); - - return SUCCESS; -} - -void SampleProcess::DestroyResource() { - aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy stream failed"); - } - stream_ = nullptr; - } - INFO_LOG("end to destroy stream"); - - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy context failed"); - } - context_ = nullptr; - } - INFO_LOG("end to destroy context"); - - ret = aclrtResetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("reset device failed"); - } - INFO_LOG("end to reset device is %d", deviceId_); - - ret = aclFinalize(); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("finalize acl failed"); - } - INFO_LOG("end to finalize acl"); -} - diff --git a/official/cv/crnn/ascend310_quant_infer/src/utils.cpp b/official/cv/crnn/ascend310_quant_infer/src/utils.cpp deleted file mode 100644 index d9208c8cfd9979e9248046e7325f260eb2d14d80..0000000000000000000000000000000000000000 --- a/official/cv/crnn/ascend310_quant_infer/src/utils.cpp +++ /dev/null @@ -1,113 +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 "../inc/utils.h" -#include <sys/stat.h> -#include <iostream> -#include <fstream> -#include <cstring> -#include "acl/acl.h" - -extern bool g_is_device; - -void* Utils::ReadBinFile(std::string fileName, uint32_t *fileSize) { - struct stat sBuf; - int fileStatus = stat(fileName.data(), &sBuf); - if (fileStatus == -1) { - ERROR_LOG("failed to get file"); - return nullptr; - } - if (S_ISREG(sBuf.st_mode) == 0) { - ERROR_LOG("%s is not a file, please enter a file", fileName.c_str()); - return nullptr; - } - - std::ifstream binFile(fileName, std::ifstream::binary); - if (binFile.is_open() == false) { - ERROR_LOG("open file %s failed", fileName.c_str()); - return nullptr; - } - - binFile.seekg(0, binFile.end); - uint32_t binFileBufferLen = binFile.tellg(); - if (binFileBufferLen == 0) { - ERROR_LOG("binfile is empty, filename is %s", fileName.c_str()); - binFile.close(); - return nullptr; - } - - binFile.seekg(0, binFile.beg); - - void* binFileBufferData = nullptr; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&binFileBufferData, binFileBufferLen); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc for binFileBufferData failed"); - binFile.close(); - return nullptr; - } - if (binFileBufferData == nullptr) { - ERROR_LOG("malloc binFileBufferData failed"); - binFile.close(); - return nullptr; - } - } else { - ret = aclrtMalloc(&binFileBufferData, binFileBufferLen, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", binFileBufferLen); - binFile.close(); - return nullptr; - } - } - binFile.read(static_cast<char *>(binFileBufferData), binFileBufferLen); - binFile.close(); - *fileSize = binFileBufferLen; - return binFileBufferData; -} - -void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize) { - uint32_t inputHostBuffSize = 0; - void* inputHostBuff = Utils::ReadBinFile(fileName, &inputHostBuffSize); - if (inputHostBuff == nullptr) { - return nullptr; - } - if (!g_is_device) { - void *inBufferDev = nullptr; - uint32_t inBufferSize = inputHostBuffSize; - aclError ret = aclrtMalloc(&inBufferDev, inBufferSize, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", inBufferSize); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - - ret = aclrtMemcpy(inBufferDev, inBufferSize, inputHostBuff, inputHostBuffSize, ACL_MEMCPY_HOST_TO_DEVICE); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("memcpy failed. device buffer size is %u, input host buffer size is %u", - inBufferSize, inputHostBuffSize); - aclrtFree(inBufferDev); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - aclrtFreeHost(inputHostBuff); - *fileSize = inBufferSize; - return inBufferDev; - } else { - *fileSize = inputHostBuffSize; - return inputHostBuff; - } -} diff --git a/official/cv/deeplabv3/README.md b/official/cv/deeplabv3/README.md index 185eda20a7a842bb85f193591a817be50b710630..4f55129f70865688a18f8eacd4cf68287714c4ab 100644 --- a/official/cv/deeplabv3/README.md +++ b/official/cv/deeplabv3/README.md @@ -30,7 +30,6 @@ - [Inference Process](#inference-process) - [Usage](#usage-2) - [result](#result-2) - - [Post Training Quantization](#post-training-quantization) - [Model Description](#model-description) - [Performance](#performance) - [Evaluation Performance](#evaluation-performance) @@ -868,40 +867,6 @@ Inference result is saved in current path, you can find result in acc.log file. | :----------: | :-----: | :----: | :----: | :-----: | :-----: | :-------------: | | deeplab_v3 | | √ | | | 78.84 | 78.51 | -## [Post Training Quantization](#contents) - -Relative executing script files reside in the directory "ascend310_quant_infer". Please implement following steps sequentially to complete post quantization. -In this project, the model is set as deeplab_v3_s8. - -1. Generate data of .bin format required for AIR model inference at Ascend310 platform. - -```shell -python export_bin.py --model [MODEL] --data_root [DATA ROOT] --data_lst [DATA LST] -``` - -2. Export quantized AIR model. - -Post quantization of model requires special toolkits for exporting quantized AIR model. Please refer to [official website](https://www.hiascend.com/software/cann/community). - -```shell -python post_quant.py --model [MODEL] --data_root [DATA ROOT] --data_lst [DATA LST] --ckpt_file [CKPT_PATH] -``` - -The quantized AIR file will be stored as "./results/deeplabv3_quant.air". - -3. Implement inference at Ascend310 platform. - -```shell -# Ascend310 quant inference -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH] [SHAPE_PATH] -``` - -Inference result is saved in current path, you can find result like this in acc.log file. - -```bash -mean Iou 0.7854572371350974 -``` - # [Model Description](#contents) ## [Performance](#contents) diff --git a/official/cv/deeplabv3/README_CN.md b/official/cv/deeplabv3/README_CN.md index 8017d3f9c8278f9a6afcb46792c969e05a80cf0e..63b111ea4c9b20932b734be6e7200d29d84f4fcd 100644 --- a/official/cv/deeplabv3/README_CN.md +++ b/official/cv/deeplabv3/README_CN.md @@ -31,7 +31,6 @@ - [推理过程](#推理过程) - [用法](#用法-2) - [结果](#结果-2) - - [训练后量化推理](#训练后量化推理) - [模型描述](#模型描述) - [性能](#性能) - [训练性能](#训练性能) @@ -835,40 +834,6 @@ bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DATA_ROOT] [DATA_LIST] [DEVICE_ | :----------: | :-----: | :----: | :----: | :-----: | :-----: | :-------------: | | deeplab_v3 | | √ | | | 78.84 | 78.51 | -## [训练后量化推理](#contents) - -训练后量化推理的相关执行脚本文件在"ascend310_quant_infer"目录下,依次执行以下步骤实现训练后量化推理。 -本训练后量化工程的模型类型是deeplab_v3_s8。 - -1、生成Ascend310平台AIR模型推理需要的.bin格式数据。 - -```shell -python export_bin.py --model [MODEL] --data_root [DATA ROOT] --data_lst [DATA LST] -``` - -2、导出训练后量化的AIR格式模型。 - -导出训练后量化模型需要配套的量化工具包,参考[官方地址](https://www.hiascend.com/software/cann/community) - -```shell -python post_quant.py --model [MODEL] --data_root [DATA ROOT] --data_lst [DATA LST] --ckpt_file [CKPT_PATH] -``` - -导出的模型会存储在./result/deeplabv3_quant.air。 - -3、在Ascend310执行推理量化模型。 - -```shell -# Ascend310 inference -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH] [SHAPE_PATH] -``` - -推理结果保存在脚本执行的当前路径,可以在acc.log中看到精度计算结果。 - -```bash -mean Iou 0.7854572371350974 -``` - # 模型描述 ## 性能 diff --git a/official/cv/deeplabv3/ascend310_quant_infer/acc.py b/official/cv/deeplabv3/ascend310_quant_infer/acc.py deleted file mode 100644 index 49ba00d34407a2b1247c87e18738ee837f4a9a0c..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/acc.py +++ /dev/null @@ -1,77 +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. -# ============================================================================ -"""post process for 310 inference""" -import os -import argparse -import numpy as np -from PIL import Image -import cv2 - - -parser = argparse.ArgumentParser(description="deeplabv3 accuracy calculation") -parser.add_argument('--crop_size', type=int, default=513, help='crop size') -parser.add_argument('--num_classes', type=int, default=21, help='number of classes') -parser.add_argument('--result_path', type=str, default='./result', help='result Files path') -parser.add_argument('--label_path', type=str, default='./01_label', help='result Files path') -parser.add_argument('--shape_path', type=str, default='./shape.npy', help='path of image shape') -args, _ = parser.parse_known_args() - - -def get_img_size(file_name): - img = Image.open(file_name) - return img.size - - -def get_resized_size(org_h, org_w, long_size=513): - if org_h > org_w: - new_h = long_size - new_w = int(1.0 * long_size * org_w / org_h) - else: - new_w = long_size - new_h = int(1.0 * long_size * org_h / org_w) - return new_h, new_w - - -def cal_hist(a, b, n): - k = (a >= 0) & (a < n) - return np.bincount(n * a[k].astype(np.int32) + b[k], minlength=n ** 2).reshape(n, n) - - -def acc_cal(result_path, label_path, shape_path): - hist = np.zeros((args.num_classes, args.num_classes)) - mask_shape = np.load(shape_path) - prefix = "deeplabv3_data_bs_1_" - for i in range(len(mask_shape)): - output = os.path.join(result_path, prefix + str(i) + "_output_0.bin") - net_out = np.fromfile(output, np.float32).reshape(args.num_classes, args.crop_size, args.crop_size) - ori_height, ori_width = mask_shape[i][0], mask_shape[i][1] - resize_h, resize_w = get_resized_size(ori_height, ori_width) - probs_ = net_out[:, :resize_h, :resize_w].transpose((1, 2, 0)) - probs_ = cv2.resize(probs_, (ori_width, ori_height)) - - result_msk = probs_.argmax(axis=2) - label = os.path.join(label_path, prefix + str(i) + ".bin") - mask = np.fromfile(label, np.uint8).reshape(mask_shape[i]) - - hist += cal_hist(mask.flatten(), result_msk.flatten(), args.num_classes) - - print(hist) - iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist)) - print('per-class IoU', iu) - print('mean IoU', np.nanmean(iu)) - - -if __name__ == '__main__': - acc_cal(args.result_path, args.label_path, args.shape_path) diff --git a/official/cv/deeplabv3/ascend310_quant_infer/config.cfg b/official/cv/deeplabv3/ascend310_quant_infer/config.cfg deleted file mode 100644 index 66fab9aad80306f03f3d869786384ea69437147f..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/config.cfg +++ /dev/null @@ -1,2 +0,0 @@ -skip_layers:"network.resnet.layer4.0.downsample.0" -skip_fusion_layers:"network.resnet.layer4.0.downsample.0" \ No newline at end of file diff --git a/official/cv/deeplabv3/ascend310_quant_infer/export_bin.py b/official/cv/deeplabv3/ascend310_quant_infer/export_bin.py deleted file mode 100644 index ad29f34553438d094fd973842bbcd5002c98a8e0..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/export_bin.py +++ /dev/null @@ -1,131 +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. -# ============================================================================ -"""generate data and label needed for AIR model inference""" -import os -import sys -import shutil -import cv2 -import numpy as np -from PIL import Image - - -prefix = "deeplabv3_data_bs_" -data_path = "./data" -if os.path.exists(data_path): - shutil.rmtree(data_path) -os.makedirs(data_path) - - -def get_img_size(file_name): - img = Image.open(file_name) - return img.size - - -def resize_long(img, long_size=513): - h, w, _ = img.shape - if h > w: - new_h = long_size - new_w = int(1.0 * long_size * w / h) - else: - new_w = long_size - new_h = int(1.0 * long_size * h / w) - imo = cv2.resize(img, (new_w, new_h)) - return imo - - -def pre_process(args, img_, crop_size=513): - # resize - img_ = resize_long(img_, crop_size) - resize_h, resize_w, _ = img_.shape - - # mean, std - image_mean = np.array(args.image_mean) - image_std = np.array(args.image_std) - img_ = (img_ - image_mean) / image_std - - # pad to crop_size - pad_h = crop_size - img_.shape[0] - pad_w = crop_size - img_.shape[1] - if pad_h > 0 or pad_w > 0: - img_ = cv2.copyMakeBorder(img_, 0, pad_h, 0, pad_w, cv2.BORDER_CONSTANT, value=0) - # hwc to chw - img_ = img_.transpose((2, 0, 1)) - return img_, resize_h, resize_w - - -def eval_batch(args, img_lst, crop_size, index): - batch_size = len(img_lst) - batch_img = np.zeros((batch_size, 3, crop_size, crop_size), dtype=np.float32) - resize_hw = [] - for l in range(batch_size): - img_ = img_lst[l] - img_, resize_h, resize_w = pre_process(args, img_, crop_size) - batch_img[l] = img_ - resize_hw.append([resize_h, resize_w]) - - batch_img = np.ascontiguousarray(batch_img) - data_dir = os.path.join(data_path, "00_data") - if not os.path.exists(data_dir): - os.makedirs(data_dir) - data_file = os.path.join(data_dir, prefix + str(batch_size) + "_" + str(index) + ".bin") - batch_img.tofile(data_file) - -def eval_batch_scales(args, img_lst, scales, base_crop_size, index): - sizes_ = [int((base_crop_size - 1) * sc) + 1 for sc in scales] - return eval_batch(args, img_lst, crop_size=sizes_[0], index=index) - - -def generate_data(): - """ - Generate data and label needed for AIR model inference at Ascend310 platform. - """ - config.scales = config.scales_list[config.scales_type] - args = config - # data list - with open(args.data_lst) as f: - img_lst = f.readlines() - - # evaluate - batch_img_lst = [] - batch_msk_lst = [] - shape_lst = [] - for i, line in enumerate(img_lst): - ori_img_path, ori_msk_path = line.strip().split(" ") - img_path = "VOCdevkit" + ori_img_path.split("VOCdevkit")[1] - msk_path = "VOCdevkit" + ori_msk_path.split("VOCdevkit")[1] - img_path = os.path.join(args.data_root, img_path) - msk_path = os.path.join(args.data_root, msk_path) - org_width, org_height = get_img_size(img_path) - shape_lst.append([org_height, org_width]) - img_ = cv2.imread(img_path) - msk_ = cv2.imread(msk_path, cv2.IMREAD_GRAYSCALE) - batch_img_lst.append(img_) - batch_msk_lst.append(msk_) - eval_batch_scales(args, batch_img_lst, scales=args.scales, base_crop_size=args.crop_size, index=i) - label_dir = os.path.join(data_path, "01_label") - if not os.path.exists(label_dir): - os.makedirs(label_dir) - label_path = os.path.join(label_dir, prefix + str(len(batch_img_lst)) + "_" + str(i) + ".bin") - msk_.tofile(label_path) - batch_img_lst = [] - batch_msk_lst = [] - np.save(os.path.join(data_path, "shape.npy"), shape_lst) - - -if __name__ == "__main__": - sys.path.append("..") - from model_utils.config import config - - generate_data() diff --git a/official/cv/deeplabv3/ascend310_quant_infer/fusion_switch.cfg b/official/cv/deeplabv3/ascend310_quant_infer/fusion_switch.cfg deleted file mode 100644 index f5fadd59385362ae3ce752172c1226162776c447..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/fusion_switch.cfg +++ /dev/null @@ -1 +0,0 @@ -ConvBatchnormFusionPass:off \ No newline at end of file diff --git a/official/cv/deeplabv3/ascend310_quant_infer/inc/model_process.h b/official/cv/deeplabv3/ascend310_quant_infer/inc/model_process.h deleted file mode 100644 index 79e19833c7a1b3e428458c23aaba953dc40b1f01..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/inc/model_process.h +++ /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. - */ - -#pragma once -#include <iostream> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* ModelProcess -*/ -class ModelProcess { - public: - /** - * @brief Constructor - */ - ModelProcess(); - - /** - * @brief Destructor - */ - ~ModelProcess(); - - /** - * @brief load model from file with mem - * @param [in] modelPath: model path - * @return result - */ - Result LoadModelFromFileWithMem(const char *modelPath); - - /** - * @brief unload model - */ - void Unload(); - - /** - * @brief create model desc - * @return result - */ - Result CreateDesc(); - - /** - * @brief destroy desc - */ - void DestroyDesc(); - - /** - * @brief create model input - * @param [in] inputDataBuffer: input buffer - * @param [in] bufferSize: input buffer size - * @return result - */ - Result CreateInput(void *inputDataBuffer, size_t bufferSize); - - /** - * @brief destroy input resource - */ - void DestroyInput(); - - /** - * @brief create output buffer - * @return result - */ - Result CreateOutput(); - - /** - * @brief destroy output resource - */ - void DestroyOutput(); - - /** - * @brief model execute - * @return result - */ - Result Execute(); - - /** - * @brief dump model output result to file - */ - void DumpModelOutputResult(char *output_name); - - /** - * @brief get model output result - */ - void OutputModelResult(); - - private: - uint32_t modelId_; - size_t modelMemSize_; - size_t modelWeightSize_; - void *modelMemPtr_; - void *modelWeightPtr_; - bool loadFlag_; // model load flag - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; -}; - diff --git a/official/cv/deeplabv3/ascend310_quant_infer/inc/sample_process.h b/official/cv/deeplabv3/ascend310_quant_infer/inc/sample_process.h deleted file mode 100644 index 24d6ea01e59925673a548a7873ab310623235549..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/inc/sample_process.h +++ /dev/null @@ -1,58 +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. - */ - -#pragma once -#include <string> -#include <vector> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* SampleProcess -*/ -class SampleProcess { - public: - /** - * @brief Constructor - */ - SampleProcess(); - - /** - * @brief Destructor - */ - ~SampleProcess(); - - /** - * @brief init reousce - * @return result - */ - Result InitResource(); - - /** - * @brief sample process - * @return result - */ - Result Process(char *om_path, char *input_folder); - - void GetAllFiles(std::string path, std::vector<std::string> *files); - - private: - void DestroyResource(); - - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; -}; diff --git a/official/cv/deeplabv3/ascend310_quant_infer/inc/utils.h b/official/cv/deeplabv3/ascend310_quant_infer/inc/utils.h deleted file mode 100644 index 3ae2a571b8e8ba51c01b02e23f36dfad5a7b18f1..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/inc/utils.h +++ /dev/null @@ -1,52 +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. - */ - -#pragma once -#include <iostream> -#include <string> - -#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) -#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args) -#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) - -typedef enum Result { - SUCCESS = 0, - FAILED = 1 -} Result; - -/** -* Utils -*/ -class Utils { - public: - /** - * @brief create device buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return device buffer of file - */ - static void *GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize); - - /** - * @brief create buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return buffer of pic - */ - static void* ReadBinFile(std::string fileName, uint32_t *fileSize); -}; - -#pragma once diff --git a/official/cv/deeplabv3/ascend310_quant_infer/post_quant.py b/official/cv/deeplabv3/ascend310_quant_infer/post_quant.py deleted file mode 100644 index 125d65741d533d781e6c44a1055f77d2fa988417..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/post_quant.py +++ /dev/null @@ -1,173 +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. -# ============================================================================ -"""do post training quantization for Ascend310""" -import os -import sys -import cv2 -import numpy as np - -from amct_mindspore.quantize_tool import create_quant_config -from amct_mindspore.quantize_tool import quantize_model -from amct_mindspore.quantize_tool import save_model -import mindspore.nn as nn -import mindspore.ops as ops -from mindspore import Tensor, context -from mindspore.train.serialization import load_checkpoint, load_param_into_net - - -def resize_long(img, long_size=513): - h, w, _ = img.shape - if h > w: - new_h = long_size - new_w = int(1.0 * long_size * w / h) - else: - new_w = long_size - new_h = int(1.0 * long_size * h / w) - imo = cv2.resize(img, (new_w, new_h)) - return imo - - -def pre_process(args, img_, crop_size=513): - # resize - img_ = resize_long(img_, crop_size) - resize_h, resize_w, _ = img_.shape - - # mean, std - image_mean = np.array(args.image_mean) - image_std = np.array(args.image_std) - img_ = (img_ - image_mean) / image_std - - # pad to crop_size - pad_h = crop_size - img_.shape[0] - pad_w = crop_size - img_.shape[1] - if pad_h > 0 or pad_w > 0: - img_ = cv2.copyMakeBorder(img_, 0, pad_h, 0, pad_w, cv2.BORDER_CONSTANT, value=0) - # hwc to chw - img_ = img_.transpose((2, 0, 1)) - return img_, resize_h, resize_w - - -def eval_batch(args, img_lst, crop_size=513): - batch_size = len(img_lst) - batch_img = np.zeros((batch_size, 3, crop_size, crop_size), dtype=np.float32) - resize_hw = [] - for l in range(batch_size): - img_ = img_lst[l] - img_, resize_h, resize_w = pre_process(args, img_, crop_size) - batch_img[l] = img_ - resize_hw.append([resize_h, resize_w]) - - batch_img = np.ascontiguousarray(batch_img) - return batch_img - - -def eval_batch_scales(args, img_lst, scales, base_crop_size): - sizes_ = [int((base_crop_size - 1) * sc) + 1 for sc in scales] - return eval_batch(args, img_lst, crop_size=sizes_[0]) - - -def generate_batch_data(): - config.scales = config.scales_list[config.scales_type] - args = config - # data list - with open(args.data_lst) as f: - img_lst = f.readlines() - - # evaluate - batch_img_lst = [] - ori_img_path, _ = img_lst[0].strip().split(" ") - img_path = "VOCdevkit" + ori_img_path.split("VOCdevkit")[1] - - img_path = os.path.join(args.data_root, img_path) - img_ = cv2.imread(img_path) - batch_img_lst.append(img_) - return eval_batch_scales(args, batch_img_lst, scales=args.scales, base_crop_size=args.crop_size) - - -def quant_deeplabv3(network, dataset, input_data): - """ - Export post training quantization model of AIR format. - - Args: - network: the origin network for inference. - dataset: the data for inference. - input_data: the data used for constructing network. The shape and format of input data should be the same as - actual data for inference. - """ - - # step2: create the quant config json file - create_quant_config("./config.json", network, input_data, config_defination="./config.cfg") - # There is value beyond 1e30 in this layer. So this layer will not be quantized. - - # step3: do some network modification and return the modified network - calibration_network = quantize_model("./config.json", network, input_data) - calibration_network.set_train(False) - - # step4: perform the evaluation of network to do activation calibration - _ = calibration_network(Tensor(dataset)) - - # step5: export the air file - save_model("results/deeplabv3_quant", calibration_network, input_data) - print("[INFO] the quantized AIR file has been stored at: \n {}".format("results/deeplabv3_quant.air")) - - -class BuildEvalNetwork(nn.Cell): - def __init__(self, net, input_format="NCHW"): - super(BuildEvalNetwork, self).__init__() - self.network = net - self.softmax = nn.Softmax(axis=1) - self.transpose = ops.Transpose() - self.format = input_format - - def construct(self, x): - if self.format == "NHWC": - x = self.transpose(x, (0, 3, 1, 2)) - output = self.network(x) - output = self.softmax(output) - return output - - -def run_export(): - '''run export.''' - context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target) - config.freeze_bn = True - context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", device_id=config.device_id) - - if config.export_model == 'deeplab_v3_s16': - network = net_factory.nets_map['deeplab_v3_s16']('eval', config.num_classes, 16, True) - else: - network = net_factory.nets_map['deeplab_v3_s8']('eval', config.num_classes, 8, True) - network = BuildEvalNetwork(network, config.input_format) - param_dict = load_checkpoint(config.ckpt_file) - - # load the parameter into net - load_param_into_net(network, param_dict) - batch_size = 1 - if config.input_format == "NHWC": - input_data = Tensor( - np.ones([batch_size, config.input_size, config.input_size, 3]).astype(np.float32)) - else: - input_data = Tensor( - np.ones([batch_size, 3, config.input_size, config.input_size]).astype(np.float32)) - batch_data = generate_batch_data() - quant_deeplabv3(network, batch_data, input_data) - - -if __name__ == "__main__": - sys.path.append("..") - from src.nets import net_factory - from model_utils.config import config - - run_export() diff --git a/official/cv/deeplabv3/ascend310_quant_infer/run_quant_infer.sh b/official/cv/deeplabv3/ascend310_quant_infer/run_quant_infer.sh deleted file mode 100644 index 832ef3cd6f5d393715d617fce8ac5b9820a7f595..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/run_quant_infer.sh +++ /dev/null @@ -1,106 +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 ]; then - echo "Usage: bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH] [SHAPE_PATH]" - echo "Example: bash run_quant_infer.sh ./deeplabv3_quant.air ./00_data ./01_label ./shape.npy" -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) -label_path=$(get_real_path $3) -shape_path=$(get_real_path $4) - -echo "air name: "$model -echo "dataset path: "$data_path -echo "label path: "$label_path -echo "shape path: "$shape_path - -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 ASCEND_HOME=/usr/local/Ascend/latest/ - export PATH=$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/fwkacllib/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/atc/lib64:$LD_LIBRARY_PATH - export TBE_IMPL_PATH=$ASCEND_HOME/opp/op_impl/built-in/ai_core/tbe - export PYTHONPATH=${TBE_IMPL_PATH}:$PYTHONPATH - export ASCEND_OPP_PATH=$ASCEND_HOME/opp -fi - -function air_to_om() -{ - atc --input_format=NCHW --framework=1 --model=$model --output=deeplabv3_quant --soc_version=Ascend310 --fusion_switch_file=./fusion_switch.cfg &> atc.log -} - -function compile_app() -{ - bash ./src/build.sh &> build.log -} - -function infer() -{ - if [ -d result ]; then - rm -rf ./result - fi - mkdir result - ./out/main ./deeplabv3_quant.om $data_path &> infer.log -} - -function cal_acc() -{ - python3.7 ./acc.py --result_path=./result --label_path=$label_path --shape_path=$shape_path &> acc.log -} - -echo "start atc================================================" -air_to_om -if [ $? -ne 0 ]; then - echo "air to om code failed" - exit 1 -fi - -echo "start compile============================================" -compile_app -if [ $? -ne 0 ]; then - echo "compile app code failed" - exit 1 -fi - -echo "start infer==============================================" -infer -if [ $? -ne 0 ]; then - echo " execute inference failed" - exit 1 -fi - -echo "start calculate acc======================================" -cal_acc -if [ $? -ne 0 ]; then - echo "calculate accuracy failed" - exit 1 -fi \ No newline at end of file diff --git a/official/cv/deeplabv3/ascend310_quant_infer/src/CMakeLists.txt b/official/cv/deeplabv3/ascend310_quant_infer/src/CMakeLists.txt deleted file mode 100644 index 655026d7d91612267a287e83e886ba2ce1304d18..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/src/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. - -# CMake lowest version requirement -cmake_minimum_required(VERSION 3.5.1) -# project information -project(InferClassification) -# Check environment variable -if(NOT DEFINED ENV{ASCEND_HOME}) - message(FATAL_ERROR "please define environment variable:ASCEND_HOME") -endif() - -# Compile options -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") - -# Skip build rpath -set(CMAKE_SKIP_BUILD_RPATH True) - -# Set output directory -set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SRC_ROOT}/../out) - -# Set include directory and library directory -set(FWKACL_LIB_DIR $ENV{ASCEND_HOME}/fwkacllib) -set(ACL_LIB_DIR $ENV{ASCEND_HOME}/acllib) -set(ATLAS_ACL_LIB_DIR $ENV{ASCEND_HOME}/ascend-toolkit/latest/acllib) - -# Header path -include_directories(${ACL_LIB_DIR}/include/) -include_directories(${FWKACL_LIB_DIR}/include/) -include_directories(${ATLAS_ACL_LIB_DIR}/include/) -include_directories(${PROJECT_SRC_ROOT}/../inc) - -# add host lib path -link_directories(${ACL_LIB_DIR} ${FWKACL_LIB_DIR}) -find_library(acl libascendcl.so ${ACL_LIB_DIR}/lib64 ${FWKACL_LIB_DIR}/lib64 ${ATLAS_ACL_LIB_DIR}/lib64) - -add_executable(main utils.cpp - sample_process.cpp - model_process.cpp - main.cpp) - -target_link_libraries(main ${acl} gflags pthread) diff --git a/official/cv/deeplabv3/ascend310_quant_infer/src/acl.json b/official/cv/deeplabv3/ascend310_quant_infer/src/acl.json deleted file mode 100644 index 0967ef424bce6791893e9a57bb952f80fd536e93..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/src/acl.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/official/cv/deeplabv3/ascend310_quant_infer/src/build.sh b/official/cv/deeplabv3/ascend310_quant_infer/src/build.sh deleted file mode 100644 index b5979b68e60b673f763a3cac98c5e147e7085291..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/src/build.sh +++ /dev/null @@ -1,55 +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. -# ============================================================================ -path_cur=$(cd "`dirname $0`" || exit; pwd) - -function preparePath() { - rm -rf $1 - mkdir -p $1 - cd $1 || exit -} - -function buildA300() { - if [ ! "${ARCH_PATTERN}" ]; then - # set ARCH_PATTERN to acllib when it was not specified by user - export ARCH_PATTERN=acllib - echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}" - else - echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user, reset it to ${ARCH_PATTERN}/acllib" - export ARCH_PATTERN=${ARCH_PATTERN}/acllib - fi - - path_build=$path_cur/build - preparePath $path_build - cmake .. - make -j - ret=$? - cd .. - return ${ret} -} - -# 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 - -buildA300 - -if [ $? -ne 0 ]; then - exit 1 -fi diff --git a/official/cv/deeplabv3/ascend310_quant_infer/src/main.cpp b/official/cv/deeplabv3/ascend310_quant_infer/src/main.cpp deleted file mode 100644 index 80165505f447d418e0f107b76d04ffae59b89a73..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/src/main.cpp +++ /dev/null @@ -1,42 +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 <iostream> -#include "../inc/sample_process.h" -#include "../inc/utils.h" -bool g_is_device = false; - -int main(int argc, char **argv) { - if (argc != 3) { - ERROR_LOG("usage:./main path_of_om path_of_inputFolder"); - return FAILED; - } - SampleProcess processSample; - Result ret = processSample.InitResource(); - if (ret != SUCCESS) { - ERROR_LOG("sample init resource failed"); - return FAILED; - } - - ret = processSample.Process(argv[1], argv[2]); - if (ret != SUCCESS) { - ERROR_LOG("sample process failed"); - return FAILED; - } - - INFO_LOG("execute sample success"); - return SUCCESS; -} diff --git a/official/cv/deeplabv3/ascend310_quant_infer/src/model_process.cpp b/official/cv/deeplabv3/ascend310_quant_infer/src/model_process.cpp deleted file mode 100644 index 04e6a42ab43cbc41720fe6b9e30bf919323c9f9e..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/src/model_process.cpp +++ /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. - */ - -#include "../inc/model_process.h" -#include <iostream> -#include <map> -#include <sstream> -#include <algorithm> -#include "../inc/utils.h" -extern bool g_is_device; - -ModelProcess::ModelProcess() :modelId_(0), modelMemSize_(0), modelWeightSize_(0), modelMemPtr_(nullptr), -modelWeightPtr_(nullptr), loadFlag_(false), modelDesc_(nullptr), input_(nullptr), output_(nullptr) { -} - -ModelProcess::~ModelProcess() { - Unload(); - DestroyDesc(); - DestroyInput(); - DestroyOutput(); -} - -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { - if (loadFlag_) { - ERROR_LOG("has already loaded a model"); - return FAILED; - } - - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("query model failed, model file is %s", modelPath); - return FAILED; - } - - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); - return FAILED; - } - - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); - return FAILED; - } - - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("load model from file failed, model file is %s", modelPath); - return FAILED; - } - - loadFlag_ = true; - INFO_LOG("load model %s success", modelPath); - return SUCCESS; -} - -Result ModelProcess::CreateDesc() { - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { - ERROR_LOG("create model description failed"); - return FAILED; - } - - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("get model description failed"); - return FAILED; - } - - INFO_LOG("create model description success"); - - return SUCCESS; -} - -void ModelProcess::DestroyDesc() { - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } -} - -Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { - ERROR_LOG("can't create dataset, create input failed"); - return FAILED; - } - - aclDataBuffer* inputData = aclCreateDataBuffer(inputDataBuffer, bufferSize); - if (inputData == nullptr) { - ERROR_LOG("can't create data buffer, create input failed"); - return FAILED; - } - - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("add input dataset buffer failed"); - aclDestroyDataBuffer(inputData); - inputData = nullptr; - return FAILED; - } - - return SUCCESS; -} - -void ModelProcess::DestroyInput() { - if (input_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); - aclDestroyDataBuffer(dataBuffer); - } - aclmdlDestroyDataset(input_); - input_ = nullptr; -} - -Result ModelProcess::CreateOutput() { - if (modelDesc_ == nullptr) { - ERROR_LOG("no model description, create output failed"); - return FAILED; - } - - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { - ERROR_LOG("can't create dataset, create output failed"); - return FAILED; - } - - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); - for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); - - void *outputBuffer = nullptr; - aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't malloc buffer, size is %zu, create output failed", buffer_size); - return FAILED; - } - - aclDataBuffer* outputData = aclCreateDataBuffer(outputBuffer, buffer_size); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't create data buffer, create output failed"); - aclrtFree(outputBuffer); - return FAILED; - } - - ret = aclmdlAddDatasetBuffer(output_, outputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't add data buffer, create output failed"); - aclrtFree(outputBuffer); - aclDestroyDataBuffer(outputData); - return FAILED; - } - } - - INFO_LOG("create model output success"); - return SUCCESS; -} - -void ModelProcess::DumpModelOutputResult(char *output_name) { - size_t outputNum = aclmdlGetDatasetNumBuffers(output_); - - for (size_t i = 0; i < outputNum; ++i) { - std::stringstream ss; - ss << "result/" << output_name << "_output_" << i << ".bin"; - std::string outputFileName = ss.str(); - FILE *outputFile = fopen(outputFileName.c_str(), "wb"); - if (outputFile != nullptr) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void* outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - (void)aclrtFreeHost(outHostData); - return; - } - - fwrite(outHostData, len, sizeof(char), outputFile); - - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } else { - fwrite(data, len, sizeof(char), outputFile); - } - fclose(outputFile); - outputFile = nullptr; - } else { - ERROR_LOG("create output file [%s] failed", outputFileName.c_str()); - return; - } - } - - INFO_LOG("dump data success"); - return; -} - -void ModelProcess::OutputModelResult() { - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void *outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - float *outData = NULL; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - return; - } - - outData = reinterpret_cast<float*>(outHostData); - } else { - outData = reinterpret_cast<float*>(data); - } - std::map<float, unsigned int, std::greater<float> > resultMap; - for (unsigned int j = 0; j < len / sizeof(float); ++j) { - resultMap[*outData] = j; - outData++; - } - - int cnt = 0; - for (auto it = resultMap.begin(); it != resultMap.end(); ++it) { - // print top 5 - if (++cnt > 5) { - break; - } - - INFO_LOG("top %d: index[%d] value[%lf]", cnt, it->second, it->first); - } - if (!g_is_device) { - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } - } - - INFO_LOG("output data success"); - return; -} - -void ModelProcess::DestroyOutput() { - if (output_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - (void)aclrtFree(data); - (void)aclDestroyDataBuffer(dataBuffer); - } - - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; -} - -Result ModelProcess::Execute() { - aclError ret = aclmdlExecute(modelId_, input_, output_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); - return FAILED; - } - - INFO_LOG("model execute success"); - return SUCCESS; -} - -void ModelProcess::Unload() { - if (!loadFlag_) { - WARN_LOG("no model had been loaded, unload failed"); - return; - } - - aclError ret = aclmdlUnload(modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); - } - - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } - - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; - } - - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; - } - - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); -} diff --git a/official/cv/deeplabv3/ascend310_quant_infer/src/sample_process.cpp b/official/cv/deeplabv3/ascend310_quant_infer/src/sample_process.cpp deleted file mode 100644 index 5f8f20f297bf6101d3a68ffd65bd0e6ed69d9486..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/src/sample_process.cpp +++ /dev/null @@ -1,252 +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 "../inc/sample_process.h" -#include <sys/time.h> -#include <sys/types.h> -#include <dirent.h> -#include <string.h> -#include <iostream> -#include <fstream> -#include "../inc/model_process.h" -#include "acl/acl.h" -#include "../inc/utils.h" -extern bool g_is_device; -using std::string; -using std::vector; - -SampleProcess::SampleProcess() :deviceId_(0), context_(nullptr), stream_(nullptr) { -} - -SampleProcess::~SampleProcess() { - DestroyResource(); -} - -Result SampleProcess::InitResource() { - // ACL init - - const char *aclConfigPath = "./src/acl.json"; - aclError ret = aclInit(aclConfigPath); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl init failed"); - return FAILED; - } - INFO_LOG("acl init success"); - - // open device - ret = aclrtSetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl open device %d failed", deviceId_); - return FAILED; - } - INFO_LOG("open device %d success", deviceId_); - - // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create context failed"); - return FAILED; - } - INFO_LOG("create context success"); - - // create stream - ret = aclrtCreateStream(&stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create stream failed"); - return FAILED; - } - INFO_LOG("create stream success"); - - // get run mode - aclrtRunMode runMode; - ret = aclrtGetRunMode(&runMode); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl get run mode failed"); - return FAILED; - } - g_is_device = (runMode == ACL_DEVICE); - INFO_LOG("get run mode success"); - return SUCCESS; -} - -void SampleProcess::GetAllFiles(std::string path, std::vector<string> *files) { - DIR *pDir = NULL; - struct dirent* ptr = nullptr; - if (!(pDir = opendir(path.c_str()))) { - return; - } - while ((ptr = readdir(pDir)) != 0) { - if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { - files->push_back(path + "/" + ptr->d_name); - } - } - closedir(pDir); -} - -Result SampleProcess::Process(char *om_path, char *input_folder) { - // model init - const double second_to_millisecond = 1000; - const double second_to_microsecond = 1000000; - - double whole_cost_time = 0.0; - struct timeval start_global = {0}; - struct timeval end_global = {0}; - double startTimeMs_global = 0.0; - double endTimeMs_global = 0.0; - - gettimeofday(&start_global, nullptr); - - ModelProcess processModel; - const char* omModelPath = om_path; - - Result ret = processModel.LoadModelFromFileWithMem(omModelPath); - if (ret != SUCCESS) { - ERROR_LOG("execute LoadModelFromFileWithMem failed"); - return FAILED; - } - - ret = processModel.CreateDesc(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateDesc failed"); - return FAILED; - } - - ret = processModel.CreateOutput(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateOutput failed"); - return FAILED; - } - - std::vector<string> testFile; - GetAllFiles(input_folder, &testFile); - - if (testFile.size() == 0) { - WARN_LOG("no input data under folder"); - } - - double model_cost_time = 0.0; - double edge_to_edge_model_cost_time = 0.0; - - for (size_t index = 0; index < testFile.size(); ++index) { - INFO_LOG("start to process file:%s", testFile[index].c_str()); - // model process - - struct timeval time_init = {0}; - double timeval_init = 0.0; - gettimeofday(&time_init, nullptr); - timeval_init = (time_init.tv_sec * second_to_microsecond + time_init.tv_usec) / second_to_millisecond; - - uint32_t devBufferSize; - void *picDevBuffer = Utils::GetDeviceBufferOfFile(testFile[index], &devBufferSize); - if (picDevBuffer == nullptr) { - ERROR_LOG("get pic device buffer failed,index is %zu", index); - return FAILED; - } - ret = processModel.CreateInput(picDevBuffer, devBufferSize); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateInput failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - struct timeval start = {0}; - struct timeval end = {0}; - double startTimeMs = 0.0; - double endTimeMs = 0.0; - gettimeofday(&start, nullptr); - startTimeMs = (start.tv_sec * second_to_microsecond + start.tv_usec) / second_to_millisecond; - - ret = processModel.Execute(); - - gettimeofday(&end, nullptr); - endTimeMs = (end.tv_sec * second_to_microsecond + end.tv_usec) / second_to_millisecond; - - double cost_time = endTimeMs - startTimeMs; - INFO_LOG("model infer time: %lf ms", cost_time); - - model_cost_time += cost_time; - - double edge_to_edge_cost_time = endTimeMs - timeval_init; - edge_to_edge_model_cost_time += edge_to_edge_cost_time; - - if (ret != SUCCESS) { - ERROR_LOG("execute inference failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - int pos = testFile[index].find_last_of('/'); - std::string name = testFile[index].substr(pos+1); - std::string outputname = name.substr(0, name.rfind(".")); - - // dump output result to file in the current directory - processModel.DumpModelOutputResult(const_cast<char *>(outputname.c_str())); - - // release model input buffer - aclrtFree(picDevBuffer); - processModel.DestroyInput(); - } - double test_file_size = 0.0; - test_file_size = testFile.size(); - INFO_LOG("infer dataset size:%lf", test_file_size); - - gettimeofday(&end_global, nullptr); - startTimeMs_global = (start_global.tv_sec * second_to_microsecond + start_global.tv_usec) / second_to_millisecond; - endTimeMs_global = (end_global.tv_sec * second_to_microsecond + end_global.tv_usec) / second_to_millisecond; - whole_cost_time = (endTimeMs_global - startTimeMs_global) / test_file_size; - - model_cost_time /= test_file_size; - INFO_LOG("model cost time per sample: %lf ms", model_cost_time); - edge_to_edge_model_cost_time /= test_file_size; - INFO_LOG("edge-to-edge model cost time per sample:%lf ms", edge_to_edge_model_cost_time); - INFO_LOG("whole cost time per sample: %lf ms", whole_cost_time); - - return SUCCESS; -} - -void SampleProcess::DestroyResource() { - aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy stream failed"); - } - stream_ = nullptr; - } - INFO_LOG("end to destroy stream"); - - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy context failed"); - } - context_ = nullptr; - } - INFO_LOG("end to destroy context"); - - ret = aclrtResetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("reset device failed"); - } - INFO_LOG("end to reset device is %d", deviceId_); - - ret = aclFinalize(); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("finalize acl failed"); - } - INFO_LOG("end to finalize acl"); -} - diff --git a/official/cv/deeplabv3/ascend310_quant_infer/src/utils.cpp b/official/cv/deeplabv3/ascend310_quant_infer/src/utils.cpp deleted file mode 100644 index d9208c8cfd9979e9248046e7325f260eb2d14d80..0000000000000000000000000000000000000000 --- a/official/cv/deeplabv3/ascend310_quant_infer/src/utils.cpp +++ /dev/null @@ -1,113 +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 "../inc/utils.h" -#include <sys/stat.h> -#include <iostream> -#include <fstream> -#include <cstring> -#include "acl/acl.h" - -extern bool g_is_device; - -void* Utils::ReadBinFile(std::string fileName, uint32_t *fileSize) { - struct stat sBuf; - int fileStatus = stat(fileName.data(), &sBuf); - if (fileStatus == -1) { - ERROR_LOG("failed to get file"); - return nullptr; - } - if (S_ISREG(sBuf.st_mode) == 0) { - ERROR_LOG("%s is not a file, please enter a file", fileName.c_str()); - return nullptr; - } - - std::ifstream binFile(fileName, std::ifstream::binary); - if (binFile.is_open() == false) { - ERROR_LOG("open file %s failed", fileName.c_str()); - return nullptr; - } - - binFile.seekg(0, binFile.end); - uint32_t binFileBufferLen = binFile.tellg(); - if (binFileBufferLen == 0) { - ERROR_LOG("binfile is empty, filename is %s", fileName.c_str()); - binFile.close(); - return nullptr; - } - - binFile.seekg(0, binFile.beg); - - void* binFileBufferData = nullptr; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&binFileBufferData, binFileBufferLen); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc for binFileBufferData failed"); - binFile.close(); - return nullptr; - } - if (binFileBufferData == nullptr) { - ERROR_LOG("malloc binFileBufferData failed"); - binFile.close(); - return nullptr; - } - } else { - ret = aclrtMalloc(&binFileBufferData, binFileBufferLen, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", binFileBufferLen); - binFile.close(); - return nullptr; - } - } - binFile.read(static_cast<char *>(binFileBufferData), binFileBufferLen); - binFile.close(); - *fileSize = binFileBufferLen; - return binFileBufferData; -} - -void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize) { - uint32_t inputHostBuffSize = 0; - void* inputHostBuff = Utils::ReadBinFile(fileName, &inputHostBuffSize); - if (inputHostBuff == nullptr) { - return nullptr; - } - if (!g_is_device) { - void *inBufferDev = nullptr; - uint32_t inBufferSize = inputHostBuffSize; - aclError ret = aclrtMalloc(&inBufferDev, inBufferSize, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", inBufferSize); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - - ret = aclrtMemcpy(inBufferDev, inBufferSize, inputHostBuff, inputHostBuffSize, ACL_MEMCPY_HOST_TO_DEVICE); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("memcpy failed. device buffer size is %u, input host buffer size is %u", - inBufferSize, inputHostBuffSize); - aclrtFree(inBufferDev); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - aclrtFreeHost(inputHostBuff); - *fileSize = inBufferSize; - return inBufferDev; - } else { - *fileSize = inputHostBuffSize; - return inputHostBuff; - } -} diff --git a/official/cv/maskrcnn/README.md b/official/cv/maskrcnn/README.md index cc3be810288457cc8e242eed0466b85a637f8206..bd70672b1802b094e3c8fc5aa9f0b6600ef113e2 100644 --- a/official/cv/maskrcnn/README.md +++ b/official/cv/maskrcnn/README.md @@ -26,7 +26,6 @@ - [Inference Process](#inference-process) - [Usage](#usage) - [result](#result) - - [Post Training Quantization](#post-training-quantization) - [Model Description](#model-description) - [Performance](#performance) - [Evaluation Performance](#evaluation-performance) @@ -753,69 +752,6 @@ Accumulating evaluation results... Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.594 ``` -## [Post Training Quantization](#contents) - -Relative executing script files reside in the directory "ascend310_quant_infer". Please implement following steps sequentially to complete post quantization. -Current quantization project bases on COCO2017 dataset. -The inference process needs about 600G hard disk space to save the reasoning results. - -1. Generate data of .bin format required for AIR model inference at Ascend310 platform. - -```shell -python export_bin.py --coco_root [COCO DATA PATH] --mindrecord_dir [MINDRECORD PATH] --ann_file [ANNOTATION PATH] -``` - -2. Export quantized AIR model. - -Post quantization of model requires special toolkits for exporting quantized AIR model. Please refer to [official website](https://www.hiascend.com/software/cann/community). - -```shell -python post_quant.py --coco_root [COCO DATA PATH] --mindrecord_dir [MINDRECORD PATH] --ckpt_file [CKPT_PATH] -``` - -The quantized AIR file will be stored as "./results/maskrcnn_quant.air". - -3. Implement inference at Ascend310 platform. - -```shell -# Ascend310 quant inference -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [SHAPE_PATH] [ANNOTATION_PATH] -``` - -Inference result is saved in current path, you can find result like this in acc.log file. - -```bash -Evaluate annotation type *bbox* -Accumulating evaluation results... - Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.378 - Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.602 - Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.407 - Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.240 - Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.420 - Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.481 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.311 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.500 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.528 - Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.367 - Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.572 - Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.657 - -Evaluate annotation type *segm* -Accumulating evaluation results... - Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.321 - Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.553 - Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.328 - Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.164 - Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.350 - Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.466 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.276 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.422 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.441 - Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.279 - Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.476 - Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.578 -``` - # Model Description ## Performance diff --git a/official/cv/maskrcnn/README_CN.md b/official/cv/maskrcnn/README_CN.md index fcca9b9d0acdac977d664df459eeff5b0f753dfd..2e37e9c03d776c751c6c07fb00f443d234ebee96 100644 --- a/official/cv/maskrcnn/README_CN.md +++ b/official/cv/maskrcnn/README_CN.md @@ -25,7 +25,6 @@ - [推理过程](#推理过程) - [使用方法](#使用方法) - [结果](#结果) - - [训练后量化推理](#训练后量化推理) - [模型说明](#模型说明) - [性能](#性能) - [训练性能](#训练性能) @@ -700,68 +699,6 @@ Accumulating evaluation results... Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.594 ``` -## [训练后量化推理](#contents) - -训练后量化推理的相关执行脚本文件在"ascend310_quant_infer"目录下,依次执行以下步骤实现训练后量化推理。本训练后量化工程基于COCO2017数据集。 -推理过程需要占用大约600G的硬盘空间来保存推理的结果。 - -1、生成Ascend310平台AIR模型推理需要的.bin格式数据。 - -```shell -python export_bin.py --coco_root [COCO DATA PATH] --mindrecord_dir [MINDRECORD PATH] --ann_file [ANNOTATION PATH] -``` - -2、导出训练后量化的AIR格式模型。 - -导出训练后量化模型需要配套的量化工具包,参考[官方地址](https://www.hiascend.com/software/cann/community) - -```shell -python post_quant.py --coco_root [COCO DATA PATH] --mindrecord_dir [MINDRECORD PATH] --ckpt_file [CKPT_PATH] -``` - -导出的模型会存储在./result/maskrcnn_quant.air。 - -3、在Ascend310执行推理量化模型。 - -```shell -# Ascend310 inference -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [SHAPE_PATH] [ANNOTATION_PATH] -``` - -推理结果保存在脚本执行的当前路径,可以在acc.log中看到精度计算结果。 - -```bash -Evaluate annotation type *bbox* -Accumulating evaluation results... - Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.378 - Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.602 - Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.407 - Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.240 - Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.420 - Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.481 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.311 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.500 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.528 - Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.367 - Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.572 - Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.657 - -Evaluate annotation type *segm* -Accumulating evaluation results... - Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.321 - Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.553 - Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.328 - Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.164 - Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.350 - Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.466 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.276 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.422 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.441 - Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.279 - Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.476 - Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.578 -``` - # 模型说明 ## 性能 diff --git a/official/cv/maskrcnn/ascend310_quant_infer/acc.py b/official/cv/maskrcnn/ascend310_quant_infer/acc.py deleted file mode 100644 index c6eaba9e73d675dabba8cb8d63dbb48bd7b9a006..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/acc.py +++ /dev/null @@ -1,80 +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. -# ============================================================================ -"""post process for 310 inference""" -import os -import argparse -import numpy as np -from pycocotools.coco import COCO -from util import coco_eval, bbox2result_1image, results2json, get_seg_masks - - -parser = argparse.ArgumentParser("maskrcnn quant postprocess") -parser.add_argument("--shape_path", type=str, required=True, help="path to image meta directory") -parser.add_argument("--annotation_path", type=str, required=True, help="path to instance_xxx.json") -parser.add_argument("--result_path", type=str, required=True, help="path to inference results.") - -args, _ = parser.parse_known_args() - - -def get_eval_result(shape_data, ann_file, result_path): - """ Get metrics result according to the annotation file and result file""" - max_num = 128 - result_path = result_path - outputs = [] - - dataset_coco = COCO(ann_file) - for index in range(len(os.listdir(shape_data))): - prefix = "coco2017_maskrcnn_bs_1_" - shape_file_path = os.path.join(shape_data, prefix + str(index) + ".bin") - shape_file = np.fromfile(shape_file_path, dtype=np.float16).reshape(1, 4) - - bbox_result_file = os.path.join(result_path, prefix + str(index) + "_output_0.bin") - label_result_file = os.path.join(result_path, prefix + str(index) + "_output_1.bin") - mask_result_file = os.path.join(result_path, prefix + str(index) + "_output_2.bin") - mask_fb_result_file = os.path.join(result_path, prefix + str(index) + "_output_3.bin") - - all_bbox = np.fromfile(bbox_result_file, dtype=np.float16).reshape(80000, 5) - all_label = np.fromfile(label_result_file, dtype=np.int32).reshape(80000, 1) - all_mask = np.fromfile(mask_result_file, dtype=np.bool_).reshape(80000, 1) - all_mask_fb = np.fromfile(mask_fb_result_file, dtype=np.float16).reshape(80000, 28, 28) - - all_bbox_squee = np.squeeze(all_bbox) - all_label_squee = np.squeeze(all_label) - all_mask_squee = np.squeeze(all_mask) - all_mask_fb_squee = np.squeeze(all_mask_fb) - - all_bboxes_tmp_mask = all_bbox_squee[all_mask_squee, :] - all_labels_tmp_mask = all_label_squee[all_mask_squee] - all_mask_fb_tmp_mask = all_mask_fb_squee[all_mask_squee, :, :] - - if all_bboxes_tmp_mask.shape[0] > max_num: - inds = np.argsort(-all_bboxes_tmp_mask[:, -1]) - inds = inds[:max_num] - all_bboxes_tmp_mask = all_bboxes_tmp_mask[inds] - all_labels_tmp_mask = all_labels_tmp_mask[inds] - all_mask_fb_tmp_mask = all_mask_fb_tmp_mask[inds] - num_classes = 81 - bbox_results = bbox2result_1image(all_bboxes_tmp_mask, all_labels_tmp_mask, num_classes) - segm_results = get_seg_masks(all_mask_fb_tmp_mask, all_bboxes_tmp_mask, all_labels_tmp_mask, shape_file[0], - True, num_classes) - outputs.append((bbox_results, segm_results)) - - eval_types = ["bbox", "segm"] - result_files = results2json(dataset_coco, outputs, "./results.pkl") - coco_eval(result_files, eval_types, dataset_coco, single_result=False) - - -if __name__ == '__main__': - get_eval_result(args.shape_path, args.annotation_path, args.result_path) diff --git a/official/cv/maskrcnn/ascend310_quant_infer/export_bin.py b/official/cv/maskrcnn/ascend310_quant_infer/export_bin.py deleted file mode 100644 index 1e651feadd86811f36bb05e285c7b04c6ff7cad6..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/export_bin.py +++ /dev/null @@ -1,77 +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. -# ============================================================================ -"""generate data and label needed for AIR model inference""" -import os -import sys -from mindspore import context - - -context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") - - -def generate_data(dataset_path): - """ - Generate data and label needed for AIR model inference at Ascend310 platform. - """ - batch_size = 1 - ds = create_maskrcnn_dataset(dataset_path, batch_size=batch_size, is_training=False) - total = ds.get_dataset_size() - - cur_dir = os.getcwd() + "/data" - img_path = os.path.join(cur_dir, "00_img_data") - if not os.path.exists(img_path): - os.makedirs(img_path) - meta_path = os.path.join(cur_dir, "01_meta_data") - if not os.path.exists(meta_path): - os.makedirs(meta_path) - print("\n========================================\n") - print("total images num: ", total) - print("Processing, please wait a moment.") - bin_prefix = "coco2017_maskrcnn_bs_1_" - for i, data in enumerate(ds.create_dict_iterator(output_numpy=True, num_epochs=1)): - img_data = data["image"] - img_metas = data["image_shape"] - file_name = bin_prefix + str(i) + ".bin" - img_data.tofile(os.path.join(img_path, file_name)) - img_metas.tofile(os.path.join(meta_path, file_name)) - - -if __name__ == "__main__": - sys.path.append("..") - from src.model_utils.config import config - from src.dataset import data_to_mindrecord_byte_image, create_maskrcnn_dataset - - prefix = "MaskRcnn_eval.mindrecord" - mindrecord_dir = config.mindrecord_dir - mindrecord_file = os.path.join(mindrecord_dir, prefix) - if not os.path.exists(mindrecord_file): - if not os.path.isdir(mindrecord_dir): - os.makedirs(mindrecord_dir) - if config.dataset == "coco": - if os.path.isdir(config.coco_root): - print("Create Mindrecord.") - data_to_mindrecord_byte_image("coco", False, prefix, file_num=1) - print("Create Mindrecord Done, at {}".format(mindrecord_dir)) - else: - print("coco_root not exits.") - else: - if os.path.isdir(config.IMAGE_DIR) and os.path.exists(config.ANNO_PATH): - print("Create Mindrecord.") - data_to_mindrecord_byte_image("other", False, prefix, file_num=1) - print("Create Mindrecord Done, at {}".format(mindrecord_dir)) - else: - print("IMAGE_DIR or ANNO_PATH not exits.") - - generate_data(mindrecord_file) diff --git a/official/cv/maskrcnn/ascend310_quant_infer/inc/model_process.h b/official/cv/maskrcnn/ascend310_quant_infer/inc/model_process.h deleted file mode 100644 index 3706b955b97ec4e4cacde04e8d7889a78522785d..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/inc/model_process.h +++ /dev/null @@ -1,112 +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. - */ - -#pragma once -#include <iostream> -#include <vector> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* ModelProcess -*/ -class ModelProcess { - public: - /** - * @brief Constructor - */ - ModelProcess(); - - /** - * @brief Destructor - */ - ~ModelProcess(); - - /** - * @brief load model from file with mem - * @param [in] modelPath: model path - * @return result - */ - Result LoadModelFromFileWithMem(const char *modelPath); - - /** - * @brief unload model - */ - void Unload(); - - /** - * @brief create model desc - * @return result - */ - Result CreateDesc(); - - /** - * @brief destroy desc - */ - void DestroyDesc(); - - /** - * @brief create model input - * @param [in] inputDataBuffer: input buffer - * @param [in] bufferSize: input buffer size - * @return result - */ - Result CreateInput(const std::vector<void *> &inputDataBuffer, const std::vector<size_t> &bufferSize); - - /** - * @brief destroy input resource - */ - void DestroyInput(); - - /** - * @brief create output buffer - * @return result - */ - Result CreateOutput(); - - /** - * @brief destroy output resource - */ - void DestroyOutput(); - - /** - * @brief model execute - * @return result - */ - Result Execute(); - - /** - * @brief dump model output result to file - */ - void DumpModelOutputResult(char *output_name); - - /** - * @brief get model output result - */ - void OutputModelResult(); - - private: - uint32_t modelId_; - size_t modelMemSize_; - size_t modelWeightSize_; - void *modelMemPtr_; - void *modelWeightPtr_; - bool loadFlag_; // model load flag - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; -}; - diff --git a/official/cv/maskrcnn/ascend310_quant_infer/inc/sample_process.h b/official/cv/maskrcnn/ascend310_quant_infer/inc/sample_process.h deleted file mode 100644 index 4e28dff95b2913a7223c4a48908640a0fc4e3d01..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/inc/sample_process.h +++ /dev/null @@ -1,58 +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. - */ - -#pragma once -#include <string> -#include <vector> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* SampleProcess -*/ -class SampleProcess { - public: - /** - * @brief Constructor - */ - SampleProcess(); - - /** - * @brief Destructor - */ - ~SampleProcess(); - - /** - * @brief init reousce - * @return result - */ - Result InitResource(); - - /** - * @brief sample process - * @return result - */ - Result Process(char *om_path, char *input_folder, char *shape_folder); - - void GetAllFiles(std::string path, std::vector<std::string> *files); - - private: - void DestroyResource(); - - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; -}; diff --git a/official/cv/maskrcnn/ascend310_quant_infer/inc/utils.h b/official/cv/maskrcnn/ascend310_quant_infer/inc/utils.h deleted file mode 100644 index 3ae2a571b8e8ba51c01b02e23f36dfad5a7b18f1..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/inc/utils.h +++ /dev/null @@ -1,52 +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. - */ - -#pragma once -#include <iostream> -#include <string> - -#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) -#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args) -#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) - -typedef enum Result { - SUCCESS = 0, - FAILED = 1 -} Result; - -/** -* Utils -*/ -class Utils { - public: - /** - * @brief create device buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return device buffer of file - */ - static void *GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize); - - /** - * @brief create buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return buffer of pic - */ - static void* ReadBinFile(std::string fileName, uint32_t *fileSize); -}; - -#pragma once diff --git a/official/cv/maskrcnn/ascend310_quant_infer/post_quant.py b/official/cv/maskrcnn/ascend310_quant_infer/post_quant.py deleted file mode 100644 index 90baada8a784257e84288f656a9e96891bd5c339..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/post_quant.py +++ /dev/null @@ -1,85 +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. -# ============================================================================ -"""do post training quantization for Ascend310""" -import os -import sys -import numpy as np - -from amct_mindspore.quantize_tool import create_quant_config -from amct_mindspore.quantize_tool import quantize_model -from amct_mindspore.quantize_tool import save_model -from mindspore import Tensor, context -from mindspore.train.serialization import load_checkpoint, load_param_into_net - - -context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") - - -def quant_maskrcnn(network, dataset, input_data): - """ - Export post training quantization model of AIR format. - - Args: - network: the origin network for inference. - dataset: the data for inference. - input_data: the data used for constructing network. The shape and format of input data should be the same as - actual data for inference. - """ - - # step2: create the quant config json file - create_quant_config("./config.json", network, *input_data) - - # step3: do some network modification and return the modified network - calibration_network = quantize_model("./config.json", network, *input_data) - calibration_network.set_train(False) - - # step4: perform the evaluation of network to do activation calibration - for data in dataset.create_dict_iterator(num_epochs=1): - _ = calibration_network(data["image"], data["image_shape"]) - - # step5: export the air file - save_model("results/maskrcnn_quant", calibration_network, *input_data) - print("[INFO] the quantized AIR file has been stored at: \n {}".format("results/maskrcnn_quant.air")) - - -def export_maskrcnn(): - """ export_maskrcnn """ - config.test_batch_size = 1 - config.batch_size = config.test_batch_size - net = MaskRcnn_Infer(config=config) - param_dict = load_checkpoint(config.ckpt_file) - param_dict_new = {} - for key, value in param_dict.items(): - param_dict_new["network." + key] = value - load_param_into_net(net, param_dict_new) - net.set_train(False) - - img = Tensor(np.zeros([config.batch_size, 3, config.img_height, config.img_width], np.float16)) - img_metas = Tensor(np.zeros([config.batch_size, 4], np.float16)) - input_data = [img, img_metas] - - mindrecord_file = os.path.join(config.mindrecord_dir, "MaskRcnn_eval.mindrecord") - ds = create_maskrcnn_dataset(mindrecord_file, batch_size=config.batch_size, is_training=False) - dataset = ds.take(1) - quant_maskrcnn(net, dataset, input_data) - - -if __name__ == "__main__": - sys.path.append("..") - from src.model_utils.config import config - from src.maskrcnn.mask_rcnn_r50 import MaskRcnn_Infer - from src.dataset import create_maskrcnn_dataset - - export_maskrcnn() diff --git a/official/cv/maskrcnn/ascend310_quant_infer/run_quant_infer.sh b/official/cv/maskrcnn/ascend310_quant_infer/run_quant_infer.sh deleted file mode 100644 index 2926bf28507a67a08d415e95c17059a148015a5e..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/run_quant_infer.sh +++ /dev/null @@ -1,106 +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 ]; then - echo "Usage: bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [SHAPE_PATH] [ANNOTATION_PATH]" - echo "Example: bash run_quant_infer.sh ./maskrcnn_quant.air ./00_img_data ./01_meta_data ./instances_val2017.json" -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) -shape_path=$(get_real_path $3) -annotation_path=$(get_real_path $4) - -echo "air name: "$model -echo "dataset path: "$data_path -echo "shape path: "$shape_path -echo "annotation path: "$annotation_path - -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 ASCEND_HOME=/usr/local/Ascend/latest/ - export PATH=$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/fwkacllib/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/atc/lib64:$LD_LIBRARY_PATH - export TBE_IMPL_PATH=$ASCEND_HOME/opp/op_impl/built-in/ai_core/tbe - export PYTHONPATH=${TBE_IMPL_PATH}:$PYTHONPATH - export ASCEND_OPP_PATH=$ASCEND_HOME/opp -fi - -function air_to_om() -{ - atc --input_format=NCHW --framework=1 --model=$model --output=maskrcnn_quant --soc_version=Ascend310 &> atc.log -} - -function compile_app() -{ - bash ./src/build.sh &> build.log -} - -function infer() -{ - if [ -d result ]; then - rm -rf ./result - fi - mkdir result - ./out/main ./maskrcnn_quant.om $data_path $shape_path &> infer.log -} - -function cal_acc() -{ - python3.7 ./acc.py --result_path=./result --shape_path=$shape_path --annotation_path=$annotation_path &> acc.log -} - -echo "start atc================================================" -air_to_om -if [ $? -ne 0 ]; then - echo "air to om code failed" - exit 1 -fi - -echo "start compile============================================" -compile_app -if [ $? -ne 0 ]; then - echo "compile app code failed" - exit 1 -fi - -echo "start infer==============================================" -infer -if [ $? -ne 0 ]; then - echo " execute inference failed" - exit 1 -fi - -echo "start calculate acc======================================" -cal_acc -if [ $? -ne 0 ]; then - echo "calculate accuracy failed" - exit 1 -fi \ No newline at end of file diff --git a/official/cv/maskrcnn/ascend310_quant_infer/src/CMakeLists.txt b/official/cv/maskrcnn/ascend310_quant_infer/src/CMakeLists.txt deleted file mode 100644 index 655026d7d91612267a287e83e886ba2ce1304d18..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/src/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. - -# CMake lowest version requirement -cmake_minimum_required(VERSION 3.5.1) -# project information -project(InferClassification) -# Check environment variable -if(NOT DEFINED ENV{ASCEND_HOME}) - message(FATAL_ERROR "please define environment variable:ASCEND_HOME") -endif() - -# Compile options -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") - -# Skip build rpath -set(CMAKE_SKIP_BUILD_RPATH True) - -# Set output directory -set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SRC_ROOT}/../out) - -# Set include directory and library directory -set(FWKACL_LIB_DIR $ENV{ASCEND_HOME}/fwkacllib) -set(ACL_LIB_DIR $ENV{ASCEND_HOME}/acllib) -set(ATLAS_ACL_LIB_DIR $ENV{ASCEND_HOME}/ascend-toolkit/latest/acllib) - -# Header path -include_directories(${ACL_LIB_DIR}/include/) -include_directories(${FWKACL_LIB_DIR}/include/) -include_directories(${ATLAS_ACL_LIB_DIR}/include/) -include_directories(${PROJECT_SRC_ROOT}/../inc) - -# add host lib path -link_directories(${ACL_LIB_DIR} ${FWKACL_LIB_DIR}) -find_library(acl libascendcl.so ${ACL_LIB_DIR}/lib64 ${FWKACL_LIB_DIR}/lib64 ${ATLAS_ACL_LIB_DIR}/lib64) - -add_executable(main utils.cpp - sample_process.cpp - model_process.cpp - main.cpp) - -target_link_libraries(main ${acl} gflags pthread) diff --git a/official/cv/maskrcnn/ascend310_quant_infer/src/acl.json b/official/cv/maskrcnn/ascend310_quant_infer/src/acl.json deleted file mode 100644 index 0967ef424bce6791893e9a57bb952f80fd536e93..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/src/acl.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/official/cv/maskrcnn/ascend310_quant_infer/src/build.sh b/official/cv/maskrcnn/ascend310_quant_infer/src/build.sh deleted file mode 100644 index b5979b68e60b673f763a3cac98c5e147e7085291..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/src/build.sh +++ /dev/null @@ -1,55 +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. -# ============================================================================ -path_cur=$(cd "`dirname $0`" || exit; pwd) - -function preparePath() { - rm -rf $1 - mkdir -p $1 - cd $1 || exit -} - -function buildA300() { - if [ ! "${ARCH_PATTERN}" ]; then - # set ARCH_PATTERN to acllib when it was not specified by user - export ARCH_PATTERN=acllib - echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}" - else - echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user, reset it to ${ARCH_PATTERN}/acllib" - export ARCH_PATTERN=${ARCH_PATTERN}/acllib - fi - - path_build=$path_cur/build - preparePath $path_build - cmake .. - make -j - ret=$? - cd .. - return ${ret} -} - -# 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 - -buildA300 - -if [ $? -ne 0 ]; then - exit 1 -fi diff --git a/official/cv/maskrcnn/ascend310_quant_infer/src/main.cpp b/official/cv/maskrcnn/ascend310_quant_infer/src/main.cpp deleted file mode 100644 index 5fb6bad7044e27eb7958277b7cc191ea8924e9df..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/src/main.cpp +++ /dev/null @@ -1,42 +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 <iostream> -#include "../inc/sample_process.h" -#include "../inc/utils.h" -bool g_is_device = false; - -int main(int argc, char **argv) { - if (argc != 4) { - ERROR_LOG("usage:./main path_of_om path_of_inputFolder path_of_shapeFolder"); - return FAILED; - } - SampleProcess processSample; - Result ret = processSample.InitResource(); - if (ret != SUCCESS) { - ERROR_LOG("sample init resource failed"); - return FAILED; - } - - ret = processSample.Process(argv[1], argv[2], argv[3]); - if (ret != SUCCESS) { - ERROR_LOG("sample process failed"); - return FAILED; - } - - INFO_LOG("execute sample success"); - return SUCCESS; -} diff --git a/official/cv/maskrcnn/ascend310_quant_infer/src/model_process.cpp b/official/cv/maskrcnn/ascend310_quant_infer/src/model_process.cpp deleted file mode 100644 index 59f2f7a2509d63e77ebcec596e73ae8225ec7e02..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/src/model_process.cpp +++ /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. - */ - -#include "../inc/model_process.h" -#include <iostream> -#include <map> -#include <sstream> -#include <algorithm> -#include "../inc/utils.h" -extern bool g_is_device; - -ModelProcess::ModelProcess() :modelId_(0), modelMemSize_(0), modelWeightSize_(0), modelMemPtr_(nullptr), -modelWeightPtr_(nullptr), loadFlag_(false), modelDesc_(nullptr), input_(nullptr), output_(nullptr) { -} - -ModelProcess::~ModelProcess() { - Unload(); - DestroyDesc(); - DestroyInput(); - DestroyOutput(); -} - -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { - if (loadFlag_) { - ERROR_LOG("has already loaded a model"); - return FAILED; - } - - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("query model failed, model file is %s", modelPath); - return FAILED; - } - - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); - return FAILED; - } - - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); - return FAILED; - } - - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("load model from file failed, model file is %s", modelPath); - return FAILED; - } - - loadFlag_ = true; - INFO_LOG("load model %s success", modelPath); - return SUCCESS; -} - -Result ModelProcess::CreateDesc() { - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { - ERROR_LOG("create model description failed"); - return FAILED; - } - - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("get model description failed"); - return FAILED; - } - - INFO_LOG("create model description success"); - - return SUCCESS; -} - -void ModelProcess::DestroyDesc() { - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } -} - -Result ModelProcess::CreateInput(const std::vector<void *> &inputDataBuffer, const std::vector<size_t> &bufferSize) { - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { - ERROR_LOG("can't create dataset, create input failed"); - return FAILED; - } - for (size_t i = 0; i < inputDataBuffer.size(); ++i) { - aclDataBuffer* inputData = aclCreateDataBuffer(inputDataBuffer[i], bufferSize[i]); - if (inputData == nullptr) { - ERROR_LOG("can't create data buffer, create input failed"); - return FAILED; - } - - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("add input dataset buffer failed"); - aclDestroyDataBuffer(inputData); - inputData = nullptr; - return FAILED; - } - } - return SUCCESS; -} - -void ModelProcess::DestroyInput() { - if (input_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); - aclDestroyDataBuffer(dataBuffer); - } - aclmdlDestroyDataset(input_); - input_ = nullptr; -} - -Result ModelProcess::CreateOutput() { - if (modelDesc_ == nullptr) { - ERROR_LOG("no model description, create output failed"); - return FAILED; - } - - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { - ERROR_LOG("can't create dataset, create output failed"); - return FAILED; - } - - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); - for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); - - void *outputBuffer = nullptr; - aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't malloc buffer, size is %zu, create output failed", buffer_size); - return FAILED; - } - - aclDataBuffer* outputData = aclCreateDataBuffer(outputBuffer, buffer_size); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't create data buffer, create output failed"); - aclrtFree(outputBuffer); - return FAILED; - } - - ret = aclmdlAddDatasetBuffer(output_, outputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't add data buffer, create output failed"); - aclrtFree(outputBuffer); - aclDestroyDataBuffer(outputData); - return FAILED; - } - } - - INFO_LOG("create model output success"); - return SUCCESS; -} - -void ModelProcess::DumpModelOutputResult(char *output_name) { - size_t outputNum = aclmdlGetDatasetNumBuffers(output_); - - for (size_t i = 0; i < outputNum; ++i) { - std::stringstream ss; - ss << "result/" << output_name << "_output_" << i << ".bin"; - std::string outputFileName = ss.str(); - FILE *outputFile = fopen(outputFileName.c_str(), "wb"); - if (outputFile != nullptr) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void* outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - (void)aclrtFreeHost(outHostData); - return; - } - - fwrite(outHostData, len, sizeof(char), outputFile); - - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } else { - fwrite(data, len, sizeof(char), outputFile); - } - fclose(outputFile); - outputFile = nullptr; - } else { - ERROR_LOG("create output file [%s] failed", outputFileName.c_str()); - return; - } - } - - INFO_LOG("dump data success"); - return; -} - -void ModelProcess::OutputModelResult() { - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void *outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - float *outData = NULL; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - return; - } - - outData = reinterpret_cast<float*>(outHostData); - } else { - outData = reinterpret_cast<float*>(data); - } - std::map<float, unsigned int, std::greater<float> > resultMap; - for (unsigned int j = 0; j < len / sizeof(float); ++j) { - resultMap[*outData] = j; - outData++; - } - - int cnt = 0; - for (auto it = resultMap.begin(); it != resultMap.end(); ++it) { - // print top 5 - if (++cnt > 5) { - break; - } - - INFO_LOG("top %d: index[%d] value[%lf]", cnt, it->second, it->first); - } - if (!g_is_device) { - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } - } - - INFO_LOG("output data success"); - return; -} - -void ModelProcess::DestroyOutput() { - if (output_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - (void)aclrtFree(data); - (void)aclDestroyDataBuffer(dataBuffer); - } - - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; -} - -Result ModelProcess::Execute() { - aclError ret = aclmdlExecute(modelId_, input_, output_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); - return FAILED; - } - - INFO_LOG("model execute success"); - return SUCCESS; -} - -void ModelProcess::Unload() { - if (!loadFlag_) { - WARN_LOG("no model had been loaded, unload failed"); - return; - } - - aclError ret = aclmdlUnload(modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); - } - - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } - - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; - } - - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; - } - - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); -} diff --git a/official/cv/maskrcnn/ascend310_quant_infer/src/sample_process.cpp b/official/cv/maskrcnn/ascend310_quant_infer/src/sample_process.cpp deleted file mode 100644 index 10e36d747d7dd42643d750948fe9a69913cff315..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/src/sample_process.cpp +++ /dev/null @@ -1,263 +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 "../inc/sample_process.h" -#include <sys/time.h> -#include <sys/types.h> -#include <dirent.h> -#include <string.h> -#include <iostream> -#include <fstream> -#include "../inc/model_process.h" -#include "acl/acl.h" -#include "../inc/utils.h" -extern bool g_is_device; -using std::string; -using std::vector; - -SampleProcess::SampleProcess() :deviceId_(0), context_(nullptr), stream_(nullptr) { -} - -SampleProcess::~SampleProcess() { - DestroyResource(); -} - -Result SampleProcess::InitResource() { - // ACL init - - const char *aclConfigPath = "./src/acl.json"; - aclError ret = aclInit(aclConfigPath); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl init failed"); - return FAILED; - } - INFO_LOG("acl init success"); - - // open device - ret = aclrtSetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl open device %d failed", deviceId_); - return FAILED; - } - INFO_LOG("open device %d success", deviceId_); - - // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create context failed"); - return FAILED; - } - INFO_LOG("create context success"); - - // create stream - ret = aclrtCreateStream(&stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create stream failed"); - return FAILED; - } - INFO_LOG("create stream success"); - - // get run mode - aclrtRunMode runMode; - ret = aclrtGetRunMode(&runMode); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl get run mode failed"); - return FAILED; - } - g_is_device = (runMode == ACL_DEVICE); - INFO_LOG("get run mode success"); - return SUCCESS; -} - -void SampleProcess::GetAllFiles(std::string path, std::vector<string> *files) { - DIR *pDir = NULL; - struct dirent* ptr = nullptr; - if (!(pDir = opendir(path.c_str()))) { - return; - } - while ((ptr = readdir(pDir)) != 0) { - if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { - files->push_back(path + "/" + ptr->d_name); - } - } - closedir(pDir); -} - -Result SampleProcess::Process(char *om_path, char *input_folder, char *shape_folder) { - // model init - const double second_to_millisecond = 1000; - const double second_to_microsecond = 1000000; - - double whole_cost_time = 0.0; - struct timeval start_global = {0}; - struct timeval end_global = {0}; - double startTimeMs_global = 0.0; - double endTimeMs_global = 0.0; - - gettimeofday(&start_global, nullptr); - - ModelProcess processModel; - const char* omModelPath = om_path; - - Result ret = processModel.LoadModelFromFileWithMem(omModelPath); - if (ret != SUCCESS) { - ERROR_LOG("execute LoadModelFromFileWithMem failed"); - return FAILED; - } - - ret = processModel.CreateDesc(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateDesc failed"); - return FAILED; - } - - ret = processModel.CreateOutput(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateOutput failed"); - return FAILED; - } - - std::vector<string> testFile; - GetAllFiles(input_folder, &testFile); - std::vector<string> shapeFile; - GetAllFiles(shape_folder, &shapeFile); - - if (testFile.size() !=shapeFile.size()) { - ERROR_LOG("number of data files is not equal to shape file"); - } - - double model_cost_time = 0.0; - double edge_to_edge_model_cost_time = 0.0; - - for (size_t index = 0; index < testFile.size(); ++index) { - INFO_LOG("start to process data file:%s", testFile[index].c_str()); - INFO_LOG("start to process shape file:%s", shapeFile[index].c_str()); - // model process - - struct timeval time_init = {0}; - double timeval_init = 0.0; - gettimeofday(&time_init, nullptr); - timeval_init = (time_init.tv_sec * second_to_microsecond + time_init.tv_usec) / second_to_millisecond; - - uint32_t devBufferSize; - void *picDevBuffer = Utils::GetDeviceBufferOfFile(testFile[index], &devBufferSize); - if (picDevBuffer == nullptr) { - ERROR_LOG("get pic device buffer failed, index is %zu", index); - return FAILED; - } - - uint32_t devBufferShapeSize; - void *shapeDevBuffer = Utils::GetDeviceBufferOfFile(shapeFile[index], &devBufferShapeSize); - if (shapeDevBuffer == nullptr) { - ERROR_LOG("get shape device buffer failed, index is %zu", index); - return FAILED; - } - - std::vector<void *> inputBuffers({picDevBuffer, shapeDevBuffer}); - std::vector<size_t> inputSizes({devBufferSize, devBufferShapeSize}); - - ret = processModel.CreateInput(inputBuffers, inputSizes); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateInput failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - struct timeval start = {0}; - struct timeval end = {0}; - gettimeofday(&start, nullptr); - double startTimeMs = (start.tv_sec * second_to_microsecond + start.tv_usec) / second_to_millisecond; - - ret = processModel.Execute(); - - gettimeofday(&end, nullptr); - double endTimeMs = (end.tv_sec * second_to_microsecond + end.tv_usec) / second_to_millisecond; - - double cost_time = endTimeMs - startTimeMs; - INFO_LOG("model infer time: %lf ms", cost_time); - - model_cost_time += cost_time; - - double edge_to_edge_cost_time = endTimeMs - timeval_init; - edge_to_edge_model_cost_time += edge_to_edge_cost_time; - - if (ret != SUCCESS) { - ERROR_LOG("execute inference failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - int pos = testFile[index].find_last_of('/'); - std::string name = testFile[index].substr(pos+1); - std::string outputname = name.substr(0, name.rfind(".")); - - // dump output result to file in the current directory - processModel.DumpModelOutputResult(const_cast<char *>(outputname.c_str())); - - // release model input buffer - aclrtFree(picDevBuffer); - processModel.DestroyInput(); - } - double test_file_size = testFile.size(); - INFO_LOG("infer dataset size:%lf", test_file_size); - - gettimeofday(&end_global, nullptr); - startTimeMs_global = (start_global.tv_sec * second_to_microsecond + start_global.tv_usec) / second_to_millisecond; - endTimeMs_global = (end_global.tv_sec * second_to_microsecond + end_global.tv_usec) / second_to_millisecond; - whole_cost_time = (endTimeMs_global - startTimeMs_global) / test_file_size; - - model_cost_time /= test_file_size; - INFO_LOG("model cost time per sample: %lf ms", model_cost_time); - edge_to_edge_model_cost_time /= test_file_size; - INFO_LOG("edge-to-edge model cost time per sample:%lf ms", edge_to_edge_model_cost_time); - INFO_LOG("whole cost time per sample: %lf ms", whole_cost_time); - - return SUCCESS; -} - -void SampleProcess::DestroyResource() { - aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy stream failed"); - } - stream_ = nullptr; - } - INFO_LOG("end to destroy stream"); - - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy context failed"); - } - context_ = nullptr; - } - INFO_LOG("end to destroy context"); - - ret = aclrtResetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("reset device failed"); - } - INFO_LOG("end to reset device is %d", deviceId_); - - ret = aclFinalize(); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("finalize acl failed"); - } - INFO_LOG("end to finalize acl"); -} - diff --git a/official/cv/maskrcnn/ascend310_quant_infer/src/utils.cpp b/official/cv/maskrcnn/ascend310_quant_infer/src/utils.cpp deleted file mode 100644 index d9208c8cfd9979e9248046e7325f260eb2d14d80..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/src/utils.cpp +++ /dev/null @@ -1,113 +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 "../inc/utils.h" -#include <sys/stat.h> -#include <iostream> -#include <fstream> -#include <cstring> -#include "acl/acl.h" - -extern bool g_is_device; - -void* Utils::ReadBinFile(std::string fileName, uint32_t *fileSize) { - struct stat sBuf; - int fileStatus = stat(fileName.data(), &sBuf); - if (fileStatus == -1) { - ERROR_LOG("failed to get file"); - return nullptr; - } - if (S_ISREG(sBuf.st_mode) == 0) { - ERROR_LOG("%s is not a file, please enter a file", fileName.c_str()); - return nullptr; - } - - std::ifstream binFile(fileName, std::ifstream::binary); - if (binFile.is_open() == false) { - ERROR_LOG("open file %s failed", fileName.c_str()); - return nullptr; - } - - binFile.seekg(0, binFile.end); - uint32_t binFileBufferLen = binFile.tellg(); - if (binFileBufferLen == 0) { - ERROR_LOG("binfile is empty, filename is %s", fileName.c_str()); - binFile.close(); - return nullptr; - } - - binFile.seekg(0, binFile.beg); - - void* binFileBufferData = nullptr; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&binFileBufferData, binFileBufferLen); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc for binFileBufferData failed"); - binFile.close(); - return nullptr; - } - if (binFileBufferData == nullptr) { - ERROR_LOG("malloc binFileBufferData failed"); - binFile.close(); - return nullptr; - } - } else { - ret = aclrtMalloc(&binFileBufferData, binFileBufferLen, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", binFileBufferLen); - binFile.close(); - return nullptr; - } - } - binFile.read(static_cast<char *>(binFileBufferData), binFileBufferLen); - binFile.close(); - *fileSize = binFileBufferLen; - return binFileBufferData; -} - -void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize) { - uint32_t inputHostBuffSize = 0; - void* inputHostBuff = Utils::ReadBinFile(fileName, &inputHostBuffSize); - if (inputHostBuff == nullptr) { - return nullptr; - } - if (!g_is_device) { - void *inBufferDev = nullptr; - uint32_t inBufferSize = inputHostBuffSize; - aclError ret = aclrtMalloc(&inBufferDev, inBufferSize, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", inBufferSize); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - - ret = aclrtMemcpy(inBufferDev, inBufferSize, inputHostBuff, inputHostBuffSize, ACL_MEMCPY_HOST_TO_DEVICE); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("memcpy failed. device buffer size is %u, input host buffer size is %u", - inBufferSize, inputHostBuffSize); - aclrtFree(inBufferDev); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - aclrtFreeHost(inputHostBuff); - *fileSize = inBufferSize; - return inBufferDev; - } else { - *fileSize = inputHostBuffSize; - return inputHostBuff; - } -} diff --git a/official/cv/maskrcnn/ascend310_quant_infer/util.py b/official/cv/maskrcnn/ascend310_quant_infer/util.py deleted file mode 100644 index 391bc24a47d5413c8800cc93c2e12348abb61853..0000000000000000000000000000000000000000 --- a/official/cv/maskrcnn/ascend310_quant_infer/util.py +++ /dev/null @@ -1,277 +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. -# ============================================================================ -"""coco eval for maskrcnn""" -import json -import cv2 -import numpy as np -from pycocotools.coco import COCO -from pycocotools.cocoeval import COCOeval -from pycocotools import mask as maskUtils - - -_init_value = np.array(0.0) -summary_init = { - 'Precision/mAP': _init_value, - 'Precision/mAP@.50IOU': _init_value, - 'Precision/mAP@.75IOU': _init_value, - 'Precision/mAP (small)': _init_value, - 'Precision/mAP (medium)': _init_value, - 'Precision/mAP (large)': _init_value, - 'Recall/AR@1': _init_value, - 'Recall/AR@10': _init_value, - 'Recall/AR@100': _init_value, - 'Recall/AR@100 (small)': _init_value, - 'Recall/AR@100 (medium)': _init_value, - 'Recall/AR@100 (large)': _init_value, -} - - -def coco_eval(result_files, result_types, coco, max_dets=(100, 300, 1000), single_result=False): - """coco eval for maskrcnn""" - anns = json.load(open(result_files['bbox'])) - if not anns: - return summary_init - if isinstance(coco, str): - coco = COCO(coco) - assert isinstance(coco, COCO) - - for res_type in result_types: - result_file = result_files[res_type] - assert result_file.endswith('.json') - - coco_dets = coco.loadRes(result_file) - gt_img_ids = coco.getImgIds() - det_img_ids = coco_dets.getImgIds() - iou_type = 'bbox' if res_type == 'proposal' else res_type - cocoEval = COCOeval(coco, coco_dets, iou_type) - if res_type == 'proposal': - cocoEval.params.useCats = 0 - cocoEval.params.maxDets = list(max_dets) - - tgt_ids = gt_img_ids if not single_result else det_img_ids - - if single_result: - res_dict = dict() - for id_i in tgt_ids: - cocoEval = COCOeval(coco, coco_dets, iou_type) - if res_type == 'proposal': - cocoEval.params.useCats = 0 - cocoEval.params.maxDets = list(max_dets) - - cocoEval.params.imgIds = [id_i] - cocoEval.evaluate() - cocoEval.accumulate() - cocoEval.summarize() - res_dict.update({coco.imgs[id_i]['file_name']: cocoEval.stats[1]}) - - cocoEval = COCOeval(coco, coco_dets, iou_type) - if res_type == 'proposal': - cocoEval.params.useCats = 0 - cocoEval.params.maxDets = list(max_dets) - - cocoEval.params.imgIds = tgt_ids - cocoEval.evaluate() - cocoEval.accumulate() - cocoEval.summarize() - - summary_metrics = { - 'Precision/mAP': cocoEval.stats[0], - 'Precision/mAP@.50IOU': cocoEval.stats[1], - 'Precision/mAP@.75IOU': cocoEval.stats[2], - 'Precision/mAP (small)': cocoEval.stats[3], - 'Precision/mAP (medium)': cocoEval.stats[4], - 'Precision/mAP (large)': cocoEval.stats[5], - 'Recall/AR@1': cocoEval.stats[6], - 'Recall/AR@10': cocoEval.stats[7], - 'Recall/AR@100': cocoEval.stats[8], - 'Recall/AR@100 (small)': cocoEval.stats[9], - 'Recall/AR@100 (medium)': cocoEval.stats[10], - 'Recall/AR@100 (large)': cocoEval.stats[11], - } - - return summary_metrics - - -def xyxy2xywh(bbox): - """convert format of coordinate point""" - _bbox = bbox.tolist() - return [ - _bbox[0], - _bbox[1], - _bbox[2] - _bbox[0] + 1, - _bbox[3] - _bbox[1] + 1, - ] - -def bbox2result_1image(bboxes, labels, num_classes): - """Convert detection results to a list of numpy arrays. - - Args: - bboxes (Tensor): shape (n, 5) - labels (Tensor): shape (n, ) - num_classes (int): class number, including background class - - Returns: - list(ndarray): bbox results of each class - """ - if bboxes.shape[0] == 0: - result = [np.zeros((0, 5), dtype=np.float32) for i in range(num_classes - 1)] - else: - result = [bboxes[labels == i, :] for i in range(num_classes - 1)] - - return result - -def proposal2json(dataset, results): - """convert proposal to json mode""" - img_ids = dataset.getImgIds() - json_results = [] - dataset_len = dataset.get_dataset_size()*2 - for idx in range(dataset_len): - img_id = img_ids[idx] - bboxes = results[idx] - for i in range(bboxes.shape[0]): - data = dict() - data['image_id'] = img_id - data['bbox'] = xyxy2xywh(bboxes[i]) - data['score'] = float(bboxes[i][4]) - data['category_id'] = 1 - json_results.append(data) - return json_results - -def det2json(dataset, results): - """convert det to json mode""" - cat_ids = dataset.getCatIds() - img_ids = dataset.getImgIds() - json_results = [] - dataset_len = len(img_ids) - for idx in range(dataset_len): - img_id = img_ids[idx] - if idx == len(results): break - result = results[idx] - for label, result_label in enumerate(result): - bboxes = result_label - for i in range(bboxes.shape[0]): - data = dict() - data['image_id'] = img_id - data['bbox'] = xyxy2xywh(bboxes[i]) - data['score'] = float(bboxes[i][4]) - data['category_id'] = cat_ids[label] - json_results.append(data) - return json_results - -def segm2json(dataset, results): - """convert segm to json mode""" - cat_ids = dataset.getCatIds() - img_ids = dataset.getImgIds() - bbox_json_results = [] - segm_json_results = [] - - dataset_len = len(img_ids) - assert dataset_len == len(results) - for idx in range(dataset_len): - img_id = img_ids[idx] - if idx == len(results): break - det, seg = results[idx] - for label, det_label in enumerate(det): - bboxes = det_label - for i in range(bboxes.shape[0]): - data = dict() - data['image_id'] = img_id - data['bbox'] = xyxy2xywh(bboxes[i]) - data['score'] = float(bboxes[i][4]) - data['category_id'] = cat_ids[label] - bbox_json_results.append(data) - - if len(seg) == 2: - segms = seg[0][label] - mask_score = seg[1][label] - else: - segms = seg[label] - mask_score = [bbox[4] for bbox in bboxes] - for i in range(bboxes.shape[0]): - data = dict() - data['image_id'] = img_id - data['score'] = float(mask_score[i]) - data['category_id'] = cat_ids[label] - segms[i]['counts'] = segms[i]['counts'].decode() - data['segmentation'] = segms[i] - segm_json_results.append(data) - return bbox_json_results, segm_json_results - -def results2json(dataset, results, out_file): - """convert result convert to json mode""" - result_files = dict() - if isinstance(results[0], list): - json_results = det2json(dataset, results) - result_files['bbox'] = '{}.{}.json'.format(out_file, 'bbox') - result_files['proposal'] = '{}.{}.json'.format(out_file, 'bbox') - with open(result_files['bbox'], 'w') as fp: - json.dump(json_results, fp) - elif isinstance(results[0], tuple): - json_results = segm2json(dataset, results) - result_files['bbox'] = '{}.{}.json'.format(out_file, 'bbox') - result_files['segm'] = '{}.{}.json'.format(out_file, 'segm') - with open(result_files['bbox'], 'w') as fp: - json.dump(json_results[0], fp) - with open(result_files['segm'], 'w') as fp: - json.dump(json_results[1], fp) - elif isinstance(results[0], np.ndarray): - json_results = proposal2json(dataset, results) - result_files['proposal'] = '{}.{}.json'.format(out_file, 'proposal') - with open(result_files['proposal'], 'w') as fp: - json.dump(json_results, fp) - else: - raise TypeError('invalid type of results') - return result_files - -def get_seg_masks(mask_pred, det_bboxes, det_labels, img_meta, rescale, num_classes): - """Get segmentation masks from mask_pred and bboxes""" - mask_pred = mask_pred.astype(np.float32) - - cls_segms = [[] for _ in range(num_classes - 1)] - bboxes = det_bboxes[:, :4] - labels = det_labels + 1 - - ori_shape = img_meta[:2].astype(np.int32) - scale_factor = img_meta[2:].astype(np.int32) - - if rescale: - img_h, img_w = ori_shape[:2] - else: - img_h = np.round(ori_shape[0] * scale_factor[0]).astype(np.int32) - img_w = np.round(ori_shape[1] * scale_factor[1]).astype(np.int32) - - for i in range(bboxes.shape[0]): - bbox = (bboxes[i, :] / 1.0).astype(np.int32) - label = labels[i] - w = max(bbox[2] - bbox[0] + 1, 1) - h = max(bbox[3] - bbox[1] + 1, 1) - w = min(w, img_w - bbox[0]) - h = min(h, img_h - bbox[1]) - if w <= 0 or h <= 0: - print("there is invalid proposal bbox, index={} bbox={} w={} h={}".format(i, bbox, w, h)) - w = max(w, 1) - h = max(h, 1) - mask_pred_ = mask_pred[i, :, :] - im_mask = np.zeros((img_h, img_w), dtype=np.uint8) - bbox_mask = cv2.resize(mask_pred_, (w, h), interpolation=cv2.INTER_LINEAR) - mask_thr_binary = 0.5 - bbox_mask = (bbox_mask > mask_thr_binary).astype(np.uint8) - im_mask[bbox[1]:bbox[1] + h, bbox[0]:bbox[0] + w] = bbox_mask - - rle = maskUtils.encode( - np.array(im_mask[:, :, np.newaxis], order='F'))[0] - cls_segms[label - 1].append(rle) - - return cls_segms diff --git a/official/cv/ssd/README.md b/official/cv/ssd/README.md index 3ab719b6b5beade3056d0d6e7ff1b40bee4c916e..1298b5b49a54b8ee6c796d6de0309b341abdfeb9 100644 --- a/official/cv/ssd/README.md +++ b/official/cv/ssd/README.md @@ -24,7 +24,6 @@ - [Export MindIR](#export-mindir) - [Infer on Ascend310](#infer-on-ascend310) - [result](#result) - - [Post Training Quantization](#post-training-quantization) - [Model Description](#model-description) - [Performance](#performance) - [Description of Random Situation](#description-of-random-situation) @@ -571,52 +570,6 @@ Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.659 mAP: 0.33880018942412393 ``` -### [Post Training Quantization](#contents) - -Relative executing script files reside in the directory "ascend310_quant_infer". Please implement following steps sequentially to complete post quantization. -Current quantization project bases on COCO2017 dataset. - -1. Generate data of .bin format required for AIR model inference at Ascend310 platform. - -```shell -python export_bin.py --config_path [YMAL CONFIG PATH] --coco_root [COCO DATA DIR] --mindrecord_dir [MINDRECORD PATH] -``` - -2. Export quantized AIR model. - -Post quantization of model requires special toolkits for exporting quantized AIR model. Please refer to [official website](https://www.hiascend.com/software/cann/community). - -```shell -python post_quant.py --config_path [YMAL CONFIG PATH] --checkpoint_path [CKPT_PATH] --coco_root [COCO DATA DIR] --mindrecord_dir [MINDRECORD PATH] -``` - -The quantized AIR file will be stored as "./results/ssd_quant.air". - -3. Implement inference at Ascend310 platform. - -```shell -# Ascend310 quant inference -bash run_quant_infer.sh [AIR_PATH] [IMAGE_DATA] [IMAGE_ID] [IMAGE_SHAPE] [ANN_FILE] -``` - -Inference result is saved in current path, you can find result like this in acc.log file. - -```bash -Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.237 -Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.386 -Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.240 -Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.042 -Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.200 -Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.425 -Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.255 -Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.404 -Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.441 -Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.136 -Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.455 -Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.707 -mAP: 0.23657619676441116 -``` - ## [Model Description](#contents) ### [Performance](#contents) diff --git a/official/cv/ssd/README_CN.md b/official/cv/ssd/README_CN.md index 40fed4347d7856fc20070b9d2f7cc338b997b00f..20dfd85491d4841913bf471a67ca7268f8dd4550 100644 --- a/official/cv/ssd/README_CN.md +++ b/official/cv/ssd/README_CN.md @@ -21,7 +21,6 @@ - [导出MindIR](#导出mindir) - [在Ascend310执行推理](#在ascend310执行推理) - [结果](#结果) - - [训练后量化推理](#训练后量化推理) - [模型描述](#模型描述) - [性能](#性能) - [随机情况说明](#随机情况说明) @@ -459,51 +458,6 @@ Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.659 mAP: 0.33880018942412393 ``` -### [训练后量化推理](#contents) - -训练后量化推理的相关执行脚本文件在"ascend310_quant_infer"目录下,依次执行以下步骤实现训练后量化推理。本训练后量化工程基于COCO2017数据集。 - -1、生成Ascend310平台AIR模型推理需要的.bin格式数据。 - -```shell -python export_bin.py --config_path [YMAL CONFIG PATH] --coco_root [COCO DATA DIR] --mindrecord_dir [MINDRECORD PATH] -``` - -2、导出训练后量化的AIR格式模型。 - -导出训练后量化模型需要配套的量化工具包,参考[官方地址](https://www.hiascend.com/software/cann/community) - -```shell -python post_quant.py --config_path [YMAL CONFIG PATH] --checkpoint_path [CKPT_PATH] --coco_root [COCO DATA DIR] --mindrecord_dir [MINDRECORD PATH] -``` - -导出的模型会存储在./result/ssd_quant.air。 - -3、在Ascend310执行推理量化模型。 - -```shell -# Ascend310 quant inference -bash run_quant_infer.sh [AIR_PATH] [IMAGE_DATA] [IMAGE_ID] [IMAGE_SHAPE] [ANN_FILE] -``` - -推理结果保存在脚本执行的当前路径,可以在acc.log中看到精度计算结果。 - -```bash -Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.237 -Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.386 -Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.240 -Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.042 -Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.200 -Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.425 -Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.255 -Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.404 -Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.441 -Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.136 -Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.455 -Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.707 -mAP: 0.23657619676441116 -``` - # 模型描述 ## 性能 diff --git a/official/cv/ssd/ascend310_quant_infer/acc.py b/official/cv/ssd/ascend310_quant_infer/acc.py deleted file mode 100644 index a07505e4eb5a51b8837ec00d0e2f92d01a0db13f..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/acc.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. -# ============================================================================ -"""postprocess for 310 inference""" -import os -import json -import argparse -import numpy as np -from pycocotools.coco import COCO -from pycocotools.cocoeval import COCOeval - - -parser = argparse.ArgumentParser("ssd quant postprocess") -parser.add_argument("--result_path", type=str, required=True, help="path to inference results.") -parser.add_argument("--image_shape", type=str, required=True, help="path to image shape directory.") -parser.add_argument("--image_id", type=str, required=True, help="path to image id directory.") -parser.add_argument("--ann_file", type=str, required=True, help="path to annotation file.") -parser.add_argument("--min_score", type=float, default=0.1, help="min box score threshold.") -parser.add_argument("--nms_threshold", type=float, default=0.6, help="threshold of nms process.") -parser.add_argument("--max_boxes", type=int, default=100, help="max number of detection boxes.") - -args, _ = parser.parse_known_args() - - -def apply_nms(all_boxes, all_scores, thres, max_boxes): - """Apply NMS to bboxes.""" - y1 = all_boxes[:, 0] - x1 = all_boxes[:, 1] - y2 = all_boxes[:, 2] - x2 = all_boxes[:, 3] - areas = (x2 - x1 + 1) * (y2 - y1 + 1) - - order = all_scores.argsort()[::-1] - keep = [] - - while order.size > 0: - i = order[0] - keep.append(i) - - if len(keep) >= max_boxes: - break - - xx1 = np.maximum(x1[i], x1[order[1:]]) - yy1 = np.maximum(y1[i], y1[order[1:]]) - xx2 = np.minimum(x2[i], x2[order[1:]]) - yy2 = np.minimum(y2[i], y2[order[1:]]) - - w = np.maximum(0.0, xx2 - xx1 + 1) - h = np.maximum(0.0, yy2 - yy1 + 1) - inter = w * h - - ovr = inter / (areas[i] + areas[order[1:]] - inter) - - inds = np.where(ovr <= thres)[0] - - order = order[inds + 1] - return keep - - -def metrics(pred_data, anno_json): - """Calculate mAP of predicted bboxes.""" - - #Classes need to train or test. - val_cls = ['background', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', - 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', - 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', - 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', - 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', - 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', - 'kite', 'baseball bat', 'baseball glove', 'skateboard', - 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', - 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', - 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', - 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', - 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', - 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', - 'refrigerator', 'book', 'clock', 'vase', 'scissors', - 'teddy bear', 'hair drier', 'toothbrush'] - num_classes = len(val_cls) - val_cls_dict = {} - for i, cls in enumerate(val_cls): - val_cls_dict[i] = cls - coco_gt = COCO(anno_json) - classs_dict = {} - cat_ids = coco_gt.loadCats(coco_gt.getCatIds()) - for cat in cat_ids: - classs_dict[cat["name"]] = cat["id"] - - predictions = [] - img_ids = [] - - for sample in pred_data: - pred_boxes = sample['boxes'] - box_scores = sample['box_scores'] - img_id = sample['img_id'] - h, w = sample['image_shape'] - - final_boxes = [] - final_label = [] - final_score = [] - img_ids.append(img_id) - - for c in range(1, num_classes): - class_box_scores = box_scores[:, c] - score_mask = class_box_scores > args.min_score - class_box_scores = class_box_scores[score_mask] - class_boxes = pred_boxes[score_mask] * [h, w, h, w] - - if score_mask.any(): - nms_index = apply_nms(class_boxes, class_box_scores, args.nms_threshold, args.max_boxes) - class_boxes = class_boxes[nms_index] - class_box_scores = class_box_scores[nms_index] - - final_boxes += class_boxes.tolist() - final_score += class_box_scores.tolist() - final_label += [classs_dict[val_cls_dict[c]]] * len(class_box_scores) - - for loc, label, score in zip(final_boxes, final_label, final_score): - res = {} - res['image_id'] = img_id - res['bbox'] = [loc[1], loc[0], loc[3] - loc[1], loc[2] - loc[0]] - res['score'] = score - res['category_id'] = label - predictions.append(res) - with open('predictions.json', 'w') as f: - json.dump(predictions, f) - - coco_dt = coco_gt.loadRes('predictions.json') - E = COCOeval(coco_gt, coco_dt, iouType='bbox') - E.params.imgIds = img_ids - E.evaluate() - E.accumulate() - E.summarize() - return E.stats[0] - - -def calculate_acc(result_path, image_id): - """ - Calculate accuracy of VGG16 inference. - - Args: - result_path (str): the directory or inference result. - image_id (str): the path of image_id directory. - """ - pred_data = [] - for file in os.listdir(image_id): - id_num = int(np.fromfile(os.path.join(image_id, file), dtype=np.int32)[0]) - img_size = np.fromfile(os.path.join(args.image_shape, file), dtype=np.float32).reshape(1, -1)[0] - img_ids_name = file.split(".")[0] - image_shape = np.array([img_size[0], img_size[1]]) - result_path_0 = os.path.join(result_path, img_ids_name + "_output_0.bin") - result_path_1 = os.path.join(result_path, img_ids_name + "_output_1.bin") - boxes = np.fromfile(result_path_0, dtype=np.float32).reshape(1917, 4) - box_scores = np.fromfile(result_path_1, dtype=np.float32).reshape(1917, 81) - pred_data.append({"boxes": boxes, "box_scores": box_scores, "img_id": id_num, "image_shape": image_shape}) - mAP = metrics(pred_data, args.ann_file) - print(f" mAP:{mAP}") - - -if __name__ == '__main__': - calculate_acc(args.result_path, args.image_id) diff --git a/official/cv/ssd/ascend310_quant_infer/config.cfg b/official/cv/ssd/ascend310_quant_infer/config.cfg deleted file mode 100644 index f5da94987ca5dceab7ac820c7c39cdfab17a5a27..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/config.cfg +++ /dev/null @@ -1 +0,0 @@ -do_fusion:false \ No newline at end of file diff --git a/official/cv/ssd/ascend310_quant_infer/export_bin.py b/official/cv/ssd/ascend310_quant_infer/export_bin.py deleted file mode 100644 index 8ff4c3637e80724e9f84935be7ceec494989f82c..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/export_bin.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. -# ============================================================================ -"""generate data and label needed for AIR model inference""" -import os -import sys - - -def generate_data(): - """ - Generate data and label needed for AIR model inference at Ascend310 platform. - """ - mindrecord_file = create_mindrecord("coco", "ssd_eval.mindrecord", False) - batch_size = 1 - ds = create_ssd_dataset(mindrecord_file, batch_size=batch_size, repeat_num=1, is_training=False, - use_multiprocessing=False) - cur_dir = os.getcwd() - image_path = os.path.join(cur_dir, "./data/00_image_data") - if not os.path.isdir(image_path): - os.makedirs(image_path) - img_id_path = os.path.join(cur_dir, "./data/01_image_id") - if not os.path.isdir(img_id_path): - os.makedirs(img_id_path) - img_shape_path = os.path.join(cur_dir, "./data/02_image_shape") - if not os.path.isdir(img_shape_path): - os.makedirs(img_shape_path) - total = ds.get_dataset_size() - iter_num = 0 - for data in ds.create_dict_iterator(output_numpy=True, num_epochs=1): - file_name = "coco_bs_" + str(batch_size) + "_" + str(iter_num) + ".bin" - img_id = data["img_id"] - img_np = data["image"] - img_shape = data["image_shape"] - img_id.tofile(os.path.join(img_id_path, file_name)) - img_np.tofile(os.path.join(image_path, file_name)) - img_shape.tofile(os.path.join(img_shape_path, file_name)) - - iter_num += 1 - print("total images num: ", total) - - -if __name__ == "__main__": - sys.path.append("..") - from src.dataset import create_ssd_dataset, create_mindrecord - - generate_data() diff --git a/official/cv/ssd/ascend310_quant_infer/inc/model_process.h b/official/cv/ssd/ascend310_quant_infer/inc/model_process.h deleted file mode 100644 index 79e19833c7a1b3e428458c23aaba953dc40b1f01..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/inc/model_process.h +++ /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. - */ - -#pragma once -#include <iostream> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* ModelProcess -*/ -class ModelProcess { - public: - /** - * @brief Constructor - */ - ModelProcess(); - - /** - * @brief Destructor - */ - ~ModelProcess(); - - /** - * @brief load model from file with mem - * @param [in] modelPath: model path - * @return result - */ - Result LoadModelFromFileWithMem(const char *modelPath); - - /** - * @brief unload model - */ - void Unload(); - - /** - * @brief create model desc - * @return result - */ - Result CreateDesc(); - - /** - * @brief destroy desc - */ - void DestroyDesc(); - - /** - * @brief create model input - * @param [in] inputDataBuffer: input buffer - * @param [in] bufferSize: input buffer size - * @return result - */ - Result CreateInput(void *inputDataBuffer, size_t bufferSize); - - /** - * @brief destroy input resource - */ - void DestroyInput(); - - /** - * @brief create output buffer - * @return result - */ - Result CreateOutput(); - - /** - * @brief destroy output resource - */ - void DestroyOutput(); - - /** - * @brief model execute - * @return result - */ - Result Execute(); - - /** - * @brief dump model output result to file - */ - void DumpModelOutputResult(char *output_name); - - /** - * @brief get model output result - */ - void OutputModelResult(); - - private: - uint32_t modelId_; - size_t modelMemSize_; - size_t modelWeightSize_; - void *modelMemPtr_; - void *modelWeightPtr_; - bool loadFlag_; // model load flag - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; -}; - diff --git a/official/cv/ssd/ascend310_quant_infer/inc/sample_process.h b/official/cv/ssd/ascend310_quant_infer/inc/sample_process.h deleted file mode 100644 index 24d6ea01e59925673a548a7873ab310623235549..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/inc/sample_process.h +++ /dev/null @@ -1,58 +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. - */ - -#pragma once -#include <string> -#include <vector> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* SampleProcess -*/ -class SampleProcess { - public: - /** - * @brief Constructor - */ - SampleProcess(); - - /** - * @brief Destructor - */ - ~SampleProcess(); - - /** - * @brief init reousce - * @return result - */ - Result InitResource(); - - /** - * @brief sample process - * @return result - */ - Result Process(char *om_path, char *input_folder); - - void GetAllFiles(std::string path, std::vector<std::string> *files); - - private: - void DestroyResource(); - - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; -}; diff --git a/official/cv/ssd/ascend310_quant_infer/inc/utils.h b/official/cv/ssd/ascend310_quant_infer/inc/utils.h deleted file mode 100644 index 3ae2a571b8e8ba51c01b02e23f36dfad5a7b18f1..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/inc/utils.h +++ /dev/null @@ -1,52 +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. - */ - -#pragma once -#include <iostream> -#include <string> - -#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) -#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args) -#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) - -typedef enum Result { - SUCCESS = 0, - FAILED = 1 -} Result; - -/** -* Utils -*/ -class Utils { - public: - /** - * @brief create device buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return device buffer of file - */ - static void *GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize); - - /** - * @brief create buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return buffer of pic - */ - static void* ReadBinFile(std::string fileName, uint32_t *fileSize); -}; - -#pragma once diff --git a/official/cv/ssd/ascend310_quant_infer/post_quant.py b/official/cv/ssd/ascend310_quant_infer/post_quant.py deleted file mode 100644 index 5f10e88baa6ec01402cf0a326ed339af29f32dd0..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/post_quant.py +++ /dev/null @@ -1,89 +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. -# ============================================================================ -"""do post training quantization for Ascend310""" -import sys -import numpy as np - -from amct_mindspore.quantize_tool import create_quant_config -from amct_mindspore.quantize_tool import quantize_model -from amct_mindspore.quantize_tool import save_model -import mindspore -from mindspore import Tensor, context -from mindspore.train.serialization import load_checkpoint, load_param_into_net - - -def quant_ssd(network, dataset, input_data): - """ - Export post training quantization model of AIR format. - - Args: - network: the origin network for inference. - dataset: the data for inference. - input_data: the data used for constructing network. The shape and format of input data should be the same as - actual data for inference. - """ - - # step2: create the quant config json file - create_quant_config("./config.json", network, input_data, config_defination="./config.cfg") - - # step3: do some network modification and return the modified network - calibration_network = quantize_model("./config.json", network, input_data) - calibration_network.set_train(False) - - # step4: perform the evaluation of network to do activation calibration - for data in dataset.create_dict_iterator(num_epochs=1): - img_data = data["image"] - _ = calibration_network(img_data) - - # step5: export the air file - save_model("results/ssd_quant", calibration_network, input_data) - print("[INFO] the quantized AIR file has been stored at: \n {}".format("results/ssd_quant.air")) - - -def run_export(): - """ - Prepare input parameters needed for exporting quantization model. - """ - context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", device_id=config.device_id) - if hasattr(config, "num_ssd_boxes") and config.num_ssd_boxes == -1: - num = 0 - h, w = config.img_shape - for i in range(len(config.steps)): - num += (h // config.step[i]) * (w // config.step[i]) * config.num_default[i] - config.num_ssd_boxes = num - net = SSD300(ssd_mobilenet_v2(), config, is_training=False) - net = SsdInferWithDecoder(net, Tensor(default_boxes), config) - param_dict = load_checkpoint(config.checkpoint_path) - net.init_parameters_data() - load_param_into_net(net, param_dict) - net.set_train(False) - batch_size = 1 - input_shp = [batch_size, 3] + config.img_shape - inputs = Tensor(np.random.uniform(-1.0, 1.0, size=input_shp), mindspore.float32) - mindrecord_file = create_mindrecord("coco", "ssd_eval.mindrecord", False) - batch_size = 1 - datasets = create_ssd_dataset(mindrecord_file, batch_size=batch_size, repeat_num=1, is_training=False, - use_multiprocessing=False) - ds = datasets.take(1) - quant_ssd(net, ds, inputs) - - -if __name__ == "__main__": - sys.path.append("..") - from src.ssd import SSD300, SsdInferWithDecoder, ssd_mobilenet_v2 - from src.model_utils.config import config - from src.dataset import create_ssd_dataset, create_mindrecord - from src.box_utils import default_boxes - run_export() diff --git a/official/cv/ssd/ascend310_quant_infer/run_quant_infer.sh b/official/cv/ssd/ascend310_quant_infer/run_quant_infer.sh deleted file mode 100644 index ccda13914100eb5d7d903b8d38605d54b06e9fda..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/run_quant_infer.sh +++ /dev/null @@ -1,109 +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 5 ]; then - echo "Usage: bash run_quant_infer.sh [AIR_PATH] [IMAGE_DATA] [IMAGE_ID] [IMAGE_SHAPE] [ANN_FILE]" - echo "Example: bash run_quant_infer.sh ./ssd_quant.air ./00_image_data ./01_image_id ./02_image_shape \ -./instances_val2017.json" -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) -id_path=$(get_real_path $3) -shape_path=$(get_real_path $4) -ann_path=$(get_real_path $5) - -echo "air name: "$model -echo "image data path: "$data_path -echo "image id path: "$id_path -echo "image shape path: "$shape_path -echo "annotation path: "$ann_path - -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 ASCEND_HOME=/usr/local/Ascend/latest/ - export PATH=$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/fwkacllib/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/fwkacllib/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$LD_LIBRARY_PATH - export TBE_IMPL_PATH=$ASCEND_HOME/opp/op_impl/built-in/ai_core/tbe - export PYTHONPATH=${TBE_IMPL_PATH}:$PYTHONPATH - export ASCEND_OPP_PATH=$ASCEND_HOME/opp -fi - -function air_to_om() -{ - atc --input_format=NCHW --framework=1 --model=$model --output=ssd_quant --soc_version=Ascend310 &> atc.log -} - -function compile_app() -{ - bash ./src/build.sh &> build.log -} - -function infer() -{ - if [ -d result ]; then - rm -rf ./result - fi - mkdir result - ./out/main ./ssd_quant.om $data_path &> infer.log -} - -function cal_acc() -{ - python3.7 ./acc.py --result_path=./result --image_id=$id_path --image_shape=$shape_path --ann_file=$ann_path &> acc.log -} - -echo "start atc================================================" -air_to_om -if [ $? -ne 0 ]; then - echo "air to om code failed" - exit 1 -fi - -echo "start compile============================================" -compile_app -if [ $? -ne 0 ]; then - echo "compile app code failed" - exit 1 -fi - -echo "start infer==============================================" -infer -if [ $? -ne 0 ]; then - echo " execute inference failed" - exit 1 -fi - -echo "start calculate acc======================================" -cal_acc -if [ $? -ne 0 ]; then - echo "calculate accuracy failed" - exit 1 -fi \ No newline at end of file diff --git a/official/cv/ssd/ascend310_quant_infer/src/CMakeLists.txt b/official/cv/ssd/ascend310_quant_infer/src/CMakeLists.txt deleted file mode 100644 index 655026d7d91612267a287e83e886ba2ce1304d18..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/src/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. - -# CMake lowest version requirement -cmake_minimum_required(VERSION 3.5.1) -# project information -project(InferClassification) -# Check environment variable -if(NOT DEFINED ENV{ASCEND_HOME}) - message(FATAL_ERROR "please define environment variable:ASCEND_HOME") -endif() - -# Compile options -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") - -# Skip build rpath -set(CMAKE_SKIP_BUILD_RPATH True) - -# Set output directory -set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SRC_ROOT}/../out) - -# Set include directory and library directory -set(FWKACL_LIB_DIR $ENV{ASCEND_HOME}/fwkacllib) -set(ACL_LIB_DIR $ENV{ASCEND_HOME}/acllib) -set(ATLAS_ACL_LIB_DIR $ENV{ASCEND_HOME}/ascend-toolkit/latest/acllib) - -# Header path -include_directories(${ACL_LIB_DIR}/include/) -include_directories(${FWKACL_LIB_DIR}/include/) -include_directories(${ATLAS_ACL_LIB_DIR}/include/) -include_directories(${PROJECT_SRC_ROOT}/../inc) - -# add host lib path -link_directories(${ACL_LIB_DIR} ${FWKACL_LIB_DIR}) -find_library(acl libascendcl.so ${ACL_LIB_DIR}/lib64 ${FWKACL_LIB_DIR}/lib64 ${ATLAS_ACL_LIB_DIR}/lib64) - -add_executable(main utils.cpp - sample_process.cpp - model_process.cpp - main.cpp) - -target_link_libraries(main ${acl} gflags pthread) diff --git a/official/cv/ssd/ascend310_quant_infer/src/acl.json b/official/cv/ssd/ascend310_quant_infer/src/acl.json deleted file mode 100644 index 0967ef424bce6791893e9a57bb952f80fd536e93..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/src/acl.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/official/cv/ssd/ascend310_quant_infer/src/build.sh b/official/cv/ssd/ascend310_quant_infer/src/build.sh deleted file mode 100644 index b5979b68e60b673f763a3cac98c5e147e7085291..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/src/build.sh +++ /dev/null @@ -1,55 +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. -# ============================================================================ -path_cur=$(cd "`dirname $0`" || exit; pwd) - -function preparePath() { - rm -rf $1 - mkdir -p $1 - cd $1 || exit -} - -function buildA300() { - if [ ! "${ARCH_PATTERN}" ]; then - # set ARCH_PATTERN to acllib when it was not specified by user - export ARCH_PATTERN=acllib - echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}" - else - echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user, reset it to ${ARCH_PATTERN}/acllib" - export ARCH_PATTERN=${ARCH_PATTERN}/acllib - fi - - path_build=$path_cur/build - preparePath $path_build - cmake .. - make -j - ret=$? - cd .. - return ${ret} -} - -# 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 - -buildA300 - -if [ $? -ne 0 ]; then - exit 1 -fi diff --git a/official/cv/ssd/ascend310_quant_infer/src/main.cpp b/official/cv/ssd/ascend310_quant_infer/src/main.cpp deleted file mode 100644 index 80165505f447d418e0f107b76d04ffae59b89a73..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/src/main.cpp +++ /dev/null @@ -1,42 +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 <iostream> -#include "../inc/sample_process.h" -#include "../inc/utils.h" -bool g_is_device = false; - -int main(int argc, char **argv) { - if (argc != 3) { - ERROR_LOG("usage:./main path_of_om path_of_inputFolder"); - return FAILED; - } - SampleProcess processSample; - Result ret = processSample.InitResource(); - if (ret != SUCCESS) { - ERROR_LOG("sample init resource failed"); - return FAILED; - } - - ret = processSample.Process(argv[1], argv[2]); - if (ret != SUCCESS) { - ERROR_LOG("sample process failed"); - return FAILED; - } - - INFO_LOG("execute sample success"); - return SUCCESS; -} diff --git a/official/cv/ssd/ascend310_quant_infer/src/model_process.cpp b/official/cv/ssd/ascend310_quant_infer/src/model_process.cpp deleted file mode 100644 index 04e6a42ab43cbc41720fe6b9e30bf919323c9f9e..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/src/model_process.cpp +++ /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. - */ - -#include "../inc/model_process.h" -#include <iostream> -#include <map> -#include <sstream> -#include <algorithm> -#include "../inc/utils.h" -extern bool g_is_device; - -ModelProcess::ModelProcess() :modelId_(0), modelMemSize_(0), modelWeightSize_(0), modelMemPtr_(nullptr), -modelWeightPtr_(nullptr), loadFlag_(false), modelDesc_(nullptr), input_(nullptr), output_(nullptr) { -} - -ModelProcess::~ModelProcess() { - Unload(); - DestroyDesc(); - DestroyInput(); - DestroyOutput(); -} - -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { - if (loadFlag_) { - ERROR_LOG("has already loaded a model"); - return FAILED; - } - - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("query model failed, model file is %s", modelPath); - return FAILED; - } - - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); - return FAILED; - } - - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); - return FAILED; - } - - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("load model from file failed, model file is %s", modelPath); - return FAILED; - } - - loadFlag_ = true; - INFO_LOG("load model %s success", modelPath); - return SUCCESS; -} - -Result ModelProcess::CreateDesc() { - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { - ERROR_LOG("create model description failed"); - return FAILED; - } - - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("get model description failed"); - return FAILED; - } - - INFO_LOG("create model description success"); - - return SUCCESS; -} - -void ModelProcess::DestroyDesc() { - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } -} - -Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { - ERROR_LOG("can't create dataset, create input failed"); - return FAILED; - } - - aclDataBuffer* inputData = aclCreateDataBuffer(inputDataBuffer, bufferSize); - if (inputData == nullptr) { - ERROR_LOG("can't create data buffer, create input failed"); - return FAILED; - } - - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("add input dataset buffer failed"); - aclDestroyDataBuffer(inputData); - inputData = nullptr; - return FAILED; - } - - return SUCCESS; -} - -void ModelProcess::DestroyInput() { - if (input_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); - aclDestroyDataBuffer(dataBuffer); - } - aclmdlDestroyDataset(input_); - input_ = nullptr; -} - -Result ModelProcess::CreateOutput() { - if (modelDesc_ == nullptr) { - ERROR_LOG("no model description, create output failed"); - return FAILED; - } - - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { - ERROR_LOG("can't create dataset, create output failed"); - return FAILED; - } - - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); - for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); - - void *outputBuffer = nullptr; - aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't malloc buffer, size is %zu, create output failed", buffer_size); - return FAILED; - } - - aclDataBuffer* outputData = aclCreateDataBuffer(outputBuffer, buffer_size); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't create data buffer, create output failed"); - aclrtFree(outputBuffer); - return FAILED; - } - - ret = aclmdlAddDatasetBuffer(output_, outputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't add data buffer, create output failed"); - aclrtFree(outputBuffer); - aclDestroyDataBuffer(outputData); - return FAILED; - } - } - - INFO_LOG("create model output success"); - return SUCCESS; -} - -void ModelProcess::DumpModelOutputResult(char *output_name) { - size_t outputNum = aclmdlGetDatasetNumBuffers(output_); - - for (size_t i = 0; i < outputNum; ++i) { - std::stringstream ss; - ss << "result/" << output_name << "_output_" << i << ".bin"; - std::string outputFileName = ss.str(); - FILE *outputFile = fopen(outputFileName.c_str(), "wb"); - if (outputFile != nullptr) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void* outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - (void)aclrtFreeHost(outHostData); - return; - } - - fwrite(outHostData, len, sizeof(char), outputFile); - - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } else { - fwrite(data, len, sizeof(char), outputFile); - } - fclose(outputFile); - outputFile = nullptr; - } else { - ERROR_LOG("create output file [%s] failed", outputFileName.c_str()); - return; - } - } - - INFO_LOG("dump data success"); - return; -} - -void ModelProcess::OutputModelResult() { - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void *outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - float *outData = NULL; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - return; - } - - outData = reinterpret_cast<float*>(outHostData); - } else { - outData = reinterpret_cast<float*>(data); - } - std::map<float, unsigned int, std::greater<float> > resultMap; - for (unsigned int j = 0; j < len / sizeof(float); ++j) { - resultMap[*outData] = j; - outData++; - } - - int cnt = 0; - for (auto it = resultMap.begin(); it != resultMap.end(); ++it) { - // print top 5 - if (++cnt > 5) { - break; - } - - INFO_LOG("top %d: index[%d] value[%lf]", cnt, it->second, it->first); - } - if (!g_is_device) { - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } - } - - INFO_LOG("output data success"); - return; -} - -void ModelProcess::DestroyOutput() { - if (output_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - (void)aclrtFree(data); - (void)aclDestroyDataBuffer(dataBuffer); - } - - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; -} - -Result ModelProcess::Execute() { - aclError ret = aclmdlExecute(modelId_, input_, output_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); - return FAILED; - } - - INFO_LOG("model execute success"); - return SUCCESS; -} - -void ModelProcess::Unload() { - if (!loadFlag_) { - WARN_LOG("no model had been loaded, unload failed"); - return; - } - - aclError ret = aclmdlUnload(modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); - } - - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } - - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; - } - - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; - } - - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); -} diff --git a/official/cv/ssd/ascend310_quant_infer/src/sample_process.cpp b/official/cv/ssd/ascend310_quant_infer/src/sample_process.cpp deleted file mode 100644 index cbf58dbae68ff8b4c1b662f228218fe607810512..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/src/sample_process.cpp +++ /dev/null @@ -1,256 +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 "../inc/sample_process.h" -#include <sys/time.h> -#include <sys/types.h> -#include <dirent.h> -#include <string.h> -#include <iostream> -#include <fstream> -#include "../inc/model_process.h" -#include "acl/acl.h" -#include "../inc/utils.h" -extern bool g_is_device; -using std::string; -using std::vector; - -SampleProcess::SampleProcess() :deviceId_(0), context_(nullptr), stream_(nullptr) { -} - -SampleProcess::~SampleProcess() { - DestroyResource(); -} - -Result SampleProcess::InitResource() { - // ACL init - - const char *aclConfigPath = "./src/acl.json"; - aclError ret = aclInit(aclConfigPath); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl init failed"); - return FAILED; - } - INFO_LOG("acl init success"); - - // open device - ret = aclrtSetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl open device %d failed", deviceId_); - return FAILED; - } - INFO_LOG("open device %d success", deviceId_); - - // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create context failed"); - return FAILED; - } - INFO_LOG("create context success"); - - // create stream - ret = aclrtCreateStream(&stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create stream failed"); - return FAILED; - } - INFO_LOG("create stream success"); - - // get run mode - aclrtRunMode runMode; - ret = aclrtGetRunMode(&runMode); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl get run mode failed"); - return FAILED; - } - g_is_device = (runMode == ACL_DEVICE); - INFO_LOG("get run mode success"); - return SUCCESS; -} - -void SampleProcess::GetAllFiles(std::string path, std::vector<string> *files) { - DIR *pDir = NULL; - struct dirent* ptr = nullptr; - if (!(pDir = opendir(path.c_str()))) { - return; - } - while ((ptr = readdir(pDir)) != 0) { - if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { - files->push_back(path + "/" + ptr->d_name); - } - } - closedir(pDir); -} - -Result SampleProcess::Process(char *om_path, char *input_folder) { - // model init - const double second_to_millisecond = 1000; - const double second_to_microsecond = 1000000; - - double whole_cost_time = 0.0; - struct timeval start_global = {0}; - struct timeval end_global = {0}; - double startTimeMs_global = 0.0; - double endTimeMs_global = 0.0; - - gettimeofday(&start_global, nullptr); - - ModelProcess processModel; - const char* omModelPath = om_path; - - Result ret = processModel.LoadModelFromFileWithMem(omModelPath); - if (ret != SUCCESS) { - ERROR_LOG("execute LoadModelFromFileWithMem failed"); - return FAILED; - } - - ret = processModel.CreateDesc(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateDesc failed"); - return FAILED; - } - - ret = processModel.CreateOutput(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateOutput failed"); - return FAILED; - } - - std::vector<string> testFile; - GetAllFiles(input_folder, &testFile); - - if (testFile.size() == 0) { - WARN_LOG("no input data under folder"); - } - - // loop begin - - double model_cost_time = 0.0; - double edge_to_edge_model_cost_time = 0.0; - - for (size_t index = 0; index < testFile.size(); ++index) { - INFO_LOG("start to process file:%s", testFile[index].c_str()); - // model process - - struct timeval time_init = {0}; - double timeval_init = 0.0; - gettimeofday(&time_init, nullptr); - timeval_init = (time_init.tv_sec * second_to_microsecond + time_init.tv_usec) / second_to_millisecond; - - uint32_t devBufferSize; - void *picDevBuffer = Utils::GetDeviceBufferOfFile(testFile[index], &devBufferSize); - if (picDevBuffer == nullptr) { - ERROR_LOG("get pic device buffer failed,index is %zu", index); - return FAILED; - } - ret = processModel.CreateInput(picDevBuffer, devBufferSize); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateInput failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - struct timeval start = {0}; - struct timeval end = {0}; - double startTimeMs = 0.0; - double endTimeMs = 0.0; - gettimeofday(&start, nullptr); - startTimeMs = (start.tv_sec * second_to_microsecond + start.tv_usec) / second_to_millisecond; - - ret = processModel.Execute(); - - gettimeofday(&end, nullptr); - endTimeMs = (end.tv_sec * second_to_microsecond + end.tv_usec) / second_to_millisecond; - - double cost_time = endTimeMs - startTimeMs; - INFO_LOG("model infer time: %lf ms", cost_time); - - model_cost_time += cost_time; - - double edge_to_edge_cost_time = endTimeMs - timeval_init; - edge_to_edge_model_cost_time += edge_to_edge_cost_time; - - if (ret != SUCCESS) { - ERROR_LOG("execute inference failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - int pos = testFile[index].find_last_of('/'); - std::string name = testFile[index].substr(pos+1); - std::string outputname = name.substr(0, name.rfind(".")); - - // dump output result to file in the current directory - processModel.DumpModelOutputResult(const_cast<char *>(outputname.c_str())); - - // release model input buffer - aclrtFree(picDevBuffer); - processModel.DestroyInput(); - } - double test_file_size = 0.0; - test_file_size = testFile.size(); - INFO_LOG("infer dataset size:%lf", test_file_size); - - gettimeofday(&end_global, nullptr); - startTimeMs_global = (start_global.tv_sec * second_to_microsecond + start_global.tv_usec) / second_to_millisecond; - endTimeMs_global = (end_global.tv_sec * second_to_microsecond + end_global.tv_usec) / second_to_millisecond; - whole_cost_time = (endTimeMs_global - startTimeMs_global) / test_file_size; - - model_cost_time /= test_file_size; - INFO_LOG("model cost time per sample: %lf ms", model_cost_time); - edge_to_edge_model_cost_time /= test_file_size; - INFO_LOG("edge-to-edge model cost time per sample:%lf ms", edge_to_edge_model_cost_time); - INFO_LOG("whole cost time per sample: %lf ms", whole_cost_time); - - // loop end - - return SUCCESS; -} - -void SampleProcess::DestroyResource() { - aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy stream failed"); - } - stream_ = nullptr; - } - INFO_LOG("end to destroy stream"); - - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy context failed"); - } - context_ = nullptr; - } - INFO_LOG("end to destroy context"); - - ret = aclrtResetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("reset device failed"); - } - INFO_LOG("end to reset device is %d", deviceId_); - - ret = aclFinalize(); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("finalize acl failed"); - } - INFO_LOG("end to finalize acl"); -} - diff --git a/official/cv/ssd/ascend310_quant_infer/src/utils.cpp b/official/cv/ssd/ascend310_quant_infer/src/utils.cpp deleted file mode 100644 index d9208c8cfd9979e9248046e7325f260eb2d14d80..0000000000000000000000000000000000000000 --- a/official/cv/ssd/ascend310_quant_infer/src/utils.cpp +++ /dev/null @@ -1,113 +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 "../inc/utils.h" -#include <sys/stat.h> -#include <iostream> -#include <fstream> -#include <cstring> -#include "acl/acl.h" - -extern bool g_is_device; - -void* Utils::ReadBinFile(std::string fileName, uint32_t *fileSize) { - struct stat sBuf; - int fileStatus = stat(fileName.data(), &sBuf); - if (fileStatus == -1) { - ERROR_LOG("failed to get file"); - return nullptr; - } - if (S_ISREG(sBuf.st_mode) == 0) { - ERROR_LOG("%s is not a file, please enter a file", fileName.c_str()); - return nullptr; - } - - std::ifstream binFile(fileName, std::ifstream::binary); - if (binFile.is_open() == false) { - ERROR_LOG("open file %s failed", fileName.c_str()); - return nullptr; - } - - binFile.seekg(0, binFile.end); - uint32_t binFileBufferLen = binFile.tellg(); - if (binFileBufferLen == 0) { - ERROR_LOG("binfile is empty, filename is %s", fileName.c_str()); - binFile.close(); - return nullptr; - } - - binFile.seekg(0, binFile.beg); - - void* binFileBufferData = nullptr; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&binFileBufferData, binFileBufferLen); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc for binFileBufferData failed"); - binFile.close(); - return nullptr; - } - if (binFileBufferData == nullptr) { - ERROR_LOG("malloc binFileBufferData failed"); - binFile.close(); - return nullptr; - } - } else { - ret = aclrtMalloc(&binFileBufferData, binFileBufferLen, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", binFileBufferLen); - binFile.close(); - return nullptr; - } - } - binFile.read(static_cast<char *>(binFileBufferData), binFileBufferLen); - binFile.close(); - *fileSize = binFileBufferLen; - return binFileBufferData; -} - -void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize) { - uint32_t inputHostBuffSize = 0; - void* inputHostBuff = Utils::ReadBinFile(fileName, &inputHostBuffSize); - if (inputHostBuff == nullptr) { - return nullptr; - } - if (!g_is_device) { - void *inBufferDev = nullptr; - uint32_t inBufferSize = inputHostBuffSize; - aclError ret = aclrtMalloc(&inBufferDev, inBufferSize, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", inBufferSize); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - - ret = aclrtMemcpy(inBufferDev, inBufferSize, inputHostBuff, inputHostBuffSize, ACL_MEMCPY_HOST_TO_DEVICE); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("memcpy failed. device buffer size is %u, input host buffer size is %u", - inBufferSize, inputHostBuffSize); - aclrtFree(inBufferDev); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - aclrtFreeHost(inputHostBuff); - *fileSize = inBufferSize; - return inBufferDev; - } else { - *fileSize = inputHostBuffSize; - return inputHostBuff; - } -} diff --git a/official/cv/unet/README.md b/official/cv/unet/README.md index 3093c54e5b20b6dfb11251848a366e03ca1495ff..1804f306acaeccbddd1030f3b6826cec607ee07b 100644 --- a/official/cv/unet/README.md +++ b/official/cv/unet/README.md @@ -21,7 +21,6 @@ - [How to use](#how-to-use) - [Inference](#inference) - [Running on Ascend 310](#running-on-ascend-310) - - [Post Training Quantization](#post-training-quantization) - [Continue Training on the Pretrained Model](#continue-training-on-the-pretrained-model) - [Transfer training](#transfer-training) - [Description of Random Situation](#description-of-random-situation) @@ -561,40 +560,6 @@ Inference result is saved in current path, you can find result in acc.log file. Cross valid dice coeff is: 0.9054352151297033 ``` -##### [Post Training Quantization](#contents) - -Relative executing script files reside in the directory "ascend310_quant_infer". Please implement following steps sequentially to complete post quantization. -Current quantization project bases on ISBI dataset. - -1. Generate data of .bin format required for AIR model inference at Ascend310 platform. - -```shell -python export_bin.py --config_path [YMAL CONFIG PATH] --data_path [DATA DIR] --result_path [RESULT PATH] -``` - -2. Export quantized AIR model. - -Post quantization of model requires special toolkits for exporting quantized AIR model. Please refer to [official website](https://www.hiascend.com/software/cann/community). - -```shell -python post_quant.py --config_path [YMAL CONFIG PATH] --data_path [DATASET PATH] --checkpoint_file_path [CKPT_PATH] -``` - -The quantized AIR file will be stored as "./results/unet_quant.air". - -3. Implement inference at Ascend310 platform. - -```shell -# Ascend310 quant inference -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH] -``` - -Inference result is saved in current path, you can find result like this in acc.log file. - -```bash -Cross valid dice coeff is: 0.9139793866877975 -``` - #### Continue Training on the Pretrained Model Set options `resume` to True in `*.yaml`, and set `resume_ckpt` to the path of your checkpoint. e.g. @@ -623,4 +588,4 @@ In data_loader.py, we set the seed inside “_get_val_train_indices" function. W ## [ModelZoo Homepage](#contents) -Please check the official [homepage](https://gitee.com/mindspore/models). \ No newline at end of file +Please check the official [homepage](https://gitee.com/mindspore/models). diff --git a/official/cv/unet/README_CN.md b/official/cv/unet/README_CN.md index 1bba434d8cb5e31e715b63ff47129a9acdc94f36..41c4ed74595853219ef10d522b353bb8d8302712 100644 --- a/official/cv/unet/README_CN.md +++ b/official/cv/unet/README_CN.md @@ -22,7 +22,6 @@ - [用法](#用法-1) - [推理](#推理) - [Ascend 310环境运行](#ascend-310环境运行) - - [训练后量化推理](#训练后量化推理) - [继续训练预训练模型](#继续训练预训练模型) - [迁移学习](#迁移学习) - [随机情况说明](#随机情况说明) @@ -556,39 +555,6 @@ bash run_infer_310.sh [NETWORK] [MINDIR_PATH] [NEED_PREPROCESS] [DEVICE_ID] Cross valid dice coeff is: 0.9054352151297033 ``` -##### [训练后量化推理](#contents) - -训练后量化推理的相关执行脚本文件在"ascend310_quant_infer"目录下,依次执行以下步骤实现训练后量化推理。本训练后量化工程基于ISBI数据集。 - -1、生成Ascend310平台AIR模型推理需要的.bin格式数据。 - -```shell -python export_bin.py --config_path [YMAL CONFIG PATH] --data_path [DATA DIR] --result_path [RESULT PATH] -``` - -2、导出训练后量化的AIR格式模型。 - -导出训练后量化模型需要配套的量化工具包,参考[官方地址](https://www.hiascend.com/software/cann/community) - -```shell -python post_quant.py --config_path [YMAL CONFIG PATH] --data_path [DATASET PATH] --checkpoint_file_path [CKPT_PATH] -``` - -导出的模型会存储在./result/unet_quant.air。 - -3、在Ascend310执行推理量化模型。 - -```shell -# Ascend310 inference -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH] -``` - -推理结果保存在脚本执行的当前路径,可以在acc.log中看到精度计算结果。 - -```bash -Cross valid dice coeff is: 0.9139793866877975 -``` - #### 继续训练预训练模型 在`*.yaml`里将`resume`设置成True,并将`resume_ckpt`设置成对应的权重文件路径,例如: diff --git a/official/cv/unet/ascend310_quant_infer/acc.py b/official/cv/unet/ascend310_quant_infer/acc.py deleted file mode 100644 index 305abd85991fd71cd06562807fc2688d86b9031b..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/acc.py +++ /dev/null @@ -1,106 +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. -# ============================================================================ -"""postprocess for 310 inference""" -import os -import argparse -import numpy as np -import cv2 - -import mindspore.nn as nn - - -parser = argparse.ArgumentParser("unet quant postprocess") -parser.add_argument("--result_path", type=str, required=True, help="path to inference results.") -parser.add_argument("--label_path", type=str, required=True, help="path to label.npy.") -parser.add_argument("--input_path", type=str, required=True, help="path to input data.") - -args, _ = parser.parse_known_args() - -class dice_coeff(nn.Metric): - """Unet Metric, return dice coefficient and IOU.""" - - def __init__(self, print_res=True, show_eval=False): - super(dice_coeff, self).__init__() - self.clear() - self.show_eval = show_eval - self.print_res = print_res - self.img_num = 0 - # network config - self.include_background = True - self.eval_resize = False - self.num_classes = 2 - - def clear(self): - self._dice_coeff_sum = 0 - self._iou_sum = 0 - self._samples_num = 0 - self.img_num = 0 - - def update(self, *inputs): - if len(inputs) != 2: - raise ValueError('Need 2 inputs (y_predict, y), but got {}'.format(len(inputs))) - y = self._convert_data(inputs[1]) - self._samples_num += y.shape[0] - y = y.transpose(0, 2, 3, 1) - b, h, w, c = y.shape - if b != 1: - raise ValueError('Batch size should be 1 when in evaluation.') - y = y.reshape((h, w, c)) - start_index = 0 - if not self.include_background: - y = y[:, :, 1:] - start_index = 1 - - y_softmax = np.squeeze(self._convert_data(inputs[0]), axis=0) - if self.eval_resize: - y_pred = [] - for i in range(start_index, self.num_classes): - y_pred.append(cv2.resize(np.uint8(y_softmax[:, :, i] * 255), (w, h)) / 255) - y_pred = np.stack(y_pred, axis=-1) - else: - y_pred = y_softmax - if not self.include_background: - y_pred = y_softmax[:, :, start_index:] - - y_pred = y_pred.astype(np.float32) - inter = np.dot(y_pred.flatten(), y.flatten()) - union = np.dot(y_pred.flatten(), y_pred.flatten()) + np.dot(y.flatten(), y.flatten()) - - single_dice_coeff = 2 * float(inter) / float(union + 1e-6) - single_iou = single_dice_coeff / (2 - single_dice_coeff) - if self.print_res: - print("single dice coeff is: {}, IOU is: {}".format(single_dice_coeff, single_iou)) - self._dice_coeff_sum += single_dice_coeff - self._iou_sum += single_iou - - def eval(self): - if self._samples_num == 0: - raise RuntimeError('Total samples num must not be 0.') - return (self._dice_coeff_sum / float(self._samples_num), self._iou_sum / float(self._samples_num)) - - -if __name__ == '__main__': - metrics = dice_coeff() - # eval_activate = "softmax" - - label_list = np.load(args.label_path) - for j in range(len(os.listdir(args.input_path))): - file_name = os.path.join(args.result_path, "ISBI_test_bs_1_" + str(j) + "_output_0.bin") - rst_out = np.fromfile(file_name, np.float32).reshape(1, 388, 388, 2) - label = label_list[j] - metrics.update(rst_out, label) - eval_score = metrics.eval() - print("==================== Cross valid dice coeff is:", eval_score[0]) - print("==================== Cross valid dice IOU is:", eval_score[1]) diff --git a/official/cv/unet/ascend310_quant_infer/export_bin.py b/official/cv/unet/ascend310_quant_infer/export_bin.py deleted file mode 100644 index c84b1dae0edffe2dc5d559b6e8e63ac136386e5d..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/export_bin.py +++ /dev/null @@ -1,48 +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. -# ============================================================================ -"""unet 310 infer preprocess dataset""" -import os -import sys -import shutil -import numpy as np - - -def generate_data(data_dir, result_path, cross_valid_ind=1): - """ - Generate data and label needed for AIR model inference at Ascend310 platform. - """ - - _, valid_dataset = create_dataset(data_dir, 1, 1, False, cross_valid_ind, False, do_crop=config.crop, - img_size=config.image_size) - labels_list = [] - img_path = os.path.join(result_path, "00_data") - if os.path.exists(img_path): - shutil.rmtree(img_path) - os.makedirs(img_path) - - for i, data in enumerate(valid_dataset): - file_name = "ISBI_test_bs_1_" + str(i) + ".bin" - file_path = os.path.join(img_path, file_name) - data[0].asnumpy().tofile(file_path) - labels_list.append(data[1].asnumpy()) - np.save(os.path.join(result_path, "label.npy"), labels_list) - - -if __name__ == '__main__': - sys.path.append("..") - from src.data_loader import create_dataset - from src.model_utils.config import config - - generate_data(data_dir=config.data_path, cross_valid_ind=config.cross_valid_ind, result_path=config.result_path) diff --git a/official/cv/unet/ascend310_quant_infer/inc/model_process.h b/official/cv/unet/ascend310_quant_infer/inc/model_process.h deleted file mode 100644 index 79e19833c7a1b3e428458c23aaba953dc40b1f01..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/inc/model_process.h +++ /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. - */ - -#pragma once -#include <iostream> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* ModelProcess -*/ -class ModelProcess { - public: - /** - * @brief Constructor - */ - ModelProcess(); - - /** - * @brief Destructor - */ - ~ModelProcess(); - - /** - * @brief load model from file with mem - * @param [in] modelPath: model path - * @return result - */ - Result LoadModelFromFileWithMem(const char *modelPath); - - /** - * @brief unload model - */ - void Unload(); - - /** - * @brief create model desc - * @return result - */ - Result CreateDesc(); - - /** - * @brief destroy desc - */ - void DestroyDesc(); - - /** - * @brief create model input - * @param [in] inputDataBuffer: input buffer - * @param [in] bufferSize: input buffer size - * @return result - */ - Result CreateInput(void *inputDataBuffer, size_t bufferSize); - - /** - * @brief destroy input resource - */ - void DestroyInput(); - - /** - * @brief create output buffer - * @return result - */ - Result CreateOutput(); - - /** - * @brief destroy output resource - */ - void DestroyOutput(); - - /** - * @brief model execute - * @return result - */ - Result Execute(); - - /** - * @brief dump model output result to file - */ - void DumpModelOutputResult(char *output_name); - - /** - * @brief get model output result - */ - void OutputModelResult(); - - private: - uint32_t modelId_; - size_t modelMemSize_; - size_t modelWeightSize_; - void *modelMemPtr_; - void *modelWeightPtr_; - bool loadFlag_; // model load flag - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; -}; - diff --git a/official/cv/unet/ascend310_quant_infer/inc/sample_process.h b/official/cv/unet/ascend310_quant_infer/inc/sample_process.h deleted file mode 100644 index 24d6ea01e59925673a548a7873ab310623235549..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/inc/sample_process.h +++ /dev/null @@ -1,58 +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. - */ - -#pragma once -#include <string> -#include <vector> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* SampleProcess -*/ -class SampleProcess { - public: - /** - * @brief Constructor - */ - SampleProcess(); - - /** - * @brief Destructor - */ - ~SampleProcess(); - - /** - * @brief init reousce - * @return result - */ - Result InitResource(); - - /** - * @brief sample process - * @return result - */ - Result Process(char *om_path, char *input_folder); - - void GetAllFiles(std::string path, std::vector<std::string> *files); - - private: - void DestroyResource(); - - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; -}; diff --git a/official/cv/unet/ascend310_quant_infer/inc/utils.h b/official/cv/unet/ascend310_quant_infer/inc/utils.h deleted file mode 100644 index 3ae2a571b8e8ba51c01b02e23f36dfad5a7b18f1..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/inc/utils.h +++ /dev/null @@ -1,52 +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. - */ - -#pragma once -#include <iostream> -#include <string> - -#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) -#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args) -#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) - -typedef enum Result { - SUCCESS = 0, - FAILED = 1 -} Result; - -/** -* Utils -*/ -class Utils { - public: - /** - * @brief create device buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return device buffer of file - */ - static void *GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize); - - /** - * @brief create buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return buffer of pic - */ - static void* ReadBinFile(std::string fileName, uint32_t *fileSize); -}; - -#pragma once diff --git a/official/cv/unet/ascend310_quant_infer/post_quant.py b/official/cv/unet/ascend310_quant_infer/post_quant.py deleted file mode 100644 index 37dc08282c4b52a41cabb46aa60bdb1452b17c70..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/post_quant.py +++ /dev/null @@ -1,83 +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. -# ============================================================================ -"""do post training quantization for Ascend310""" -import sys -import numpy as np - -from amct_mindspore.quantize_tool import create_quant_config -from amct_mindspore.quantize_tool import quantize_model -from amct_mindspore.quantize_tool import save_model -from mindspore import Tensor, context -from mindspore.train.model import Model -from mindspore.train.serialization import load_checkpoint, load_param_into_net - - -def quant_unet(network, dataset, input_data): - """ - Export post training quantization model of AIR format. - - Args: - network: the origin network for inference. - dataset: the data for inference. - input_data: the data used for constructing network. The shape and format of input data should be the same as - actual data for inference. - """ - - # step2: create the quant config json file - create_quant_config("./config.json", network, input_data) - - # step3: do some network modification and return the modified network - calibration_network = quantize_model("./config.json", network, input_data) - calibration_network.set_train(False) - - # step4: perform the evaluation of network to do activation calibration - model = Model(calibration_network, loss_fn=TempLoss(), metrics={"dice_coff": dice_coeff()}) - _ = model.eval(dataset, dataset_sink_mode=False) - - # step5: export the air file - save_model("results/unet_quant", calibration_network, input_data) - print("[INFO] the quantized AIR file has been stored at: \n {}".format("results/unet_quant.air")) - - -def run_export(): - """run export.""" - if config.model_name == 'unet_medical': - net = UNetMedical(n_channels=config.num_channels, n_classes=config.num_classes) - else: - raise ValueError("post training quantization currently does not support model: {}".format(config.model_name)) - # return a parameter dict for model - param_dict = load_checkpoint(config.checkpoint_file_path) - # load the parameter into net - load_param_into_net(net, param_dict) - net = UnetEval(net, eval_activate="softmax") - batch_size = 1 - input_data = Tensor(np.ones([batch_size, config.num_channels, config.height, config.width]).astype(np.float32)) - _, valid_dataset = create_dataset(config.data_path, 1, batch_size, False, 1, False, do_crop=config.crop, - img_size=config.image_size) - dataset = valid_dataset.take(1) - quant_unet(net, dataset, input_data) - - -if __name__ == "__main__": - sys.path.append("..") - from src.data_loader import create_dataset - from src.unet_medical import UNetMedical - from src.utils import UnetEval, TempLoss, dice_coeff - from src.model_utils.config import config - from src.model_utils.device_adapter import get_device_id - - context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", device_id=get_device_id()) - - run_export() diff --git a/official/cv/unet/ascend310_quant_infer/run_quant_infer.sh b/official/cv/unet/ascend310_quant_infer/run_quant_infer.sh deleted file mode 100644 index 6877e451a3adb2ff02923957643f0afcc43d7072..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/run_quant_infer.sh +++ /dev/null @@ -1,104 +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 3 ]; then - echo "Usage: bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH]" - echo "Example: bash run_quant_infer.sh ./unet_quant.air ./data ./label_ids.npy" -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) -label_path=$(get_real_path $3) - -echo "air name: "$model -echo "dataset path: "$data_path -echo "label path: "$label_path - -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 ASCEND_HOME=/usr/local/Ascend/latest/ - export PATH=$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/fwkacllib/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/atc/lib64:$LD_LIBRARY_PATH - export TBE_IMPL_PATH=$ASCEND_HOME/opp/op_impl/built-in/ai_core/tbe - export PYTHONPATH=${TBE_IMPL_PATH}:$PYTHONPATH - export ASCEND_OPP_PATH=$ASCEND_HOME/opp -fi - -function air_to_om() -{ - atc --input_format=NCHW --framework=1 --model=$model --output=unet_quant --soc_version=Ascend310 &> atc.log -} - -function compile_app() -{ - bash ./src/build.sh &> build.log -} - -function infer() -{ - if [ -d result ]; then - rm -rf ./result - fi - mkdir result - ./out/main ./unet_quant.om $data_path &> infer.log -} - -function cal_acc() -{ - python3.7 ./acc.py --result_path=./result --label_path=$label_path --input_path=$data_path &> acc.log -} - -echo "start atc================================================" -air_to_om -if [ $? -ne 0 ]; then - echo "air to om code failed" - exit 1 -fi - -echo "start compile============================================" -compile_app -if [ $? -ne 0 ]; then - echo "compile app code failed" - exit 1 -fi - -echo "start infer==============================================" -infer -if [ $? -ne 0 ]; then - echo " execute inference failed" - exit 1 -fi - -echo "start calculate acc======================================" -cal_acc -if [ $? -ne 0 ]; then - echo "calculate accuracy failed" - exit 1 -fi \ No newline at end of file diff --git a/official/cv/unet/ascend310_quant_infer/src/CMakeLists.txt b/official/cv/unet/ascend310_quant_infer/src/CMakeLists.txt deleted file mode 100644 index 655026d7d91612267a287e83e886ba2ce1304d18..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/src/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. - -# CMake lowest version requirement -cmake_minimum_required(VERSION 3.5.1) -# project information -project(InferClassification) -# Check environment variable -if(NOT DEFINED ENV{ASCEND_HOME}) - message(FATAL_ERROR "please define environment variable:ASCEND_HOME") -endif() - -# Compile options -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") - -# Skip build rpath -set(CMAKE_SKIP_BUILD_RPATH True) - -# Set output directory -set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SRC_ROOT}/../out) - -# Set include directory and library directory -set(FWKACL_LIB_DIR $ENV{ASCEND_HOME}/fwkacllib) -set(ACL_LIB_DIR $ENV{ASCEND_HOME}/acllib) -set(ATLAS_ACL_LIB_DIR $ENV{ASCEND_HOME}/ascend-toolkit/latest/acllib) - -# Header path -include_directories(${ACL_LIB_DIR}/include/) -include_directories(${FWKACL_LIB_DIR}/include/) -include_directories(${ATLAS_ACL_LIB_DIR}/include/) -include_directories(${PROJECT_SRC_ROOT}/../inc) - -# add host lib path -link_directories(${ACL_LIB_DIR} ${FWKACL_LIB_DIR}) -find_library(acl libascendcl.so ${ACL_LIB_DIR}/lib64 ${FWKACL_LIB_DIR}/lib64 ${ATLAS_ACL_LIB_DIR}/lib64) - -add_executable(main utils.cpp - sample_process.cpp - model_process.cpp - main.cpp) - -target_link_libraries(main ${acl} gflags pthread) diff --git a/official/cv/unet/ascend310_quant_infer/src/acl.json b/official/cv/unet/ascend310_quant_infer/src/acl.json deleted file mode 100644 index 0967ef424bce6791893e9a57bb952f80fd536e93..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/src/acl.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/official/cv/unet/ascend310_quant_infer/src/build.sh b/official/cv/unet/ascend310_quant_infer/src/build.sh deleted file mode 100644 index b5979b68e60b673f763a3cac98c5e147e7085291..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/src/build.sh +++ /dev/null @@ -1,55 +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. -# ============================================================================ -path_cur=$(cd "`dirname $0`" || exit; pwd) - -function preparePath() { - rm -rf $1 - mkdir -p $1 - cd $1 || exit -} - -function buildA300() { - if [ ! "${ARCH_PATTERN}" ]; then - # set ARCH_PATTERN to acllib when it was not specified by user - export ARCH_PATTERN=acllib - echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}" - else - echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user, reset it to ${ARCH_PATTERN}/acllib" - export ARCH_PATTERN=${ARCH_PATTERN}/acllib - fi - - path_build=$path_cur/build - preparePath $path_build - cmake .. - make -j - ret=$? - cd .. - return ${ret} -} - -# 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 - -buildA300 - -if [ $? -ne 0 ]; then - exit 1 -fi diff --git a/official/cv/unet/ascend310_quant_infer/src/main.cpp b/official/cv/unet/ascend310_quant_infer/src/main.cpp deleted file mode 100644 index 80165505f447d418e0f107b76d04ffae59b89a73..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/src/main.cpp +++ /dev/null @@ -1,42 +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 <iostream> -#include "../inc/sample_process.h" -#include "../inc/utils.h" -bool g_is_device = false; - -int main(int argc, char **argv) { - if (argc != 3) { - ERROR_LOG("usage:./main path_of_om path_of_inputFolder"); - return FAILED; - } - SampleProcess processSample; - Result ret = processSample.InitResource(); - if (ret != SUCCESS) { - ERROR_LOG("sample init resource failed"); - return FAILED; - } - - ret = processSample.Process(argv[1], argv[2]); - if (ret != SUCCESS) { - ERROR_LOG("sample process failed"); - return FAILED; - } - - INFO_LOG("execute sample success"); - return SUCCESS; -} diff --git a/official/cv/unet/ascend310_quant_infer/src/model_process.cpp b/official/cv/unet/ascend310_quant_infer/src/model_process.cpp deleted file mode 100644 index 04e6a42ab43cbc41720fe6b9e30bf919323c9f9e..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/src/model_process.cpp +++ /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. - */ - -#include "../inc/model_process.h" -#include <iostream> -#include <map> -#include <sstream> -#include <algorithm> -#include "../inc/utils.h" -extern bool g_is_device; - -ModelProcess::ModelProcess() :modelId_(0), modelMemSize_(0), modelWeightSize_(0), modelMemPtr_(nullptr), -modelWeightPtr_(nullptr), loadFlag_(false), modelDesc_(nullptr), input_(nullptr), output_(nullptr) { -} - -ModelProcess::~ModelProcess() { - Unload(); - DestroyDesc(); - DestroyInput(); - DestroyOutput(); -} - -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { - if (loadFlag_) { - ERROR_LOG("has already loaded a model"); - return FAILED; - } - - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("query model failed, model file is %s", modelPath); - return FAILED; - } - - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); - return FAILED; - } - - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); - return FAILED; - } - - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("load model from file failed, model file is %s", modelPath); - return FAILED; - } - - loadFlag_ = true; - INFO_LOG("load model %s success", modelPath); - return SUCCESS; -} - -Result ModelProcess::CreateDesc() { - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { - ERROR_LOG("create model description failed"); - return FAILED; - } - - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("get model description failed"); - return FAILED; - } - - INFO_LOG("create model description success"); - - return SUCCESS; -} - -void ModelProcess::DestroyDesc() { - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } -} - -Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { - ERROR_LOG("can't create dataset, create input failed"); - return FAILED; - } - - aclDataBuffer* inputData = aclCreateDataBuffer(inputDataBuffer, bufferSize); - if (inputData == nullptr) { - ERROR_LOG("can't create data buffer, create input failed"); - return FAILED; - } - - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("add input dataset buffer failed"); - aclDestroyDataBuffer(inputData); - inputData = nullptr; - return FAILED; - } - - return SUCCESS; -} - -void ModelProcess::DestroyInput() { - if (input_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); - aclDestroyDataBuffer(dataBuffer); - } - aclmdlDestroyDataset(input_); - input_ = nullptr; -} - -Result ModelProcess::CreateOutput() { - if (modelDesc_ == nullptr) { - ERROR_LOG("no model description, create output failed"); - return FAILED; - } - - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { - ERROR_LOG("can't create dataset, create output failed"); - return FAILED; - } - - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); - for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); - - void *outputBuffer = nullptr; - aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't malloc buffer, size is %zu, create output failed", buffer_size); - return FAILED; - } - - aclDataBuffer* outputData = aclCreateDataBuffer(outputBuffer, buffer_size); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't create data buffer, create output failed"); - aclrtFree(outputBuffer); - return FAILED; - } - - ret = aclmdlAddDatasetBuffer(output_, outputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't add data buffer, create output failed"); - aclrtFree(outputBuffer); - aclDestroyDataBuffer(outputData); - return FAILED; - } - } - - INFO_LOG("create model output success"); - return SUCCESS; -} - -void ModelProcess::DumpModelOutputResult(char *output_name) { - size_t outputNum = aclmdlGetDatasetNumBuffers(output_); - - for (size_t i = 0; i < outputNum; ++i) { - std::stringstream ss; - ss << "result/" << output_name << "_output_" << i << ".bin"; - std::string outputFileName = ss.str(); - FILE *outputFile = fopen(outputFileName.c_str(), "wb"); - if (outputFile != nullptr) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void* outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - (void)aclrtFreeHost(outHostData); - return; - } - - fwrite(outHostData, len, sizeof(char), outputFile); - - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } else { - fwrite(data, len, sizeof(char), outputFile); - } - fclose(outputFile); - outputFile = nullptr; - } else { - ERROR_LOG("create output file [%s] failed", outputFileName.c_str()); - return; - } - } - - INFO_LOG("dump data success"); - return; -} - -void ModelProcess::OutputModelResult() { - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void *outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - float *outData = NULL; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - return; - } - - outData = reinterpret_cast<float*>(outHostData); - } else { - outData = reinterpret_cast<float*>(data); - } - std::map<float, unsigned int, std::greater<float> > resultMap; - for (unsigned int j = 0; j < len / sizeof(float); ++j) { - resultMap[*outData] = j; - outData++; - } - - int cnt = 0; - for (auto it = resultMap.begin(); it != resultMap.end(); ++it) { - // print top 5 - if (++cnt > 5) { - break; - } - - INFO_LOG("top %d: index[%d] value[%lf]", cnt, it->second, it->first); - } - if (!g_is_device) { - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } - } - - INFO_LOG("output data success"); - return; -} - -void ModelProcess::DestroyOutput() { - if (output_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - (void)aclrtFree(data); - (void)aclDestroyDataBuffer(dataBuffer); - } - - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; -} - -Result ModelProcess::Execute() { - aclError ret = aclmdlExecute(modelId_, input_, output_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); - return FAILED; - } - - INFO_LOG("model execute success"); - return SUCCESS; -} - -void ModelProcess::Unload() { - if (!loadFlag_) { - WARN_LOG("no model had been loaded, unload failed"); - return; - } - - aclError ret = aclmdlUnload(modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); - } - - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } - - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; - } - - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; - } - - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); -} diff --git a/official/cv/unet/ascend310_quant_infer/src/sample_process.cpp b/official/cv/unet/ascend310_quant_infer/src/sample_process.cpp deleted file mode 100644 index 5f8f20f297bf6101d3a68ffd65bd0e6ed69d9486..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/src/sample_process.cpp +++ /dev/null @@ -1,252 +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 "../inc/sample_process.h" -#include <sys/time.h> -#include <sys/types.h> -#include <dirent.h> -#include <string.h> -#include <iostream> -#include <fstream> -#include "../inc/model_process.h" -#include "acl/acl.h" -#include "../inc/utils.h" -extern bool g_is_device; -using std::string; -using std::vector; - -SampleProcess::SampleProcess() :deviceId_(0), context_(nullptr), stream_(nullptr) { -} - -SampleProcess::~SampleProcess() { - DestroyResource(); -} - -Result SampleProcess::InitResource() { - // ACL init - - const char *aclConfigPath = "./src/acl.json"; - aclError ret = aclInit(aclConfigPath); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl init failed"); - return FAILED; - } - INFO_LOG("acl init success"); - - // open device - ret = aclrtSetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl open device %d failed", deviceId_); - return FAILED; - } - INFO_LOG("open device %d success", deviceId_); - - // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create context failed"); - return FAILED; - } - INFO_LOG("create context success"); - - // create stream - ret = aclrtCreateStream(&stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create stream failed"); - return FAILED; - } - INFO_LOG("create stream success"); - - // get run mode - aclrtRunMode runMode; - ret = aclrtGetRunMode(&runMode); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl get run mode failed"); - return FAILED; - } - g_is_device = (runMode == ACL_DEVICE); - INFO_LOG("get run mode success"); - return SUCCESS; -} - -void SampleProcess::GetAllFiles(std::string path, std::vector<string> *files) { - DIR *pDir = NULL; - struct dirent* ptr = nullptr; - if (!(pDir = opendir(path.c_str()))) { - return; - } - while ((ptr = readdir(pDir)) != 0) { - if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { - files->push_back(path + "/" + ptr->d_name); - } - } - closedir(pDir); -} - -Result SampleProcess::Process(char *om_path, char *input_folder) { - // model init - const double second_to_millisecond = 1000; - const double second_to_microsecond = 1000000; - - double whole_cost_time = 0.0; - struct timeval start_global = {0}; - struct timeval end_global = {0}; - double startTimeMs_global = 0.0; - double endTimeMs_global = 0.0; - - gettimeofday(&start_global, nullptr); - - ModelProcess processModel; - const char* omModelPath = om_path; - - Result ret = processModel.LoadModelFromFileWithMem(omModelPath); - if (ret != SUCCESS) { - ERROR_LOG("execute LoadModelFromFileWithMem failed"); - return FAILED; - } - - ret = processModel.CreateDesc(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateDesc failed"); - return FAILED; - } - - ret = processModel.CreateOutput(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateOutput failed"); - return FAILED; - } - - std::vector<string> testFile; - GetAllFiles(input_folder, &testFile); - - if (testFile.size() == 0) { - WARN_LOG("no input data under folder"); - } - - double model_cost_time = 0.0; - double edge_to_edge_model_cost_time = 0.0; - - for (size_t index = 0; index < testFile.size(); ++index) { - INFO_LOG("start to process file:%s", testFile[index].c_str()); - // model process - - struct timeval time_init = {0}; - double timeval_init = 0.0; - gettimeofday(&time_init, nullptr); - timeval_init = (time_init.tv_sec * second_to_microsecond + time_init.tv_usec) / second_to_millisecond; - - uint32_t devBufferSize; - void *picDevBuffer = Utils::GetDeviceBufferOfFile(testFile[index], &devBufferSize); - if (picDevBuffer == nullptr) { - ERROR_LOG("get pic device buffer failed,index is %zu", index); - return FAILED; - } - ret = processModel.CreateInput(picDevBuffer, devBufferSize); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateInput failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - struct timeval start = {0}; - struct timeval end = {0}; - double startTimeMs = 0.0; - double endTimeMs = 0.0; - gettimeofday(&start, nullptr); - startTimeMs = (start.tv_sec * second_to_microsecond + start.tv_usec) / second_to_millisecond; - - ret = processModel.Execute(); - - gettimeofday(&end, nullptr); - endTimeMs = (end.tv_sec * second_to_microsecond + end.tv_usec) / second_to_millisecond; - - double cost_time = endTimeMs - startTimeMs; - INFO_LOG("model infer time: %lf ms", cost_time); - - model_cost_time += cost_time; - - double edge_to_edge_cost_time = endTimeMs - timeval_init; - edge_to_edge_model_cost_time += edge_to_edge_cost_time; - - if (ret != SUCCESS) { - ERROR_LOG("execute inference failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - int pos = testFile[index].find_last_of('/'); - std::string name = testFile[index].substr(pos+1); - std::string outputname = name.substr(0, name.rfind(".")); - - // dump output result to file in the current directory - processModel.DumpModelOutputResult(const_cast<char *>(outputname.c_str())); - - // release model input buffer - aclrtFree(picDevBuffer); - processModel.DestroyInput(); - } - double test_file_size = 0.0; - test_file_size = testFile.size(); - INFO_LOG("infer dataset size:%lf", test_file_size); - - gettimeofday(&end_global, nullptr); - startTimeMs_global = (start_global.tv_sec * second_to_microsecond + start_global.tv_usec) / second_to_millisecond; - endTimeMs_global = (end_global.tv_sec * second_to_microsecond + end_global.tv_usec) / second_to_millisecond; - whole_cost_time = (endTimeMs_global - startTimeMs_global) / test_file_size; - - model_cost_time /= test_file_size; - INFO_LOG("model cost time per sample: %lf ms", model_cost_time); - edge_to_edge_model_cost_time /= test_file_size; - INFO_LOG("edge-to-edge model cost time per sample:%lf ms", edge_to_edge_model_cost_time); - INFO_LOG("whole cost time per sample: %lf ms", whole_cost_time); - - return SUCCESS; -} - -void SampleProcess::DestroyResource() { - aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy stream failed"); - } - stream_ = nullptr; - } - INFO_LOG("end to destroy stream"); - - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy context failed"); - } - context_ = nullptr; - } - INFO_LOG("end to destroy context"); - - ret = aclrtResetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("reset device failed"); - } - INFO_LOG("end to reset device is %d", deviceId_); - - ret = aclFinalize(); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("finalize acl failed"); - } - INFO_LOG("end to finalize acl"); -} - diff --git a/official/cv/unet/ascend310_quant_infer/src/utils.cpp b/official/cv/unet/ascend310_quant_infer/src/utils.cpp deleted file mode 100644 index d9208c8cfd9979e9248046e7325f260eb2d14d80..0000000000000000000000000000000000000000 --- a/official/cv/unet/ascend310_quant_infer/src/utils.cpp +++ /dev/null @@ -1,113 +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 "../inc/utils.h" -#include <sys/stat.h> -#include <iostream> -#include <fstream> -#include <cstring> -#include "acl/acl.h" - -extern bool g_is_device; - -void* Utils::ReadBinFile(std::string fileName, uint32_t *fileSize) { - struct stat sBuf; - int fileStatus = stat(fileName.data(), &sBuf); - if (fileStatus == -1) { - ERROR_LOG("failed to get file"); - return nullptr; - } - if (S_ISREG(sBuf.st_mode) == 0) { - ERROR_LOG("%s is not a file, please enter a file", fileName.c_str()); - return nullptr; - } - - std::ifstream binFile(fileName, std::ifstream::binary); - if (binFile.is_open() == false) { - ERROR_LOG("open file %s failed", fileName.c_str()); - return nullptr; - } - - binFile.seekg(0, binFile.end); - uint32_t binFileBufferLen = binFile.tellg(); - if (binFileBufferLen == 0) { - ERROR_LOG("binfile is empty, filename is %s", fileName.c_str()); - binFile.close(); - return nullptr; - } - - binFile.seekg(0, binFile.beg); - - void* binFileBufferData = nullptr; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&binFileBufferData, binFileBufferLen); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc for binFileBufferData failed"); - binFile.close(); - return nullptr; - } - if (binFileBufferData == nullptr) { - ERROR_LOG("malloc binFileBufferData failed"); - binFile.close(); - return nullptr; - } - } else { - ret = aclrtMalloc(&binFileBufferData, binFileBufferLen, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", binFileBufferLen); - binFile.close(); - return nullptr; - } - } - binFile.read(static_cast<char *>(binFileBufferData), binFileBufferLen); - binFile.close(); - *fileSize = binFileBufferLen; - return binFileBufferData; -} - -void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize) { - uint32_t inputHostBuffSize = 0; - void* inputHostBuff = Utils::ReadBinFile(fileName, &inputHostBuffSize); - if (inputHostBuff == nullptr) { - return nullptr; - } - if (!g_is_device) { - void *inBufferDev = nullptr; - uint32_t inBufferSize = inputHostBuffSize; - aclError ret = aclrtMalloc(&inBufferDev, inBufferSize, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", inBufferSize); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - - ret = aclrtMemcpy(inBufferDev, inBufferSize, inputHostBuff, inputHostBuffSize, ACL_MEMCPY_HOST_TO_DEVICE); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("memcpy failed. device buffer size is %u, input host buffer size is %u", - inBufferSize, inputHostBuffSize); - aclrtFree(inBufferDev); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - aclrtFreeHost(inputHostBuff); - *fileSize = inBufferSize; - return inBufferDev; - } else { - *fileSize = inputHostBuffSize; - return inputHostBuff; - } -} diff --git a/official/cv/vgg16/README.md b/official/cv/vgg16/README.md index e47a112fb56e63c7605ee125f8466efce876e7a7..a2431e540781a9ec5370b7873cfd3a9cec2c8574 100644 --- a/official/cv/vgg16/README.md +++ b/official/cv/vgg16/README.md @@ -28,7 +28,6 @@ - [Export MindIR](#export-mindir) - [Infer on Ascend310](#infer-on-ascend310) - [result](#result) - - [Post Training Quantization](#post-training-quantization) - [Model Description](#model-description) - [Performance](#performance) - [Training Performance](#training-performance) @@ -280,7 +279,6 @@ python eval.py --config_path=[YAML_CONFIG_PATH] --device_target="GPU" --dataset= ├── README.md // descriptions about vgg ├── README_CN.md // descriptions about vgg with Chinese ├── ascend310_infer // Ascend310 infer folder - ├── ascend310_quant_infer // Ascend310 infer folder (quant) ├── infer // MindX infer folder ├── model_utils │ ├── __init__.py // init file @@ -573,40 +571,6 @@ Inference result is saved in current path, you can find result like this in acc. 'acc': 0.92 ``` -### [Post Training Quantization](#contents) - -Relative executing script files reside in the directory "ascend310_quant_infer". Please implement following steps sequentially to complete post quantization. -Current quantization project bases on CIFAR-10 dataset. - -1. Generate data of .bin format required for AIR model inference at Ascend310 platform. - -```shell -python export_bin.py --config_path [YMAL CONFIG PATH] --data_dir [DATA DIR] --result_path [RESULT PATH] -``` - -2. Export quantized AIR model. - -Post quantization of model requires special toolkits for exporting quantized AIR model. Please refer to [official website](https://www.hiascend.com/software/cann/community). - -```shell -python post_quant.py --config_path [YMAL CONFIG PATH] --ckpt_file [CKPT_PATH] --data_dir [DATASET PATH] -``` - -The quantized AIR file will be stored as "./results/vgg_quant.air". - -3. Implement inference at Ascend310 platform. - -```shell -# Ascend310 quant inference -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH] -``` - -Inference result is saved in current path, you can find result like this in acc.log file. - -```bash -'acc': 0.91 -``` - ## [Model Description](#contents) ### [Performance](#contents) diff --git a/official/cv/vgg16/README_CN.md b/official/cv/vgg16/README_CN.md index 62a4695255b16a02ddafbf7fd52b5278902ae2e3..46b4e6f514a2becff571f8793e73021839668956 100644 --- a/official/cv/vgg16/README_CN.md +++ b/official/cv/vgg16/README_CN.md @@ -29,7 +29,6 @@ - [导出MindIR](#导出mindir) - [在Ascend310执行推理](#在ascend310执行推理) - [结果](#结果) - - [训练后量化推理](#训练后量化推理) - [模型描述](#模型描述) - [性能](#性能) - [训练性能](#训练性能) @@ -281,7 +280,6 @@ python eval.py --config_path=[YAML_CONFIG_PATH] --device_target="GPU" --dataset= ├── README.md // VGG 相关说明 ├── README_CN.md // VGG 相关中文说明 ├── ascend310_infer // Ascend310 推理目录 - ├── ascend310_quant_infer // Ascend310 推理目录(量化) ├── infer // MindX 推理目录 ├── model_utils │ ├── __init__.py // 初始化文件 @@ -539,39 +537,6 @@ bash run_infer_310.sh [MINDIR_PATH] [DATASET_NAME] [DATASET_PATH] [NEED_PREPROCE 'acc': 0.92 ``` -### [训练后量化推理](#contents) - -训练后量化推理的相关执行脚本文件在"ascend310_quant_infer"目录下,依次执行以下步骤实现训练后量化推理。本训练后量化工程基于CIFAR-10数据集。 - -1、生成Ascend310平台AIR模型推理需要的.bin格式数据。 - -```shell -python export_bin.py --config_path [YMAL CONFIG PATH] --data_dir [DATA DIR] --result_path [RESULT PATH] -``` - -2、导出训练后量化的AIR格式模型。 - -导出训练后量化模型需要配套的量化工具包,参考[官方地址](https://www.hiascend.com/software/cann/community) - -```shell -python post_quant.py --config_path [YMAL_CONFIG_PATH] --ckpt_file [CKPT_PATH] --data_dir [DATASET PATH] -``` - -导出的模型会存储在./result/vgg_quant.air。 - -3、在Ascend310执行推理量化模型。 - -```shell -# Ascend310 inference -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH] -``` - -推理结果保存在脚本执行的当前路径,可以在acc.log中看到精度计算结果。 - -```bash -'acc': 0.91 -``` - ## 模型描述 ### 性能 diff --git a/official/cv/vgg16/ascend310_quant_infer/acc.py b/official/cv/vgg16/ascend310_quant_infer/acc.py deleted file mode 100644 index 1204fe43203f6bf9a48efea1a8253e7dc3da668d..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/acc.py +++ /dev/null @@ -1,49 +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. -# ============================================================================ -"""postprocess for 310 inference""" -import os -import argparse -import numpy as np - -from mindspore.nn import Top1CategoricalAccuracy - - -parser = argparse.ArgumentParser("vgg16 quant postprocess") -parser.add_argument("--result_path", type=str, required=True, help="path to inference results.") -parser.add_argument("--label_path", type=str, required=True, help="path to label.npy.") - -args, _ = parser.parse_known_args() - -def calculate_acc(result_path, label_path): - """ - Calculate accuracy of VGG16 inference. - - Args: - result_path (str): the directory or inference result. - label_path (str): the path of data label in .npy format. - """ - top1_acc = Top1CategoricalAccuracy() - labels = np.load(label_path, allow_pickle=True) - batch_size = 1 - for idx, _ in enumerate(labels): - f_name = os.path.join(result_path, "VGG16_data_bs" + str(batch_size) + "_" + str(idx) + "_output_0.bin") - pred = np.fromfile(f_name, np.float32) - pred = pred.reshape(batch_size, int(pred.shape[0] / batch_size)) - top1_acc.update(pred, labels[idx]) - print("acc: ", top1_acc.eval()) - - -if __name__ == '__main__': - calculate_acc(args.result_path, args.label_path) diff --git a/official/cv/vgg16/ascend310_quant_infer/export_bin.py b/official/cv/vgg16/ascend310_quant_infer/export_bin.py deleted file mode 100644 index eeddb5cd8cc8ef197348d037ce0b5773ddad812f..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/export_bin.py +++ /dev/null @@ -1,50 +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. -# ============================================================================ -"""generate data and label needed for AIR model inference""" -import os -import sys -import shutil -import numpy as np - - -def generate_data(): - """ - Generate data and label needed for AIR model inference at Ascend310 platform. - """ - config.batch_size = 1 - config.image_size = list(map(int, config.image_size.split(','))) - config.dataset = "cifar10" - - dataset = vgg_create_dataset(config.data_dir, config.image_size, config.batch_size, training=False) - img_path = os.path.join(config.result_path, "00_data") - if os.path.exists(img_path): - shutil.rmtree(img_path) - os.makedirs(img_path) - label_list = [] - for idx, data in enumerate(dataset.create_dict_iterator(output_numpy=True)): - file_name = "VGG16_data_bs" + str(config.batch_size) + "_" + str(idx) + ".bin" - file_path = os.path.join(img_path, file_name) - data["image"].tofile(file_path) - label_list.append(data["label"]) - np.save(os.path.join(config.result_path, "cifar10_label_ids.npy"), label_list) - print("=" * 20, "export bin files finished", "=" * 20) - - -if __name__ == "__main__": - sys.path.append("..") - from src.dataset import vgg_create_dataset - from model_utils.moxing_adapter import config - - generate_data() diff --git a/official/cv/vgg16/ascend310_quant_infer/inc/model_process.h b/official/cv/vgg16/ascend310_quant_infer/inc/model_process.h deleted file mode 100644 index 79e19833c7a1b3e428458c23aaba953dc40b1f01..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/inc/model_process.h +++ /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. - */ - -#pragma once -#include <iostream> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* ModelProcess -*/ -class ModelProcess { - public: - /** - * @brief Constructor - */ - ModelProcess(); - - /** - * @brief Destructor - */ - ~ModelProcess(); - - /** - * @brief load model from file with mem - * @param [in] modelPath: model path - * @return result - */ - Result LoadModelFromFileWithMem(const char *modelPath); - - /** - * @brief unload model - */ - void Unload(); - - /** - * @brief create model desc - * @return result - */ - Result CreateDesc(); - - /** - * @brief destroy desc - */ - void DestroyDesc(); - - /** - * @brief create model input - * @param [in] inputDataBuffer: input buffer - * @param [in] bufferSize: input buffer size - * @return result - */ - Result CreateInput(void *inputDataBuffer, size_t bufferSize); - - /** - * @brief destroy input resource - */ - void DestroyInput(); - - /** - * @brief create output buffer - * @return result - */ - Result CreateOutput(); - - /** - * @brief destroy output resource - */ - void DestroyOutput(); - - /** - * @brief model execute - * @return result - */ - Result Execute(); - - /** - * @brief dump model output result to file - */ - void DumpModelOutputResult(char *output_name); - - /** - * @brief get model output result - */ - void OutputModelResult(); - - private: - uint32_t modelId_; - size_t modelMemSize_; - size_t modelWeightSize_; - void *modelMemPtr_; - void *modelWeightPtr_; - bool loadFlag_; // model load flag - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; -}; - diff --git a/official/cv/vgg16/ascend310_quant_infer/inc/sample_process.h b/official/cv/vgg16/ascend310_quant_infer/inc/sample_process.h deleted file mode 100644 index 24d6ea01e59925673a548a7873ab310623235549..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/inc/sample_process.h +++ /dev/null @@ -1,58 +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. - */ - -#pragma once -#include <string> -#include <vector> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* SampleProcess -*/ -class SampleProcess { - public: - /** - * @brief Constructor - */ - SampleProcess(); - - /** - * @brief Destructor - */ - ~SampleProcess(); - - /** - * @brief init reousce - * @return result - */ - Result InitResource(); - - /** - * @brief sample process - * @return result - */ - Result Process(char *om_path, char *input_folder); - - void GetAllFiles(std::string path, std::vector<std::string> *files); - - private: - void DestroyResource(); - - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; -}; diff --git a/official/cv/vgg16/ascend310_quant_infer/inc/utils.h b/official/cv/vgg16/ascend310_quant_infer/inc/utils.h deleted file mode 100644 index 3ae2a571b8e8ba51c01b02e23f36dfad5a7b18f1..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/inc/utils.h +++ /dev/null @@ -1,52 +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. - */ - -#pragma once -#include <iostream> -#include <string> - -#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) -#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args) -#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) - -typedef enum Result { - SUCCESS = 0, - FAILED = 1 -} Result; - -/** -* Utils -*/ -class Utils { - public: - /** - * @brief create device buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return device buffer of file - */ - static void *GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize); - - /** - * @brief create buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return buffer of pic - */ - static void* ReadBinFile(std::string fileName, uint32_t *fileSize); -}; - -#pragma once diff --git a/official/cv/vgg16/ascend310_quant_infer/post_quant.py b/official/cv/vgg16/ascend310_quant_infer/post_quant.py deleted file mode 100644 index 16f1274155dfbae878d36580ab15d72b0ad0dc3f..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/post_quant.py +++ /dev/null @@ -1,90 +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. -# ============================================================================ -"""do post training quantization for Ascend310""" -import sys -import numpy as np - -from amct_mindspore.quantize_tool import create_quant_config -from amct_mindspore.quantize_tool import quantize_model -from amct_mindspore.quantize_tool import save_model -import mindspore.nn as nn -from mindspore import Tensor, context -from mindspore.nn.optim.momentum import Momentum -from mindspore.train.model import Model -from mindspore.train.serialization import load_checkpoint -from mindspore.common import dtype as mstype - - -def quant_vgg(network, dataset, input_data): - """ - Export post training quantization model of AIR format. - - Args: - network: the origin network for inference. - dataset: the data for inference. - input_data: the data used for constructing network. The shape and format of input data should be the same as - actual data for inference. - """ - - # step2: create the quant config json file - create_quant_config("./config.json", network, input_data) - - # step3: do some network modification and return the modified network - calibration_network = quantize_model("./config.json", network, input_data) - calibration_network.set_train(False) - - # step4: perform the evaluation of network to do activation calibration - loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean") - opt = Momentum(filter(lambda x: x.requires_grad, calibration_network.get_parameters()), 0.01, config.momentum, - weight_decay=config.weight_decay) - model = Model(calibration_network, loss_fn=loss, optimizer=opt, metrics={"acc"}) - _ = model.eval(dataset, dataset_sink_mode=False) - - # step5: export the air file - save_model("results/vgg_quant", calibration_network, input_data) - print("[INFO] the quantized AIR file has been stored at: \n {}".format("results/vgg_quant.air")) - - -def run_export(): - """ - Prepare input parameters needed for exporting quantization model. - """ - context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target) - if config.device_target == "Ascend": - config.device_id = get_device_id() - context.set_context(device_id=config.device_id) - - config.image_size = list(map(int, config.image_size.split(','))) - if config.dataset == "cifar10": - net = vgg16(num_classes=config.num_classes, args=config) - else: - net = vgg16(config.num_classes, config, phase="test") - - load_checkpoint(config.ckpt_file, net=net) - net.set_train(False) - batch_size = 1 - input_data = Tensor(np.zeros([batch_size, 3, config.image_size[0], config.image_size[1]]), mstype.float32) - dataset = vgg_create_dataset(config.data_dir, config.image_size, batch_size, training=False) - ds = dataset.take(1) - quant_vgg(net, ds, input_data) - - -if __name__ == "__main__": - sys.path.append("..") - from src.vgg import vgg16 - from src.dataset import vgg_create_dataset - from model_utils.moxing_adapter import config - from model_utils.device_adapter import get_device_id - run_export() diff --git a/official/cv/vgg16/ascend310_quant_infer/run_quant_infer.sh b/official/cv/vgg16/ascend310_quant_infer/run_quant_infer.sh deleted file mode 100644 index eb157c53eb576aa16cd15250f3114ef8dc6c0bb5..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/run_quant_infer.sh +++ /dev/null @@ -1,104 +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 3 ]; then - echo "Usage: bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [LABEL_PATH]" - echo "Example: bash run_quant_infer.sh ./vgg_quant.air ./00_data ./cifar10_label_ids.npy" -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) -label_path=$(get_real_path $3) - -echo "air name: "$model -echo "dataset path: "$data_path -echo "label path: "$label_path - -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 ASCEND_HOME=/usr/local/Ascend/latest/ - export PATH=$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/fwkacllib/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/atc/lib64:$LD_LIBRARY_PATH - export TBE_IMPL_PATH=$ASCEND_HOME/opp/op_impl/built-in/ai_core/tbe - export PYTHONPATH=${TBE_IMPL_PATH}:$PYTHONPATH - export ASCEND_OPP_PATH=$ASCEND_HOME/opp -fi - -function air_to_om() -{ - atc --input_format=NCHW --framework=1 --model=$model --output=vgg_quant --soc_version=Ascend310 &> atc.log -} - -function compile_app() -{ - bash ./src/build.sh &> build.log -} - -function infer() -{ - if [ -d result ]; then - rm -rf ./result - fi - mkdir result - ./out/main ./vgg_quant.om $data_path &> infer.log -} - -function cal_acc() -{ - python3.7 ./acc.py --result_path=./result --label_path=$label_path &> acc.log -} - -echo "start atc================================================" -air_to_om -if [ $? -ne 0 ]; then - echo "air to om code failed" - exit 1 -fi - -echo "start compile============================================" -compile_app -if [ $? -ne 0 ]; then - echo "compile app code failed" - exit 1 -fi - -echo "start infer==============================================" -infer -if [ $? -ne 0 ]; then - echo " execute inference failed" - exit 1 -fi - -echo "start calculate acc======================================" -cal_acc -if [ $? -ne 0 ]; then - echo "calculate accuracy failed" - exit 1 -fi \ No newline at end of file diff --git a/official/cv/vgg16/ascend310_quant_infer/src/CMakeLists.txt b/official/cv/vgg16/ascend310_quant_infer/src/CMakeLists.txt deleted file mode 100644 index 655026d7d91612267a287e83e886ba2ce1304d18..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/src/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. - -# CMake lowest version requirement -cmake_minimum_required(VERSION 3.5.1) -# project information -project(InferClassification) -# Check environment variable -if(NOT DEFINED ENV{ASCEND_HOME}) - message(FATAL_ERROR "please define environment variable:ASCEND_HOME") -endif() - -# Compile options -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") - -# Skip build rpath -set(CMAKE_SKIP_BUILD_RPATH True) - -# Set output directory -set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SRC_ROOT}/../out) - -# Set include directory and library directory -set(FWKACL_LIB_DIR $ENV{ASCEND_HOME}/fwkacllib) -set(ACL_LIB_DIR $ENV{ASCEND_HOME}/acllib) -set(ATLAS_ACL_LIB_DIR $ENV{ASCEND_HOME}/ascend-toolkit/latest/acllib) - -# Header path -include_directories(${ACL_LIB_DIR}/include/) -include_directories(${FWKACL_LIB_DIR}/include/) -include_directories(${ATLAS_ACL_LIB_DIR}/include/) -include_directories(${PROJECT_SRC_ROOT}/../inc) - -# add host lib path -link_directories(${ACL_LIB_DIR} ${FWKACL_LIB_DIR}) -find_library(acl libascendcl.so ${ACL_LIB_DIR}/lib64 ${FWKACL_LIB_DIR}/lib64 ${ATLAS_ACL_LIB_DIR}/lib64) - -add_executable(main utils.cpp - sample_process.cpp - model_process.cpp - main.cpp) - -target_link_libraries(main ${acl} gflags pthread) diff --git a/official/cv/vgg16/ascend310_quant_infer/src/acl.json b/official/cv/vgg16/ascend310_quant_infer/src/acl.json deleted file mode 100644 index 0967ef424bce6791893e9a57bb952f80fd536e93..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/src/acl.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/official/cv/vgg16/ascend310_quant_infer/src/build.sh b/official/cv/vgg16/ascend310_quant_infer/src/build.sh deleted file mode 100644 index b5979b68e60b673f763a3cac98c5e147e7085291..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/src/build.sh +++ /dev/null @@ -1,55 +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. -# ============================================================================ -path_cur=$(cd "`dirname $0`" || exit; pwd) - -function preparePath() { - rm -rf $1 - mkdir -p $1 - cd $1 || exit -} - -function buildA300() { - if [ ! "${ARCH_PATTERN}" ]; then - # set ARCH_PATTERN to acllib when it was not specified by user - export ARCH_PATTERN=acllib - echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}" - else - echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user, reset it to ${ARCH_PATTERN}/acllib" - export ARCH_PATTERN=${ARCH_PATTERN}/acllib - fi - - path_build=$path_cur/build - preparePath $path_build - cmake .. - make -j - ret=$? - cd .. - return ${ret} -} - -# 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 - -buildA300 - -if [ $? -ne 0 ]; then - exit 1 -fi diff --git a/official/cv/vgg16/ascend310_quant_infer/src/main.cpp b/official/cv/vgg16/ascend310_quant_infer/src/main.cpp deleted file mode 100644 index 80165505f447d418e0f107b76d04ffae59b89a73..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/src/main.cpp +++ /dev/null @@ -1,42 +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 <iostream> -#include "../inc/sample_process.h" -#include "../inc/utils.h" -bool g_is_device = false; - -int main(int argc, char **argv) { - if (argc != 3) { - ERROR_LOG("usage:./main path_of_om path_of_inputFolder"); - return FAILED; - } - SampleProcess processSample; - Result ret = processSample.InitResource(); - if (ret != SUCCESS) { - ERROR_LOG("sample init resource failed"); - return FAILED; - } - - ret = processSample.Process(argv[1], argv[2]); - if (ret != SUCCESS) { - ERROR_LOG("sample process failed"); - return FAILED; - } - - INFO_LOG("execute sample success"); - return SUCCESS; -} diff --git a/official/cv/vgg16/ascend310_quant_infer/src/model_process.cpp b/official/cv/vgg16/ascend310_quant_infer/src/model_process.cpp deleted file mode 100644 index 04e6a42ab43cbc41720fe6b9e30bf919323c9f9e..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/src/model_process.cpp +++ /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. - */ - -#include "../inc/model_process.h" -#include <iostream> -#include <map> -#include <sstream> -#include <algorithm> -#include "../inc/utils.h" -extern bool g_is_device; - -ModelProcess::ModelProcess() :modelId_(0), modelMemSize_(0), modelWeightSize_(0), modelMemPtr_(nullptr), -modelWeightPtr_(nullptr), loadFlag_(false), modelDesc_(nullptr), input_(nullptr), output_(nullptr) { -} - -ModelProcess::~ModelProcess() { - Unload(); - DestroyDesc(); - DestroyInput(); - DestroyOutput(); -} - -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { - if (loadFlag_) { - ERROR_LOG("has already loaded a model"); - return FAILED; - } - - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("query model failed, model file is %s", modelPath); - return FAILED; - } - - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); - return FAILED; - } - - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); - return FAILED; - } - - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("load model from file failed, model file is %s", modelPath); - return FAILED; - } - - loadFlag_ = true; - INFO_LOG("load model %s success", modelPath); - return SUCCESS; -} - -Result ModelProcess::CreateDesc() { - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { - ERROR_LOG("create model description failed"); - return FAILED; - } - - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("get model description failed"); - return FAILED; - } - - INFO_LOG("create model description success"); - - return SUCCESS; -} - -void ModelProcess::DestroyDesc() { - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } -} - -Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { - ERROR_LOG("can't create dataset, create input failed"); - return FAILED; - } - - aclDataBuffer* inputData = aclCreateDataBuffer(inputDataBuffer, bufferSize); - if (inputData == nullptr) { - ERROR_LOG("can't create data buffer, create input failed"); - return FAILED; - } - - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("add input dataset buffer failed"); - aclDestroyDataBuffer(inputData); - inputData = nullptr; - return FAILED; - } - - return SUCCESS; -} - -void ModelProcess::DestroyInput() { - if (input_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); - aclDestroyDataBuffer(dataBuffer); - } - aclmdlDestroyDataset(input_); - input_ = nullptr; -} - -Result ModelProcess::CreateOutput() { - if (modelDesc_ == nullptr) { - ERROR_LOG("no model description, create output failed"); - return FAILED; - } - - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { - ERROR_LOG("can't create dataset, create output failed"); - return FAILED; - } - - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); - for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); - - void *outputBuffer = nullptr; - aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't malloc buffer, size is %zu, create output failed", buffer_size); - return FAILED; - } - - aclDataBuffer* outputData = aclCreateDataBuffer(outputBuffer, buffer_size); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't create data buffer, create output failed"); - aclrtFree(outputBuffer); - return FAILED; - } - - ret = aclmdlAddDatasetBuffer(output_, outputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't add data buffer, create output failed"); - aclrtFree(outputBuffer); - aclDestroyDataBuffer(outputData); - return FAILED; - } - } - - INFO_LOG("create model output success"); - return SUCCESS; -} - -void ModelProcess::DumpModelOutputResult(char *output_name) { - size_t outputNum = aclmdlGetDatasetNumBuffers(output_); - - for (size_t i = 0; i < outputNum; ++i) { - std::stringstream ss; - ss << "result/" << output_name << "_output_" << i << ".bin"; - std::string outputFileName = ss.str(); - FILE *outputFile = fopen(outputFileName.c_str(), "wb"); - if (outputFile != nullptr) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void* outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - (void)aclrtFreeHost(outHostData); - return; - } - - fwrite(outHostData, len, sizeof(char), outputFile); - - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } else { - fwrite(data, len, sizeof(char), outputFile); - } - fclose(outputFile); - outputFile = nullptr; - } else { - ERROR_LOG("create output file [%s] failed", outputFileName.c_str()); - return; - } - } - - INFO_LOG("dump data success"); - return; -} - -void ModelProcess::OutputModelResult() { - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void *outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - float *outData = NULL; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - return; - } - - outData = reinterpret_cast<float*>(outHostData); - } else { - outData = reinterpret_cast<float*>(data); - } - std::map<float, unsigned int, std::greater<float> > resultMap; - for (unsigned int j = 0; j < len / sizeof(float); ++j) { - resultMap[*outData] = j; - outData++; - } - - int cnt = 0; - for (auto it = resultMap.begin(); it != resultMap.end(); ++it) { - // print top 5 - if (++cnt > 5) { - break; - } - - INFO_LOG("top %d: index[%d] value[%lf]", cnt, it->second, it->first); - } - if (!g_is_device) { - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } - } - - INFO_LOG("output data success"); - return; -} - -void ModelProcess::DestroyOutput() { - if (output_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - (void)aclrtFree(data); - (void)aclDestroyDataBuffer(dataBuffer); - } - - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; -} - -Result ModelProcess::Execute() { - aclError ret = aclmdlExecute(modelId_, input_, output_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); - return FAILED; - } - - INFO_LOG("model execute success"); - return SUCCESS; -} - -void ModelProcess::Unload() { - if (!loadFlag_) { - WARN_LOG("no model had been loaded, unload failed"); - return; - } - - aclError ret = aclmdlUnload(modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); - } - - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } - - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; - } - - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; - } - - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); -} diff --git a/official/cv/vgg16/ascend310_quant_infer/src/sample_process.cpp b/official/cv/vgg16/ascend310_quant_infer/src/sample_process.cpp deleted file mode 100644 index cbf58dbae68ff8b4c1b662f228218fe607810512..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/src/sample_process.cpp +++ /dev/null @@ -1,256 +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 "../inc/sample_process.h" -#include <sys/time.h> -#include <sys/types.h> -#include <dirent.h> -#include <string.h> -#include <iostream> -#include <fstream> -#include "../inc/model_process.h" -#include "acl/acl.h" -#include "../inc/utils.h" -extern bool g_is_device; -using std::string; -using std::vector; - -SampleProcess::SampleProcess() :deviceId_(0), context_(nullptr), stream_(nullptr) { -} - -SampleProcess::~SampleProcess() { - DestroyResource(); -} - -Result SampleProcess::InitResource() { - // ACL init - - const char *aclConfigPath = "./src/acl.json"; - aclError ret = aclInit(aclConfigPath); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl init failed"); - return FAILED; - } - INFO_LOG("acl init success"); - - // open device - ret = aclrtSetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl open device %d failed", deviceId_); - return FAILED; - } - INFO_LOG("open device %d success", deviceId_); - - // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create context failed"); - return FAILED; - } - INFO_LOG("create context success"); - - // create stream - ret = aclrtCreateStream(&stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create stream failed"); - return FAILED; - } - INFO_LOG("create stream success"); - - // get run mode - aclrtRunMode runMode; - ret = aclrtGetRunMode(&runMode); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl get run mode failed"); - return FAILED; - } - g_is_device = (runMode == ACL_DEVICE); - INFO_LOG("get run mode success"); - return SUCCESS; -} - -void SampleProcess::GetAllFiles(std::string path, std::vector<string> *files) { - DIR *pDir = NULL; - struct dirent* ptr = nullptr; - if (!(pDir = opendir(path.c_str()))) { - return; - } - while ((ptr = readdir(pDir)) != 0) { - if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { - files->push_back(path + "/" + ptr->d_name); - } - } - closedir(pDir); -} - -Result SampleProcess::Process(char *om_path, char *input_folder) { - // model init - const double second_to_millisecond = 1000; - const double second_to_microsecond = 1000000; - - double whole_cost_time = 0.0; - struct timeval start_global = {0}; - struct timeval end_global = {0}; - double startTimeMs_global = 0.0; - double endTimeMs_global = 0.0; - - gettimeofday(&start_global, nullptr); - - ModelProcess processModel; - const char* omModelPath = om_path; - - Result ret = processModel.LoadModelFromFileWithMem(omModelPath); - if (ret != SUCCESS) { - ERROR_LOG("execute LoadModelFromFileWithMem failed"); - return FAILED; - } - - ret = processModel.CreateDesc(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateDesc failed"); - return FAILED; - } - - ret = processModel.CreateOutput(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateOutput failed"); - return FAILED; - } - - std::vector<string> testFile; - GetAllFiles(input_folder, &testFile); - - if (testFile.size() == 0) { - WARN_LOG("no input data under folder"); - } - - // loop begin - - double model_cost_time = 0.0; - double edge_to_edge_model_cost_time = 0.0; - - for (size_t index = 0; index < testFile.size(); ++index) { - INFO_LOG("start to process file:%s", testFile[index].c_str()); - // model process - - struct timeval time_init = {0}; - double timeval_init = 0.0; - gettimeofday(&time_init, nullptr); - timeval_init = (time_init.tv_sec * second_to_microsecond + time_init.tv_usec) / second_to_millisecond; - - uint32_t devBufferSize; - void *picDevBuffer = Utils::GetDeviceBufferOfFile(testFile[index], &devBufferSize); - if (picDevBuffer == nullptr) { - ERROR_LOG("get pic device buffer failed,index is %zu", index); - return FAILED; - } - ret = processModel.CreateInput(picDevBuffer, devBufferSize); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateInput failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - struct timeval start = {0}; - struct timeval end = {0}; - double startTimeMs = 0.0; - double endTimeMs = 0.0; - gettimeofday(&start, nullptr); - startTimeMs = (start.tv_sec * second_to_microsecond + start.tv_usec) / second_to_millisecond; - - ret = processModel.Execute(); - - gettimeofday(&end, nullptr); - endTimeMs = (end.tv_sec * second_to_microsecond + end.tv_usec) / second_to_millisecond; - - double cost_time = endTimeMs - startTimeMs; - INFO_LOG("model infer time: %lf ms", cost_time); - - model_cost_time += cost_time; - - double edge_to_edge_cost_time = endTimeMs - timeval_init; - edge_to_edge_model_cost_time += edge_to_edge_cost_time; - - if (ret != SUCCESS) { - ERROR_LOG("execute inference failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - int pos = testFile[index].find_last_of('/'); - std::string name = testFile[index].substr(pos+1); - std::string outputname = name.substr(0, name.rfind(".")); - - // dump output result to file in the current directory - processModel.DumpModelOutputResult(const_cast<char *>(outputname.c_str())); - - // release model input buffer - aclrtFree(picDevBuffer); - processModel.DestroyInput(); - } - double test_file_size = 0.0; - test_file_size = testFile.size(); - INFO_LOG("infer dataset size:%lf", test_file_size); - - gettimeofday(&end_global, nullptr); - startTimeMs_global = (start_global.tv_sec * second_to_microsecond + start_global.tv_usec) / second_to_millisecond; - endTimeMs_global = (end_global.tv_sec * second_to_microsecond + end_global.tv_usec) / second_to_millisecond; - whole_cost_time = (endTimeMs_global - startTimeMs_global) / test_file_size; - - model_cost_time /= test_file_size; - INFO_LOG("model cost time per sample: %lf ms", model_cost_time); - edge_to_edge_model_cost_time /= test_file_size; - INFO_LOG("edge-to-edge model cost time per sample:%lf ms", edge_to_edge_model_cost_time); - INFO_LOG("whole cost time per sample: %lf ms", whole_cost_time); - - // loop end - - return SUCCESS; -} - -void SampleProcess::DestroyResource() { - aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy stream failed"); - } - stream_ = nullptr; - } - INFO_LOG("end to destroy stream"); - - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy context failed"); - } - context_ = nullptr; - } - INFO_LOG("end to destroy context"); - - ret = aclrtResetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("reset device failed"); - } - INFO_LOG("end to reset device is %d", deviceId_); - - ret = aclFinalize(); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("finalize acl failed"); - } - INFO_LOG("end to finalize acl"); -} - diff --git a/official/cv/vgg16/ascend310_quant_infer/src/utils.cpp b/official/cv/vgg16/ascend310_quant_infer/src/utils.cpp deleted file mode 100644 index d9208c8cfd9979e9248046e7325f260eb2d14d80..0000000000000000000000000000000000000000 --- a/official/cv/vgg16/ascend310_quant_infer/src/utils.cpp +++ /dev/null @@ -1,113 +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 "../inc/utils.h" -#include <sys/stat.h> -#include <iostream> -#include <fstream> -#include <cstring> -#include "acl/acl.h" - -extern bool g_is_device; - -void* Utils::ReadBinFile(std::string fileName, uint32_t *fileSize) { - struct stat sBuf; - int fileStatus = stat(fileName.data(), &sBuf); - if (fileStatus == -1) { - ERROR_LOG("failed to get file"); - return nullptr; - } - if (S_ISREG(sBuf.st_mode) == 0) { - ERROR_LOG("%s is not a file, please enter a file", fileName.c_str()); - return nullptr; - } - - std::ifstream binFile(fileName, std::ifstream::binary); - if (binFile.is_open() == false) { - ERROR_LOG("open file %s failed", fileName.c_str()); - return nullptr; - } - - binFile.seekg(0, binFile.end); - uint32_t binFileBufferLen = binFile.tellg(); - if (binFileBufferLen == 0) { - ERROR_LOG("binfile is empty, filename is %s", fileName.c_str()); - binFile.close(); - return nullptr; - } - - binFile.seekg(0, binFile.beg); - - void* binFileBufferData = nullptr; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&binFileBufferData, binFileBufferLen); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc for binFileBufferData failed"); - binFile.close(); - return nullptr; - } - if (binFileBufferData == nullptr) { - ERROR_LOG("malloc binFileBufferData failed"); - binFile.close(); - return nullptr; - } - } else { - ret = aclrtMalloc(&binFileBufferData, binFileBufferLen, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", binFileBufferLen); - binFile.close(); - return nullptr; - } - } - binFile.read(static_cast<char *>(binFileBufferData), binFileBufferLen); - binFile.close(); - *fileSize = binFileBufferLen; - return binFileBufferData; -} - -void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize) { - uint32_t inputHostBuffSize = 0; - void* inputHostBuff = Utils::ReadBinFile(fileName, &inputHostBuffSize); - if (inputHostBuff == nullptr) { - return nullptr; - } - if (!g_is_device) { - void *inBufferDev = nullptr; - uint32_t inBufferSize = inputHostBuffSize; - aclError ret = aclrtMalloc(&inBufferDev, inBufferSize, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", inBufferSize); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - - ret = aclrtMemcpy(inBufferDev, inBufferSize, inputHostBuff, inputHostBuffSize, ACL_MEMCPY_HOST_TO_DEVICE); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("memcpy failed. device buffer size is %u, input host buffer size is %u", - inBufferSize, inputHostBuffSize); - aclrtFree(inBufferDev); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - aclrtFreeHost(inputHostBuff); - *fileSize = inBufferSize; - return inBufferDev; - } else { - *fileSize = inputHostBuffSize; - return inputHostBuff; - } -} diff --git a/official/cv/yolov3_darknet53/README.md b/official/cv/yolov3_darknet53/README.md index e0e8c528012aa05ffb00d587c6e93b6f23b39dc1..56541538e43319cb2b3fdecb8ed8d8dd9f601ace 100644 --- a/official/cv/yolov3_darknet53/README.md +++ b/official/cv/yolov3_darknet53/README.md @@ -16,7 +16,6 @@ - [Evaluation](#evaluation) - [Export MindIR](#export-mindir) - [Inference Process](#inference-process) - - [Post Training Quantization](#post-training-quantization) - [Model Description](#model-description) - [Performance](#performance) - [Evaluation Performance](#evaluation-performance) @@ -449,59 +448,6 @@ Inference result is saved in current path, you can find result in acc.log file. Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.551 ``` -### [Post Training Quantization(Only useful for Ascend device)](#contents) - -Relative executing script files reside in the directory "ascend310_quant_infer". Please implement following steps sequentially to complete post quantization. -Current quantization project bases on COCO2014 dataset. - -1. In Ascend910 platform, generate data of .bin format required for AIR model inference at Ascend310 platform. - -```shell -python export_bin.py --config_path [YMAL CONFIG PATH] --data_dir [DATA DIR] --annFile [ANNOTATION FILE PATH] -``` - -[DATA DIR] is for eval data path, [ANNOTATION FILE PATH] is for annotation path. - -2. In Ascend910 platform, export quantized AIR model. - -Post quantization of model requires special toolkits for exporting quantized AIR model. Please refer to [official website](https://www.hiascend.com/software/cann/community). - -```shell -python post_quant.py --config_path [YMAL CONFIG PATH] --ckpt_file [CKPT_PATH] --data_dir [DATASET PATH] --annFile [ANNOTATION FILE PATH] -``` - -[DATASET PATH] is for eval data path, [ANNOTATION FILE PATH] is for annotation path. - -The quantized AIR file will be stored as "./results/yolov3_quant.air". - -3. Copy bin data and AIR model to Ascend310 platform, and implement inference at Ascend310 platform. - -```shell -# Ascend310 quant inference -cd ascend310_quant_infer/ -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [IMAGE_ID] [IMAGE_SHAPE] [ANN_FILE] -``` - -AIR_PATH is air path generated in step 2, DATA_PATH is evaluation data path, IMAGE_ID is image binary file generated in step 1,IMAGE_SHAPE is shape binary file generated in step 2, ANN_FILE is annotation file. - -Inference result is saved in current path, you can find result like this in acc.log file. - -```bash -=============coco eval result========= - Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.306 - Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.524 - Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.314 - Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.122 - Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.319 - Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.423 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.256 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.395 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.419 - Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.219 - Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.438 - Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.548 -``` - ## [Model Description](#contents) ### [Performance](#contents) diff --git a/official/cv/yolov3_darknet53/README_CN.md b/official/cv/yolov3_darknet53/README_CN.md index c806a37209faec68ee7c4eddb97d89040b2831e4..7632049cf2863f361a97c1bc5f68a13faa040bd5 100644 --- a/official/cv/yolov3_darknet53/README_CN.md +++ b/official/cv/yolov3_darknet53/README_CN.md @@ -20,7 +20,6 @@ - [推理过程](#推理过程) - [用法](#用法-2) - [结果](#结果-2) - - [训练后量化推理](#训练后量化推理) - [模型描述](#模型描述) - [性能](#性能) - [评估性能](#评估性能) @@ -443,58 +442,6 @@ bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANNO_PATH] [DEVICE_ID] Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.551 ``` -## [训练后量化推理(仅在Ascend场景下)](#contents) - -训练后量化推理的相关执行脚本文件在"ascend310_quant_infer"目录下,依次执行以下步骤实现训练后量化推理。本训练后量化工程基于COCO2014数据集。 - -1、在Ascend910环境上,生成Ascend310平台AIR模型推理需要的.bin格式数据。 - -```shell -python export_bin.py --config_path [YMAL CONFIG PATH] --data_dir [DATA DIR] --annFile [ANNOTATION FILE PATH] -``` - -其中,[DATA DIR]为推理数据的路径,[ANNOTATION FILE PATH]为用到的注解文件路径 - -2、在Ascend910环境上,生成训练后量化的AIR格式模型。 - -导出训练后量化模型需要配套的量化工具包,参考[官方地址](https://www.hiascend.com/software/cann/community) - -```shell -python post_quant.py --config_path [YMAL CONFIG PATH] --ckpt_file [CKPT_PATH] --data_dir [DATASET PATH] --annFile [ANNOTATION FILE PATH] -``` - -其中,[DATASET PATH]为推理数据的路径,[ANNOTATION FILE PATH]为注解文件的路径。 - -导出的模型会存储在./result/yolov3_quant.air。 - -3、将前两步生成的数据拷贝到310环境上,在Ascend310执行推理量化模型。 - -```shell -# Ascend310 quant inference -cd ascend310_quant_infer/ -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [IMAGE_ID] [IMAGE_SHAPE] [ANN_FILE] -``` - -其中,AIR_PATH为第二步生成的air文件路径,DATA_PATH为推理数据的路径,IMAGE_ID为第一步生成的image_id相关二进制文件,IMAGE_SHAPE为第一步生成的shape相关二进制文件,ANN_FILE为注解文件路径 - -推理结果保存在脚本执行的当前路径,可以在acc.log中看到精度计算结果。 - -```bash -=============coco eval result========= - Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.306 - Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.524 - Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.314 - Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.122 - Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.319 - Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.423 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.256 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.395 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.419 - Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.219 - Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.438 - Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.548 -``` - # 模型描述 ## 性能 diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/acc.py b/official/cv/yolov3_darknet53/ascend310_quant_infer/acc.py deleted file mode 100644 index a6d72c158daed3c659a39bd02fbb17cb61235dbc..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/acc.py +++ /dev/null @@ -1,231 +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. -# ============================================================================ -"""postprocess for 310 inference""" -import os -import datetime -import argparse -import sys -from collections import defaultdict -import numpy as np -from pycocotools.coco import COCO -from pycocotools.cocoeval import COCOeval - - -parser = argparse.ArgumentParser('YoloV3 quant postprocess') -parser.add_argument('--result_path', type=str, required=True, help='result files path.') -parser.add_argument('--batch_size', default=1, type=int, help='batch size for per gpu') -parser.add_argument('--nms_thresh', type=float, default=0.5, help='threshold for NMS') -parser.add_argument('--eval_ignore_threshold', type=float, default=0.001, help='threshold to throw low quality boxes') -parser.add_argument('--annFile', type=str, default='', help='path to annotation') -parser.add_argument('--image_shape', type=str, default='./image_shape.npy', help='path to image_shape.npy') -parser.add_argument('--image_id', type=str, default='./image_id.npy', help='path to image_id.npy') -parser.add_argument('--log_path', type=str, default='outputs/', help='inference result save location') - -args, _ = parser.parse_known_args() - - -class Redirct: - def __init__(self): - self.content = "" - - def write(self, content): - self.content += content - - def flush(self): - self.content = "" - - -class DetectionEngine: - """Detection engine.""" - def __init__(self): - self.eval_ignore_threshold = args.eval_ignore_threshold - self.labels = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', - 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', - 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', - 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', - 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', - 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', - 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', - 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', - 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', - 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'] - self.num_classes = len(self.labels) - self.results = {} - self.file_path = '' - self.save_prefix = args.outputs_dir - self.annFile = args.annFile - self._coco = COCO(self.annFile) - self._img_ids = list(sorted(self._coco.imgs.keys())) - self.det_boxes = [] - self.nms_thresh = args.nms_thresh - self.coco_catIds = self._coco.getCatIds() - - def do_nms_for_results(self): - """Get result boxes.""" - for img_id in self.results: - for clsi in self.results[img_id]: - dets = self.results[img_id][clsi] - dets = np.array(dets) - keep_index = self._nms(dets, self.nms_thresh) - - keep_box = [{'image_id': int(img_id), - 'category_id': int(clsi), - 'bbox': list(dets[i][:4].astype(float)), - 'score': dets[i][4].astype(float)} - for i in keep_index] - self.det_boxes.extend(keep_box) - - def _nms(self, predicts, threshold): - """Calculate NMS.""" - # convert xywh -> xmin ymin xmax ymax - x1 = predicts[:, 0] - y1 = predicts[:, 1] - x2 = x1 + predicts[:, 2] - y2 = y1 + predicts[:, 3] - scores = predicts[:, 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) - - indexes = np.where(ovr <= threshold)[0] - order = order[indexes + 1] - return reserved_boxes - - def write_result(self): - """Save result to file.""" - import json - t = datetime.datetime.now().strftime('_%Y_%m_%d_%H_%M_%S') - try: - self.file_path = self.save_prefix + '/predict' + t + '.json' - f = open(self.file_path, 'w') - json.dump(self.det_boxes, f) - except IOError as e: - raise RuntimeError("Unable to open json file to dump. What(): {}".format(str(e))) - else: - f.close() - return self.file_path - - def get_eval_result(self): - """Get eval result.""" - cocoGt = COCO(self.annFile) - cocoDt = cocoGt.loadRes(self.file_path) - cocoEval = COCOeval(cocoGt, cocoDt, 'bbox') - cocoEval.evaluate() - cocoEval.accumulate() - rdct = Redirct() - stdout = sys.stdout - sys.stdout = rdct - cocoEval.summarize() - sys.stdout = stdout - return rdct.content - - def detect(self, outputs, batch, image_shape, image_id): - """Detect boxes.""" - outputs_num = len(outputs) - # output [|32, 52, 52, 3, 85| ] - for batch_id in range(batch): - for out_id in range(outputs_num): - # 32, 52, 52, 3, 85 - out_item = outputs[out_id] - # 52, 52, 3, 85 - out_item_single = out_item[batch_id, :] - # get number of items in one head, [B, gx, gy, anchors, 5+80] - dimensions = out_item_single.shape[:-1] - out_num = 1 - for d in dimensions: - out_num *= d - ori_w, ori_h = image_shape[batch_id] - img_id = int(image_id[batch_id]) - x = out_item_single[..., 0] * ori_w - y = out_item_single[..., 1] * ori_h - w = out_item_single[..., 2] * ori_w - h = out_item_single[..., 3] * ori_h - - conf = out_item_single[..., 4:5] - cls_emb = out_item_single[..., 5:] - - cls_argmax = np.expand_dims(np.argmax(cls_emb, axis=-1), axis=-1) - x = x.reshape(-1) - y = y.reshape(-1) - w = w.reshape(-1) - h = h.reshape(-1) - cls_emb = cls_emb.reshape(-1, self.num_classes) - conf = conf.reshape(-1) - cls_argmax = cls_argmax.reshape(-1) - - x_top_left = x - w / 2. - y_top_left = y - h / 2. - # create all False - flag = np.random.random(cls_emb.shape) > sys.maxsize - for i in range(flag.shape[0]): - c = cls_argmax[i] - flag[i, c] = True - confidence = cls_emb[flag] * conf - for x_lefti, y_lefti, wi, hi, confi, clsi in zip(x_top_left, y_top_left, w, h, confidence, cls_argmax): - if confi < self.eval_ignore_threshold: - continue - if img_id not in self.results: - self.results[img_id] = defaultdict(list) - x_lefti = max(0, x_lefti) - y_lefti = max(0, y_lefti) - wi = min(wi, ori_w) - hi = min(hi, ori_h) - # transform catId to match coco - coco_clsi = self.coco_catIds[clsi] - self.results[img_id][coco_clsi].append([x_lefti, y_lefti, wi, hi, confi]) - - -if __name__ == "__main__": - args.outputs_dir = os.path.join(args.log_path, datetime.datetime.now().strftime('%Y-%m-%d_time_%H_%M_%S')) - if not os.path.exists(args.outputs_dir): - os.makedirs(args.outputs_dir) - detection = DetectionEngine() - bs = args.batch_size - shape_list = np.load(args.image_shape) - id_list = np.load(args.image_id) - prefix = "YoloV3-DarkNet_coco_bs_" + str(bs) + "_" - iter_num = 0 - for id_img in id_list: - shape_img = shape_list[iter_num] - path_small = os.path.join(args.result_path, prefix + str(iter_num) + '_output_0.bin') - path_medium = os.path.join(args.result_path, prefix + str(iter_num) + '_output_1.bin') - path_big = os.path.join(args.result_path, prefix + str(iter_num) + '_output_2.bin') - if os.path.exists(path_small) and os.path.exists(path_medium) and os.path.exists(path_big): - output_small = np.fromfile(path_small, np.float32).reshape(bs, 13, 13, 3, 85) - output_medium = np.fromfile(path_medium, np.float32).reshape(bs, 26, 26, 3, 85) - output_big = np.fromfile(path_big, np.float32).reshape(bs, 52, 52, 3, 85) - detection.detect([output_small, output_medium, output_big], bs, shape_img, id_img) - else: - print("Error: Image ", iter_num, " is not exist.") - iter_num += 1 - - detection.do_nms_for_results() - result_file_path = detection.write_result() - eval_result = detection.get_eval_result() - print('\n=============coco eval result=========\n' + eval_result) diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/export_bin.py b/official/cv/yolov3_darknet53/ascend310_quant_infer/export_bin.py deleted file mode 100644 index 6403824e82519d9e9b96f9b67539e17b5a3f80f4..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/export_bin.py +++ /dev/null @@ -1,60 +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. -# ============================================================================ -"""generate data and label needed for AIR model inference""" -import os -import sys -import numpy as np - - -def generate_data(): - """ - Generate data and label needed for AIR model inference at Ascend310 platform. - """ - config.batch_size = 1 - data_path = os.path.join(config.data_dir, "val2014") - ds, data_size = create_yolo_dataset(data_path, config.annFile, is_training=False, batch_size=config.batch_size, - max_epoch=1, device_num=1, rank=0, shuffle=False, config=config) - print('testing shape : {}'.format(config.test_img_shape)) - print('total {} images to eval'.format(data_size)) - - save_folder = "./data" - image_folder = os.path.join(save_folder, "image_bin") - if not os.path.exists(image_folder): - os.makedirs(image_folder) - - list_image_shape = [] - list_image_id = [] - - for i, data in enumerate(ds.create_dict_iterator()): - image = data["image"].asnumpy() - image_shape = data["image_shape"] - image_id = data["img_id"] - file_name = "YoloV3-DarkNet_coco_bs_" + str(config.batch_size) + "_" + str(i) + ".bin" - file_path = image_folder + "/" + file_name - image.tofile(file_path) - list_image_shape.append(image_shape.asnumpy()) - list_image_id.append(image_id.asnumpy()) - shapes = np.array(list_image_shape) - ids = np.array(list_image_id) - np.save(save_folder + "/image_shape.npy", shapes) - np.save(save_folder + "/image_id.npy", ids) - - -if __name__ == '__main__': - sys.path.append("..") - from model_utils.config import config - from src.yolo_dataset import create_yolo_dataset - - generate_data() diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/inc/model_process.h b/official/cv/yolov3_darknet53/ascend310_quant_infer/inc/model_process.h deleted file mode 100644 index 79e19833c7a1b3e428458c23aaba953dc40b1f01..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/inc/model_process.h +++ /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. - */ - -#pragma once -#include <iostream> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* ModelProcess -*/ -class ModelProcess { - public: - /** - * @brief Constructor - */ - ModelProcess(); - - /** - * @brief Destructor - */ - ~ModelProcess(); - - /** - * @brief load model from file with mem - * @param [in] modelPath: model path - * @return result - */ - Result LoadModelFromFileWithMem(const char *modelPath); - - /** - * @brief unload model - */ - void Unload(); - - /** - * @brief create model desc - * @return result - */ - Result CreateDesc(); - - /** - * @brief destroy desc - */ - void DestroyDesc(); - - /** - * @brief create model input - * @param [in] inputDataBuffer: input buffer - * @param [in] bufferSize: input buffer size - * @return result - */ - Result CreateInput(void *inputDataBuffer, size_t bufferSize); - - /** - * @brief destroy input resource - */ - void DestroyInput(); - - /** - * @brief create output buffer - * @return result - */ - Result CreateOutput(); - - /** - * @brief destroy output resource - */ - void DestroyOutput(); - - /** - * @brief model execute - * @return result - */ - Result Execute(); - - /** - * @brief dump model output result to file - */ - void DumpModelOutputResult(char *output_name); - - /** - * @brief get model output result - */ - void OutputModelResult(); - - private: - uint32_t modelId_; - size_t modelMemSize_; - size_t modelWeightSize_; - void *modelMemPtr_; - void *modelWeightPtr_; - bool loadFlag_; // model load flag - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; -}; - diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/inc/sample_process.h b/official/cv/yolov3_darknet53/ascend310_quant_infer/inc/sample_process.h deleted file mode 100644 index 24d6ea01e59925673a548a7873ab310623235549..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/inc/sample_process.h +++ /dev/null @@ -1,58 +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. - */ - -#pragma once -#include <string> -#include <vector> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* SampleProcess -*/ -class SampleProcess { - public: - /** - * @brief Constructor - */ - SampleProcess(); - - /** - * @brief Destructor - */ - ~SampleProcess(); - - /** - * @brief init reousce - * @return result - */ - Result InitResource(); - - /** - * @brief sample process - * @return result - */ - Result Process(char *om_path, char *input_folder); - - void GetAllFiles(std::string path, std::vector<std::string> *files); - - private: - void DestroyResource(); - - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; -}; diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/inc/utils.h b/official/cv/yolov3_darknet53/ascend310_quant_infer/inc/utils.h deleted file mode 100644 index 3ae2a571b8e8ba51c01b02e23f36dfad5a7b18f1..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/inc/utils.h +++ /dev/null @@ -1,52 +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. - */ - -#pragma once -#include <iostream> -#include <string> - -#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) -#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args) -#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) - -typedef enum Result { - SUCCESS = 0, - FAILED = 1 -} Result; - -/** -* Utils -*/ -class Utils { - public: - /** - * @brief create device buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return device buffer of file - */ - static void *GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize); - - /** - * @brief create buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return buffer of pic - */ - static void* ReadBinFile(std::string fileName, uint32_t *fileSize); -}; - -#pragma once diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/post_quant.py b/official/cv/yolov3_darknet53/ascend310_quant_infer/post_quant.py deleted file mode 100644 index 89adccb605ceca0c1b6695d0c9b36c42b45edc04..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/post_quant.py +++ /dev/null @@ -1,78 +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. -# ============================================================================ -"""do post training quantization for Ascend310""" -import os -import sys -import numpy as np - -from amct_mindspore.quantize_tool import create_quant_config -from amct_mindspore.quantize_tool import quantize_model -from amct_mindspore.quantize_tool import save_model -import mindspore as ms -from mindspore import context, Tensor -from mindspore.train.serialization import load_checkpoint, load_param_into_net - - -def quant_yolov3(network, dataset, input_data): - """ - Export post training quantization model of AIR format. - - Args: - network: the origin network for inference. - dataset: the data for inference. - input_data: the data used for constructing network. The shape and format of input data should be the same as - actual data for inference. - """ - - # step2: create the quant config json file - create_quant_config("./config.json", network, input_data) - - # step3: do some network modification and return the modified network - calibration_network = quantize_model("./config.json", network, input_data) - calibration_network.set_train(False) - - # step4: perform the evaluation of network to do activation calibration - for _, data in enumerate(dataset.create_dict_iterator(num_epochs=1)): - image = data["image"] - _ = calibration_network(image) - - # step5: export the air file - save_model("results/yolov3_quant", calibration_network, input_data) - print("[INFO] the quantized AIR file has been stored at: \n {}".format("results/yolov3_quant.air")) - - -if __name__ == "__main__": - sys.path.append("..") - from src.yolo import YOLOV3DarkNet53 - from src.yolo_dataset import create_yolo_dataset - from model_utils.config import config - - context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") - net = YOLOV3DarkNet53(is_training=False) - - param_dict = load_checkpoint(config.ckpt_file) - load_param_into_net(net, param_dict) - net.set_train(False) - - config.batch_size = 16 - data_path = os.path.join(config.data_dir, "val2014") - datasets, data_size = create_yolo_dataset(data_path, config.annFile, is_training=False, - batch_size=config.batch_size, max_epoch=1, device_num=1, rank=0, - shuffle=False, config=config) - ds = datasets.take(1) - export_batch_size = 1 - shape = [export_batch_size, 3] + config.test_img_shape - inputs = Tensor(np.zeros(shape), ms.float32) - quant_yolov3(net, ds, inputs) diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/run_quant_infer.sh b/official/cv/yolov3_darknet53/ascend310_quant_infer/run_quant_infer.sh deleted file mode 100644 index 016ed0133c814c7558c30842e191fad50e163614..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/run_quant_infer.sh +++ /dev/null @@ -1,110 +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 5 ]; then - echo "Usage: bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [IMAGE_ID] [IMAGE_SHAPE] [ANN_FILE]" - echo "Example: bash run_quant_infer.sh ./yolov3_quant.air ./image_bin ./image_id.npy ./image_shape.npy \ -./instances_val2014.json" -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) -id_path=$(get_real_path $3) -shape_path=$(get_real_path $4) -ann_path=$(get_real_path $5) - -echo "air name: "$model -echo "dataset path: "$data_path -echo "id path: "$id_path -echo "shape path: "$shape_path -echo "ann path: "$ann_path - -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 ASCEND_HOME=/usr/local/Ascend/latest/ - export PATH=$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/fwkacllib/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/atc/lib64:$LD_LIBRARY_PATH - export TBE_IMPL_PATH=$ASCEND_HOME/opp/op_impl/built-in/ai_core/tbe - export PYTHONPATH=${TBE_IMPL_PATH}:$PYTHONPATH - export ASCEND_OPP_PATH=$ASCEND_HOME/opp -fi - -function air_to_om() -{ - atc --input_format=NCHW --framework=1 --model=$model --output=yolov3_quant --soc_version=Ascend310 &> atc.log -} - -function compile_app() -{ - bash ./src/build.sh &> build.log -} - -function infer() -{ - if [ -d result ]; then - rm -rf ./result - fi - mkdir result - ./out/main ./yolov3_quant.om $data_path &> infer.log -} - -function cal_acc() -{ - python3.7 ./acc.py --result_path=./result --annFile=$ann_path --image_shape=$shape_path \ - --image_id=$id_path &> acc.log -} - -echo "start atc================================================" -air_to_om -if [ $? -ne 0 ]; then - echo "air to om code failed" - exit 1 -fi - -echo "start compile============================================" -compile_app -if [ $? -ne 0 ]; then - echo "compile app code failed" - exit 1 -fi - -echo "start infer==============================================" -infer -if [ $? -ne 0 ]; then - echo " execute inference failed" - exit 1 -fi - -echo "start calculate acc======================================" -cal_acc -if [ $? -ne 0 ]; then - echo "calculate accuracy failed" - exit 1 -fi \ No newline at end of file diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/CMakeLists.txt b/official/cv/yolov3_darknet53/ascend310_quant_infer/src/CMakeLists.txt deleted file mode 100644 index 655026d7d91612267a287e83e886ba2ce1304d18..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. - -# CMake lowest version requirement -cmake_minimum_required(VERSION 3.5.1) -# project information -project(InferClassification) -# Check environment variable -if(NOT DEFINED ENV{ASCEND_HOME}) - message(FATAL_ERROR "please define environment variable:ASCEND_HOME") -endif() - -# Compile options -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") - -# Skip build rpath -set(CMAKE_SKIP_BUILD_RPATH True) - -# Set output directory -set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SRC_ROOT}/../out) - -# Set include directory and library directory -set(FWKACL_LIB_DIR $ENV{ASCEND_HOME}/fwkacllib) -set(ACL_LIB_DIR $ENV{ASCEND_HOME}/acllib) -set(ATLAS_ACL_LIB_DIR $ENV{ASCEND_HOME}/ascend-toolkit/latest/acllib) - -# Header path -include_directories(${ACL_LIB_DIR}/include/) -include_directories(${FWKACL_LIB_DIR}/include/) -include_directories(${ATLAS_ACL_LIB_DIR}/include/) -include_directories(${PROJECT_SRC_ROOT}/../inc) - -# add host lib path -link_directories(${ACL_LIB_DIR} ${FWKACL_LIB_DIR}) -find_library(acl libascendcl.so ${ACL_LIB_DIR}/lib64 ${FWKACL_LIB_DIR}/lib64 ${ATLAS_ACL_LIB_DIR}/lib64) - -add_executable(main utils.cpp - sample_process.cpp - model_process.cpp - main.cpp) - -target_link_libraries(main ${acl} gflags pthread) diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/acl.json b/official/cv/yolov3_darknet53/ascend310_quant_infer/src/acl.json deleted file mode 100644 index 0967ef424bce6791893e9a57bb952f80fd536e93..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/acl.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/build.sh b/official/cv/yolov3_darknet53/ascend310_quant_infer/src/build.sh deleted file mode 100644 index b5979b68e60b673f763a3cac98c5e147e7085291..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/build.sh +++ /dev/null @@ -1,55 +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. -# ============================================================================ -path_cur=$(cd "`dirname $0`" || exit; pwd) - -function preparePath() { - rm -rf $1 - mkdir -p $1 - cd $1 || exit -} - -function buildA300() { - if [ ! "${ARCH_PATTERN}" ]; then - # set ARCH_PATTERN to acllib when it was not specified by user - export ARCH_PATTERN=acllib - echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}" - else - echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user, reset it to ${ARCH_PATTERN}/acllib" - export ARCH_PATTERN=${ARCH_PATTERN}/acllib - fi - - path_build=$path_cur/build - preparePath $path_build - cmake .. - make -j - ret=$? - cd .. - return ${ret} -} - -# 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 - -buildA300 - -if [ $? -ne 0 ]; then - exit 1 -fi diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/main.cpp b/official/cv/yolov3_darknet53/ascend310_quant_infer/src/main.cpp deleted file mode 100644 index 80165505f447d418e0f107b76d04ffae59b89a73..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/main.cpp +++ /dev/null @@ -1,42 +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 <iostream> -#include "../inc/sample_process.h" -#include "../inc/utils.h" -bool g_is_device = false; - -int main(int argc, char **argv) { - if (argc != 3) { - ERROR_LOG("usage:./main path_of_om path_of_inputFolder"); - return FAILED; - } - SampleProcess processSample; - Result ret = processSample.InitResource(); - if (ret != SUCCESS) { - ERROR_LOG("sample init resource failed"); - return FAILED; - } - - ret = processSample.Process(argv[1], argv[2]); - if (ret != SUCCESS) { - ERROR_LOG("sample process failed"); - return FAILED; - } - - INFO_LOG("execute sample success"); - return SUCCESS; -} diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/model_process.cpp b/official/cv/yolov3_darknet53/ascend310_quant_infer/src/model_process.cpp deleted file mode 100644 index 04e6a42ab43cbc41720fe6b9e30bf919323c9f9e..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/model_process.cpp +++ /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. - */ - -#include "../inc/model_process.h" -#include <iostream> -#include <map> -#include <sstream> -#include <algorithm> -#include "../inc/utils.h" -extern bool g_is_device; - -ModelProcess::ModelProcess() :modelId_(0), modelMemSize_(0), modelWeightSize_(0), modelMemPtr_(nullptr), -modelWeightPtr_(nullptr), loadFlag_(false), modelDesc_(nullptr), input_(nullptr), output_(nullptr) { -} - -ModelProcess::~ModelProcess() { - Unload(); - DestroyDesc(); - DestroyInput(); - DestroyOutput(); -} - -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { - if (loadFlag_) { - ERROR_LOG("has already loaded a model"); - return FAILED; - } - - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("query model failed, model file is %s", modelPath); - return FAILED; - } - - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); - return FAILED; - } - - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); - return FAILED; - } - - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("load model from file failed, model file is %s", modelPath); - return FAILED; - } - - loadFlag_ = true; - INFO_LOG("load model %s success", modelPath); - return SUCCESS; -} - -Result ModelProcess::CreateDesc() { - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { - ERROR_LOG("create model description failed"); - return FAILED; - } - - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("get model description failed"); - return FAILED; - } - - INFO_LOG("create model description success"); - - return SUCCESS; -} - -void ModelProcess::DestroyDesc() { - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } -} - -Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { - ERROR_LOG("can't create dataset, create input failed"); - return FAILED; - } - - aclDataBuffer* inputData = aclCreateDataBuffer(inputDataBuffer, bufferSize); - if (inputData == nullptr) { - ERROR_LOG("can't create data buffer, create input failed"); - return FAILED; - } - - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("add input dataset buffer failed"); - aclDestroyDataBuffer(inputData); - inputData = nullptr; - return FAILED; - } - - return SUCCESS; -} - -void ModelProcess::DestroyInput() { - if (input_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); - aclDestroyDataBuffer(dataBuffer); - } - aclmdlDestroyDataset(input_); - input_ = nullptr; -} - -Result ModelProcess::CreateOutput() { - if (modelDesc_ == nullptr) { - ERROR_LOG("no model description, create output failed"); - return FAILED; - } - - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { - ERROR_LOG("can't create dataset, create output failed"); - return FAILED; - } - - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); - for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); - - void *outputBuffer = nullptr; - aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't malloc buffer, size is %zu, create output failed", buffer_size); - return FAILED; - } - - aclDataBuffer* outputData = aclCreateDataBuffer(outputBuffer, buffer_size); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't create data buffer, create output failed"); - aclrtFree(outputBuffer); - return FAILED; - } - - ret = aclmdlAddDatasetBuffer(output_, outputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't add data buffer, create output failed"); - aclrtFree(outputBuffer); - aclDestroyDataBuffer(outputData); - return FAILED; - } - } - - INFO_LOG("create model output success"); - return SUCCESS; -} - -void ModelProcess::DumpModelOutputResult(char *output_name) { - size_t outputNum = aclmdlGetDatasetNumBuffers(output_); - - for (size_t i = 0; i < outputNum; ++i) { - std::stringstream ss; - ss << "result/" << output_name << "_output_" << i << ".bin"; - std::string outputFileName = ss.str(); - FILE *outputFile = fopen(outputFileName.c_str(), "wb"); - if (outputFile != nullptr) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void* outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - (void)aclrtFreeHost(outHostData); - return; - } - - fwrite(outHostData, len, sizeof(char), outputFile); - - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } else { - fwrite(data, len, sizeof(char), outputFile); - } - fclose(outputFile); - outputFile = nullptr; - } else { - ERROR_LOG("create output file [%s] failed", outputFileName.c_str()); - return; - } - } - - INFO_LOG("dump data success"); - return; -} - -void ModelProcess::OutputModelResult() { - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void *outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - float *outData = NULL; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - return; - } - - outData = reinterpret_cast<float*>(outHostData); - } else { - outData = reinterpret_cast<float*>(data); - } - std::map<float, unsigned int, std::greater<float> > resultMap; - for (unsigned int j = 0; j < len / sizeof(float); ++j) { - resultMap[*outData] = j; - outData++; - } - - int cnt = 0; - for (auto it = resultMap.begin(); it != resultMap.end(); ++it) { - // print top 5 - if (++cnt > 5) { - break; - } - - INFO_LOG("top %d: index[%d] value[%lf]", cnt, it->second, it->first); - } - if (!g_is_device) { - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } - } - - INFO_LOG("output data success"); - return; -} - -void ModelProcess::DestroyOutput() { - if (output_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - (void)aclrtFree(data); - (void)aclDestroyDataBuffer(dataBuffer); - } - - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; -} - -Result ModelProcess::Execute() { - aclError ret = aclmdlExecute(modelId_, input_, output_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); - return FAILED; - } - - INFO_LOG("model execute success"); - return SUCCESS; -} - -void ModelProcess::Unload() { - if (!loadFlag_) { - WARN_LOG("no model had been loaded, unload failed"); - return; - } - - aclError ret = aclmdlUnload(modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); - } - - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } - - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; - } - - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; - } - - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); -} diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/sample_process.cpp b/official/cv/yolov3_darknet53/ascend310_quant_infer/src/sample_process.cpp deleted file mode 100644 index 5f8f20f297bf6101d3a68ffd65bd0e6ed69d9486..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/sample_process.cpp +++ /dev/null @@ -1,252 +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 "../inc/sample_process.h" -#include <sys/time.h> -#include <sys/types.h> -#include <dirent.h> -#include <string.h> -#include <iostream> -#include <fstream> -#include "../inc/model_process.h" -#include "acl/acl.h" -#include "../inc/utils.h" -extern bool g_is_device; -using std::string; -using std::vector; - -SampleProcess::SampleProcess() :deviceId_(0), context_(nullptr), stream_(nullptr) { -} - -SampleProcess::~SampleProcess() { - DestroyResource(); -} - -Result SampleProcess::InitResource() { - // ACL init - - const char *aclConfigPath = "./src/acl.json"; - aclError ret = aclInit(aclConfigPath); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl init failed"); - return FAILED; - } - INFO_LOG("acl init success"); - - // open device - ret = aclrtSetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl open device %d failed", deviceId_); - return FAILED; - } - INFO_LOG("open device %d success", deviceId_); - - // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create context failed"); - return FAILED; - } - INFO_LOG("create context success"); - - // create stream - ret = aclrtCreateStream(&stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create stream failed"); - return FAILED; - } - INFO_LOG("create stream success"); - - // get run mode - aclrtRunMode runMode; - ret = aclrtGetRunMode(&runMode); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl get run mode failed"); - return FAILED; - } - g_is_device = (runMode == ACL_DEVICE); - INFO_LOG("get run mode success"); - return SUCCESS; -} - -void SampleProcess::GetAllFiles(std::string path, std::vector<string> *files) { - DIR *pDir = NULL; - struct dirent* ptr = nullptr; - if (!(pDir = opendir(path.c_str()))) { - return; - } - while ((ptr = readdir(pDir)) != 0) { - if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { - files->push_back(path + "/" + ptr->d_name); - } - } - closedir(pDir); -} - -Result SampleProcess::Process(char *om_path, char *input_folder) { - // model init - const double second_to_millisecond = 1000; - const double second_to_microsecond = 1000000; - - double whole_cost_time = 0.0; - struct timeval start_global = {0}; - struct timeval end_global = {0}; - double startTimeMs_global = 0.0; - double endTimeMs_global = 0.0; - - gettimeofday(&start_global, nullptr); - - ModelProcess processModel; - const char* omModelPath = om_path; - - Result ret = processModel.LoadModelFromFileWithMem(omModelPath); - if (ret != SUCCESS) { - ERROR_LOG("execute LoadModelFromFileWithMem failed"); - return FAILED; - } - - ret = processModel.CreateDesc(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateDesc failed"); - return FAILED; - } - - ret = processModel.CreateOutput(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateOutput failed"); - return FAILED; - } - - std::vector<string> testFile; - GetAllFiles(input_folder, &testFile); - - if (testFile.size() == 0) { - WARN_LOG("no input data under folder"); - } - - double model_cost_time = 0.0; - double edge_to_edge_model_cost_time = 0.0; - - for (size_t index = 0; index < testFile.size(); ++index) { - INFO_LOG("start to process file:%s", testFile[index].c_str()); - // model process - - struct timeval time_init = {0}; - double timeval_init = 0.0; - gettimeofday(&time_init, nullptr); - timeval_init = (time_init.tv_sec * second_to_microsecond + time_init.tv_usec) / second_to_millisecond; - - uint32_t devBufferSize; - void *picDevBuffer = Utils::GetDeviceBufferOfFile(testFile[index], &devBufferSize); - if (picDevBuffer == nullptr) { - ERROR_LOG("get pic device buffer failed,index is %zu", index); - return FAILED; - } - ret = processModel.CreateInput(picDevBuffer, devBufferSize); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateInput failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - struct timeval start = {0}; - struct timeval end = {0}; - double startTimeMs = 0.0; - double endTimeMs = 0.0; - gettimeofday(&start, nullptr); - startTimeMs = (start.tv_sec * second_to_microsecond + start.tv_usec) / second_to_millisecond; - - ret = processModel.Execute(); - - gettimeofday(&end, nullptr); - endTimeMs = (end.tv_sec * second_to_microsecond + end.tv_usec) / second_to_millisecond; - - double cost_time = endTimeMs - startTimeMs; - INFO_LOG("model infer time: %lf ms", cost_time); - - model_cost_time += cost_time; - - double edge_to_edge_cost_time = endTimeMs - timeval_init; - edge_to_edge_model_cost_time += edge_to_edge_cost_time; - - if (ret != SUCCESS) { - ERROR_LOG("execute inference failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - int pos = testFile[index].find_last_of('/'); - std::string name = testFile[index].substr(pos+1); - std::string outputname = name.substr(0, name.rfind(".")); - - // dump output result to file in the current directory - processModel.DumpModelOutputResult(const_cast<char *>(outputname.c_str())); - - // release model input buffer - aclrtFree(picDevBuffer); - processModel.DestroyInput(); - } - double test_file_size = 0.0; - test_file_size = testFile.size(); - INFO_LOG("infer dataset size:%lf", test_file_size); - - gettimeofday(&end_global, nullptr); - startTimeMs_global = (start_global.tv_sec * second_to_microsecond + start_global.tv_usec) / second_to_millisecond; - endTimeMs_global = (end_global.tv_sec * second_to_microsecond + end_global.tv_usec) / second_to_millisecond; - whole_cost_time = (endTimeMs_global - startTimeMs_global) / test_file_size; - - model_cost_time /= test_file_size; - INFO_LOG("model cost time per sample: %lf ms", model_cost_time); - edge_to_edge_model_cost_time /= test_file_size; - INFO_LOG("edge-to-edge model cost time per sample:%lf ms", edge_to_edge_model_cost_time); - INFO_LOG("whole cost time per sample: %lf ms", whole_cost_time); - - return SUCCESS; -} - -void SampleProcess::DestroyResource() { - aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy stream failed"); - } - stream_ = nullptr; - } - INFO_LOG("end to destroy stream"); - - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy context failed"); - } - context_ = nullptr; - } - INFO_LOG("end to destroy context"); - - ret = aclrtResetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("reset device failed"); - } - INFO_LOG("end to reset device is %d", deviceId_); - - ret = aclFinalize(); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("finalize acl failed"); - } - INFO_LOG("end to finalize acl"); -} - diff --git a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/utils.cpp b/official/cv/yolov3_darknet53/ascend310_quant_infer/src/utils.cpp deleted file mode 100644 index d9208c8cfd9979e9248046e7325f260eb2d14d80..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_darknet53/ascend310_quant_infer/src/utils.cpp +++ /dev/null @@ -1,113 +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 "../inc/utils.h" -#include <sys/stat.h> -#include <iostream> -#include <fstream> -#include <cstring> -#include "acl/acl.h" - -extern bool g_is_device; - -void* Utils::ReadBinFile(std::string fileName, uint32_t *fileSize) { - struct stat sBuf; - int fileStatus = stat(fileName.data(), &sBuf); - if (fileStatus == -1) { - ERROR_LOG("failed to get file"); - return nullptr; - } - if (S_ISREG(sBuf.st_mode) == 0) { - ERROR_LOG("%s is not a file, please enter a file", fileName.c_str()); - return nullptr; - } - - std::ifstream binFile(fileName, std::ifstream::binary); - if (binFile.is_open() == false) { - ERROR_LOG("open file %s failed", fileName.c_str()); - return nullptr; - } - - binFile.seekg(0, binFile.end); - uint32_t binFileBufferLen = binFile.tellg(); - if (binFileBufferLen == 0) { - ERROR_LOG("binfile is empty, filename is %s", fileName.c_str()); - binFile.close(); - return nullptr; - } - - binFile.seekg(0, binFile.beg); - - void* binFileBufferData = nullptr; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&binFileBufferData, binFileBufferLen); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc for binFileBufferData failed"); - binFile.close(); - return nullptr; - } - if (binFileBufferData == nullptr) { - ERROR_LOG("malloc binFileBufferData failed"); - binFile.close(); - return nullptr; - } - } else { - ret = aclrtMalloc(&binFileBufferData, binFileBufferLen, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", binFileBufferLen); - binFile.close(); - return nullptr; - } - } - binFile.read(static_cast<char *>(binFileBufferData), binFileBufferLen); - binFile.close(); - *fileSize = binFileBufferLen; - return binFileBufferData; -} - -void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize) { - uint32_t inputHostBuffSize = 0; - void* inputHostBuff = Utils::ReadBinFile(fileName, &inputHostBuffSize); - if (inputHostBuff == nullptr) { - return nullptr; - } - if (!g_is_device) { - void *inBufferDev = nullptr; - uint32_t inBufferSize = inputHostBuffSize; - aclError ret = aclrtMalloc(&inBufferDev, inBufferSize, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", inBufferSize); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - - ret = aclrtMemcpy(inBufferDev, inBufferSize, inputHostBuff, inputHostBuffSize, ACL_MEMCPY_HOST_TO_DEVICE); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("memcpy failed. device buffer size is %u, input host buffer size is %u", - inBufferSize, inputHostBuffSize); - aclrtFree(inBufferDev); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - aclrtFreeHost(inputHostBuff); - *fileSize = inBufferSize; - return inBufferDev; - } else { - *fileSize = inputHostBuffSize; - return inputHostBuff; - } -} diff --git a/official/cv/yolov3_resnet18/README.md b/official/cv/yolov3_resnet18/README.md index 14e4425575a5c255cc2aaec2327eae7e6ed3935e..91dead8e25de60c0726ec8ede0af3c51b8eabb5f 100644 --- a/official/cv/yolov3_resnet18/README.md +++ b/official/cv/yolov3_resnet18/README.md @@ -17,7 +17,6 @@ - [Inference Process](#inference-process) - [Usage](#usage) - [result](#result) - - [Post Training Quantization](#post-training-quantization) - [Model Description](#model-description) - [Performance](#performance) - [Evaluation Performance](#evaluation-performance) @@ -362,44 +361,6 @@ Inference result is saved in current path, you can find result in acc.log file. class 1 precision is 85.34%, recall is 79.13% ``` -## [Post Training Quantization](#contents) - -Relative executing script files reside in the directory "ascend310_quant_infer". Please implement following steps sequentially to complete post quantization. -Note the precision and recall values are results of two-classification(person and face) used our own annotations with COCO2017 dataset. -Note quantization-related config file utils.py is located in the directory ascend310_quant_infer. - -1. Generate data of .bin format required for AIR model inference at Ascend310 platform. - -```shell -python export_bin.py --image_dir [COCO DATA PATH] --eval_mindrecord_dir [MINDRECORD PATH] --anno_path [LABEL PATH] -``` - -Note that image_dir is set as the parent directory of COCO dataset. - -2. Export quantized AIR model. - -Post quantization of model requires special toolkits for exporting quantized AIR model. Please refer to [official website](https://www.hiascend.com/software/cann/community). - -```shell -python post_quant.py --image_dir [COCO DATA PATH] --eval_mindrecord_dir [MINDRECORD PATH] --anno_path [LABEL PATH] --ckpt_file [CKPT_PATH] -``` - -The quantized AIR file will be stored as "./results/yolov3_resnet_quant.air". - -3. Implement inference at Ascend310 platform. - -```shell -# Ascend310 quant inference -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [SHAPE_PATH] [ANNOTATION_PATH] -``` - -Inference result is saved in current path, you can find result like this in acc.log file. - -```bash -class 0 precision is 91.34%, recall is 64.92% -class 1 precision is 94.61%, recall is 64.07% -``` - # [Model Description](#contents) ## [Performance](#contents) diff --git a/official/cv/yolov3_resnet18/README_CN.md b/official/cv/yolov3_resnet18/README_CN.md index 6dd798f1a82e0bad702613b56f88e34710fa68c9..9a9239e7200e03aa7b05fb7cf7e21fc10f9c1107 100644 --- a/official/cv/yolov3_resnet18/README_CN.md +++ b/official/cv/yolov3_resnet18/README_CN.md @@ -19,7 +19,6 @@ - [推理过程](#推理过程) - [用法](#用法) - [结果](#结果) - - [训练后量化推理](#训练后量化推理) - [模型描述](#模型描述) - [性能](#性能) - [评估性能](#评估性能) @@ -358,44 +357,6 @@ bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [ANNO_PATH] [DEVICE_ID] class 1 precision is 85.34%, recall is 79.13% ``` -## [训练后量化推理](#contents) - -训练后量化推理的相关执行脚本文件在"ascend310_quant_infer"目录下,依次执行以下步骤实现训练后量化推理。 -注意精度和召回值是使用我们自己的标注和COCO2017的两种分类(人与脸)的结果。 -注意训练后量化端测推理有关的文件utils.py位于ascend310_quant_infer目录下。 - -1、生成Ascend310平台AIR模型推理需要的.bin格式数据。 - -```shell -python export_bin.py --image_dir [COCO DATA PATH] --eval_mindrecord_dir [MINDRECORD PATH] --ann_file [ANNOTATION PATH] -``` - -注意image_dir设置成COCO数据集的上级目录。 - -2、导出训练后量化的AIR格式模型。 - -导出训练后量化模型需要配套的量化工具包,参考[官方地址](https://www.hiascend.com/software/cann/community) - -```shell -python post_quant.py --image_dir [COCO DATA PATH] --eval_mindrecord_dir [MINDRECORD PATH] --ckpt_file [CKPT_PATH] -``` - -导出的模型会存储在./result/yolov3_resnet_quant.air。 - -3、在Ascend310执行推理量化模型。 - -```shell -# Ascend310 inference -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [SHAPE_PATH] [ANNOTATION_PATH] -``` - -推理结果保存在脚本执行的当前路径,可以在acc.log中看到精度计算结果。 - -```bash -class 0 precision is 91.34%, recall is 64.92% -class 1 precision is 94.61%, recall is 64.07% -``` - # 模型描述 ## 性能 diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/acc.py b/official/cv/yolov3_resnet18/ascend310_quant_infer/acc.py deleted file mode 100644 index da1990b0db509ba81a77de28c5940e1ecffc2c29..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/acc.py +++ /dev/null @@ -1,51 +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. -# ============================================================================ -"""post process for 310 inference""" -import os -import argparse -import numpy as np -from utils import metrics - - -parser = argparse.ArgumentParser("yolov3_resnet18 quant postprocess") -parser.add_argument("--anno_path", type=str, required=True, help="path to annotation.npy") -parser.add_argument("--result_path", type=str, required=True, help="path to inference results.") -parser.add_argument("--batch_size", type=int, default=1, help="batch size of data.") -parser.add_argument("--num_classes", type=int, default=2, help="number of classed to detect.") - -args, _ = parser.parse_known_args() - - -def calculate_acc(): - """ Calculate accuracy of yolov3_resnet18 inference""" - ann = np.load(args.anno_path, allow_pickle=True) - pred_data = [] - prefix = "Yolov3-resnet18_coco_bs_" + str(args.batch_size) + "_" - for i in range(len(ann)): - result0 = os.path.join(args.result_path, prefix + str(i) + "_output_0.bin") - result1 = os.path.join(args.result_path, prefix + str(i) + "_output_1.bin") - output0 = np.fromfile(result0, np.float32).reshape(args.batch_size, 13860, 4) - output1 = np.fromfile(result1, np.float32).reshape(args.batch_size, 13860, 2) - for batch_idx in range(args.batch_size): - pred_data.append({"boxes": output0[batch_idx], - "box_scores": output1[batch_idx], - "annotation": ann[i]}) - precisions, recalls = metrics(pred_data) - for j in range(args.num_classes): - print("class {} precision is {:.2f}%, recall is {:.2f}%".format(j, precisions[j] * 100, recalls[j] * 100)) - - -if __name__ == '__main__': - calculate_acc() diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/export_bin.py b/official/cv/yolov3_resnet18/ascend310_quant_infer/export_bin.py deleted file mode 100644 index b1950a668d22c5c0d323b470943ffd57cd51b2dc..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/export_bin.py +++ /dev/null @@ -1,80 +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. -# ============================================================================ -"""generate data and label needed for AIR model inference""" -import os -import sys -import numpy as np -from mindspore import context - - -context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") - - -def generate_data(dataset_path): - """ - Generate data and label needed for AIR model inference at Ascend310 platform. - """ - ds = create_yolo_dataset(dataset_path, is_training=False) - cur_dir = os.getcwd() + "/data" - img_folder = cur_dir + "/00_image" - if not os.path.exists(img_folder): - os.makedirs(img_folder) - shape_folder = cur_dir + "/01_image_shape" - if not os.path.exists(shape_folder): - os.makedirs(shape_folder) - total = ds.get_dataset_size() - ann_list = [] - print("\n========================================\n") - print("total images num: ", total) - print("Processing, please wait a moment.") - prefix = "Yolov3-resnet18_coco_bs_1_" - for i, data in enumerate(ds.create_dict_iterator(output_numpy=True, num_epochs=1)): - image_np = data['image'] - image_shape = data['image_shape'] - annotation = data['annotation'] - file_name = prefix + str(i) + ".bin" - image_path = os.path.join(img_folder, file_name) - image_np.tofile(image_path) - shape_path = os.path.join(shape_folder, file_name) - image_shape.tofile(shape_path) - - ann_list.append(annotation) - ann_file = np.array(ann_list) - np.save(os.path.join(cur_dir, "annotation_list.npy"), ann_file) - - -if __name__ == "__main__": - sys.path.append("..") - from src.dataset import create_yolo_dataset, data_to_mindrecord_byte_image - from model_utils.config import config as default_config - - if not os.path.isdir(default_config.eval_mindrecord_dir): - os.makedirs(default_config.eval_mindrecord_dir) - - yolo_prefix = "yolo.mindrecord" - mindrecord_file = os.path.join(default_config.eval_mindrecord_dir, yolo_prefix + "0") - if not os.path.exists(mindrecord_file): - if os.path.isdir(default_config.image_dir) and os.path.exists(default_config.anno_path): - print("Create Mindrecord") - data_to_mindrecord_byte_image(default_config.image_dir, - default_config.anno_path, - default_config.eval_mindrecord_dir, - prefix=yolo_prefix, - file_num=8) - print("Create Mindrecord Done, at {}".format(default_config.eval_mindrecord_dir)) - else: - print("image_dir or anno_path not exits") - - generate_data(mindrecord_file) diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/inc/model_process.h b/official/cv/yolov3_resnet18/ascend310_quant_infer/inc/model_process.h deleted file mode 100644 index 3706b955b97ec4e4cacde04e8d7889a78522785d..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/inc/model_process.h +++ /dev/null @@ -1,112 +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. - */ - -#pragma once -#include <iostream> -#include <vector> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* ModelProcess -*/ -class ModelProcess { - public: - /** - * @brief Constructor - */ - ModelProcess(); - - /** - * @brief Destructor - */ - ~ModelProcess(); - - /** - * @brief load model from file with mem - * @param [in] modelPath: model path - * @return result - */ - Result LoadModelFromFileWithMem(const char *modelPath); - - /** - * @brief unload model - */ - void Unload(); - - /** - * @brief create model desc - * @return result - */ - Result CreateDesc(); - - /** - * @brief destroy desc - */ - void DestroyDesc(); - - /** - * @brief create model input - * @param [in] inputDataBuffer: input buffer - * @param [in] bufferSize: input buffer size - * @return result - */ - Result CreateInput(const std::vector<void *> &inputDataBuffer, const std::vector<size_t> &bufferSize); - - /** - * @brief destroy input resource - */ - void DestroyInput(); - - /** - * @brief create output buffer - * @return result - */ - Result CreateOutput(); - - /** - * @brief destroy output resource - */ - void DestroyOutput(); - - /** - * @brief model execute - * @return result - */ - Result Execute(); - - /** - * @brief dump model output result to file - */ - void DumpModelOutputResult(char *output_name); - - /** - * @brief get model output result - */ - void OutputModelResult(); - - private: - uint32_t modelId_; - size_t modelMemSize_; - size_t modelWeightSize_; - void *modelMemPtr_; - void *modelWeightPtr_; - bool loadFlag_; // model load flag - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; -}; - diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/inc/sample_process.h b/official/cv/yolov3_resnet18/ascend310_quant_infer/inc/sample_process.h deleted file mode 100644 index 4e28dff95b2913a7223c4a48908640a0fc4e3d01..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/inc/sample_process.h +++ /dev/null @@ -1,58 +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. - */ - -#pragma once -#include <string> -#include <vector> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* SampleProcess -*/ -class SampleProcess { - public: - /** - * @brief Constructor - */ - SampleProcess(); - - /** - * @brief Destructor - */ - ~SampleProcess(); - - /** - * @brief init reousce - * @return result - */ - Result InitResource(); - - /** - * @brief sample process - * @return result - */ - Result Process(char *om_path, char *input_folder, char *shape_folder); - - void GetAllFiles(std::string path, std::vector<std::string> *files); - - private: - void DestroyResource(); - - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; -}; diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/inc/utils.h b/official/cv/yolov3_resnet18/ascend310_quant_infer/inc/utils.h deleted file mode 100644 index 3ae2a571b8e8ba51c01b02e23f36dfad5a7b18f1..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/inc/utils.h +++ /dev/null @@ -1,52 +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. - */ - -#pragma once -#include <iostream> -#include <string> - -#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) -#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args) -#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) - -typedef enum Result { - SUCCESS = 0, - FAILED = 1 -} Result; - -/** -* Utils -*/ -class Utils { - public: - /** - * @brief create device buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return device buffer of file - */ - static void *GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize); - - /** - * @brief create buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return buffer of pic - */ - static void* ReadBinFile(std::string fileName, uint32_t *fileSize); -}; - -#pragma once diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/post_quant.py b/official/cv/yolov3_resnet18/ascend310_quant_infer/post_quant.py deleted file mode 100644 index ca00080a41a5070ee582e1ebaa9aa121277f0813..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/post_quant.py +++ /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. -# ============================================================================ -"""do post training quantization for Ascend310""" -import os -import sys -import numpy as np - -from amct_mindspore.quantize_tool import create_quant_config -from amct_mindspore.quantize_tool import quantize_model -from amct_mindspore.quantize_tool import save_model -import mindspore as ms -import mindspore.ops as ops -from mindspore import context, Tensor -from mindspore.train.serialization import load_checkpoint, load_param_into_net - - -context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") - - -def quant_yolov3_resnet(network, dataset, input_data): - """ - Export post training quantization model of AIR format. - - Args: - network: the origin network for inference. - dataset: the data for inference. - input_data: the data used for constructing network. The shape and format of input data should be the same as - actual data for inference. - """ - - # step2: create the quant config json file - create_quant_config("./config.json", network, *input_data) - - # step3: do some network modification and return the modified network - calibration_network = quantize_model("./config.json", network, *input_data) - calibration_network.set_train(False) - - # step4: perform the evaluation of network to do activation calibration - concat = ops.Concat() - index = 0 - image_data = [] - for data in dataset.create_dict_iterator(num_epochs=1): - index += 1 - if index == 1: - image_data = data["image"] - else: - image_data = concat((image_data, data["image"])) - if index == dataset.get_dataset_size(): - _ = calibration_network(image_data, data["image_shape"]) - - # step5: export the air file - save_model("results/yolov3_resnet_quant", calibration_network, *input_data) - print("[INFO] the quantized AIR file has been stored at: \n {}".format("results/yolov3_resnet_quant.air")) - - -def export_yolov3_resnet(): - """ prepare for quantization of yolov3_resnet """ - cfg = ConfigYOLOV3ResNet18() - net = yolov3_resnet18(cfg) - eval_net = YoloWithEval(net, cfg) - param_dict = load_checkpoint(default_config.ckpt_file) - load_param_into_net(eval_net, param_dict) - eval_net.set_train(False) - - default_config.export_batch_size = 1 - shape = [default_config.export_batch_size, 3] + cfg.img_shape - input_data = Tensor(np.zeros(shape), ms.float32) - input_shape = Tensor(np.zeros([1, 2]), ms.float32) - inputs = (input_data, input_shape) - - if not os.path.isdir(default_config.eval_mindrecord_dir): - os.makedirs(default_config.eval_mindrecord_dir) - - yolo_prefix = "yolo.mindrecord" - mindrecord_file = os.path.join(default_config.eval_mindrecord_dir, yolo_prefix + "0") - if not os.path.exists(mindrecord_file): - if os.path.isdir(default_config.image_dir) and os.path.exists(default_config.anno_path): - print("Create Mindrecord") - data_to_mindrecord_byte_image(default_config.image_dir, - default_config.anno_path, - default_config.eval_mindrecord_dir, - prefix=yolo_prefix, - file_num=8) - print("Create Mindrecord Done, at {}".format(default_config.eval_mindrecord_dir)) - else: - print("image_dir or anno_path not exits") - datasets = create_yolo_dataset(mindrecord_file, is_training=False) - ds = datasets.take(16) - quant_yolov3_resnet(eval_net, ds, inputs) - - -if __name__ == "__main__": - sys.path.append("..") - from src.yolov3 import yolov3_resnet18, YoloWithEval - from src.config import ConfigYOLOV3ResNet18 - from src.dataset import create_yolo_dataset, data_to_mindrecord_byte_image - from model_utils.config import config as default_config - - export_yolov3_resnet() diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/run_quant_infer.sh b/official/cv/yolov3_resnet18/ascend310_quant_infer/run_quant_infer.sh deleted file mode 100644 index c118d783e4149050b77b234d5511f5309710cbd9..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/run_quant_infer.sh +++ /dev/null @@ -1,106 +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 ]; then - echo "Usage: bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [SHAPE_PATH] [ANNOTATION_PATH]" - echo "Example: bash run_quant_infer.sh ./yolov3_resnet_quant.air ./00_image ./01_image_shape ./annotation_list.npy" -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) -shape_path=$(get_real_path $3) -annotation_path=$(get_real_path $4) - -echo "air name: "$model -echo "dataset path: "$data_path -echo "shape path: "$shape_path -echo "annotation path: "$annotation_path - -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 ASCEND_HOME=/usr/local/Ascend/latest/ - export PATH=$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/fwkacllib/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/atc/lib64:$LD_LIBRARY_PATH - export TBE_IMPL_PATH=$ASCEND_HOME/opp/op_impl/built-in/ai_core/tbe - export PYTHONPATH=${TBE_IMPL_PATH}:$PYTHONPATH - export ASCEND_OPP_PATH=$ASCEND_HOME/opp -fi - -function air_to_om() -{ - atc --input_format=NCHW --framework=1 --model=$model --output=yolov3_resnet_quant --soc_version=Ascend310 &> atc.log -} - -function compile_app() -{ - bash ./src/build.sh &> build.log -} - -function infer() -{ - if [ -d result ]; then - rm -rf ./result - fi - mkdir result - ./out/main ./yolov3_resnet_quant.om $data_path $shape_path &> infer.log -} - -function cal_acc() -{ - python3.7 ./acc.py --result_path=./result --anno_path=$annotation_path &> acc.log -} - -echo "start atc================================================" -air_to_om -if [ $? -ne 0 ]; then - echo "air to om code failed" - exit 1 -fi - -echo "start compile============================================" -compile_app -if [ $? -ne 0 ]; then - echo "compile app code failed" - exit 1 -fi - -echo "start infer==============================================" -infer -if [ $? -ne 0 ]; then - echo " execute inference failed" - exit 1 -fi - -echo "start calculate acc======================================" -cal_acc -if [ $? -ne 0 ]; then - echo "calculate accuracy failed" - exit 1 -fi \ No newline at end of file diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/CMakeLists.txt b/official/cv/yolov3_resnet18/ascend310_quant_infer/src/CMakeLists.txt deleted file mode 100644 index 655026d7d91612267a287e83e886ba2ce1304d18..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. - -# CMake lowest version requirement -cmake_minimum_required(VERSION 3.5.1) -# project information -project(InferClassification) -# Check environment variable -if(NOT DEFINED ENV{ASCEND_HOME}) - message(FATAL_ERROR "please define environment variable:ASCEND_HOME") -endif() - -# Compile options -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") - -# Skip build rpath -set(CMAKE_SKIP_BUILD_RPATH True) - -# Set output directory -set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SRC_ROOT}/../out) - -# Set include directory and library directory -set(FWKACL_LIB_DIR $ENV{ASCEND_HOME}/fwkacllib) -set(ACL_LIB_DIR $ENV{ASCEND_HOME}/acllib) -set(ATLAS_ACL_LIB_DIR $ENV{ASCEND_HOME}/ascend-toolkit/latest/acllib) - -# Header path -include_directories(${ACL_LIB_DIR}/include/) -include_directories(${FWKACL_LIB_DIR}/include/) -include_directories(${ATLAS_ACL_LIB_DIR}/include/) -include_directories(${PROJECT_SRC_ROOT}/../inc) - -# add host lib path -link_directories(${ACL_LIB_DIR} ${FWKACL_LIB_DIR}) -find_library(acl libascendcl.so ${ACL_LIB_DIR}/lib64 ${FWKACL_LIB_DIR}/lib64 ${ATLAS_ACL_LIB_DIR}/lib64) - -add_executable(main utils.cpp - sample_process.cpp - model_process.cpp - main.cpp) - -target_link_libraries(main ${acl} gflags pthread) diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/acl.json b/official/cv/yolov3_resnet18/ascend310_quant_infer/src/acl.json deleted file mode 100644 index 0967ef424bce6791893e9a57bb952f80fd536e93..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/acl.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/build.sh b/official/cv/yolov3_resnet18/ascend310_quant_infer/src/build.sh deleted file mode 100644 index b5979b68e60b673f763a3cac98c5e147e7085291..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/build.sh +++ /dev/null @@ -1,55 +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. -# ============================================================================ -path_cur=$(cd "`dirname $0`" || exit; pwd) - -function preparePath() { - rm -rf $1 - mkdir -p $1 - cd $1 || exit -} - -function buildA300() { - if [ ! "${ARCH_PATTERN}" ]; then - # set ARCH_PATTERN to acllib when it was not specified by user - export ARCH_PATTERN=acllib - echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}" - else - echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user, reset it to ${ARCH_PATTERN}/acllib" - export ARCH_PATTERN=${ARCH_PATTERN}/acllib - fi - - path_build=$path_cur/build - preparePath $path_build - cmake .. - make -j - ret=$? - cd .. - return ${ret} -} - -# 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 - -buildA300 - -if [ $? -ne 0 ]; then - exit 1 -fi diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/main.cpp b/official/cv/yolov3_resnet18/ascend310_quant_infer/src/main.cpp deleted file mode 100644 index 5fb6bad7044e27eb7958277b7cc191ea8924e9df..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/main.cpp +++ /dev/null @@ -1,42 +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 <iostream> -#include "../inc/sample_process.h" -#include "../inc/utils.h" -bool g_is_device = false; - -int main(int argc, char **argv) { - if (argc != 4) { - ERROR_LOG("usage:./main path_of_om path_of_inputFolder path_of_shapeFolder"); - return FAILED; - } - SampleProcess processSample; - Result ret = processSample.InitResource(); - if (ret != SUCCESS) { - ERROR_LOG("sample init resource failed"); - return FAILED; - } - - ret = processSample.Process(argv[1], argv[2], argv[3]); - if (ret != SUCCESS) { - ERROR_LOG("sample process failed"); - return FAILED; - } - - INFO_LOG("execute sample success"); - return SUCCESS; -} diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/model_process.cpp b/official/cv/yolov3_resnet18/ascend310_quant_infer/src/model_process.cpp deleted file mode 100644 index 59f2f7a2509d63e77ebcec596e73ae8225ec7e02..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/model_process.cpp +++ /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. - */ - -#include "../inc/model_process.h" -#include <iostream> -#include <map> -#include <sstream> -#include <algorithm> -#include "../inc/utils.h" -extern bool g_is_device; - -ModelProcess::ModelProcess() :modelId_(0), modelMemSize_(0), modelWeightSize_(0), modelMemPtr_(nullptr), -modelWeightPtr_(nullptr), loadFlag_(false), modelDesc_(nullptr), input_(nullptr), output_(nullptr) { -} - -ModelProcess::~ModelProcess() { - Unload(); - DestroyDesc(); - DestroyInput(); - DestroyOutput(); -} - -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { - if (loadFlag_) { - ERROR_LOG("has already loaded a model"); - return FAILED; - } - - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("query model failed, model file is %s", modelPath); - return FAILED; - } - - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); - return FAILED; - } - - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); - return FAILED; - } - - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("load model from file failed, model file is %s", modelPath); - return FAILED; - } - - loadFlag_ = true; - INFO_LOG("load model %s success", modelPath); - return SUCCESS; -} - -Result ModelProcess::CreateDesc() { - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { - ERROR_LOG("create model description failed"); - return FAILED; - } - - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("get model description failed"); - return FAILED; - } - - INFO_LOG("create model description success"); - - return SUCCESS; -} - -void ModelProcess::DestroyDesc() { - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } -} - -Result ModelProcess::CreateInput(const std::vector<void *> &inputDataBuffer, const std::vector<size_t> &bufferSize) { - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { - ERROR_LOG("can't create dataset, create input failed"); - return FAILED; - } - for (size_t i = 0; i < inputDataBuffer.size(); ++i) { - aclDataBuffer* inputData = aclCreateDataBuffer(inputDataBuffer[i], bufferSize[i]); - if (inputData == nullptr) { - ERROR_LOG("can't create data buffer, create input failed"); - return FAILED; - } - - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("add input dataset buffer failed"); - aclDestroyDataBuffer(inputData); - inputData = nullptr; - return FAILED; - } - } - return SUCCESS; -} - -void ModelProcess::DestroyInput() { - if (input_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); - aclDestroyDataBuffer(dataBuffer); - } - aclmdlDestroyDataset(input_); - input_ = nullptr; -} - -Result ModelProcess::CreateOutput() { - if (modelDesc_ == nullptr) { - ERROR_LOG("no model description, create output failed"); - return FAILED; - } - - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { - ERROR_LOG("can't create dataset, create output failed"); - return FAILED; - } - - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); - for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); - - void *outputBuffer = nullptr; - aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't malloc buffer, size is %zu, create output failed", buffer_size); - return FAILED; - } - - aclDataBuffer* outputData = aclCreateDataBuffer(outputBuffer, buffer_size); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't create data buffer, create output failed"); - aclrtFree(outputBuffer); - return FAILED; - } - - ret = aclmdlAddDatasetBuffer(output_, outputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't add data buffer, create output failed"); - aclrtFree(outputBuffer); - aclDestroyDataBuffer(outputData); - return FAILED; - } - } - - INFO_LOG("create model output success"); - return SUCCESS; -} - -void ModelProcess::DumpModelOutputResult(char *output_name) { - size_t outputNum = aclmdlGetDatasetNumBuffers(output_); - - for (size_t i = 0; i < outputNum; ++i) { - std::stringstream ss; - ss << "result/" << output_name << "_output_" << i << ".bin"; - std::string outputFileName = ss.str(); - FILE *outputFile = fopen(outputFileName.c_str(), "wb"); - if (outputFile != nullptr) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void* outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - (void)aclrtFreeHost(outHostData); - return; - } - - fwrite(outHostData, len, sizeof(char), outputFile); - - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } else { - fwrite(data, len, sizeof(char), outputFile); - } - fclose(outputFile); - outputFile = nullptr; - } else { - ERROR_LOG("create output file [%s] failed", outputFileName.c_str()); - return; - } - } - - INFO_LOG("dump data success"); - return; -} - -void ModelProcess::OutputModelResult() { - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void *outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - float *outData = NULL; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - return; - } - - outData = reinterpret_cast<float*>(outHostData); - } else { - outData = reinterpret_cast<float*>(data); - } - std::map<float, unsigned int, std::greater<float> > resultMap; - for (unsigned int j = 0; j < len / sizeof(float); ++j) { - resultMap[*outData] = j; - outData++; - } - - int cnt = 0; - for (auto it = resultMap.begin(); it != resultMap.end(); ++it) { - // print top 5 - if (++cnt > 5) { - break; - } - - INFO_LOG("top %d: index[%d] value[%lf]", cnt, it->second, it->first); - } - if (!g_is_device) { - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } - } - - INFO_LOG("output data success"); - return; -} - -void ModelProcess::DestroyOutput() { - if (output_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - (void)aclrtFree(data); - (void)aclDestroyDataBuffer(dataBuffer); - } - - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; -} - -Result ModelProcess::Execute() { - aclError ret = aclmdlExecute(modelId_, input_, output_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); - return FAILED; - } - - INFO_LOG("model execute success"); - return SUCCESS; -} - -void ModelProcess::Unload() { - if (!loadFlag_) { - WARN_LOG("no model had been loaded, unload failed"); - return; - } - - aclError ret = aclmdlUnload(modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); - } - - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } - - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; - } - - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; - } - - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); -} diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/sample_process.cpp b/official/cv/yolov3_resnet18/ascend310_quant_infer/src/sample_process.cpp deleted file mode 100644 index 10e36d747d7dd42643d750948fe9a69913cff315..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/sample_process.cpp +++ /dev/null @@ -1,263 +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 "../inc/sample_process.h" -#include <sys/time.h> -#include <sys/types.h> -#include <dirent.h> -#include <string.h> -#include <iostream> -#include <fstream> -#include "../inc/model_process.h" -#include "acl/acl.h" -#include "../inc/utils.h" -extern bool g_is_device; -using std::string; -using std::vector; - -SampleProcess::SampleProcess() :deviceId_(0), context_(nullptr), stream_(nullptr) { -} - -SampleProcess::~SampleProcess() { - DestroyResource(); -} - -Result SampleProcess::InitResource() { - // ACL init - - const char *aclConfigPath = "./src/acl.json"; - aclError ret = aclInit(aclConfigPath); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl init failed"); - return FAILED; - } - INFO_LOG("acl init success"); - - // open device - ret = aclrtSetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl open device %d failed", deviceId_); - return FAILED; - } - INFO_LOG("open device %d success", deviceId_); - - // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create context failed"); - return FAILED; - } - INFO_LOG("create context success"); - - // create stream - ret = aclrtCreateStream(&stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create stream failed"); - return FAILED; - } - INFO_LOG("create stream success"); - - // get run mode - aclrtRunMode runMode; - ret = aclrtGetRunMode(&runMode); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl get run mode failed"); - return FAILED; - } - g_is_device = (runMode == ACL_DEVICE); - INFO_LOG("get run mode success"); - return SUCCESS; -} - -void SampleProcess::GetAllFiles(std::string path, std::vector<string> *files) { - DIR *pDir = NULL; - struct dirent* ptr = nullptr; - if (!(pDir = opendir(path.c_str()))) { - return; - } - while ((ptr = readdir(pDir)) != 0) { - if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { - files->push_back(path + "/" + ptr->d_name); - } - } - closedir(pDir); -} - -Result SampleProcess::Process(char *om_path, char *input_folder, char *shape_folder) { - // model init - const double second_to_millisecond = 1000; - const double second_to_microsecond = 1000000; - - double whole_cost_time = 0.0; - struct timeval start_global = {0}; - struct timeval end_global = {0}; - double startTimeMs_global = 0.0; - double endTimeMs_global = 0.0; - - gettimeofday(&start_global, nullptr); - - ModelProcess processModel; - const char* omModelPath = om_path; - - Result ret = processModel.LoadModelFromFileWithMem(omModelPath); - if (ret != SUCCESS) { - ERROR_LOG("execute LoadModelFromFileWithMem failed"); - return FAILED; - } - - ret = processModel.CreateDesc(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateDesc failed"); - return FAILED; - } - - ret = processModel.CreateOutput(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateOutput failed"); - return FAILED; - } - - std::vector<string> testFile; - GetAllFiles(input_folder, &testFile); - std::vector<string> shapeFile; - GetAllFiles(shape_folder, &shapeFile); - - if (testFile.size() !=shapeFile.size()) { - ERROR_LOG("number of data files is not equal to shape file"); - } - - double model_cost_time = 0.0; - double edge_to_edge_model_cost_time = 0.0; - - for (size_t index = 0; index < testFile.size(); ++index) { - INFO_LOG("start to process data file:%s", testFile[index].c_str()); - INFO_LOG("start to process shape file:%s", shapeFile[index].c_str()); - // model process - - struct timeval time_init = {0}; - double timeval_init = 0.0; - gettimeofday(&time_init, nullptr); - timeval_init = (time_init.tv_sec * second_to_microsecond + time_init.tv_usec) / second_to_millisecond; - - uint32_t devBufferSize; - void *picDevBuffer = Utils::GetDeviceBufferOfFile(testFile[index], &devBufferSize); - if (picDevBuffer == nullptr) { - ERROR_LOG("get pic device buffer failed, index is %zu", index); - return FAILED; - } - - uint32_t devBufferShapeSize; - void *shapeDevBuffer = Utils::GetDeviceBufferOfFile(shapeFile[index], &devBufferShapeSize); - if (shapeDevBuffer == nullptr) { - ERROR_LOG("get shape device buffer failed, index is %zu", index); - return FAILED; - } - - std::vector<void *> inputBuffers({picDevBuffer, shapeDevBuffer}); - std::vector<size_t> inputSizes({devBufferSize, devBufferShapeSize}); - - ret = processModel.CreateInput(inputBuffers, inputSizes); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateInput failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - struct timeval start = {0}; - struct timeval end = {0}; - gettimeofday(&start, nullptr); - double startTimeMs = (start.tv_sec * second_to_microsecond + start.tv_usec) / second_to_millisecond; - - ret = processModel.Execute(); - - gettimeofday(&end, nullptr); - double endTimeMs = (end.tv_sec * second_to_microsecond + end.tv_usec) / second_to_millisecond; - - double cost_time = endTimeMs - startTimeMs; - INFO_LOG("model infer time: %lf ms", cost_time); - - model_cost_time += cost_time; - - double edge_to_edge_cost_time = endTimeMs - timeval_init; - edge_to_edge_model_cost_time += edge_to_edge_cost_time; - - if (ret != SUCCESS) { - ERROR_LOG("execute inference failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - int pos = testFile[index].find_last_of('/'); - std::string name = testFile[index].substr(pos+1); - std::string outputname = name.substr(0, name.rfind(".")); - - // dump output result to file in the current directory - processModel.DumpModelOutputResult(const_cast<char *>(outputname.c_str())); - - // release model input buffer - aclrtFree(picDevBuffer); - processModel.DestroyInput(); - } - double test_file_size = testFile.size(); - INFO_LOG("infer dataset size:%lf", test_file_size); - - gettimeofday(&end_global, nullptr); - startTimeMs_global = (start_global.tv_sec * second_to_microsecond + start_global.tv_usec) / second_to_millisecond; - endTimeMs_global = (end_global.tv_sec * second_to_microsecond + end_global.tv_usec) / second_to_millisecond; - whole_cost_time = (endTimeMs_global - startTimeMs_global) / test_file_size; - - model_cost_time /= test_file_size; - INFO_LOG("model cost time per sample: %lf ms", model_cost_time); - edge_to_edge_model_cost_time /= test_file_size; - INFO_LOG("edge-to-edge model cost time per sample:%lf ms", edge_to_edge_model_cost_time); - INFO_LOG("whole cost time per sample: %lf ms", whole_cost_time); - - return SUCCESS; -} - -void SampleProcess::DestroyResource() { - aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy stream failed"); - } - stream_ = nullptr; - } - INFO_LOG("end to destroy stream"); - - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy context failed"); - } - context_ = nullptr; - } - INFO_LOG("end to destroy context"); - - ret = aclrtResetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("reset device failed"); - } - INFO_LOG("end to reset device is %d", deviceId_); - - ret = aclFinalize(); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("finalize acl failed"); - } - INFO_LOG("end to finalize acl"); -} - diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/utils.cpp b/official/cv/yolov3_resnet18/ascend310_quant_infer/src/utils.cpp deleted file mode 100644 index d9208c8cfd9979e9248046e7325f260eb2d14d80..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/src/utils.cpp +++ /dev/null @@ -1,113 +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 "../inc/utils.h" -#include <sys/stat.h> -#include <iostream> -#include <fstream> -#include <cstring> -#include "acl/acl.h" - -extern bool g_is_device; - -void* Utils::ReadBinFile(std::string fileName, uint32_t *fileSize) { - struct stat sBuf; - int fileStatus = stat(fileName.data(), &sBuf); - if (fileStatus == -1) { - ERROR_LOG("failed to get file"); - return nullptr; - } - if (S_ISREG(sBuf.st_mode) == 0) { - ERROR_LOG("%s is not a file, please enter a file", fileName.c_str()); - return nullptr; - } - - std::ifstream binFile(fileName, std::ifstream::binary); - if (binFile.is_open() == false) { - ERROR_LOG("open file %s failed", fileName.c_str()); - return nullptr; - } - - binFile.seekg(0, binFile.end); - uint32_t binFileBufferLen = binFile.tellg(); - if (binFileBufferLen == 0) { - ERROR_LOG("binfile is empty, filename is %s", fileName.c_str()); - binFile.close(); - return nullptr; - } - - binFile.seekg(0, binFile.beg); - - void* binFileBufferData = nullptr; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&binFileBufferData, binFileBufferLen); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc for binFileBufferData failed"); - binFile.close(); - return nullptr; - } - if (binFileBufferData == nullptr) { - ERROR_LOG("malloc binFileBufferData failed"); - binFile.close(); - return nullptr; - } - } else { - ret = aclrtMalloc(&binFileBufferData, binFileBufferLen, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", binFileBufferLen); - binFile.close(); - return nullptr; - } - } - binFile.read(static_cast<char *>(binFileBufferData), binFileBufferLen); - binFile.close(); - *fileSize = binFileBufferLen; - return binFileBufferData; -} - -void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize) { - uint32_t inputHostBuffSize = 0; - void* inputHostBuff = Utils::ReadBinFile(fileName, &inputHostBuffSize); - if (inputHostBuff == nullptr) { - return nullptr; - } - if (!g_is_device) { - void *inBufferDev = nullptr; - uint32_t inBufferSize = inputHostBuffSize; - aclError ret = aclrtMalloc(&inBufferDev, inBufferSize, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", inBufferSize); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - - ret = aclrtMemcpy(inBufferDev, inBufferSize, inputHostBuff, inputHostBuffSize, ACL_MEMCPY_HOST_TO_DEVICE); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("memcpy failed. device buffer size is %u, input host buffer size is %u", - inBufferSize, inputHostBuffSize); - aclrtFree(inBufferDev); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - aclrtFreeHost(inputHostBuff); - *fileSize = inBufferSize; - return inBufferDev; - } else { - *fileSize = inputHostBuffSize; - return inputHostBuff; - } -} diff --git a/official/cv/yolov3_resnet18/ascend310_quant_infer/utils.py b/official/cv/yolov3_resnet18/ascend310_quant_infer/utils.py deleted file mode 100644 index f2d78f31b2fcc88373faaa6dac36dad86363b30f..0000000000000000000000000000000000000000 --- a/official/cv/yolov3_resnet18/ascend310_quant_infer/utils.py +++ /dev/null @@ -1,176 +0,0 @@ -# Copyright 2020 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. -# ============================================================================ -"""metrics utils""" - -import numpy as np - - -def calc_iou(bbox_pred, bbox_ground): - """Calculate iou of predicted bbox and ground truth.""" - x1 = bbox_pred[0] - y1 = bbox_pred[1] - width1 = bbox_pred[2] - bbox_pred[0] - height1 = bbox_pred[3] - bbox_pred[1] - - x2 = bbox_ground[0] - y2 = bbox_ground[1] - width2 = bbox_ground[2] - bbox_ground[0] - height2 = bbox_ground[3] - bbox_ground[1] - - endx = max(x1 + width1, x2 + width2) - startx = min(x1, x2) - width = width1 + width2 - (endx - startx) - - endy = max(y1 + height1, y2 + height2) - starty = min(y1, y2) - height = height1 + height2 - (endy - starty) - - if width <= 0 or height <= 0: - iou = 0 - else: - area = width * height - area1 = width1 * height1 - area2 = width2 * height2 - iou = area * 1. / (area1 + area2 - area) - - return iou - - -def apply_nms(all_boxes, all_scores, thres, max_boxes): - """Apply NMS to bboxes.""" - x1 = all_boxes[:, 0] - y1 = all_boxes[:, 1] - x2 = all_boxes[:, 2] - y2 = all_boxes[:, 3] - areas = (x2 - x1 + 1) * (y2 - y1 + 1) - - order = all_scores.argsort()[::-1] - keep = [] - - while order.size > 0: - i = order[0] - keep.append(i) - - if len(keep) >= max_boxes: - break - - xx1 = np.maximum(x1[i], x1[order[1:]]) - yy1 = np.maximum(y1[i], y1[order[1:]]) - xx2 = np.minimum(x2[i], x2[order[1:]]) - yy2 = np.minimum(y2[i], y2[order[1:]]) - - w = np.maximum(0.0, xx2 - xx1 + 1) - h = np.maximum(0.0, yy2 - yy1 + 1) - inter = w * h - - ovr = inter / (areas[i] + areas[order[1:]] - inter) - - inds = np.where(ovr <= thres)[0] - - order = order[inds + 1] - return keep - - -class ConfigYOLOV3ResNet18: - """ - Config parameters for YOLOv3. - - Examples: - ConfigYoloV3ResNet18. - """ - def __init__(self): - self.img_shape = [352, 640] - self.feature_shape = [32, 3, 352, 640] - self.num_classes = 2 - self.nms_max_num = 50 - - self.backbone_input_shape = [64, 64, 128, 256] - self.backbone_shape = [64, 128, 256, 512] - self.backbone_layers = [2, 2, 2, 2] - self.backbone_stride = [1, 2, 2, 2] - self.ignore_threshold = 0.5 - self.obj_threshold = 0.3 - self.nms_threshold = 0.4 - self.anchor_scales = [(10, 13), - (16, 30), - (33, 23), - (30, 61), - (62, 45), - (59, 119), - (116, 90), - (156, 198), - (163, 326)] - self.out_channel = int(len(self.anchor_scales) / 3 * (self.num_classes + 5)) - - -def metrics(pred_data): - """Calculate precision and recall of predicted bboxes.""" - config = ConfigYOLOV3ResNet18() - num_classes = config.num_classes - count_corrects = [1e-6 for _ in range(num_classes)] - count_grounds = [1e-6 for _ in range(num_classes)] - count_preds = [1e-6 for _ in range(num_classes)] - - for i, sample in enumerate(pred_data): - gt_anno = sample["annotation"] - box_scores = sample['box_scores'] - boxes = sample['boxes'] - mask = box_scores >= config.obj_threshold - boxes_ = [] - scores_ = [] - classes_ = [] - max_boxes = config.nms_max_num - for c in range(num_classes): - class_boxes = np.reshape(boxes, [-1, 4])[np.reshape(mask[:, c], [-1])] - class_box_scores = np.reshape(box_scores[:, c], [-1])[np.reshape(mask[:, c], [-1])] - nms_index = apply_nms(class_boxes, class_box_scores, config.nms_threshold, max_boxes) - class_boxes = class_boxes[nms_index] - class_box_scores = class_box_scores[nms_index] - classes = np.ones_like(class_box_scores, 'int32') * c - boxes_.append(class_boxes) - scores_.append(class_box_scores) - classes_.append(classes) - - boxes = np.concatenate(boxes_, axis=0) - classes = np.concatenate(classes_, axis=0) - - # metric - count_correct = [1e-6 for _ in range(num_classes)] - count_ground = [1e-6 for _ in range(num_classes)] - count_pred = [1e-6 for _ in range(num_classes)] - - for anno in gt_anno: - count_ground[anno[4]] += 1 - - for box_index, box in enumerate(boxes): - bbox_pred = [box[1], box[0], box[3], box[2]] - count_pred[classes[box_index]] += 1 - - for anno in gt_anno: - class_ground = anno[4] - - if classes[box_index] == class_ground: - iou = calc_iou(bbox_pred, anno) - if iou >= 0.5: - count_correct[class_ground] += 1 - break - - count_corrects = [count_corrects[i] + count_correct[i] for i in range(num_classes)] - count_preds = [count_preds[i] + count_pred[i] for i in range(num_classes)] - count_grounds = [count_grounds[i] + count_ground[i] for i in range(num_classes)] - - precision = np.array([count_corrects[ix] / count_preds[ix] for ix in range(num_classes)]) - recall = np.array([count_corrects[ix] / count_grounds[ix] for ix in range(num_classes)]) - return precision, recall diff --git a/official/cv/yolov4/README.md b/official/cv/yolov4/README.md index 89bc1a5f9431430815ea3140364b566dc1667095..0399575d558dadf142a6fc1cc97e8f606f576e9c 100644 --- a/official/cv/yolov4/README.md +++ b/official/cv/yolov4/README.md @@ -15,7 +15,6 @@ - [Evaluation](#evaluation) - [Convert Process](#convert-process) - [Convert](#convert) - - [Post Training Quantization](#post-training-quantization) - [Model Description](#model-description) - [Performance](#performance) - [Evaluation Performance](#evaluation-performance) @@ -530,52 +529,6 @@ Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.636 Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.716 ``` -## [Post Training Quantization](#contents) - -Relative executing script files reside in the directory "ascend310_quant_infer". Please implement following steps sequentially to complete post quantization. -Current quantization project bases on COCO2017 dataset. - -1. Generate data of .bin format required for AIR model inference at Ascend310 platform. - -```shell -python export_bin.py --config_path [YMAL CONFIG PATH] --data_dir [DATA DIR] --annFile [ANNOTATION FILE PATH] -``` - -2. Export quantized AIR model. - -Post quantization of model requires special toolkits for exporting quantized AIR model. Please refer to [official website](https://www.hiascend.com/software/cann/community). - -```shell -python post_quant.py --config_path [YMAL CONFIG PATH] --ckpt_file [CKPT_PATH] --data_dir [DATASET PATH] --annFile [ANNOTATION FILE PATH] -``` - -The quantized AIR file will be stored as "./results/yolov4_quant.air". - -3. Implement inference at Ascend310 platform. - -```shell -# Ascend310 quant inference -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [IMAGE_ID] [IMAGE_SHAPE] [ANN_FILE] -``` - -Inference result is saved in current path, you can find result like this in acc.log file. - -```bash -=============coco eval result========= - Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.433 - Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.633 - Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.467 - Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.273 - Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.475 - Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.555 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.329 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.532 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.568 - Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.395 - Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.611 - Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.690 -``` - # [Model Description](#contents) ## [Performance](#contents) diff --git a/official/cv/yolov4/README_CN.md b/official/cv/yolov4/README_CN.md index 39529d507a6a1de44a304a476ca79f31188a1967..9baa09db39ff0c7d7a19c0fad3a7a51ddb36d620 100644 --- a/official/cv/yolov4/README_CN.md +++ b/official/cv/yolov4/README_CN.md @@ -22,7 +22,6 @@ - [推理过程](#推理过程) - [用法](#用法) - [结果](#结果) - - [训练后量化推理](#训练后量化推理) - [模型说明](#模型说明) - [性能](#性能) - [评估性能](#评估性能) @@ -537,51 +536,6 @@ Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.636 Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.716 ``` -## [训练后量化推理](#contents) - -训练后量化推理的相关执行脚本文件在"ascend310_quant_infer"目录下,依次执行以下步骤实现训练后量化推理。本训练后量化工程基于COCO2017数据集。 - -1、生成Ascend310平台AIR模型推理需要的.bin格式数据。 - -```shell -python export_bin.py --config_path [YMAL CONFIG PATH] --data_dir [DATA DIR] --annFile [ANNOTATION FILE PATH] -``` - -2、导出训练后量化的AIR格式模型。 - -导出训练后量化模型需要配套的量化工具包,参考[官方地址](https://www.hiascend.com/software/cann/community) - -```shell -python post_quant.py --config_path [YMAL CONFIG PATH] --ckpt_file [CKPT_PATH] --data_dir [DATASET PATH] --annFile [ANNOTATION FILE PATH] -``` - -导出的模型会存储在./result/yolov4_quant.air。 - -3、在Ascend310执行推理量化模型。 - -```shell -# Ascend310 quant inference -bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [IMAGE_ID] [IMAGE_SHAPE] [ANN_FILE] -``` - -推理结果保存在脚本执行的当前路径,可以在acc.log中看到精度计算结果。 - -```bash -=============coco eval result========= - Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.433 - Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.633 - Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.467 - Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.273 - Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.475 - Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.555 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.329 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.532 - Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.568 - Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.395 - Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.611 - Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.690 -``` - # [模型说明](#目录) ## [性能](#目录) diff --git a/official/cv/yolov4/ascend310_quant_infer/acc.py b/official/cv/yolov4/ascend310_quant_infer/acc.py deleted file mode 100644 index 2a3a947a900fcee410b07934de37ebf04a4922e5..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/acc.py +++ /dev/null @@ -1,231 +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. -# ============================================================================ -"""postprocess for 310 inference""" -import os -import datetime -import argparse -import sys -from collections import defaultdict -import numpy as np -from pycocotools.coco import COCO -from pycocotools.cocoeval import COCOeval - - -parser = argparse.ArgumentParser('YoloV4 quant postprocess') -parser.add_argument('--result_path', type=str, required=True, help='result files path.') -parser.add_argument('--batch_size', default=1, type=int, help='batch size for per gpu') -parser.add_argument('--nms_thresh', type=float, default=0.5, help='threshold for NMS') -parser.add_argument('--eval_ignore_threshold', type=float, default=0.001, help='threshold to throw low quality boxes') -parser.add_argument('--annFile', type=str, default='', help='path to annotation') -parser.add_argument('--image_shape', type=str, default='./image_shape.npy', help='path to image_shape.npy') -parser.add_argument('--image_id', type=str, default='./image_id.npy', help='path to image_id.npy') -parser.add_argument('--log_path', type=str, default='outputs/', help='inference result save location') - -args, _ = parser.parse_known_args() - - -class Redirct: - def __init__(self): - self.content = "" - - def write(self, content): - self.content += content - - def flush(self): - self.content = "" - - -class DetectionEngine: - """Detection engine.""" - def __init__(self): - self.eval_ignore_threshold = args.eval_ignore_threshold - self.labels = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', - 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', - 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', - 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', - 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', - 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', - 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', - 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', - 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', - 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'] - self.num_classes = len(self.labels) - self.results = {} - self.file_path = '' - self.save_prefix = args.outputs_dir - self.annFile = args.annFile - self._coco = COCO(self.annFile) - self._img_ids = list(sorted(self._coco.imgs.keys())) - self.det_boxes = [] - self.nms_thresh = args.nms_thresh - self.coco_catIds = self._coco.getCatIds() - - def do_nms_for_results(self): - """Get result boxes.""" - for img_id in self.results: - for clsi in self.results[img_id]: - dets = self.results[img_id][clsi] - dets = np.array(dets) - keep_index = self._nms(dets, self.nms_thresh) - - keep_box = [{'image_id': int(img_id), - 'category_id': int(clsi), - 'bbox': list(dets[i][:4].astype(float)), - 'score': dets[i][4].astype(float)} - for i in keep_index] - self.det_boxes.extend(keep_box) - - def _nms(self, predicts, threshold): - """Calculate NMS.""" - # convert xywh -> xmin ymin xmax ymax - x1 = predicts[:, 0] - y1 = predicts[:, 1] - x2 = x1 + predicts[:, 2] - y2 = y1 + predicts[:, 3] - scores = predicts[:, 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) - - indexes = np.where(ovr <= threshold)[0] - order = order[indexes + 1] - return reserved_boxes - - def write_result(self): - """Save result to file.""" - import json - t = datetime.datetime.now().strftime('_%Y_%m_%d_%H_%M_%S') - try: - self.file_path = self.save_prefix + '/predict' + t + '.json' - f = open(self.file_path, 'w') - json.dump(self.det_boxes, f) - except IOError as e: - raise RuntimeError("Unable to open json file to dump. What(): {}".format(str(e))) - else: - f.close() - return self.file_path - - def get_eval_result(self): - """Get eval result.""" - cocoGt = COCO(self.annFile) - cocoDt = cocoGt.loadRes(self.file_path) - cocoEval = COCOeval(cocoGt, cocoDt, 'bbox') - cocoEval.evaluate() - cocoEval.accumulate() - rdct = Redirct() - stdout = sys.stdout - sys.stdout = rdct - cocoEval.summarize() - sys.stdout = stdout - return rdct.content - - def detect(self, outputs, batch, image_shape, image_id): - """Detect boxes.""" - outputs_num = len(outputs) - # output [|32, 52, 52, 3, 85| ] - for batch_id in range(batch): - for out_id in range(outputs_num): - # 32, 52, 52, 3, 85 - out_item = outputs[out_id] - # 52, 52, 3, 85 - out_item_single = out_item[batch_id, :] - # get number of items in one head, [B, gx, gy, anchors, 5+80] - dimensions = out_item_single.shape[:-1] - out_num = 1 - for d in dimensions: - out_num *= d - ori_w, ori_h = image_shape[batch_id] - img_id = int(image_id[batch_id]) - x = out_item_single[..., 0] * ori_w - y = out_item_single[..., 1] * ori_h - w = out_item_single[..., 2] * ori_w - h = out_item_single[..., 3] * ori_h - - conf = out_item_single[..., 4:5] - cls_emb = out_item_single[..., 5:] - - cls_argmax = np.expand_dims(np.argmax(cls_emb, axis=-1), axis=-1) - x = x.reshape(-1) - y = y.reshape(-1) - w = w.reshape(-1) - h = h.reshape(-1) - cls_emb = cls_emb.reshape(-1, self.num_classes) - conf = conf.reshape(-1) - cls_argmax = cls_argmax.reshape(-1) - - x_top_left = x - w / 2. - y_top_left = y - h / 2. - # create all False - flag = np.random.random(cls_emb.shape) > sys.maxsize - for i in range(flag.shape[0]): - c = cls_argmax[i] - flag[i, c] = True - confidence = cls_emb[flag] * conf - for x_lefti, y_lefti, wi, hi, confi, clsi in zip(x_top_left, y_top_left, w, h, confidence, cls_argmax): - if confi < self.eval_ignore_threshold: - continue - if img_id not in self.results: - self.results[img_id] = defaultdict(list) - x_lefti = max(0, x_lefti) - y_lefti = max(0, y_lefti) - wi = min(wi, ori_w) - hi = min(hi, ori_h) - # transform catId to match coco - coco_clsi = self.coco_catIds[clsi] - self.results[img_id][coco_clsi].append([x_lefti, y_lefti, wi, hi, confi]) - - -if __name__ == "__main__": - args.outputs_dir = os.path.join(args.log_path, datetime.datetime.now().strftime('%Y-%m-%d_time_%H_%M_%S')) - if not os.path.exists(args.outputs_dir): - os.makedirs(args.outputs_dir) - detection = DetectionEngine() - bs = args.batch_size - shape_list = np.load(args.image_shape) - id_list = np.load(args.image_id) - prefix = "YoloV4_coco_bs_" + str(bs) + "_" - iter_num = 0 - for id_img in id_list: - shape_img = shape_list[iter_num] - path_small = os.path.join(args.result_path, prefix + str(iter_num) + '_output_0.bin') - path_medium = os.path.join(args.result_path, prefix + str(iter_num) + '_output_1.bin') - path_big = os.path.join(args.result_path, prefix + str(iter_num) + '_output_2.bin') - if os.path.exists(path_small) and os.path.exists(path_medium) and os.path.exists(path_big): - output_small = np.fromfile(path_small, np.float32).reshape(bs, 19, 19, 3, 85) - output_medium = np.fromfile(path_medium, np.float32).reshape(bs, 38, 38, 3, 85) - output_big = np.fromfile(path_big, np.float32).reshape(bs, 76, 76, 3, 85) - detection.detect([output_small, output_medium, output_big], bs, shape_img, id_img) - else: - print("Error: Image ", iter_num, " is not exist.") - iter_num += 1 - - detection.do_nms_for_results() - result_file_path = detection.write_result() - eval_result = detection.get_eval_result() - print('\n=============coco eval result=========\n' + eval_result) diff --git a/official/cv/yolov4/ascend310_quant_infer/export_bin.py b/official/cv/yolov4/ascend310_quant_infer/export_bin.py deleted file mode 100644 index 17761a914f4a5f88b26abd7fb25ca8391381994b..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/export_bin.py +++ /dev/null @@ -1,61 +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. -# ============================================================================ -"""generate data and label needed for AIR model inference""" -import os -import sys -import numpy as np - - -def generate_data(): - """ - Generate data and label needed for AIR model inference at Ascend310 platform. - """ - config.batch_size = 1 - data_path = os.path.join(config.data_dir, "val2017") - ann_file = os.path.join(config.data_dir, "annotations/instances_val2017.json") - ds, data_size = create_yolo_dataset(data_path, ann_file, is_training=False, batch_size=config.batch_size, - max_epoch=1, device_num=1, rank=0, shuffle=False, default_config=config) - print('testing shape : {}'.format(config.test_img_shape)) - print('total {} images to eval'.format(data_size)) - - save_folder = "./data" - image_folder = os.path.join(save_folder, "00_image") - if not os.path.exists(image_folder): - os.makedirs(image_folder) - - list_image_shape = [] - list_image_id = [] - - for i, data in enumerate(ds.create_dict_iterator()): - image = data["image"].asnumpy() - image_shape = data["image_shape"] - image_id = data["img_id"] - file_name = "YoloV4_coco_bs_" + str(config.batch_size) + "_" + str(i) + ".bin" - file_path = image_folder + "/" + file_name - image.tofile(file_path) - list_image_shape.append(image_shape.asnumpy()) - list_image_id.append(image_id.asnumpy()) - shapes = np.array(list_image_shape) - ids = np.array(list_image_id) - np.save(save_folder + "/image_shape.npy", shapes) - np.save(save_folder + "/image_id.npy", ids) - - -if __name__ == '__main__': - sys.path.append("..") - from model_utils.config import config - from src.yolo_dataset import create_yolo_dataset - - generate_data() diff --git a/official/cv/yolov4/ascend310_quant_infer/inc/model_process.h b/official/cv/yolov4/ascend310_quant_infer/inc/model_process.h deleted file mode 100644 index 79e19833c7a1b3e428458c23aaba953dc40b1f01..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/inc/model_process.h +++ /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. - */ - -#pragma once -#include <iostream> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* ModelProcess -*/ -class ModelProcess { - public: - /** - * @brief Constructor - */ - ModelProcess(); - - /** - * @brief Destructor - */ - ~ModelProcess(); - - /** - * @brief load model from file with mem - * @param [in] modelPath: model path - * @return result - */ - Result LoadModelFromFileWithMem(const char *modelPath); - - /** - * @brief unload model - */ - void Unload(); - - /** - * @brief create model desc - * @return result - */ - Result CreateDesc(); - - /** - * @brief destroy desc - */ - void DestroyDesc(); - - /** - * @brief create model input - * @param [in] inputDataBuffer: input buffer - * @param [in] bufferSize: input buffer size - * @return result - */ - Result CreateInput(void *inputDataBuffer, size_t bufferSize); - - /** - * @brief destroy input resource - */ - void DestroyInput(); - - /** - * @brief create output buffer - * @return result - */ - Result CreateOutput(); - - /** - * @brief destroy output resource - */ - void DestroyOutput(); - - /** - * @brief model execute - * @return result - */ - Result Execute(); - - /** - * @brief dump model output result to file - */ - void DumpModelOutputResult(char *output_name); - - /** - * @brief get model output result - */ - void OutputModelResult(); - - private: - uint32_t modelId_; - size_t modelMemSize_; - size_t modelWeightSize_; - void *modelMemPtr_; - void *modelWeightPtr_; - bool loadFlag_; // model load flag - aclmdlDesc *modelDesc_; - aclmdlDataset *input_; - aclmdlDataset *output_; -}; - diff --git a/official/cv/yolov4/ascend310_quant_infer/inc/sample_process.h b/official/cv/yolov4/ascend310_quant_infer/inc/sample_process.h deleted file mode 100644 index 24d6ea01e59925673a548a7873ab310623235549..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/inc/sample_process.h +++ /dev/null @@ -1,58 +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. - */ - -#pragma once -#include <string> -#include <vector> -#include "../inc/utils.h" -#include "acl/acl.h" - -/** -* SampleProcess -*/ -class SampleProcess { - public: - /** - * @brief Constructor - */ - SampleProcess(); - - /** - * @brief Destructor - */ - ~SampleProcess(); - - /** - * @brief init reousce - * @return result - */ - Result InitResource(); - - /** - * @brief sample process - * @return result - */ - Result Process(char *om_path, char *input_folder); - - void GetAllFiles(std::string path, std::vector<std::string> *files); - - private: - void DestroyResource(); - - int32_t deviceId_; - aclrtContext context_; - aclrtStream stream_; -}; diff --git a/official/cv/yolov4/ascend310_quant_infer/inc/utils.h b/official/cv/yolov4/ascend310_quant_infer/inc/utils.h deleted file mode 100644 index 3ae2a571b8e8ba51c01b02e23f36dfad5a7b18f1..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/inc/utils.h +++ /dev/null @@ -1,52 +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. - */ - -#pragma once -#include <iostream> -#include <string> - -#define INFO_LOG(fmt, args...) fprintf(stdout, "[INFO] " fmt "\n", ##args) -#define WARN_LOG(fmt, args...) fprintf(stdout, "[WARN] " fmt "\n", ##args) -#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) - -typedef enum Result { - SUCCESS = 0, - FAILED = 1 -} Result; - -/** -* Utils -*/ -class Utils { - public: - /** - * @brief create device buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return device buffer of file - */ - static void *GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize); - - /** - * @brief create buffer of file - * @param [in] fileName: file name - * @param [out] fileSize: size of file - * @return buffer of pic - */ - static void* ReadBinFile(std::string fileName, uint32_t *fileSize); -}; - -#pragma once diff --git a/official/cv/yolov4/ascend310_quant_infer/post_quant.py b/official/cv/yolov4/ascend310_quant_infer/post_quant.py deleted file mode 100644 index 815034a5e92e8cf1e5ee0f471595f333ede10ef8..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/post_quant.py +++ /dev/null @@ -1,77 +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. -# ============================================================================ -"""do post training quantization for Ascend310""" -import os -import sys -import numpy as np - -from amct_mindspore.quantize_tool import create_quant_config -from amct_mindspore.quantize_tool import quantize_model -from amct_mindspore.quantize_tool import save_model -import mindspore as ms -from mindspore import context, Tensor -from mindspore.train.serialization import load_checkpoint, load_param_into_net - - -def quant_yolov4(network, dataset, input_data): - """ - Export post training quantization model of AIR format. - - Args: - network: the origin network for inference.YOLOV3DarkNet53 - dataset: the data for inference. - input_data: the data used for constructing network. The shape and format of input data should be the same as - actual data for inference. - """ - - # step2: create the quant config json file - create_quant_config("./config.json", network, input_data) - - # step3: do some network modification and return the modified network - calibration_network = quantize_model("./config.json", network, input_data) - calibration_network.set_train(False) - - # step4: perform the evaluation of network to do activation calibration - for _, data in enumerate(dataset.create_dict_iterator(num_epochs=1)): - image = data["image"] - _ = calibration_network(image) - - # step5: export the air file - save_model("results/yolov4_quant", calibration_network, input_data) - print("[INFO] the quantized AIR file has been stored at: \n {}".format("results/yolov4_quant.air")) - - -if __name__ == "__main__": - sys.path.append("..") - from src.yolo import YOLOV4CspDarkNet53 - from src.yolo_dataset import create_yolo_dataset - from model_utils.config import config - - context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") - net = YOLOV4CspDarkNet53() - - param_dict = load_checkpoint(config.ckpt_file) - load_param_into_net(net, param_dict) - net.set_train(False) - - config.batch_size = 1 - data_path = os.path.join(config.data_dir, "val2017") - ann_file = os.path.join(config.data_dir, "annotations/instances_val2017.json") - datasets, data_size = create_yolo_dataset(data_path, ann_file, is_training=False, batch_size=config.batch_size, - max_epoch=1, device_num=1, rank=0, shuffle=False, default_config=config) - ds = datasets.take(1) - shape = [config.batch_size, 3] + config.test_img_shape - inputs = Tensor(np.zeros(shape), ms.float32) - quant_yolov4(net, ds, inputs) diff --git a/official/cv/yolov4/ascend310_quant_infer/run_quant_infer.sh b/official/cv/yolov4/ascend310_quant_infer/run_quant_infer.sh deleted file mode 100644 index 6b66b0eabeebbc49b75823466827a8149b7b8d56..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/run_quant_infer.sh +++ /dev/null @@ -1,110 +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 5 ]; then - echo "Usage: bash run_quant_infer.sh [AIR_PATH] [DATA_PATH] [IMAGE_ID] [IMAGE_SHAPE] [ANN_FILE]" - echo "Example: bash run_quant_infer.sh ./yolov4_quant.air ./image_bin ./image_id.npy ./image_shape.npy \ -./instances_val2017.json" -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) -id_path=$(get_real_path $3) -shape_path=$(get_real_path $4) -ann_path=$(get_real_path $5) - -echo "air name: "$model -echo "dataset path: "$data_path -echo "id path: "$id_path -echo "shape path: "$shape_path -echo "ann path: "$ann_path - -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 ASCEND_HOME=/usr/local/Ascend/latest/ - export PATH=$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/fwkacllib/bin:$PATH - export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/fwkacllib/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$LD_LIBRARY_PATH - export TBE_IMPL_PATH=$ASCEND_HOME/opp/op_impl/built-in/ai_core/tbe - export PYTHONPATH=${TBE_IMPL_PATH}:$PYTHONPATH - export ASCEND_OPP_PATH=$ASCEND_HOME/opp -fi - -function air_to_om() -{ - atc --input_format=NCHW --framework=1 --model=$model --output=yolov4_quant --soc_version=Ascend310 &> atc.log -} - -function compile_app() -{ - bash ./src/build.sh &> build.log -} - -function infer() -{ - if [ -d result ]; then - rm -rf ./result - fi - mkdir result - ./out/main ./yolov4_quant.om $data_path &> infer.log -} - -function cal_acc() -{ - python3.7 ./acc.py --result_path=./result --annFile=$ann_path --image_shape=$shape_path \ - --image_id=$id_path &> acc.log -} - -echo "start atc================================================" -air_to_om -if [ $? -ne 0 ]; then - echo "air to om code failed" - exit 1 -fi - -echo "start compile============================================" -compile_app -if [ $? -ne 0 ]; then - echo "compile app code failed" - exit 1 -fi - -echo "start infer==============================================" -infer -if [ $? -ne 0 ]; then - echo " execute inference failed" - exit 1 -fi - -echo "start calculate acc======================================" -cal_acc -if [ $? -ne 0 ]; then - echo "calculate accuracy failed" - exit 1 -fi \ No newline at end of file diff --git a/official/cv/yolov4/ascend310_quant_infer/src/CMakeLists.txt b/official/cv/yolov4/ascend310_quant_infer/src/CMakeLists.txt deleted file mode 100644 index 655026d7d91612267a287e83e886ba2ce1304d18..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/src/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. - -# CMake lowest version requirement -cmake_minimum_required(VERSION 3.5.1) -# project information -project(InferClassification) -# Check environment variable -if(NOT DEFINED ENV{ASCEND_HOME}) - message(FATAL_ERROR "please define environment variable:ASCEND_HOME") -endif() - -# Compile options -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") - -# Skip build rpath -set(CMAKE_SKIP_BUILD_RPATH True) - -# Set output directory -set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SRC_ROOT}/../out) - -# Set include directory and library directory -set(FWKACL_LIB_DIR $ENV{ASCEND_HOME}/fwkacllib) -set(ACL_LIB_DIR $ENV{ASCEND_HOME}/acllib) -set(ATLAS_ACL_LIB_DIR $ENV{ASCEND_HOME}/ascend-toolkit/latest/acllib) - -# Header path -include_directories(${ACL_LIB_DIR}/include/) -include_directories(${FWKACL_LIB_DIR}/include/) -include_directories(${ATLAS_ACL_LIB_DIR}/include/) -include_directories(${PROJECT_SRC_ROOT}/../inc) - -# add host lib path -link_directories(${ACL_LIB_DIR} ${FWKACL_LIB_DIR}) -find_library(acl libascendcl.so ${ACL_LIB_DIR}/lib64 ${FWKACL_LIB_DIR}/lib64 ${ATLAS_ACL_LIB_DIR}/lib64) - -add_executable(main utils.cpp - sample_process.cpp - model_process.cpp - main.cpp) - -target_link_libraries(main ${acl} gflags pthread) diff --git a/official/cv/yolov4/ascend310_quant_infer/src/acl.json b/official/cv/yolov4/ascend310_quant_infer/src/acl.json deleted file mode 100644 index 0967ef424bce6791893e9a57bb952f80fd536e93..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/src/acl.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/official/cv/yolov4/ascend310_quant_infer/src/build.sh b/official/cv/yolov4/ascend310_quant_infer/src/build.sh deleted file mode 100644 index b5979b68e60b673f763a3cac98c5e147e7085291..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/src/build.sh +++ /dev/null @@ -1,55 +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. -# ============================================================================ -path_cur=$(cd "`dirname $0`" || exit; pwd) - -function preparePath() { - rm -rf $1 - mkdir -p $1 - cd $1 || exit -} - -function buildA300() { - if [ ! "${ARCH_PATTERN}" ]; then - # set ARCH_PATTERN to acllib when it was not specified by user - export ARCH_PATTERN=acllib - echo "ARCH_PATTERN is set to the default value: ${ARCH_PATTERN}" - else - echo "ARCH_PATTERN is set to ${ARCH_PATTERN} by user, reset it to ${ARCH_PATTERN}/acllib" - export ARCH_PATTERN=${ARCH_PATTERN}/acllib - fi - - path_build=$path_cur/build - preparePath $path_build - cmake .. - make -j - ret=$? - cd .. - return ${ret} -} - -# 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 - -buildA300 - -if [ $? -ne 0 ]; then - exit 1 -fi diff --git a/official/cv/yolov4/ascend310_quant_infer/src/main.cpp b/official/cv/yolov4/ascend310_quant_infer/src/main.cpp deleted file mode 100644 index 80165505f447d418e0f107b76d04ffae59b89a73..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/src/main.cpp +++ /dev/null @@ -1,42 +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 <iostream> -#include "../inc/sample_process.h" -#include "../inc/utils.h" -bool g_is_device = false; - -int main(int argc, char **argv) { - if (argc != 3) { - ERROR_LOG("usage:./main path_of_om path_of_inputFolder"); - return FAILED; - } - SampleProcess processSample; - Result ret = processSample.InitResource(); - if (ret != SUCCESS) { - ERROR_LOG("sample init resource failed"); - return FAILED; - } - - ret = processSample.Process(argv[1], argv[2]); - if (ret != SUCCESS) { - ERROR_LOG("sample process failed"); - return FAILED; - } - - INFO_LOG("execute sample success"); - return SUCCESS; -} diff --git a/official/cv/yolov4/ascend310_quant_infer/src/model_process.cpp b/official/cv/yolov4/ascend310_quant_infer/src/model_process.cpp deleted file mode 100644 index 04e6a42ab43cbc41720fe6b9e30bf919323c9f9e..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/src/model_process.cpp +++ /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. - */ - -#include "../inc/model_process.h" -#include <iostream> -#include <map> -#include <sstream> -#include <algorithm> -#include "../inc/utils.h" -extern bool g_is_device; - -ModelProcess::ModelProcess() :modelId_(0), modelMemSize_(0), modelWeightSize_(0), modelMemPtr_(nullptr), -modelWeightPtr_(nullptr), loadFlag_(false), modelDesc_(nullptr), input_(nullptr), output_(nullptr) { -} - -ModelProcess::~ModelProcess() { - Unload(); - DestroyDesc(); - DestroyInput(); - DestroyOutput(); -} - -Result ModelProcess::LoadModelFromFileWithMem(const char *modelPath) { - if (loadFlag_) { - ERROR_LOG("has already loaded a model"); - return FAILED; - } - - aclError ret = aclmdlQuerySize(modelPath, &modelMemSize_, &modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("query model failed, model file is %s", modelPath); - return FAILED; - } - - ret = aclrtMalloc(&modelMemPtr_, modelMemSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for mem failed, require size is %zu", modelMemSize_); - return FAILED; - } - - ret = aclrtMalloc(&modelWeightPtr_, modelWeightSize_, ACL_MEM_MALLOC_HUGE_FIRST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc buffer for weight failed, require size is %zu", modelWeightSize_); - return FAILED; - } - - ret = aclmdlLoadFromFileWithMem(modelPath, &modelId_, modelMemPtr_, - modelMemSize_, modelWeightPtr_, modelWeightSize_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("load model from file failed, model file is %s", modelPath); - return FAILED; - } - - loadFlag_ = true; - INFO_LOG("load model %s success", modelPath); - return SUCCESS; -} - -Result ModelProcess::CreateDesc() { - modelDesc_ = aclmdlCreateDesc(); - if (modelDesc_ == nullptr) { - ERROR_LOG("create model description failed"); - return FAILED; - } - - aclError ret = aclmdlGetDesc(modelDesc_, modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("get model description failed"); - return FAILED; - } - - INFO_LOG("create model description success"); - - return SUCCESS; -} - -void ModelProcess::DestroyDesc() { - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } -} - -Result ModelProcess::CreateInput(void *inputDataBuffer, size_t bufferSize) { - input_ = aclmdlCreateDataset(); - if (input_ == nullptr) { - ERROR_LOG("can't create dataset, create input failed"); - return FAILED; - } - - aclDataBuffer* inputData = aclCreateDataBuffer(inputDataBuffer, bufferSize); - if (inputData == nullptr) { - ERROR_LOG("can't create data buffer, create input failed"); - return FAILED; - } - - aclError ret = aclmdlAddDatasetBuffer(input_, inputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("add input dataset buffer failed"); - aclDestroyDataBuffer(inputData); - inputData = nullptr; - return FAILED; - } - - return SUCCESS; -} - -void ModelProcess::DestroyInput() { - if (input_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(input_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(input_, i); - aclDestroyDataBuffer(dataBuffer); - } - aclmdlDestroyDataset(input_); - input_ = nullptr; -} - -Result ModelProcess::CreateOutput() { - if (modelDesc_ == nullptr) { - ERROR_LOG("no model description, create output failed"); - return FAILED; - } - - output_ = aclmdlCreateDataset(); - if (output_ == nullptr) { - ERROR_LOG("can't create dataset, create output failed"); - return FAILED; - } - - size_t outputSize = aclmdlGetNumOutputs(modelDesc_); - for (size_t i = 0; i < outputSize; ++i) { - size_t buffer_size = aclmdlGetOutputSizeByIndex(modelDesc_, i); - - void *outputBuffer = nullptr; - aclError ret = aclrtMalloc(&outputBuffer, buffer_size, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't malloc buffer, size is %zu, create output failed", buffer_size); - return FAILED; - } - - aclDataBuffer* outputData = aclCreateDataBuffer(outputBuffer, buffer_size); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't create data buffer, create output failed"); - aclrtFree(outputBuffer); - return FAILED; - } - - ret = aclmdlAddDatasetBuffer(output_, outputData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("can't add data buffer, create output failed"); - aclrtFree(outputBuffer); - aclDestroyDataBuffer(outputData); - return FAILED; - } - } - - INFO_LOG("create model output success"); - return SUCCESS; -} - -void ModelProcess::DumpModelOutputResult(char *output_name) { - size_t outputNum = aclmdlGetDatasetNumBuffers(output_); - - for (size_t i = 0; i < outputNum; ++i) { - std::stringstream ss; - ss << "result/" << output_name << "_output_" << i << ".bin"; - std::string outputFileName = ss.str(); - FILE *outputFile = fopen(outputFileName.c_str(), "wb"); - if (outputFile != nullptr) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void* outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - (void)aclrtFreeHost(outHostData); - return; - } - - fwrite(outHostData, len, sizeof(char), outputFile); - - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } else { - fwrite(data, len, sizeof(char), outputFile); - } - fclose(outputFile); - outputFile = nullptr; - } else { - ERROR_LOG("create output file [%s] failed", outputFileName.c_str()); - return; - } - } - - INFO_LOG("dump data success"); - return; -} - -void ModelProcess::OutputModelResult() { - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - uint32_t len = aclGetDataBufferSizeV2(dataBuffer); - - void *outHostData = NULL; - aclError ret = ACL_ERROR_NONE; - float *outData = NULL; - if (!g_is_device) { - ret = aclrtMallocHost(&outHostData, len); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMallocHost failed, ret[%d]", ret); - return; - } - - ret = aclrtMemcpy(outHostData, len, data, len, ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtMemcpy failed, ret[%d]", ret); - return; - } - - outData = reinterpret_cast<float*>(outHostData); - } else { - outData = reinterpret_cast<float*>(data); - } - std::map<float, unsigned int, std::greater<float> > resultMap; - for (unsigned int j = 0; j < len / sizeof(float); ++j) { - resultMap[*outData] = j; - outData++; - } - - int cnt = 0; - for (auto it = resultMap.begin(); it != resultMap.end(); ++it) { - // print top 5 - if (++cnt > 5) { - break; - } - - INFO_LOG("top %d: index[%d] value[%lf]", cnt, it->second, it->first); - } - if (!g_is_device) { - ret = aclrtFreeHost(outHostData); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("aclrtFreeHost failed, ret[%d]", ret); - return; - } - } - } - - INFO_LOG("output data success"); - return; -} - -void ModelProcess::DestroyOutput() { - if (output_ == nullptr) { - return; - } - - for (size_t i = 0; i < aclmdlGetDatasetNumBuffers(output_); ++i) { - aclDataBuffer* dataBuffer = aclmdlGetDatasetBuffer(output_, i); - void* data = aclGetDataBufferAddr(dataBuffer); - (void)aclrtFree(data); - (void)aclDestroyDataBuffer(dataBuffer); - } - - (void)aclmdlDestroyDataset(output_); - output_ = nullptr; -} - -Result ModelProcess::Execute() { - aclError ret = aclmdlExecute(modelId_, input_, output_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("execute model failed, modelId is %u", modelId_); - return FAILED; - } - - INFO_LOG("model execute success"); - return SUCCESS; -} - -void ModelProcess::Unload() { - if (!loadFlag_) { - WARN_LOG("no model had been loaded, unload failed"); - return; - } - - aclError ret = aclmdlUnload(modelId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("unload model failed, modelId is %u", modelId_); - } - - if (modelDesc_ != nullptr) { - (void)aclmdlDestroyDesc(modelDesc_); - modelDesc_ = nullptr; - } - - if (modelMemPtr_ != nullptr) { - aclrtFree(modelMemPtr_); - modelMemPtr_ = nullptr; - modelMemSize_ = 0; - } - - if (modelWeightPtr_ != nullptr) { - aclrtFree(modelWeightPtr_); - modelWeightPtr_ = nullptr; - modelWeightSize_ = 0; - } - - loadFlag_ = false; - INFO_LOG("unload model success, modelId is %u", modelId_); -} diff --git a/official/cv/yolov4/ascend310_quant_infer/src/sample_process.cpp b/official/cv/yolov4/ascend310_quant_infer/src/sample_process.cpp deleted file mode 100644 index 5f8f20f297bf6101d3a68ffd65bd0e6ed69d9486..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/src/sample_process.cpp +++ /dev/null @@ -1,252 +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 "../inc/sample_process.h" -#include <sys/time.h> -#include <sys/types.h> -#include <dirent.h> -#include <string.h> -#include <iostream> -#include <fstream> -#include "../inc/model_process.h" -#include "acl/acl.h" -#include "../inc/utils.h" -extern bool g_is_device; -using std::string; -using std::vector; - -SampleProcess::SampleProcess() :deviceId_(0), context_(nullptr), stream_(nullptr) { -} - -SampleProcess::~SampleProcess() { - DestroyResource(); -} - -Result SampleProcess::InitResource() { - // ACL init - - const char *aclConfigPath = "./src/acl.json"; - aclError ret = aclInit(aclConfigPath); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl init failed"); - return FAILED; - } - INFO_LOG("acl init success"); - - // open device - ret = aclrtSetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl open device %d failed", deviceId_); - return FAILED; - } - INFO_LOG("open device %d success", deviceId_); - - // create context (set current) - ret = aclrtCreateContext(&context_, deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create context failed"); - return FAILED; - } - INFO_LOG("create context success"); - - // create stream - ret = aclrtCreateStream(&stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl create stream failed"); - return FAILED; - } - INFO_LOG("create stream success"); - - // get run mode - aclrtRunMode runMode; - ret = aclrtGetRunMode(&runMode); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("acl get run mode failed"); - return FAILED; - } - g_is_device = (runMode == ACL_DEVICE); - INFO_LOG("get run mode success"); - return SUCCESS; -} - -void SampleProcess::GetAllFiles(std::string path, std::vector<string> *files) { - DIR *pDir = NULL; - struct dirent* ptr = nullptr; - if (!(pDir = opendir(path.c_str()))) { - return; - } - while ((ptr = readdir(pDir)) != 0) { - if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { - files->push_back(path + "/" + ptr->d_name); - } - } - closedir(pDir); -} - -Result SampleProcess::Process(char *om_path, char *input_folder) { - // model init - const double second_to_millisecond = 1000; - const double second_to_microsecond = 1000000; - - double whole_cost_time = 0.0; - struct timeval start_global = {0}; - struct timeval end_global = {0}; - double startTimeMs_global = 0.0; - double endTimeMs_global = 0.0; - - gettimeofday(&start_global, nullptr); - - ModelProcess processModel; - const char* omModelPath = om_path; - - Result ret = processModel.LoadModelFromFileWithMem(omModelPath); - if (ret != SUCCESS) { - ERROR_LOG("execute LoadModelFromFileWithMem failed"); - return FAILED; - } - - ret = processModel.CreateDesc(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateDesc failed"); - return FAILED; - } - - ret = processModel.CreateOutput(); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateOutput failed"); - return FAILED; - } - - std::vector<string> testFile; - GetAllFiles(input_folder, &testFile); - - if (testFile.size() == 0) { - WARN_LOG("no input data under folder"); - } - - double model_cost_time = 0.0; - double edge_to_edge_model_cost_time = 0.0; - - for (size_t index = 0; index < testFile.size(); ++index) { - INFO_LOG("start to process file:%s", testFile[index].c_str()); - // model process - - struct timeval time_init = {0}; - double timeval_init = 0.0; - gettimeofday(&time_init, nullptr); - timeval_init = (time_init.tv_sec * second_to_microsecond + time_init.tv_usec) / second_to_millisecond; - - uint32_t devBufferSize; - void *picDevBuffer = Utils::GetDeviceBufferOfFile(testFile[index], &devBufferSize); - if (picDevBuffer == nullptr) { - ERROR_LOG("get pic device buffer failed,index is %zu", index); - return FAILED; - } - ret = processModel.CreateInput(picDevBuffer, devBufferSize); - if (ret != SUCCESS) { - ERROR_LOG("execute CreateInput failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - struct timeval start = {0}; - struct timeval end = {0}; - double startTimeMs = 0.0; - double endTimeMs = 0.0; - gettimeofday(&start, nullptr); - startTimeMs = (start.tv_sec * second_to_microsecond + start.tv_usec) / second_to_millisecond; - - ret = processModel.Execute(); - - gettimeofday(&end, nullptr); - endTimeMs = (end.tv_sec * second_to_microsecond + end.tv_usec) / second_to_millisecond; - - double cost_time = endTimeMs - startTimeMs; - INFO_LOG("model infer time: %lf ms", cost_time); - - model_cost_time += cost_time; - - double edge_to_edge_cost_time = endTimeMs - timeval_init; - edge_to_edge_model_cost_time += edge_to_edge_cost_time; - - if (ret != SUCCESS) { - ERROR_LOG("execute inference failed"); - aclrtFree(picDevBuffer); - return FAILED; - } - - int pos = testFile[index].find_last_of('/'); - std::string name = testFile[index].substr(pos+1); - std::string outputname = name.substr(0, name.rfind(".")); - - // dump output result to file in the current directory - processModel.DumpModelOutputResult(const_cast<char *>(outputname.c_str())); - - // release model input buffer - aclrtFree(picDevBuffer); - processModel.DestroyInput(); - } - double test_file_size = 0.0; - test_file_size = testFile.size(); - INFO_LOG("infer dataset size:%lf", test_file_size); - - gettimeofday(&end_global, nullptr); - startTimeMs_global = (start_global.tv_sec * second_to_microsecond + start_global.tv_usec) / second_to_millisecond; - endTimeMs_global = (end_global.tv_sec * second_to_microsecond + end_global.tv_usec) / second_to_millisecond; - whole_cost_time = (endTimeMs_global - startTimeMs_global) / test_file_size; - - model_cost_time /= test_file_size; - INFO_LOG("model cost time per sample: %lf ms", model_cost_time); - edge_to_edge_model_cost_time /= test_file_size; - INFO_LOG("edge-to-edge model cost time per sample:%lf ms", edge_to_edge_model_cost_time); - INFO_LOG("whole cost time per sample: %lf ms", whole_cost_time); - - return SUCCESS; -} - -void SampleProcess::DestroyResource() { - aclError ret; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy stream failed"); - } - stream_ = nullptr; - } - INFO_LOG("end to destroy stream"); - - if (context_ != nullptr) { - ret = aclrtDestroyContext(context_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("destroy context failed"); - } - context_ = nullptr; - } - INFO_LOG("end to destroy context"); - - ret = aclrtResetDevice(deviceId_); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("reset device failed"); - } - INFO_LOG("end to reset device is %d", deviceId_); - - ret = aclFinalize(); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("finalize acl failed"); - } - INFO_LOG("end to finalize acl"); -} - diff --git a/official/cv/yolov4/ascend310_quant_infer/src/utils.cpp b/official/cv/yolov4/ascend310_quant_infer/src/utils.cpp deleted file mode 100644 index d9208c8cfd9979e9248046e7325f260eb2d14d80..0000000000000000000000000000000000000000 --- a/official/cv/yolov4/ascend310_quant_infer/src/utils.cpp +++ /dev/null @@ -1,113 +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 "../inc/utils.h" -#include <sys/stat.h> -#include <iostream> -#include <fstream> -#include <cstring> -#include "acl/acl.h" - -extern bool g_is_device; - -void* Utils::ReadBinFile(std::string fileName, uint32_t *fileSize) { - struct stat sBuf; - int fileStatus = stat(fileName.data(), &sBuf); - if (fileStatus == -1) { - ERROR_LOG("failed to get file"); - return nullptr; - } - if (S_ISREG(sBuf.st_mode) == 0) { - ERROR_LOG("%s is not a file, please enter a file", fileName.c_str()); - return nullptr; - } - - std::ifstream binFile(fileName, std::ifstream::binary); - if (binFile.is_open() == false) { - ERROR_LOG("open file %s failed", fileName.c_str()); - return nullptr; - } - - binFile.seekg(0, binFile.end); - uint32_t binFileBufferLen = binFile.tellg(); - if (binFileBufferLen == 0) { - ERROR_LOG("binfile is empty, filename is %s", fileName.c_str()); - binFile.close(); - return nullptr; - } - - binFile.seekg(0, binFile.beg); - - void* binFileBufferData = nullptr; - aclError ret = ACL_ERROR_NONE; - if (!g_is_device) { - ret = aclrtMallocHost(&binFileBufferData, binFileBufferLen); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc for binFileBufferData failed"); - binFile.close(); - return nullptr; - } - if (binFileBufferData == nullptr) { - ERROR_LOG("malloc binFileBufferData failed"); - binFile.close(); - return nullptr; - } - } else { - ret = aclrtMalloc(&binFileBufferData, binFileBufferLen, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", binFileBufferLen); - binFile.close(); - return nullptr; - } - } - binFile.read(static_cast<char *>(binFileBufferData), binFileBufferLen); - binFile.close(); - *fileSize = binFileBufferLen; - return binFileBufferData; -} - -void* Utils::GetDeviceBufferOfFile(std::string fileName, uint32_t *fileSize) { - uint32_t inputHostBuffSize = 0; - void* inputHostBuff = Utils::ReadBinFile(fileName, &inputHostBuffSize); - if (inputHostBuff == nullptr) { - return nullptr; - } - if (!g_is_device) { - void *inBufferDev = nullptr; - uint32_t inBufferSize = inputHostBuffSize; - aclError ret = aclrtMalloc(&inBufferDev, inBufferSize, ACL_MEM_MALLOC_NORMAL_ONLY); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("malloc device buffer failed. size is %u", inBufferSize); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - - ret = aclrtMemcpy(inBufferDev, inBufferSize, inputHostBuff, inputHostBuffSize, ACL_MEMCPY_HOST_TO_DEVICE); - if (ret != ACL_ERROR_NONE) { - ERROR_LOG("memcpy failed. device buffer size is %u, input host buffer size is %u", - inBufferSize, inputHostBuffSize); - aclrtFree(inBufferDev); - aclrtFreeHost(inputHostBuff); - return nullptr; - } - aclrtFreeHost(inputHostBuff); - *fileSize = inBufferSize; - return inBufferDev; - } else { - *fileSize = inputHostBuffSize; - return inputHostBuff; - } -}