diff --git a/research/cv/UNet3+/README_CN.md b/research/cv/UNet3+/README_CN.md index 6aaebaa049ae440a2b08d845ca84c55036288dba..c9504527c8d02fde954ab5bd746bf68a2cbfa040 100644 --- a/research/cv/UNet3+/README_CN.md +++ b/research/cv/UNet3+/README_CN.md @@ -15,6 +15,10 @@ - [璁粌](#璁粌) - [璇勪及杩囩▼](#璇勪及杩囩▼) - [璇勪及](#璇勪及) + - [Mindir鎺ㄧ悊](#Mindir鎺ㄧ悊) + - [瀵煎嚭妯″瀷](#瀵煎嚭妯″瀷) + - [鍦ˋscend310鎵ц鎺ㄧ悊](#鍦ˋscend310鎵ц鎺ㄧ悊) + - [缁撴灉](#缁撴灉) - [妯″瀷鎻忚堪](#妯″瀷鎻忚堪) - [鎬ц兘](#鎬ц兘) - [璇勪及鎬ц兘](#璇勪及鎬ц兘) @@ -90,10 +94,19 @@ Ascend璁粌锛氱敓鎴怺RANK_TABLE_FILE](https://gitee.com/mindspore/models/tree/m 鈹溾攢鈹€ README.md // 鎵€鏈夋ā鍨嬬殑璇存槑鏂囦欢 鈹溾攢鈹€ UNet3+ 鈹溾攢鈹€ README_CN.md // UNet3+ 鐨勮鏄庢枃浠� + 鈹溾攢鈹€ ascend310_infer // 310鎺ㄧ悊涓讳唬鐮佹枃浠跺す + | 鈹溾攢鈹€ CMakeLists.txt // CMake璁剧疆鏂囦欢 + | 鈹溾攢鈹€ build.sh // 缂栬瘧鍚姩鑴氭湰 + | 鈹溾攢鈹€ inc + | 鈹溾攢鈹€ utils.h // 宸ュ叿绫诲ご鏂囦欢 + | 鈹溾攢鈹€ src + | 鈹溾攢鈹€ main.cc // 鎺ㄧ悊浠g爜鏂囦欢 + | 鈹溾攢鈹€ utils.cc // 宸ュ叿绫绘枃浠� 鈹溾攢鈹€ scripts 鈹� 鈹溾攢鈹€run_distribute_train.sh // Ascend 8鍗¤缁冭剼鏈� 鈹� 鈹溾攢鈹€run_eval.sh // 鎺ㄧ悊鍚姩鑴氭湰 鈹� 鈹溾攢鈹€run_train.sh // 璁粌鍚姩鑴氭湰 + | 鈹溾攢鈹€run_infer_310.sh // 310鎺ㄧ悊鍚姩鑴氭湰 鈹溾攢鈹€ src 鈹� 鈹溾攢鈹€config.py // 閰嶇疆鍔犺浇鏂囦欢 鈹� 鈹溾攢鈹€dataset.py // 鏁版嵁闆嗗鐞� @@ -105,6 +118,9 @@ Ascend璁粌锛氱敓鎴怺RANK_TABLE_FILE](https://gitee.com/mindspore/models/tree/m 鈹溾攢鈹€ eval.py // 鎺ㄧ悊鑴氭湰 鈹溾攢鈹€ export.py // 灏嗘潈閲嶆枃浠跺鍑轰负 MINDIR 绛夋牸寮忕殑鑴氭湰 鈹溾攢鈹€ dataset_preprocess.py // 鏁版嵁闆嗛澶勭悊鑴氭湰 + 鈹溾攢鈹€ postprocess.py // 310绮惧害璁$畻鑴氭湰 + 鈹溾攢鈹€ preprocess.py // 310棰勫鐞嗚剼鏈� + ``` ### 鑴氭湰鍙傛暟 @@ -213,6 +229,34 @@ Ascend璁粌锛氱敓鎴怺RANK_TABLE_FILE](https://gitee.com/mindspore/models/tree/m 杩愯瀹屾垚鍚庯紝鎮ㄥ彲浠ュ湪 output_path 鎸囧畾鐨勭洰褰曚笅鎵惧埌鎺ㄧ悊杩愯鏃ュ織銆� +## Mindir鎺ㄧ悊 + +### [瀵煎嚭MindIR](#contents) + +```shell +python export.py --ckpt_file [CKPT_PATH] --file_name [FILE_NAME] --file_format [FILE_FORMAT] +``` + +- `ckpt_file`涓哄繀濉」銆� +- `file_format` 蹇呴』鍦� ["AIR", "MINDIR"]涓€夋嫨銆� + +### 鍦ˋscend310鎵ц鎺ㄧ悊 + +鍦ㄦ墽琛屾帹鐞嗗墠锛宮indir鏂囦欢蹇呴』閫氳繃`export.py`鑴氭湰瀵煎嚭銆備互涓嬪睍绀轰簡浣跨敤mindir妯″瀷鎵ц鎺ㄧ悊鐨勭ず渚嬨€� + +```shell +# Ascend310 inference +bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [NEED_PREPROCESS] [DEVICE_ID] +``` + +- `DATA_PATH` 涓烘祴璇曢泦鎵€鍦ㄧ殑璺緞銆� +- `NEED_PREPROCESS` 琛ㄧず鏁版嵁鏄惁闇€瑕侀澶勭悊锛屽彇鍊艰寖鍥翠负 'y' 鎴栬€� 'n'銆� +- `DEVICE_ID` 鍙€夛紝榛樿鍊间负0銆� + +### 缁撴灉 + +鎺ㄧ悊缁撴灉淇濆瓨鍦ㄨ剼鏈墽琛岀殑褰撳墠璺緞锛屼綘鍙互鍦╝cc.log涓湅鍒颁互涓嬬簿搴﹁绠楃粨鏋溿€� + ## 妯″瀷鎻忚堪 ### 鎬ц兘 diff --git a/research/cv/UNet3+/ascend310_infer/CMakeLists.txt b/research/cv/UNet3+/ascend310_infer/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..cf49a4e9f2554b51b8866ace8dcdc4f03b0495aa --- /dev/null +++ b/research/cv/UNet3+/ascend310_infer/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.14.1) +project(Ascend310Infer) +add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -std=c++17 -Werror -Wall -fPIE -Wl,--allow-shlib-undefined") +set(PROJECT_SRC_ROOT ${CMAKE_CURRENT_LIST_DIR}/) +option(MINDSPORE_PATH "mindspore install path" "") +include_directories(${MINDSPORE_PATH}) +include_directories(${MINDSPORE_PATH}/include) +include_directories(${PROJECT_SRC_ROOT}) +find_library(MS_LIB libmindspore.so ${MINDSPORE_PATH}/lib) +file(GLOB_RECURSE MD_LIB ${MINDSPORE_PATH}/_c_dataengine*) + +add_executable(main src/main.cc src/utils.cc) +target_link_libraries(main ${MS_LIB} ${MD_LIB} gflags) + diff --git a/research/cv/UNet3+/ascend310_infer/build.sh b/research/cv/UNet3+/ascend310_infer/build.sh new file mode 100644 index 0000000000000000000000000000000000000000..d95d735d4d4fb2bfed3cd7f3df1beae6676ddf4d --- /dev/null +++ b/research/cv/UNet3+/ascend310_infer/build.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ + +if [ ! -d out ]; then + mkdir out +fi +cd out || exit +cmake .. \ + -DMINDSPORE_PATH="`pip show mindspore-ascend | grep Location | awk '{print $2"/mindspore"}' | xargs realpath`" +make + diff --git a/research/cv/UNet3+/ascend310_infer/inc/utils.h b/research/cv/UNet3+/ascend310_infer/inc/utils.h new file mode 100644 index 0000000000000000000000000000000000000000..0b400632f51ee34707a5becc00f7f5ba05899b3a --- /dev/null +++ b/research/cv/UNet3+/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 <sys/stat.h> +#include <dirent.h> +#include <vector> +#include <string> +#include <memory> +#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<mindspore::MSTensor> &outputs); +std::vector<std::string> GetAllFiles(std::string dir_name); + +#endif diff --git a/research/cv/UNet3+/ascend310_infer/src/main.cc b/research/cv/UNet3+/ascend310_infer/src/main.cc new file mode 100644 index 0000000000000000000000000000000000000000..fc0bb156a29f90e84c2a19528ca20ea33ff4f06b --- /dev/null +++ b/research/cv/UNet3+/ascend310_infer/src/main.cc @@ -0,0 +1,129 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include <sys/time.h> +#include <gflags/gflags.h> +#include <dirent.h> +#include <iostream> +#include <string> +#include <algorithm> +#include <iosfwd> +#include <vector> +#include <fstream> +#include <sstream> + +#include "include/api/model.h" +#include "include/api/context.h" +#include "include/api/types.h" +#include "include/api/serialization.h" +#include "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<Context>(); + auto ascend310 = std::make_shared<mindspore::Ascend310DeviceInfo>(); + ascend310->SetDeviceID(FLAGS_device_id); + context->MutableDeviceInfo().push_back(ascend310); + mindspore::Graph graph; + Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph); + Model model; + Status ret = model.Build(GraphCell(graph), context); + if (ret != kSuccess) { + std::cout << "ERROR: Build failed." << std::endl; + return 1; + } + + auto all_files = GetAllFiles(FLAGS_dataset_path); + if (all_files.empty()) { + std::cout << "ERROR: no input data." << std::endl; + return 1; + } + + std::vector<MSTensor> modelInputs = model.GetInputs(); + std::map<double, double> costTime_map; + size_t size = all_files.size(); + + for (size_t i = 0; i < size; ++i) { + struct timeval start = {0}; + struct timeval end = {0}; + double startTimeMs; + double endTimeMs; + std::vector<MSTensor> inputs; + std::vector<MSTensor> outputs; + std::cout << "Start predict input files:" << all_files[i] <<std::endl; + + MSTensor image = ReadFileToTensor(all_files[i]); + + inputs.emplace_back(modelInputs[0].Name(), modelInputs[0].DataType(), modelInputs[0].Shape(), + image.Data().get(), image.DataSize()); + gettimeofday(&start, nullptr); + ret = model.Predict(inputs, &outputs); + gettimeofday(&end, nullptr); + if (ret != kSuccess) { + 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; + + 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/research/cv/UNet3+/ascend310_infer/src/utils.cc b/research/cv/UNet3+/ascend310_infer/src/utils.cc new file mode 100644 index 0000000000000000000000000000000000000000..728d57d93624e69f25fc0cd9a3c45afe80dab858 --- /dev/null +++ b/research/cv/UNet3+/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 <fstream> +#include <algorithm> +#include <iostream> +#include "inc/utils.h" + +using mindspore::MSTensor; +using mindspore::DataType; + +std::vector<std::string> GetAllFiles(std::string dirName) { + struct dirent *filename; + DIR *dir = OpenDir(dirName); + if (dir == nullptr) { + return {}; + } + std::vector<std::string> dirs; + std::vector<std::string> 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<MSTensor> &outputs) { + std::string homePath = "./result_Files"; + for (size_t i = 0; i < outputs.size(); ++i) { + size_t outputSize; + std::shared_ptr<const void> netOutput; + netOutput = outputs[i].Data(); + outputSize = outputs[i].DataSize(); + int pos = imageFile.rfind('/'); + std::string fileName(imageFile, pos + 1); + fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin"); + std::string outFileName = homePath + "/" + fileName; + FILE *outputFile = fopen(outFileName.c_str(), "wb"); + fwrite(netOutput.get(), outputSize, sizeof(char), outputFile); + fclose(outputFile); + outputFile = nullptr; + } + return 0; +} + +mindspore::MSTensor ReadFileToTensor(const std::string &file) { + if (file.empty()) { + std::cout << "Pointer file is nullptr" << std::endl; + return mindspore::MSTensor(); + } + + std::ifstream ifs(file); + if (!ifs.good()) { + std::cout << "File: " << file << " is not exist" << std::endl; + return mindspore::MSTensor(); + } + + if (!ifs.is_open()) { + std::cout << "File: " << file << "open failed" << std::endl; + return mindspore::MSTensor(); + } + + ifs.seekg(0, std::ios::end); + size_t size = ifs.tellg(); + mindspore::MSTensor buffer(file, mindspore::DataType::kNumberTypeUInt8, {static_cast<int64_t>(size)}, nullptr, size); + + ifs.seekg(0, std::ios::beg); + ifs.read(reinterpret_cast<char *>(buffer.MutableData()), size); + ifs.close(); + + return buffer; +} + + +DIR *OpenDir(std::string_view dirName) { + if (dirName.empty()) { + std::cout << " dirName is null ! " << std::endl; + return nullptr; + } + std::string realPath = RealPath(dirName); + struct stat s; + lstat(realPath.c_str(), &s); + if (!S_ISDIR(s.st_mode)) { + std::cout << "dirName is not a valid directory !" << std::endl; + return nullptr; + } + DIR *dir; + dir = opendir(realPath.c_str()); + if (dir == nullptr) { + std::cout << "Can not open dir " << dirName << std::endl; + return nullptr; + } + std::cout << "Successfully opened the dir " << dirName << std::endl; + return dir; +} + +std::string RealPath(std::string_view path) { + char realPathMem[PATH_MAX] = {0}; + char *realPathRet = nullptr; + realPathRet = realpath(path.data(), realPathMem); + if (realPathRet == nullptr) { + std::cout << "File: " << path << " is not exist."; + return ""; + } + + std::string realPath(realPathMem); + std::cout << path << " realpath is: " << realPath << std::endl; + return realPath; +} diff --git a/research/cv/UNet3+/default_config.yaml b/research/cv/UNet3+/default_config.yaml index 5e1ded6c29d0d93237c52990d67485c667e7a63a..77b750c18574647526469d6383a37dc90a2960aa 100644 --- a/research/cv/UNet3+/default_config.yaml +++ b/research/cv/UNet3+/default_config.yaml @@ -34,12 +34,17 @@ pretrain_path: "./" ckpt_name: "best_map.ckpt" # export settings stand alone, other hyperparameters are shared with training -export_batch_size: 1 +export_batch_size: 2 image_height: 512 image_width: 512 ckpt_file: "./best_map.ckpt" file_name: "unet3plus" -file_format: "AIR" +file_format: "MINDIR" + +#310 infer +pre_result_path: "./preprocess_Result" +post_result_path: "./result_Files" +label_path: "./preprocess_Result/label.npy" # dataset preprocess settings source_path: "../origin_dataset/train/" diff --git a/research/cv/UNet3+/postprocess.py b/research/cv/UNet3+/postprocess.py new file mode 100644 index 0000000000000000000000000000000000000000..9ba51c2e0e283e0f499bc7f57c36bcc408a790de --- /dev/null +++ b/research/cv/UNet3+/postprocess.py @@ -0,0 +1,44 @@ +# 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''' +import os +import numpy as np + +from src.config import config as cfg +from eval import dice_coef, AverageMeter + +def sigmoid(z): + '''define sigmoid''' + return 1/(1 + np.exp(-z)) + +def cal_acc(): + '''calculate accuracy''' + all_mask = np.load(cfg.label_path) + file_num = len(os.listdir(cfg.post_result_path)) + bs = cfg.export_batch_size + dices = AverageMeter() + + for i in range(file_num): + file_name = "unet3p_bs" + str(bs) + "_" + str(i) + "_0.bin" + mask = all_mask[i] + result_f = np.fromfile(os.path.join(cfg.post_result_path, file_name), np.float32) + result_f = result_f.reshape(bs, 1, cfg.image_height, cfg.image_width) + output = sigmoid(result_f) + dice = dice_coef(output, mask) + dices.update(dice, bs) + print("Final dices: ", str(dices.avg)) + +if __name__ == '__main__': + cal_acc() diff --git a/research/cv/UNet3+/preprocess.py b/research/cv/UNet3+/preprocess.py new file mode 100644 index 0000000000000000000000000000000000000000..0042e2396fd2a4fed32e2d4765c4d26e5f7c1c7e --- /dev/null +++ b/research/cv/UNet3+/preprocess.py @@ -0,0 +1,39 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================ +'''preprocess''' +import os +import numpy as np +from src.dataset import create_Dataset +from src.config import config as cfg + +def gen_bin(): + '''generate binary files''' + os.makedirs(os.path.join(cfg.pre_result_path, "image"), exist_ok=True) + + val_dataset, _ = create_Dataset(cfg.val_data_path, 0, cfg.export_batch_size,\ + 1, 0, shuffle=False) + data_loader = val_dataset.create_dict_iterator(output_numpy=True) + labels_list = [] + label_path = cfg.label_path + for i, data in enumerate(data_loader): + file_name = "unet3p_bs" + str(cfg.export_batch_size) + "_" + str(i) + ".bin" + file_path = os.path.join(cfg.pre_result_path, "image", file_name) + data["image"].tofile(file_path) + labels_list.append(data["mask"]) + np.save(label_path, labels_list) + print("="*20, "export bin files finished", "="*20) + +if __name__ == '__main__': + gen_bin() diff --git a/research/cv/UNet3+/scripts/run_infer_310.sh b/research/cv/UNet3+/scripts/run_infer_310.sh new file mode 100644 index 0000000000000000000000000000000000000000..93f44e21ebf56675e8a04dec684527d546d1e660 --- /dev/null +++ b/research/cv/UNet3+/scripts/run_infer_310.sh @@ -0,0 +1,123 @@ +#!/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 || $# -gt 4 ]]; then + echo "Usage: bash run_infer_310.sh [MINDIR_PATH] [DATA_PATH] [NEED_PREPROCESS] [DEVICE_ID] + DEVICE_ID is optional, it can be set by environment variable device_id, otherwise the value is zero" +exit 1 +fi + +get_real_path(){ + if [ "${1:0:1}" == "/" ]; then + echo "$1" + else + echo "$(realpath -m $PWD/$1)" + fi +} +model=$(get_real_path $1) +dataset_path=$(get_real_path $2) + +if [ "$3" == "y" ] || [ "$3" == "n" ];then + need_preprocess=$3 +else + echo "weather need preprocess or not, it's value must be in [y, n]" + exit 1 +fi + +device_id=0 +if [ $# == 4 ]; then + device_id=$4 +fi + +echo "mindir name: "$model +echo "dataset path: "$dataset_path +echo "need preprocess: "$need_preprocess +echo "device id: "$device_id + +export ASCEND_HOME=/usr/local/Ascend/ +if [ -d ${ASCEND_HOME}/ascend-toolkit ]; then + export PATH=$ASCEND_HOME/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH + export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/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=$ASCEND_HOME/fwkacllib/python/site-packages:${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/fwkacllib/bin:$ASCEND_HOME/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH + export LD_LIBRARY_PATH=$ASCEND_HOME/fwkacllib/lib64:/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/fwkacllib/python/site-packages:$ASCEND_HOME/atc/python/site-packages:$PYTHONPATH + export ASCEND_OPP_PATH=$ASCEND_HOME/opp +fi + +function compile_app() +{ + cd ../ascend310_infer/ || exit + + bash build.sh &> build.log +} + +function preprocess_data() +{ + if [ -d preprocess_Result ]; then + rm -rf ./preprocess_Result + fi + mkdir preprocess_Result + python ../preprocess.py --val_data_path $dataset_path &> preprocess.log +} + +function infer() +{ + cd - || exit + if [ -d result_Files ]; then + rm -rf ./result_Files + fi + if [ -d time_Result ]; then + rm -rf ./time_Result + fi + mkdir result_Files + mkdir time_Result + ../ascend310_infer/out/main --mindir_path=$model --dataset_path=$dataset_path --device_id=$device_id &> infer.log +} + +function cal_acc() +{ + python ../postprocess.py &> acc.log +} + +if [ $need_preprocess == "y" ]; then + preprocess_data + dataset_path=./preprocess_Result/image + if [ $? -ne 0 ]; then + echo "preprocess dataset failed" + exit 1 + fi +fi + + +compile_app +if [ $? -ne 0 ]; then + echo "compile app code failed" + exit 1 +fi +infer +if [ $? -ne 0 ]; then + echo " execute inference failed" + exit 1 +fi +cal_acc +if [ $? -ne 0 ]; then + echo "calculate accuracy failed" + exit 1 +fi \ No newline at end of file diff --git a/research/cv/deeplabv3plus/scripts/run_eval_s8_multiscale_flip_gpu.sh b/research/cv/deeplabv3plus/scripts/run_eval_s8_multiscale_flip_gpu.sh index cad07678f09908ad17548e2582850023273aa1bc..71b2a2064c9262ba48f985b9b59677ca7d223ca8 100644 --- a/research/cv/deeplabv3plus/scripts/run_eval_s8_multiscale_flip_gpu.sh +++ b/research/cv/deeplabv3plus/scripts/run_eval_s8_multiscale_flip_gpu.sh @@ -44,7 +44,7 @@ cd ${eval_path} || exit python ./eval.py --data_root=$DATA_ROOT \ --data_lst=$DATA_LST \ - --batch_size=16 \ + --batch_size=8 \ --crop_size=513 \ --ignore_label=255 \ --num_classes=21 \ diff --git a/research/cv/deeplabv3plus/scripts/run_eval_s8_multiscale_gpu.sh b/research/cv/deeplabv3plus/scripts/run_eval_s8_multiscale_gpu.sh index 8fd2cff5b730f9d61216ead66ff0c19ce253a625..a0688ff39eb99cc65b75f5ab607a12aeaaf007ec 100644 --- a/research/cv/deeplabv3plus/scripts/run_eval_s8_multiscale_gpu.sh +++ b/research/cv/deeplabv3plus/scripts/run_eval_s8_multiscale_gpu.sh @@ -42,7 +42,7 @@ cd ${eval_path} || exit python ./eval.py --data_root=$DATA_ROOT \ --data_lst=$DATA_LST \ - --batch_size=8 \ + --batch_size=16 \ --crop_size=513 \ --ignore_label=255 \ --num_classes=21 \