| @@ -37,7 +37,7 @@ The SSD approach is based on a feed-forward convolutional network that produces | |||
| We present two different base architecture. | |||
| - **ssd300**, reference from the paper. Using mobilenetv2 as backbone and the same bbox predictor as the paper pressent. | |||
| - **ssd300**, reference from the paper. Using mobilenetv2 as backbone and the same bbox predictor as the paper present. | |||
| - ***ssd-mobilenet-v1-fpn**, using mobilenet-v1 and FPN as feature extractor with weight-shared box predcitors. | |||
| ## [Dataset](#contents) | |||
| @@ -85,7 +85,7 @@ Dataset used: [COCO2017](<http://images.cocodataset.org/>) | |||
| ``` | |||
| 2. If VOC dataset is used. **Select dataset to voc when run script.** | |||
| Change `classes`, `num_classes`, `voc_json` and `voc_root` in `src/config.py`. `voc_json` is the path of json file with coco format for evalution, `voc_root` is the path of VOC dataset, the directory structure is as follows: | |||
| Change `classes`, `num_classes`, `voc_json` and `voc_root` in `src/config.py`. `voc_json` is the path of json file with coco format for evaluation, `voc_root` is the path of VOC dataset, the directory structure is as follows: | |||
| ```shell | |||
| . | |||
| @@ -105,7 +105,7 @@ Dataset used: [COCO2017](<http://images.cocodataset.org/>) | |||
| ``` | |||
| 3. If your own dataset is used. **Select dataset to other when run script.** | |||
| Organize the dataset infomation into a TXT file, each row in the file is as follows: | |||
| Organize the dataset information into a TXT file, each row in the file is as follows: | |||
| ```shell | |||
| train2017/0000001.jpg 0,259,401,459,7 35,28,324,201,2 0,30,59,80,2 | |||
| @@ -117,15 +117,15 @@ Dataset used: [COCO2017](<http://images.cocodataset.org/>) | |||
| ### Prepare the model | |||
| 1. Chose the model by chaning the `using_model` in `src/confgi.py`. The optional models are: `ssd300`, `ssd_mobilenet_v1_fpn`. | |||
| 2. Change the datset config in the corresponding config. `src/config_ssd300.py` or `src/config_ssd_mobilenet_v1_fpn.py`. | |||
| 1. Chose the model by changing the `using_model` in `src/confgi.py`. The optional models are: `ssd300`, `ssd_mobilenet_v1_fpn`. | |||
| 2. Change the dataset config in the corresponding config. `src/config_ssd300.py` or `src/config_ssd_mobilenet_v1_fpn.py`. | |||
| 3. If you are running with `ssd_mobilenet_v1_fpn`, you need a pretrained model for `mobilenet_v1`. Set the checkpoint path to `feature_extractor_base_param` in `src/config_ssd_mobilenet_v1_fpn.py`. For more detail about training mobilnet_v1, please refer to the mobilenetv1 model. | |||
| ### Run the scripts | |||
| After installing MindSpore via the official website, you can start training and evaluation as follows: | |||
| - runing on Ascend | |||
| - running on Ascend | |||
| ```shell | |||
| # distributed training on Ascend | |||
| @@ -135,7 +135,7 @@ sh run_distribute_train.sh [DEVICE_NUM] [EPOCH_SIZE] [LR] [DATASET] [RANK_TABLE_ | |||
| sh run_eval.sh [DATASET] [CHECKPOINT_PATH] [DEVICE_ID] | |||
| ``` | |||
| - runing on GPU | |||
| - running on GPU | |||
| ```shell | |||
| # distributed training on GPU | |||
| @@ -145,7 +145,7 @@ sh run_distribute_train_gpu.sh [DEVICE_NUM] [EPOCH_SIZE] [LR] [DATASET] | |||
| sh run_eval_gpu.sh [DATASET] [CHECKPOINT_PATH] [DEVICE_ID] | |||
| ``` | |||
| - runing on CPU(support Windows and Ubuntu) | |||
| - running on CPU(support Windows and Ubuntu) | |||
| **CPU is usually used for fine-tuning, which needs pre_trained checkpoint.** | |||
| @@ -200,15 +200,15 @@ python eval.py --run_platform=CPU --dataset=[DATASET] --checkpoint_path=[PRETRAI | |||
| "pre_trained_epoch_size": 0 # Pretrained epoch size | |||
| "save_checkpoint_epochs": 10 # The epoch interval between two checkpoints. By default, the checkpoint will be saved per 10 epochs | |||
| "loss_scale": 1024 # Loss scale | |||
| "filter_weight": False # Load paramters in head layer or not. If the class numbers of train dataset is different from the class numbers in pre_trained checkpoint, please set True. | |||
| "freeze_layer": "none" # Freeze the backbone paramters or not, support none and backbone. | |||
| "filter_weight": False # Load parameters in head layer or not. If the class numbers of train dataset is different from the class numbers in pre_trained checkpoint, please set True. | |||
| "freeze_layer": "none" # Freeze the backbone parameters or not, support none and backbone. | |||
| "class_num": 81 # Dataset class number | |||
| "image_shape": [300, 300] # Image height and width used as input to the model | |||
| "mindrecord_dir": "/data/MindRecord_COCO" # MindRecord path | |||
| "coco_root": "/data/coco2017" # COCO2017 dataset path | |||
| "voc_root": "/data/voc_dataset" # VOC original dataset path | |||
| "voc_json": "annotations/voc_instances_val.json" # is the path of json file with coco format for evalution | |||
| "voc_json": "annotations/voc_instances_val.json" # is the path of json file with coco format for evaluation | |||
| "image_dir": "" # Other dataset image path, if coco or voc used, it will be useless | |||
| "anno_path": "" # Other dataset annotation path, if coco or voc used, it will be useless | |||
| @@ -304,18 +304,18 @@ We need two parameters for this scripts. | |||
| Inference result will be stored in the example path, whose folder name begins with "eval". Under this, you can find result like the followings in log. | |||
| ```shell | |||
| Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.238 | |||
| Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.400 | |||
| Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.240 | |||
| Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.238 | |||
| Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.400 | |||
| 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.039 | |||
| Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.198 | |||
| Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.438 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.250 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.389 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.424 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.122 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.434 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.697 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.250 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.389 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.424 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.122 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.434 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.697 | |||
| ======================================== | |||
| @@ -339,18 +339,18 @@ We need two parameters for this scripts. | |||
| Inference result will be stored in the example path, whose folder name begins with "eval". Under this, you can find result like the followings in log. | |||
| ```shell | |||
| Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.224 | |||
| Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.375 | |||
| Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.228 | |||
| Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.224 | |||
| Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.375 | |||
| Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.228 | |||
| Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.034 | |||
| Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.189 | |||
| Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.407 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.243 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.382 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.417 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.120 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.425 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.686 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.243 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.382 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.417 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.120 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.425 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.686 | |||
| ======================================== | |||
| @@ -363,7 +363,42 @@ mAP: 0.2244936111705981 | |||
| python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] | |||
| ``` | |||
| The ckpt_file parameter is required. | |||
| The ckpt_file parameter is required, | |||
| `EXPORT_FORMAT` should be in ["AIR", "ONNX", "MINDIR"] | |||
| ## Inference Process | |||
| ### Usage | |||
| Before performing inference, the air file must bu exported by export script on the 910 environment. | |||
| Current batch_Size can only be set to 1. The precision calculation process needs about 70G+ memory space. | |||
| ```shell | |||
| # Ascend310 inference | |||
| sh run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DEVICE_ID] | |||
| ``` | |||
| `DEVICE_ID` is optional, default value is 0. | |||
| ### result | |||
| Inference result is saved in current path, you can find result like this in acc.log file. | |||
| ```bash | |||
| Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.354 | |||
| Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.459 | |||
| Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.432 | |||
| Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.228 | |||
| Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.455 | |||
| Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.604 | |||
| 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.409 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.507 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.325 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.670 | |||
| Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.733 | |||
| mAP: 0.35406563212712244 | |||
| ``` | |||
| ## [Model Description](#contents) | |||
| @@ -0,0 +1,32 @@ | |||
| /** | |||
| * Copyright 2021 Huawei Technologies Co., Ltd | |||
| * | |||
| * Licensed under the Apache License, Version 2.0 (the "License"); | |||
| * you may not use this file except in compliance with the License. | |||
| * You may obtain a copy of the License at | |||
| * | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * | |||
| * Unless required by applicable law or agreed to in writing, software | |||
| * distributed under the License is distributed on an "AS IS" BASIS, | |||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
| * See the License for the specific language governing permissions and | |||
| * limitations under the License. | |||
| */ | |||
| #ifndef MINDSPORE_INFERENCE_UTILS_H_ | |||
| #define MINDSPORE_INFERENCE_UTILS_H_ | |||
| #include <sys/stat.h> | |||
| #include <dirent.h> | |||
| #include <vector> | |||
| #include <string> | |||
| #include <memory> | |||
| #include "include/api/types.h" | |||
| std::vector<std::string> GetAllFiles(std::string_view dirName); | |||
| DIR *OpenDir(std::string_view dirName); | |||
| std::string RealPath(std::string_view path); | |||
| std::shared_ptr<mindspore::api::Tensor> ReadFileToTensor(const std::string &file); | |||
| int WriteResult(const std::string& imageFile, const std::vector<mindspore::api::Buffer> &outputs); | |||
| #endif | |||
| @@ -0,0 +1,14 @@ | |||
| cmake_minimum_required(VERSION 3.14.1) | |||
| project(MindSporeCxxTestcase[CXX]) | |||
| add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) | |||
| set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -std=c++17 -Werror -Wall -fPIE -Wl,--allow-shlib-undefined") | |||
| set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) | |||
| option(MINDSPORE_PATH "mindspore install path" "") | |||
| include_directories(${MINDSPORE_PATH}) | |||
| include_directories(${MINDSPORE_PATH}/include) | |||
| include_directories(${PROJECT_SRC_ROOT}/../inc) | |||
| find_library(MS_LIB libmindspore.so ${MINDSPORE_PATH}/lib) | |||
| file(GLOB_RECURSE MD_LIB ${MINDSPORE_PATH}/_c_dataengine*) | |||
| add_executable(main main.cc utils.cc) | |||
| target_link_libraries(main ${MS_LIB} ${MD_LIB} gflags) | |||
| @@ -0,0 +1,18 @@ | |||
| #!/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. | |||
| # ============================================================================ | |||
| cmake . -DMINDSPORE_PATH="`pip3.7 show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`" | |||
| make | |||
| @@ -0,0 +1,127 @@ | |||
| /** | |||
| * Copyright 2021 Huawei Technologies Co., Ltd | |||
| * | |||
| * Licensed under the Apache License, Version 2.0 (the "License"); | |||
| * you may not use this file except in compliance with the License. | |||
| * You may obtain a copy of the License at | |||
| * | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * | |||
| * Unless required by applicable law or agreed to in writing, software | |||
| * distributed under the License is distributed on an "AS IS" BASIS, | |||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
| * See the License for the specific language governing permissions and | |||
| * limitations under the License. | |||
| */ | |||
| #include <sys/time.h> | |||
| #include <gflags/gflags.h> | |||
| #include <dirent.h> | |||
| #include <iostream> | |||
| #include <string> | |||
| #include <algorithm> | |||
| #include <iosfwd> | |||
| #include <vector> | |||
| #include <fstream> | |||
| #include "include/api/model.h" | |||
| #include "include/api/context.h" | |||
| #include "minddata/dataset/include/minddata_eager.h" | |||
| #include "../inc/utils.h" | |||
| #include "include/api/types.h" | |||
| #include "include/api/serialization.h" | |||
| #include "minddata/dataset/include/vision.h" | |||
| using mindspore::api::Context; | |||
| using mindspore::api::Serialization; | |||
| using mindspore::api::Model; | |||
| using mindspore::api::kModelOptionInsertOpCfgPath; | |||
| using mindspore::api::Status; | |||
| using mindspore::api::MindDataEager; | |||
| using mindspore::api::Buffer; | |||
| using mindspore::api::ModelType; | |||
| using mindspore::api::GraphCell; | |||
| using mindspore::api::SUCCESS; | |||
| using mindspore::dataset::vision::DvppDecodeResizeJpeg; | |||
| DEFINE_string(mindir_path, "", "mindir path"); | |||
| DEFINE_string(dataset_path, ".", "dataset path"); | |||
| DEFINE_int32(device_id, 0, "device id"); | |||
| DEFINE_string(aipp_path, "./aipp.cfg", "aipp path"); | |||
| int main(int argc, char **argv) { | |||
| gflags::ParseCommandLineFlags(&argc, &argv, true); | |||
| if (RealPath(FLAGS_mindir_path).empty()) { | |||
| std::cout << "Invalid mindir" << std::endl; | |||
| return 1; | |||
| } | |||
| if (RealPath(FLAGS_aipp_path).empty()) { | |||
| std::cout << "Invalid aipp path" << std::endl; | |||
| return 1; | |||
| } | |||
| Context::Instance().SetDeviceTarget("Ascend310").SetDeviceID(FLAGS_device_id); | |||
| auto graph = Serialization::LoadModel(FLAGS_mindir_path, ModelType::kMindIR); | |||
| Model model((GraphCell(graph))); | |||
| std::map<std::string, std::string> build_options; | |||
| if (!FLAGS_aipp_path.empty()) { | |||
| build_options.emplace(kModelOptionInsertOpCfgPath, FLAGS_aipp_path); | |||
| } | |||
| Status ret = model.Build(build_options); | |||
| if (ret != SUCCESS) { | |||
| std::cout << "ERROR: Build failed." << std::endl; | |||
| return 1; | |||
| } | |||
| auto all_files = GetAllFiles(FLAGS_dataset_path); | |||
| if (all_files.empty()) { | |||
| std::cout << "ERROR: no input data." << std::endl; | |||
| return 1; | |||
| } | |||
| std::map<double, double> costTime_map; | |||
| size_t size = all_files.size(); | |||
| MindDataEager SingleOp({DvppDecodeResizeJpeg({640, 640})}); | |||
| for (size_t i = 0; i < size; ++i) { | |||
| struct timeval start = {0}; | |||
| struct timeval end = {0}; | |||
| double startTimeMs; | |||
| double endTimeMs; | |||
| std::vector<Buffer> inputs; | |||
| std::vector<Buffer> outputs; | |||
| std::cout << "Start predict input files:" << all_files[i] << std::endl; | |||
| auto imgDvpp = SingleOp(ReadFileToTensor(all_files[i])); | |||
| inputs.emplace_back(imgDvpp->Data(), imgDvpp->DataSize()); | |||
| gettimeofday(&start, nullptr); | |||
| ret = model.Predict(inputs, &outputs); | |||
| gettimeofday(&end, nullptr); | |||
| if (ret != SUCCESS) { | |||
| std::cout << "Predict " << all_files[i] << " failed." << std::endl; | |||
| return 1; | |||
| } | |||
| startTimeMs = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000; | |||
| endTimeMs = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000; | |||
| costTime_map.insert(std::pair<double, double>(startTimeMs, endTimeMs)); | |||
| WriteResult(all_files[i], outputs); | |||
| } | |||
| double average = 0.0; | |||
| int inferCount = 0; | |||
| char tmpCh[256] = {0}; | |||
| for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) { | |||
| double diff = 0.0; | |||
| diff = iter->second - iter->first; | |||
| average += diff; | |||
| inferCount++; | |||
| } | |||
| average = average / inferCount; | |||
| snprintf(tmpCh, sizeof(tmpCh), \ | |||
| "NN inference cost average time: %4.3f ms of infer_count %d \n", average, inferCount); | |||
| std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << inferCount << std::endl; | |||
| std::string fileName = "./time_Result" + std::string("/test_perform_static.txt"); | |||
| std::ofstream fileStream(fileName.c_str(), std::ios::trunc); | |||
| fileStream << tmpCh; | |||
| fileStream.close(); | |||
| costTime_map.clear(); | |||
| return 0; | |||
| } | |||
| @@ -0,0 +1,133 @@ | |||
| /** | |||
| * 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 <fstream> | |||
| #include <algorithm> | |||
| #include <iostream> | |||
| #include "../inc/utils.h" | |||
| using mindspore::api::Tensor; | |||
| using mindspore::api::Buffer; | |||
| using mindspore::api::DataType; | |||
| std::vector<std::string> GetAllFiles(std::string_view dirName) { | |||
| struct dirent *filename; | |||
| DIR *dir = OpenDir(dirName); | |||
| if (dir == nullptr) { | |||
| return {}; | |||
| } | |||
| std::vector<std::string> res; | |||
| while ((filename = readdir(dir)) != nullptr) { | |||
| std::string dName = std::string(filename->d_name); | |||
| if (dName == "." || dName == ".." || filename->d_type != DT_REG) { | |||
| continue; | |||
| } | |||
| res.emplace_back(std::string(dirName) + "/" + filename->d_name); | |||
| } | |||
| std::sort(res.begin(), res.end()); | |||
| for (auto &f : res) { | |||
| std::cout << "image file: " << f << std::endl; | |||
| } | |||
| return res; | |||
| } | |||
| int WriteResult(const std::string& imageFile, const std::vector<Buffer> &outputs) { | |||
| std::string homePath = "./result_Files"; | |||
| for (size_t i = 0; i < outputs.size(); ++i) { | |||
| size_t outputSize; | |||
| const void * netOutput; | |||
| netOutput = outputs[i].Data(); | |||
| outputSize = outputs[i].DataSize(); | |||
| int pos = imageFile.rfind('/'); | |||
| std::string fileName(imageFile, pos + 1); | |||
| fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin"); | |||
| std::string outFileName = homePath + "/" + fileName; | |||
| FILE * outputFile = fopen(outFileName.c_str(), "wb"); | |||
| fwrite(netOutput, outputSize, sizeof(char), outputFile); | |||
| fclose(outputFile); | |||
| outputFile = nullptr; | |||
| } | |||
| return 0; | |||
| } | |||
| std::shared_ptr<Tensor> ReadFileToTensor(const std::string &file) { | |||
| auto buffer = std::make_shared<Tensor>(); | |||
| if (file.empty()) { | |||
| std::cout << "Pointer file is nullptr" << std::endl; | |||
| return buffer; | |||
| } | |||
| std::ifstream ifs(file); | |||
| if (!ifs.good()) { | |||
| std::cout << "File: " << file << " is not exist" << std::endl; | |||
| return buffer; | |||
| } | |||
| if (!ifs.is_open()) { | |||
| std::cout << "File: " << file << "open failed" << std::endl; | |||
| return buffer; | |||
| } | |||
| ifs.seekg(0, std::ios::end); | |||
| size_t size = ifs.tellg(); | |||
| buffer->ResizeData(size); | |||
| if (buffer->DataSize() != size) { | |||
| std::cout << "Malloc buf failed, file: " << file << std::endl; | |||
| ifs.close(); | |||
| return buffer; | |||
| } | |||
| ifs.seekg(0, std::ios::beg); | |||
| ifs.read(reinterpret_cast<char *>(buffer->MutableData()), size); | |||
| ifs.close(); | |||
| buffer->SetDataType(DataType::kMsUint8); | |||
| buffer->SetShape({static_cast<int64_t>(size)}); | |||
| return buffer; | |||
| } | |||
| DIR *OpenDir(std::string_view dirName) { | |||
| if (dirName.empty()) { | |||
| std::cout << " dirName is null ! " << std::endl; | |||
| return nullptr; | |||
| } | |||
| std::string realPath = RealPath(dirName); | |||
| struct stat s; | |||
| lstat(realPath.c_str(), &s); | |||
| if (!S_ISDIR(s.st_mode)) { | |||
| std::cout << "dirName is not a valid directory !" << std::endl; | |||
| return nullptr; | |||
| } | |||
| DIR *dir; | |||
| dir = opendir(realPath.c_str()); | |||
| if (dir == nullptr) { | |||
| std::cout << "Can not open dir " << dirName << std::endl; | |||
| return nullptr; | |||
| } | |||
| std::cout << "Successfully opened the dir " << dirName << std::endl; | |||
| return dir; | |||
| } | |||
| std::string RealPath(std::string_view path) { | |||
| char realPathMem[PATH_MAX] = {0}; | |||
| char *realPathRet = nullptr; | |||
| realPathRet = realpath(path.data(), realPathMem); | |||
| if (realPathRet == nullptr) { | |||
| std::cout << "File: " << path << " is not exist."; | |||
| return ""; | |||
| } | |||
| std::string realPath(realPathMem); | |||
| std::cout << path << " realpath is: " << realPath << std::endl; | |||
| return realPath; | |||
| } | |||
| @@ -0,0 +1,61 @@ | |||
| # 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 | |||
| # | |||
| # less 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 | |||
| from src.config import config | |||
| from src.eval_utils import metrics | |||
| batch_size = 1 | |||
| parser = argparse.ArgumentParser(description="ssd_mobilenet_v1_fpn inference") | |||
| parser.add_argument("--result_path", type=str, required=True, help="result files path.") | |||
| parser.add_argument("--img_path", type=str, required=True, help="image file path.") | |||
| args = parser.parse_args() | |||
| def get_imgSize(file_name): | |||
| img = Image.open(file_name) | |||
| return img.size | |||
| def get_result(result_path, img_id_file_path): | |||
| anno_json = os.path.join(config.coco_root, config.instances_set.format(config.val_data_type)) | |||
| files = os.listdir(img_id_file_path) | |||
| pred_data = [] | |||
| for file in files: | |||
| img_ids_name = file.split('.')[0] | |||
| img_id = int(np.squeeze(img_ids_name)) | |||
| img_size = get_imgSize(os.path.join(img_id_file_path, file)) | |||
| image_shape = np.array([img_size[1], img_size[0]]) | |||
| result_path_0 = os.path.join(result_path, img_ids_name + "_0.bin") | |||
| result_path_1 = os.path.join(result_path, img_ids_name + "_1.bin") | |||
| boxes = np.fromfile(result_path_0, dtype=np.float32).reshape(51150, 4) | |||
| box_scores = np.fromfile(result_path_1, dtype=np.float32).reshape(51150, 81) | |||
| pred_data.append({ | |||
| "boxes": boxes, | |||
| "box_scores": box_scores, | |||
| "img_id": img_id, | |||
| "image_shape": image_shape | |||
| }) | |||
| mAP = metrics(pred_data, anno_json) | |||
| print(f" mAP:{mAP}") | |||
| if __name__ == '__main__': | |||
| get_result(args.result_path, args.img_path) | |||
| @@ -0,0 +1,98 @@ | |||
| #!/bin/bash | |||
| # Copyright 2021 Huawei Technologies Co., Ltd | |||
| # | |||
| # Licensed under the Apache License, Version 2.0 (the "License"); | |||
| # you may not use this file except in compliance with the License. | |||
| # You may obtain a copy of the License at | |||
| # | |||
| # http://www.apache.org/licenses/LICENSE-2.0 | |||
| # | |||
| # Unless required by applicable law or agreed to in writing, software | |||
| # distributed under the License is distributed on an "AS IS" BASIS, | |||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
| # See the License for the specific language governing permissions and | |||
| # limitations under the License. | |||
| # ============================================================================ | |||
| if [[ $# -lt 2 || $# -gt 3 ]]; then | |||
| echo "Usage: sh run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DEVICE_ID] | |||
| DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero" | |||
| exit 1 | |||
| fi | |||
| get_real_path(){ | |||
| if [ "${1:0:1}" == "/" ]; then | |||
| echo "$1" | |||
| else | |||
| echo "$(realpath -m $PWD/$1)" | |||
| fi | |||
| } | |||
| model=$(get_real_path $1) | |||
| data_path=$(get_real_path $2) | |||
| device_id=0 | |||
| if [ $# == 3 ]; then | |||
| device_id=$3 | |||
| fi | |||
| echo "mindir name: "$model | |||
| echo "dataset path: "$data_path | |||
| echo "device id: "$device_id | |||
| export ASCEND_HOME=/usr/local/Ascend/ | |||
| if [ -d ${ASCEND_HOME}/ascend-toolkit ]; then | |||
| export PATH=$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH | |||
| export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/ascend-toolkit/latest/atc/lib64:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH | |||
| export TBE_IMPL_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe | |||
| export PYTHONPATH=${TBE_IMPL_PATH}:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/python/site-packages:$PYTHONPATH | |||
| export ASCEND_OPP_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp | |||
| else | |||
| export PATH=$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH | |||
| export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH | |||
| export PYTHONPATH=$ASCEND_HOME/atc/python/site-packages:$PYTHONPATH | |||
| export ASCEND_OPP_PATH=$ASCEND_HOME/opp | |||
| fi | |||
| function compile_app() | |||
| { | |||
| cd ../ascend310_infer/src | |||
| if [ -f "Makefile" ]; then | |||
| make clean | |||
| fi | |||
| sh build.sh &> build.log | |||
| } | |||
| function infer() | |||
| { | |||
| cd - | |||
| if [ -d result_Files ]; then | |||
| rm -rf ./result_Files | |||
| fi | |||
| if [ -d time_Result ]; then | |||
| rm -rf ./time_Result | |||
| fi | |||
| mkdir result_Files | |||
| mkdir time_Result | |||
| ../ascend310_infer/src/main --mindir_path=$model --dataset_path=$data_path --device_id=$device_id --aipp_path ../src/aipp.cfg &> infer.log | |||
| } | |||
| function cal_acc() | |||
| { | |||
| python3.7 ../postprocess.py --result_path=./result_Files --img_path=$data_path &> acc.log & | |||
| } | |||
| compile_app | |||
| if [ $? -ne 0 ]; then | |||
| echo "compile app code failed" | |||
| exit 1 | |||
| fi | |||
| infer | |||
| if [ $? -ne 0 ]; then | |||
| echo " execute inference failed" | |||
| exit 1 | |||
| fi | |||
| cal_acc | |||
| if [ $? -ne 0 ]; then | |||
| echo "calculate accuracy failed" | |||
| exit 1 | |||
| fi | |||
| @@ -0,0 +1,26 @@ | |||
| aipp_op { | |||
| aipp_mode : static | |||
| input_format : YUV420SP_U8 | |||
| related_input_rank : 0 | |||
| csc_switch : true | |||
| rbuv_swap_switch : false | |||
| matrix_r0c0 : 256 | |||
| matrix_r0c1 : 0 | |||
| matrix_r0c2 : 359 | |||
| matrix_r1c0 : 256 | |||
| matrix_r1c1 : -88 | |||
| matrix_r1c2 : -183 | |||
| matrix_r2c0 : 256 | |||
| matrix_r2c1 : 454 | |||
| matrix_r2c2 : 0 | |||
| input_bias_0 : 0 | |||
| input_bias_1 : 128 | |||
| input_bias_2 : 128 | |||
| mean_chn_0 : 124 | |||
| mean_chn_1 : 117 | |||
| mean_chn_2 : 104 | |||
| var_reci_chn_0 : 0.0171247538316637 | |||
| var_reci_chn_1 : 0.0175070028011204 | |||
| var_reci_chn_2 : 0.0174291938997821 | |||
| } | |||