From e7af421669cdaad764b04ed6f5062fb648dd13ff Mon Sep 17 00:00:00 2001 From: yuzhenhua Date: Thu, 29 Apr 2021 15:34:19 +0800 Subject: [PATCH] fix codex --- .../cv/ctpn/ascend310_infer/src/main.cc | 10 ++--- .../cv/ctpn/ascend310_infer/src/utils.cc | 6 +-- .../cv/deeptext/ascend310_infer/src/main.cc | 5 ++- .../cv/deeptext/ascend310_infer/src/utils.cc | 9 ++--- .../cv/resnet/ascend310_infer/src/main.cc | 1 - .../cv/unet/ascend310_infer/src/main.cc | 12 +++--- model_zoo/official/cv/unet/postprocess.py | 37 ++++++------------- model_zoo/official/cv/unet/preprocess.py | 4 ++ .../cv/yolov4/ascend310_infer/src/main.cc | 3 +- .../ascend310_infer/src/sample_process.cpp | 3 +- .../naml/ascend310_infer/src/utils.cpp | 3 +- 11 files changed, 38 insertions(+), 55 deletions(-) diff --git a/model_zoo/official/cv/ctpn/ascend310_infer/src/main.cc b/model_zoo/official/cv/ctpn/ascend310_infer/src/main.cc index 026e62329a..74dd530bd7 100644 --- a/model_zoo/official/cv/ctpn/ascend310_infer/src/main.cc +++ b/model_zoo/official/cv/ctpn/ascend310_infer/src/main.cc @@ -99,11 +99,11 @@ int main(int argc, char **argv) { return 1; } - std::shared_ptr decode(new Decode()); - std::shared_ptr resize(new Resize({576, 960})); - std::shared_ptr normalize(new Normalize({123.675, 116.28, 103.53}, {58.395, 57.12, 57.375})); - std::shared_ptr hwc2chw(new HWC2CHW()); - std::shared_ptr typeCast(new TypeCast(DataType::kNumberTypeFloat16)); + auto decode = Decode(); + auto resize = Resize({576, 960}); + auto normalize = Normalize({123.675, 116.28, 103.53}, {58.395, 57.12, 57.375}); + auto hwc2chw = HWC2CHW(); + auto typeCast = TypeCast(DataType::kNumberTypeFloat16); mindspore::dataset::Execute transformDecode(decode); mindspore::dataset::Execute transform({resize, normalize, hwc2chw}); diff --git a/model_zoo/official/cv/ctpn/ascend310_infer/src/utils.cc b/model_zoo/official/cv/ctpn/ascend310_infer/src/utils.cc index e753dafa89..5c1ce7d076 100644 --- a/model_zoo/official/cv/ctpn/ascend310_infer/src/utils.cc +++ b/model_zoo/official/cv/ctpn/ascend310_infer/src/utils.cc @@ -48,8 +48,7 @@ int WriteResult(const std::string& imageFile, const std::vector &outpu 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(); + std::shared_ptr netOutput = outputs[i].Data(); outputSize = outputs[i].DataSize(); int pos = imageFile.rfind('/'); std::string fileName(imageFile, pos + 1); @@ -104,8 +103,7 @@ DIR *OpenDir(std::string_view dirName) { std::cout << "dirName is not a valid directory !" << std::endl; return nullptr; } - DIR *dir; - dir = opendir(realPath.c_str()); + DIR *dir = opendir(realPath.c_str()); if (dir == nullptr) { std::cout << "Can not open dir " << dirName << std::endl; return nullptr; diff --git a/model_zoo/official/cv/deeptext/ascend310_infer/src/main.cc b/model_zoo/official/cv/deeptext/ascend310_infer/src/main.cc index 2bbe5448fa..d3ebed4547 100644 --- a/model_zoo/official/cv/deeptext/ascend310_infer/src/main.cc +++ b/model_zoo/official/cv/deeptext/ascend310_infer/src/main.cc @@ -101,7 +101,8 @@ int main(int argc, char **argv) { std::map costTime_map; size_t size = all_files.size(); - Execute transform(std::shared_ptr(new DvppDecodeResizeJpeg({576, 960}))); + auto dvppDecodeResizeJpeg = DvppDecodeResizeJpeg({576, 960}); + Execute transform(dvppDecodeResizeJpeg); for (size_t i = 0; i < size; ++i) { struct timeval start; @@ -141,7 +142,7 @@ int main(int argc, char **argv) { infer_cnt++; } - average = average/infer_cnt; + average = average / infer_cnt; std::stringstream timeCost; timeCost << "NN inference cost average time: "<< average << " ms of infer_count " << infer_cnt << std::endl; std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << infer_cnt << std::endl; diff --git a/model_zoo/official/cv/deeptext/ascend310_infer/src/utils.cc b/model_zoo/official/cv/deeptext/ascend310_infer/src/utils.cc index b509c57f82..5c1ce7d076 100644 --- a/model_zoo/official/cv/deeptext/ascend310_infer/src/utils.cc +++ b/model_zoo/official/cv/deeptext/ascend310_infer/src/utils.cc @@ -48,14 +48,13 @@ int WriteResult(const std::string& imageFile, const std::vector &outpu 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(); + std::shared_ptr 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"); + FILE *outputFile = fopen(outFileName.c_str(), "wb"); fwrite(netOutput.get(), outputSize, sizeof(char), outputFile); fclose(outputFile); outputFile = nullptr; @@ -104,8 +103,7 @@ DIR *OpenDir(std::string_view dirName) { std::cout << "dirName is not a valid directory !" << std::endl; return nullptr; } - DIR *dir; - dir = opendir(realPath.c_str()); + DIR *dir = opendir(realPath.c_str()); if (dir == nullptr) { std::cout << "Can not open dir " << dirName << std::endl; return nullptr; @@ -118,7 +116,6 @@ 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 ""; diff --git a/model_zoo/official/cv/resnet/ascend310_infer/src/main.cc b/model_zoo/official/cv/resnet/ascend310_infer/src/main.cc index 84a1c29ae6..03f3c960ba 100644 --- a/model_zoo/official/cv/resnet/ascend310_infer/src/main.cc +++ b/model_zoo/official/cv/resnet/ascend310_infer/src/main.cc @@ -70,7 +70,6 @@ int main(int argc, char **argv) { 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; diff --git a/model_zoo/official/cv/unet/ascend310_infer/src/main.cc b/model_zoo/official/cv/unet/ascend310_infer/src/main.cc index 8e8742e9aa..a95d6a7588 100644 --- a/model_zoo/official/cv/unet/ascend310_infer/src/main.cc +++ b/model_zoo/official/cv/unet/ascend310_infer/src/main.cc @@ -72,7 +72,7 @@ int main(int argc, char **argv) { Model model; Status ret = model.Build(GraphCell(graph), context); if (ret != kSuccess) { - std::cout << "EEEEEEEERROR Build failed." << std::endl; + std::cout << "ERROR Build failed." << std::endl; return 1; } @@ -86,11 +86,11 @@ int main(int argc, char **argv) { std::map costTime_map; size_t size = all_files.size(); - auto decode(new Decode()); - auto swapredblue(new SwapRedBlue()); - auto resize(new Resize({96, 96})); - auto normalize(new Normalize({127.5, 127.5, 127.5}, {127.5, 127.5, 127.5})); - auto hwc2chw(new HWC2CHW()); + auto decode = Decode(); + auto swapredblue = SwapRedBlue(); + auto resize = Resize({96, 96}); + auto normalize = Normalize({127.5, 127.5, 127.5}, {127.5, 127.5, 127.5}); + auto hwc2chw = HWC2CHW(); Execute preprocess({decode, swapredblue, resize, normalize, hwc2chw}); for (size_t i = 0; i < size; ++i) { diff --git a/model_zoo/official/cv/unet/postprocess.py b/model_zoo/official/cv/unet/postprocess.py index 47aa39f66b..f94b970cdc 100644 --- a/model_zoo/official/cv/unet/postprocess.py +++ b/model_zoo/official/cv/unet/postprocess.py @@ -18,7 +18,6 @@ import argparse import cv2 import numpy as np -from src.data_loader import create_dataset, create_cell_nuclei_dataset from src.config import cfg_unet class dice_coeff(): @@ -74,25 +73,6 @@ class dice_coeff(): 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)) - -def test_net(data_dir, - cross_valid_ind=1, - cfg=None): - - if 'dataset' in cfg and cfg['dataset'] == "Cell_nuclei": - valid_dataset = create_cell_nuclei_dataset(data_dir, cfg['img_size'], 1, 1, is_train=False, - eval_resize=cfg["eval_resize"], split=0.8) - else: - _, valid_dataset = create_dataset(data_dir, 1, 1, False, cross_valid_ind, False, do_crop=cfg['crop'], - img_size=cfg['img_size']) - labels_list = [] - - for data in valid_dataset: - labels_list.append(data[1].asnumpy()) - - return labels_list - - def get_args(): parser = argparse.ArgumentParser(description='Test the UNet on images and target masks', formatter_class=argparse.ArgumentDefaultsHelpFormatter) @@ -105,24 +85,31 @@ def get_args(): if __name__ == '__main__': - args = get_args() - label_list = test_net(data_dir=args.data_url, cross_valid_ind=cfg_unet['cross_valid_ind'], cfg=cfg_unet) rst_path = args.rst_path metrics = dice_coeff() if 'dataset' in cfg_unet and cfg_unet['dataset'] == "Cell_nuclei": + img_size = tuple(cfg_unet['img_size']) for i, bin_name in enumerate(os.listdir('./preprocess_Result/')): - bin_name_softmax = bin_name.replace(".png", "") + "_0.bin" - bin_name_argmax = bin_name.replace(".png", "") + "_1.bin" + f = bin_name.replace(".png", "") + bin_name_softmax = f + "_0.bin" + bin_name_argmax = f + "_1.bin" file_name_sof = rst_path + bin_name_softmax file_name_arg = rst_path + bin_name_argmax softmax_out = np.fromfile(file_name_sof, np.float32).reshape(1, 96, 96, 2) argmax_out = np.fromfile(file_name_arg, np.float32).reshape(1, 96, 96) - label = label_list[i] + mask = cv2.imread(os.path.join(args.data_url, f, "mask.png"), cv2.IMREAD_GRAYSCALE) + mask = cv2.resize(mask, img_size) + mask = mask.astype(np.float32) / 255 + mask = (mask > 0.5).astype(np.int) + mask = (np.arange(2) == mask[..., None]).astype(int) + mask = mask.transpose(2, 0, 1).astype(np.float32) + label = mask.reshape(1, 2, 96, 96) metrics.update((softmax_out, argmax_out), label) else: + label_list = np.load('label.npy') for j in range(len(os.listdir('./preprocess_Result/'))): file_name_sof = rst_path + "ISBI_test_bs_1_" + str(j) + "_0" + ".bin" file_name_arg = rst_path + "ISBI_test_bs_1_" + str(j) + "_1" + ".bin" diff --git a/model_zoo/official/cv/unet/preprocess.py b/model_zoo/official/cv/unet/preprocess.py index 28f6cdc623..5b6015eb90 100644 --- a/model_zoo/official/cv/unet/preprocess.py +++ b/model_zoo/official/cv/unet/preprocess.py @@ -27,11 +27,15 @@ def preprocess_dataset(data_dir, result_path, cross_valid_ind=1, cfg=None): _, valid_dataset = create_dataset(data_dir, 1, 1, False, cross_valid_ind, False, do_crop=cfg['crop'], img_size=cfg['img_size']) + labels_list = [] for i, data in enumerate(valid_dataset): file_name = "ISBI_test_bs_1_" + str(i) + ".bin" file_path = result_path + file_name data[0].asnumpy().tofile(file_path) + labels_list.append(data[1].asnumpy()) + + np.save("./label.npy", labels_list) class CellNucleiDataset: """ diff --git a/model_zoo/official/cv/yolov4/ascend310_infer/src/main.cc b/model_zoo/official/cv/yolov4/ascend310_infer/src/main.cc index 4971fba68a..2e50de5dc2 100644 --- a/model_zoo/official/cv/yolov4/ascend310_infer/src/main.cc +++ b/model_zoo/official/cv/yolov4/ascend310_infer/src/main.cc @@ -69,7 +69,6 @@ int main(int argc, char **argv) { mindspore::Graph graph; Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph); - if (!FLAGS_precision_mode.empty()) { ascend310->SetPrecisionMode(FLAGS_precision_mode); } @@ -83,7 +82,7 @@ int main(int argc, char **argv) { Model model; Status ret = model.Build(GraphCell(graph), context); if (ret != kSuccess) { - std::cout << "EEEEEEEERROR Build failed." << std::endl; + std::cout << "ERROR Build failed." << std::endl; return 1; } diff --git a/model_zoo/official/recommend/naml/ascend310_infer/src/sample_process.cpp b/model_zoo/official/recommend/naml/ascend310_infer/src/sample_process.cpp index 9e5614bf0c..e0772ca09b 100644 --- a/model_zoo/official/recommend/naml/ascend310_infer/src/sample_process.cpp +++ b/model_zoo/official/recommend/naml/ascend310_infer/src/sample_process.cpp @@ -350,8 +350,7 @@ std::vector SampleProcess::GetModelExecCostTimeInfo() { " ms of infer_count " << infer_cnt << std::endl; result.emplace_back(timeCost.str()); - double totalCostTime; - totalCostTime = totalCostTime_map_.begin()->second - totalCostTime_map_.begin()->first; + double totalCostTime = totalCostTime_map_.begin()->second - totalCostTime_map_.begin()->first; std::stringstream totalTimeCost; totalTimeCost << "total inference cost time: "<< totalCostTime << " ms; count " << infer_cnt << std::endl; result.emplace_back(totalTimeCost.str()); diff --git a/model_zoo/official/recommend/naml/ascend310_infer/src/utils.cpp b/model_zoo/official/recommend/naml/ascend310_infer/src/utils.cpp index 085dcde12b..6898dc6770 100644 --- a/model_zoo/official/recommend/naml/ascend310_infer/src/utils.cpp +++ b/model_zoo/official/recommend/naml/ascend310_infer/src/utils.cpp @@ -157,8 +157,7 @@ DIR *Utils::OpenDir(std::string dir_name) { return nullptr; } - DIR *dir; - dir = opendir(real_path.c_str()); + DIR *dir = opendir(real_path.c_str()); if (dir == nullptr) { std::cout << "Can not open dir " << dir_name << std::endl; return nullptr;