diff --git a/model_zoo/research/cv/ghostnet/README_CN.md b/model_zoo/research/cv/ghostnet/README_CN.md index 7381175d4b8..dc37f9a456a 100644 --- a/model_zoo/research/cv/ghostnet/README_CN.md +++ b/model_zoo/research/cv/ghostnet/README_CN.md @@ -22,6 +22,7 @@ - [结果](#结果-1) - [推理过程](#推理过程) - [导出MindIR](#导出MindIR) + - [在Ascend310执行推理](#在Ascend310执行推理) - [结果](#结果) - [模型描述](#模型描述) - [性能](#性能) @@ -98,17 +99,32 @@ GhostNet的总体网络架构如下:[链接](https://arxiv.org/pdf/1911.11907. ```text └──ghostnet ├── README.md - ├── scripts - ├── run_distribute_train.sh # 启动Ascend分布式训练(8卡) - ├── run_eval.sh # 启动Ascend评估 - └── run_standalone_train.sh # 启动Ascend单机训练(单卡) + ├── ascend310_infer # ascend310推理 + ├── inc + └── utils.h # ascend310推理 + ├── src + ├── build.sh # ascend310推理 + ├── CMakeLists.txt # ascend310推理 + ├── main.cc # ascend310推理 + └── utils.cc # ascend310推理 + ├── scripts + ├── run_distribute_train.sh # 启动Ascend分布式训练(8卡) + ├── run_eval.sh # 启动Ascend评估 + ├── run_infer_310.sh # 启动Ascend310推理 + └── run_standalone_train.sh # 启动Ascend单机训练(单卡) ├── src ├── config.py # 参数配置 ├── dataset.py # 数据预处理 ├── CrossEntropySmooth.py # ImageNet2012数据集的损失定义 ├── lr_generator.py # 生成每个步骤的学习率 - └── ghostnet.py # ghostnet网络 + ├── ghostnet600.py + ├── launch.py + └── ghostnet.py # ghostnet网络 ├── eval.py # 评估网络 + ├── create_imagenet2012_label.py # 创建ImageNet2012标签 + ├── export.py # 导出MindIR模型 + ├── postprocess.py # 310推理的后期处理 + ├── requirements.txt # 需求文件 └── train.py # 训练网络 ``` @@ -147,10 +163,10 @@ GhostNet的总体网络架构如下:[链接](https://arxiv.org/pdf/1911.11907. ```Shell # 分布式训练 -用法:sh run_distribute_train.sh [RANK_TABLE_FILE] [DATASET_PATH] [PRETRAINED_CKPT_PATH](可选) +用法:sh run_distribute_train.sh [RANK_TABLE_FILE] [DATASET_PATH] [PRETRAINED_CKPT_PATH](可选) # 单机训练 -用法:sh run_standalone_train.sh [DATASET_PATH] [PRETRAINED_CKPT_PATH](可选) +用法:sh run_standalone_train.sh [DATASET_PATH] [PRETRAINED_CKPT_PATH](可选) ``` @@ -219,9 +235,25 @@ python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [ 参数ckpt_file为必填项, `EXPORT_FORMAT` 必须在 ["AIR", "MINDIR"]中选择。 +## 在Ascend310执行推理 + +在执行推理前, mindir文件必须通过export.py脚本导出。以下展示了使用mindir模型执行推理的示例。目前仅支持batch_Size为1的推理。 + +```shell +bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [DEVICE_ID] +``` + +- DEVICE_ID 可选,默认值为0。 + ## 结果 -导出“.mindir”文件可在当前目录查看 +推理结果保存在脚本执行的当前路径, 你可以在acc.log中看到以下精度计算结果。 + +- 使用ImageNet2012数据集评估ghostnet + +```shell +Total data: 50000, top1 accuracy: 0.73816, top5 accuracy: 0.9178. +``` # 模型描述 @@ -253,4 +285,4 @@ dataset.py中设置了“create_dataset”函数内的种子,同时还使用 # ModelZoo主页 -请浏览官网[主页](https://gitee.com/mindspore/mindspore/tree/master/model_zoo)。 \ No newline at end of file +请浏览官网[主页](https://gitee.com/mindspore/mindspore/tree/r1.3/model_zoo)。 \ No newline at end of file diff --git a/model_zoo/research/cv/ghostnet/ascend310_infer/inc/utils.h b/model_zoo/research/cv/ghostnet/ascend310_infer/inc/utils.h new file mode 100644 index 00000000000..0b400632f51 --- /dev/null +++ b/model_zoo/research/cv/ghostnet/ascend310_infer/inc/utils.h @@ -0,0 +1,33 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MINDSPORE_INFERENCE_UTILS_H_ +#define MINDSPORE_INFERENCE_UTILS_H_ + +#include +#include +#include +#include +#include +#include "include/api/types.h" + +DIR *OpenDir(std::string_view dirName); +std::string RealPath(std::string_view path); +mindspore::MSTensor ReadFileToTensor(const std::string &file); +int WriteResult(const std::string& imageFile, const std::vector &outputs); +std::vector GetAllFiles(std::string dir_name); + +#endif diff --git a/model_zoo/research/cv/ghostnet/ascend310_infer/src/CMakeLists.txt b/model_zoo/research/cv/ghostnet/ascend310_infer/src/CMakeLists.txt new file mode 100644 index 00000000000..0397995b0e0 --- /dev/null +++ b/model_zoo/research/cv/ghostnet/ascend310_infer/src/CMakeLists.txt @@ -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}/../) +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) diff --git a/model_zoo/research/cv/ghostnet/ascend310_infer/src/build.sh b/model_zoo/research/cv/ghostnet/ascend310_infer/src/build.sh new file mode 100644 index 00000000000..2e25c8650d0 --- /dev/null +++ b/model_zoo/research/cv/ghostnet/ascend310_infer/src/build.sh @@ -0,0 +1,19 @@ +#!/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. +# ============================================================================ + +# shellcheck disable=SC2006 +cmake . -DMINDSPORE_PATH="`pip3.7 show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`" +make diff --git a/model_zoo/research/cv/ghostnet/ascend310_infer/src/main.cc b/model_zoo/research/cv/ghostnet/ascend310_infer/src/main.cc new file mode 100644 index 00000000000..ee262256f41 --- /dev/null +++ b/model_zoo/research/cv/ghostnet/ascend310_infer/src/main.cc @@ -0,0 +1,141 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "include/api/model.h" +#include "include/api/context.h" +#include "include/api/types.h" +#include "include/api/serialization.h" +#include "include/dataset/vision_ascend.h" +#include "include/dataset/execute.h" +#include "include/dataset/transforms.h" +#include "include/dataset/vision.h" +#include "inc/utils.h" + +using mindspore::dataset::vision::Decode; +using mindspore::dataset::vision::Resize; +using mindspore::dataset::vision::CenterCrop; +using mindspore::dataset::vision::Normalize; +using mindspore::dataset::vision::HWC2CHW; +using mindspore::dataset::TensorTransform; +using mindspore::Context; +using mindspore::Serialization; +using mindspore::Model; +using mindspore::Status; +using mindspore::ModelType; +using mindspore::GraphCell; +using mindspore::kSuccess; +using mindspore::MSTensor; +using mindspore::dataset::Execute; + +DEFINE_string(mindir_path, "", "mindir path"); +DEFINE_string(dataset_path, ".", "dataset path"); +DEFINE_int32(device_id, 0, "device id"); + +int main(int argc, char **argv) { + gflags::ParseCommandLineFlags(&argc, &argv, true); + if (RealPath(FLAGS_mindir_path).empty()) { + std::cout << "Invalid mindir" << std::endl; + return 1; + } + + auto context = std::make_shared(); + auto ascend310 = std::make_shared(); + ascend310->SetDeviceID(FLAGS_device_id); + context->MutableDeviceInfo().push_back(ascend310); + mindspore::Graph graph; + Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph); + Model model; + Status ret = model.Build(GraphCell(graph), context); + if (ret != kSuccess) { + std::cout << "ERROR: Build failed." << std::endl; + return 1; + } + + auto all_files = GetAllFiles(FLAGS_dataset_path); + if (all_files.empty()) { + std::cout << "ERROR: no input data." << std::endl; + return 1; + } + + std::vector modelInputs = model.GetInputs(); + std::map costTime_map; + size_t size = all_files.size(); + + std::shared_ptr decode = std::make_shared(); + std::shared_ptr resize = std::make_shared(std::vector{256}); + std::shared_ptr centercrop = std::make_shared(std::vector{224}); + std::shared_ptr normalize = std::make_shared( + std::vector{123.675, 116.28, 103.53}, std::vector{58.395, 57.12, 57.375}); + std::shared_ptr hwc2chw = std::make_shared(); + + std::vector> trans_list; + trans_list = {decode, resize, centercrop, normalize, hwc2chw}; + mindspore::dataset::Execute SingleOp(trans_list); + + for (size_t i = 0; i < size; ++i) { + struct timeval start = {0}; + struct timeval end = {0}; + double startTimeMs; + double endTimeMs; + std::vector inputs; + std::vector outputs; + std::cout << "Start predict input files:" << all_files[i] <(startTimeMs, endTimeMs)); + WriteResult(all_files[i], outputs); + } + double average = 0.0; + int inferCount = 0; + + for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) { + average += iter->second - iter->first; + inferCount++; + } + average = average / inferCount; + std::stringstream timeCost; + timeCost << "NN inference cost average time: "<< average << " ms of infer_count " << inferCount << std::endl; + std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << inferCount << std::endl; + std::string fileName = "./time_Result" + std::string("/test_perform_static.txt"); + std::ofstream fileStream(fileName.c_str(), std::ios::trunc); + fileStream << timeCost.str(); + fileStream.close(); + costTime_map.clear(); + return 0; +} diff --git a/model_zoo/research/cv/ghostnet/ascend310_infer/src/utils.cc b/model_zoo/research/cv/ghostnet/ascend310_infer/src/utils.cc new file mode 100644 index 00000000000..098a654bd7d --- /dev/null +++ b/model_zoo/research/cv/ghostnet/ascend310_infer/src/utils.cc @@ -0,0 +1,145 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include "inc/utils.h" + +using mindspore::MSTensor; +using mindspore::DataType; + +std::vector GetAllFiles(std::string dirName) { + struct dirent *filename; + DIR *dir = OpenDir(dirName); + if (dir == nullptr) { + return {}; + } + std::vector dirs; + std::vector files; + while ((filename = readdir(dir)) != nullptr) { + std::string dName = std::string(filename->d_name); + if (dName == "." || dName == "..") { + continue; + } else if (filename->d_type == DT_DIR) { + dirs.emplace_back(std::string(dirName) + "/" + filename->d_name); + } else if (filename->d_type == DT_REG) { + files.emplace_back(std::string(dirName) + "/" + filename->d_name); + } else { + continue; + } + } + + for (auto d : dirs) { + dir = OpenDir(d); + while ((filename = readdir(dir)) != nullptr) { + std::string dName = std::string(filename->d_name); + if (dName == "." || dName == ".." || filename->d_type != DT_REG) { + continue; + } + files.emplace_back(std::string(d) + "/" + filename->d_name); + } + } + std::sort(files.begin(), files.end()); + for (auto &f : files) { + std::cout << "image file: " << f << std::endl; + } + return files; +} + +int WriteResult(const std::string& imageFile, const std::vector &outputs) { + std::string homePath = "./result_Files"; + for (size_t i = 0; i < outputs.size(); ++i) { + size_t outputSize; + std::shared_ptr netOutput; + netOutput = outputs[i].Data(); + outputSize = outputs[i].DataSize(); + int pos = imageFile.rfind('/'); + std::string fileName(imageFile, pos + 1); + fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin"); + std::string outFileName = homePath + "/" + fileName; + FILE *outputFile = fopen(outFileName.c_str(), "wb"); + fwrite(netOutput.get(), outputSize, sizeof(char), outputFile); + fclose(outputFile); + outputFile = nullptr; + } + return 0; +} + +mindspore::MSTensor ReadFileToTensor(const std::string &file) { + if (file.empty()) { + std::cout << "Pointer file is nullptr" << std::endl; + return mindspore::MSTensor(); + } + + std::ifstream ifs(file); + if (!ifs.good()) { + std::cout << "File: " << file << " is not exist" << std::endl; + return mindspore::MSTensor(); + } + + if (!ifs.is_open()) { + std::cout << "File: " << file << "open failed" << std::endl; + return mindspore::MSTensor(); + } + + ifs.seekg(0, std::ios::end); + size_t size = ifs.tellg(); + mindspore::MSTensor buffer(file, mindspore::DataType::kNumberTypeUInt8, {static_cast(size)}, nullptr, size); + + ifs.seekg(0, std::ios::beg); + ifs.read(reinterpret_cast(buffer.MutableData()), size); + ifs.close(); + + return buffer; +} + + +DIR *OpenDir(std::string_view dirName) { + if (dirName.empty()) { + std::cout << " dirName is null ! " << std::endl; + return nullptr; + } + std::string realPath = RealPath(dirName); + struct stat s; + lstat(realPath.c_str(), &s); + if (!S_ISDIR(s.st_mode)) { + std::cout << "dirName is not a valid directory !" << std::endl; + return nullptr; + } + DIR *dir; + dir = opendir(realPath.c_str()); + if (dir == nullptr) { + std::cout << "Can not open dir " << dirName << std::endl; + return nullptr; + } + std::cout << "Successfully opened the dir " << dirName << std::endl; + return dir; +} + +std::string RealPath(std::string_view path) { + char realPathMem[PATH_MAX] = {0}; + char *realPathRet = nullptr; + realPathRet = realpath(path.data(), realPathMem); + if (realPathRet == nullptr) { + std::cout << "File: " << path << " is not exist."; + return ""; + } + + std::string realPath(realPathMem); + std::cout << path << " realpath is: " << realPath << std::endl; + return realPath; +} diff --git a/model_zoo/research/cv/ghostnet/create_imagenet2012_label.py b/model_zoo/research/cv/ghostnet/create_imagenet2012_label.py new file mode 100644 index 00000000000..5e048aa76ed --- /dev/null +++ b/model_zoo/research/cv/ghostnet/create_imagenet2012_label.py @@ -0,0 +1,56 @@ +# 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. +# ============================================================================ +"""create_imagenet2012_label""" +import os +import json +import argparse + +parser = argparse.ArgumentParser(description="ghostnet imagenet2012 label") +parser.add_argument("--img_path", type=str, required=True, help="imagenet2012 file path.") +args = parser.parse_args() + + +def create_label(file_path): + """ + create_imagenet2012_label + Args: + file_path: + + Returns: + + """ + print("[WARNING] Create imagenet label. Currently only use for Imagenet2012!") + dirs = os.listdir(file_path) + file_list = [] + for file in dirs: + file_list.append(file) + file_list = sorted(file_list) + + total = 0 + img_label = {} + for i, file_dir in enumerate(file_list): + files = os.listdir(os.path.join(file_path, file_dir)) + for f in files: + img_label[f] = i + total += len(files) + + with open("imagenet_label.json", "w+") as label: + json.dump(img_label, label) + + print("[INFO] Completed! Total {} data.".format(total)) + + +if __name__ == '__main__': + create_label(args.img_path) diff --git a/model_zoo/research/cv/ghostnet/export.py b/model_zoo/research/cv/ghostnet/export.py index 8b195956505..07456147664 100644 --- a/model_zoo/research/cv/ghostnet/export.py +++ b/model_zoo/research/cv/ghostnet/export.py @@ -40,4 +40,5 @@ if __name__ == '__main__': net.set_train(False) input_data = Tensor(np.zeros([1, 3, 224, 224]), ms.float32) + print(input_data.shape) export(net, input_data, file_name='ghost', file_format=args.file_format) diff --git a/model_zoo/research/cv/ghostnet/postprocess.py b/model_zoo/research/cv/ghostnet/postprocess.py new file mode 100644 index 00000000000..0f087ab8bd5 --- /dev/null +++ b/model_zoo/research/cv/ghostnet/postprocess.py @@ -0,0 +1,77 @@ +# 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 json +import argparse +import numpy as np + +batch_size = 1 +parser = argparse.ArgumentParser(description="ghostnet inference") +parser.add_argument("--result_path", type=str, required=True, help="result files path.") +parser.add_argument("--label_path", type=str, required=True, help="image file path.") +args = parser.parse_args() + + +def get_top5_acc(top5_arg, gt_class): + """ + get top5 accuracy + Args: + top5_arg: + gt_class: + + Returns: + + """ + sub_count = 0 + for top5, gt in zip(top5_arg, gt_class): + if gt in top5: + sub_count += 1 + return sub_count + + +def cal_acc_imagenet(result_path, label_path): + """ + top1 accuracy, top5 accuracy + Args: + result_path: + label_path: + + Returns: + + """ + files = os.listdir(result_path) + with open(label_path, "r") as label: + labels = json.load(label) + result_shape = (1, 1000) + top1 = 0 + top5 = 0 + total_data = len(files) + for file in files: + img_ids_name = file.split('_0.')[0] + data_path = os.path.join(result_path, img_ids_name + "_0.bin") + result = np.fromfile(data_path, dtype=np.float32).reshape(result_shape) + for batch in range(batch_size): + predict = np.argsort(-result[batch], axis=-1) + if labels[img_ids_name+".JPEG"] == predict[0]: + top1 += 1 + if labels[img_ids_name+".JPEG"] in predict[:5]: + top5 += 1 + print(f"Total data: {total_data}, top1 accuracy: {top1/total_data}, top5 accuracy: {top5/total_data}.") + + +if __name__ == '__main__': + + cal_acc_imagenet(args.result_path, args.label_path) diff --git a/model_zoo/research/cv/ghostnet/scripts/run_infer_310.sh b/model_zoo/research/cv/ghostnet/scripts/run_infer_310.sh new file mode 100644 index 00000000000..a04fb116ae0 --- /dev/null +++ b/model_zoo/research/cv/ghostnet/scripts/run_infer_310.sh @@ -0,0 +1,101 @@ +#!/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. +# ============================================================================ + +# shellcheck disable=SC1073 +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/ || exit + if [ -f "Makefile" ]; then + make clean + fi + sh build.sh &> build.log +} + +function infer() { + cd - || exit + if [ -d result_Files ]; then + rm -rf ./result_Files + fi + if [ -d time_Result ]; then + rm -rf ./time_Result + fi + mkdir result_Files + mkdir time_Result + ../ascend310_infer/src/main --mindir_path=$model --dataset_path=$data_path --device_id=$device_id &> infer.log +} + +function cal_acc() +{ + python3.7 ../create_imagenet2012_label.py --img_path=$data_path + python3.7 ../postprocess.py --result_path=./result_Files --label_path=./imagenet_label.json &> 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 diff --git a/model_zoo/research/cv/ghostnet/src/dataset.py b/model_zoo/research/cv/ghostnet/src/dataset.py index 53188620c2c..05acf36e1d5 100644 --- a/model_zoo/research/cv/ghostnet/src/dataset.py +++ b/model_zoo/research/cv/ghostnet/src/dataset.py @@ -14,19 +14,20 @@ # ============================================================================ """Data operations, will be used in train.py and eval.py""" import os -from src.config import config import mindspore.common.dtype as mstype import mindspore.dataset.engine as de import mindspore.dataset.transforms.c_transforms as C2 import mindspore.dataset.vision.c_transforms as C -from mindspore.communication.management import get_rank, get_group_size -def create_dataset(dataset_path, do_train, target="Ascend"): +def create_dataset(dataset_path, do_train, repeat_num=1, infer_910=True, device_id=0, batch_size=128): """ create a train or eval dataset Args: + batch_size: + device_id: + infer_910: dataset_path(string): the path of dataset. do_train(bool): whether dataset is used for train or eval. rank (int): The shard ID within num_shards (default=None). @@ -36,12 +37,16 @@ def create_dataset(dataset_path, do_train, target="Ascend"): Returns: dataset """ + device_num = 1 + device_id = device_id + if infer_910: + device_id = int(os.getenv('DEVICE_ID')) + device_num = int(os.getenv('RANK_SIZE')) + if not do_train: dataset_path = os.path.join(dataset_path, 'val') else: dataset_path = os.path.join(dataset_path, 'train') - if target == "Ascend": - device_num, rank_id = _get_rank_info() if device_num == 1: ds = de.ImageFolderDataset(dataset_path, num_parallel_workers=8, shuffle=True) @@ -74,21 +79,5 @@ def create_dataset(dataset_path, do_train, target="Ascend"): ds = ds.map(input_columns="label", operations=type_cast_op, num_parallel_workers=8) # apply batch operations - ds = ds.batch(config.batch_size, drop_remainder=True) + ds = ds.batch(batch_size, drop_remainder=True) return ds - - -def _get_rank_info(): - """ - get rank size and rank id - """ - rank_size = int(os.environ.get("RANK_SIZE", 1)) - - if rank_size > 1: - rank_size = get_group_size() - rank_id = get_rank() - else: - rank_size = 1 - rank_id = 0 - - return rank_size, rank_id diff --git a/model_zoo/research/cv/ghostnet/src/lr_generator.py b/model_zoo/research/cv/ghostnet/src/lr_generator.py index 3ba8c8cca73..95c1dd5b191 100644 --- a/model_zoo/research/cv/ghostnet/src/lr_generator.py +++ b/model_zoo/research/cv/ghostnet/src/lr_generator.py @@ -16,12 +16,12 @@ import math import numpy as np + def get_lr(lr_init, lr_end, lr_max, warmup_epochs, total_epochs, steps_per_epoch): """ generate learning rate array Args: - global_step(int): total steps of the training lr_init(float): init learning rate lr_end(float): end learning rate lr_max(float): max learning rate