From: @yuzhenhua666 Reviewed-by: @c_34,@oacjiewen Signed-off-by: @c_34pull/15936/MERGE
| @@ -99,11 +99,11 @@ int main(int argc, char **argv) { | |||||
| return 1; | return 1; | ||||
| } | } | ||||
| std::shared_ptr<TensorTransform> decode(new Decode()); | |||||
| std::shared_ptr<TensorTransform> resize(new Resize({576, 960})); | |||||
| std::shared_ptr<TensorTransform> normalize(new Normalize({123.675, 116.28, 103.53}, {58.395, 57.12, 57.375})); | |||||
| std::shared_ptr<TensorTransform> hwc2chw(new HWC2CHW()); | |||||
| std::shared_ptr<TensorTransform> 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 transformDecode(decode); | ||||
| mindspore::dataset::Execute transform({resize, normalize, hwc2chw}); | mindspore::dataset::Execute transform({resize, normalize, hwc2chw}); | ||||
| @@ -48,8 +48,7 @@ int WriteResult(const std::string& imageFile, const std::vector<MSTensor> &outpu | |||||
| std::string homePath = "./result_Files"; | std::string homePath = "./result_Files"; | ||||
| for (size_t i = 0; i < outputs.size(); ++i) { | for (size_t i = 0; i < outputs.size(); ++i) { | ||||
| size_t outputSize; | size_t outputSize; | ||||
| std::shared_ptr<const void> netOutput; | |||||
| netOutput = outputs[i].Data(); | |||||
| std::shared_ptr<const void> netOutput = outputs[i].Data(); | |||||
| outputSize = outputs[i].DataSize(); | outputSize = outputs[i].DataSize(); | ||||
| int pos = imageFile.rfind('/'); | int pos = imageFile.rfind('/'); | ||||
| std::string fileName(imageFile, pos + 1); | 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; | std::cout << "dirName is not a valid directory !" << std::endl; | ||||
| return nullptr; | return nullptr; | ||||
| } | } | ||||
| DIR *dir; | |||||
| dir = opendir(realPath.c_str()); | |||||
| DIR *dir = opendir(realPath.c_str()); | |||||
| if (dir == nullptr) { | if (dir == nullptr) { | ||||
| std::cout << "Can not open dir " << dirName << std::endl; | std::cout << "Can not open dir " << dirName << std::endl; | ||||
| return nullptr; | return nullptr; | ||||
| @@ -101,7 +101,8 @@ int main(int argc, char **argv) { | |||||
| std::map<double, double> costTime_map; | std::map<double, double> costTime_map; | ||||
| size_t size = all_files.size(); | size_t size = all_files.size(); | ||||
| Execute transform(std::shared_ptr<DvppDecodeResizeJpeg>(new DvppDecodeResizeJpeg({576, 960}))); | |||||
| auto dvppDecodeResizeJpeg = DvppDecodeResizeJpeg({576, 960}); | |||||
| Execute transform(dvppDecodeResizeJpeg); | |||||
| for (size_t i = 0; i < size; ++i) { | for (size_t i = 0; i < size; ++i) { | ||||
| struct timeval start; | struct timeval start; | ||||
| @@ -141,7 +142,7 @@ int main(int argc, char **argv) { | |||||
| infer_cnt++; | infer_cnt++; | ||||
| } | } | ||||
| average = average/infer_cnt; | |||||
| average = average / infer_cnt; | |||||
| std::stringstream timeCost; | std::stringstream timeCost; | ||||
| timeCost << "NN inference cost average time: "<< average << " ms of infer_count " << infer_cnt << std::endl; | 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; | std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << infer_cnt << std::endl; | ||||
| @@ -48,14 +48,13 @@ int WriteResult(const std::string& imageFile, const std::vector<MSTensor> &outpu | |||||
| std::string homePath = "./result_Files"; | std::string homePath = "./result_Files"; | ||||
| for (size_t i = 0; i < outputs.size(); ++i) { | for (size_t i = 0; i < outputs.size(); ++i) { | ||||
| size_t outputSize; | size_t outputSize; | ||||
| std::shared_ptr<const void> netOutput; | |||||
| netOutput = outputs[i].Data(); | |||||
| std::shared_ptr<const void> netOutput = outputs[i].Data(); | |||||
| outputSize = outputs[i].DataSize(); | outputSize = outputs[i].DataSize(); | ||||
| int pos = imageFile.rfind('/'); | int pos = imageFile.rfind('/'); | ||||
| std::string fileName(imageFile, pos + 1); | std::string fileName(imageFile, pos + 1); | ||||
| fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin"); | fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin"); | ||||
| std::string outFileName = homePath + "/" + fileName; | 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); | fwrite(netOutput.get(), outputSize, sizeof(char), outputFile); | ||||
| fclose(outputFile); | fclose(outputFile); | ||||
| outputFile = nullptr; | outputFile = nullptr; | ||||
| @@ -104,8 +103,7 @@ DIR *OpenDir(std::string_view dirName) { | |||||
| std::cout << "dirName is not a valid directory !" << std::endl; | std::cout << "dirName is not a valid directory !" << std::endl; | ||||
| return nullptr; | return nullptr; | ||||
| } | } | ||||
| DIR *dir; | |||||
| dir = opendir(realPath.c_str()); | |||||
| DIR *dir = opendir(realPath.c_str()); | |||||
| if (dir == nullptr) { | if (dir == nullptr) { | ||||
| std::cout << "Can not open dir " << dirName << std::endl; | std::cout << "Can not open dir " << dirName << std::endl; | ||||
| return nullptr; | return nullptr; | ||||
| @@ -118,7 +116,6 @@ std::string RealPath(std::string_view path) { | |||||
| char realPathMem[PATH_MAX] = {0}; | char realPathMem[PATH_MAX] = {0}; | ||||
| char *realPathRet = nullptr; | char *realPathRet = nullptr; | ||||
| realPathRet = realpath(path.data(), realPathMem); | realPathRet = realpath(path.data(), realPathMem); | ||||
| if (realPathRet == nullptr) { | if (realPathRet == nullptr) { | ||||
| std::cout << "File: " << path << " is not exist."; | std::cout << "File: " << path << " is not exist."; | ||||
| return ""; | return ""; | ||||
| @@ -70,7 +70,6 @@ int main(int argc, char **argv) { | |||||
| Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph); | Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph); | ||||
| Model model; | Model model; | ||||
| Status ret = model.Build(GraphCell(graph), context); | Status ret = model.Build(GraphCell(graph), context); | ||||
| if (ret != kSuccess) { | if (ret != kSuccess) { | ||||
| std::cout << "ERROR: Build failed." << std::endl; | std::cout << "ERROR: Build failed." << std::endl; | ||||
| return 1; | return 1; | ||||
| @@ -72,7 +72,7 @@ int main(int argc, char **argv) { | |||||
| Model model; | Model model; | ||||
| Status ret = model.Build(GraphCell(graph), context); | Status ret = model.Build(GraphCell(graph), context); | ||||
| if (ret != kSuccess) { | if (ret != kSuccess) { | ||||
| std::cout << "EEEEEEEERROR Build failed." << std::endl; | |||||
| std::cout << "ERROR Build failed." << std::endl; | |||||
| return 1; | return 1; | ||||
| } | } | ||||
| @@ -86,11 +86,11 @@ int main(int argc, char **argv) { | |||||
| std::map<double, double> costTime_map; | std::map<double, double> costTime_map; | ||||
| size_t size = all_files.size(); | 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}); | Execute preprocess({decode, swapredblue, resize, normalize, hwc2chw}); | ||||
| for (size_t i = 0; i < size; ++i) { | for (size_t i = 0; i < size; ++i) { | ||||
| @@ -18,7 +18,6 @@ import argparse | |||||
| import cv2 | import cv2 | ||||
| import numpy as np | import numpy as np | ||||
| from src.data_loader import create_dataset, create_cell_nuclei_dataset | |||||
| from src.config import cfg_unet | from src.config import cfg_unet | ||||
| class dice_coeff(): | class dice_coeff(): | ||||
| @@ -74,25 +73,6 @@ class dice_coeff(): | |||||
| raise RuntimeError('Total samples num must not be 0.') | raise RuntimeError('Total samples num must not be 0.') | ||||
| return (self._dice_coeff_sum / float(self._samples_num), self._iou_sum / float(self._samples_num)) | 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(): | def get_args(): | ||||
| parser = argparse.ArgumentParser(description='Test the UNet on images and target masks', | parser = argparse.ArgumentParser(description='Test the UNet on images and target masks', | ||||
| formatter_class=argparse.ArgumentDefaultsHelpFormatter) | formatter_class=argparse.ArgumentDefaultsHelpFormatter) | ||||
| @@ -105,24 +85,31 @@ def get_args(): | |||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||
| args = get_args() | 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 | rst_path = args.rst_path | ||||
| metrics = dice_coeff() | metrics = dice_coeff() | ||||
| if 'dataset' in cfg_unet and cfg_unet['dataset'] == "Cell_nuclei": | 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/')): | 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_sof = rst_path + bin_name_softmax | ||||
| file_name_arg = rst_path + bin_name_argmax | file_name_arg = rst_path + bin_name_argmax | ||||
| softmax_out = np.fromfile(file_name_sof, np.float32).reshape(1, 96, 96, 2) | 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) | 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) | metrics.update((softmax_out, argmax_out), label) | ||||
| else: | else: | ||||
| label_list = np.load('label.npy') | |||||
| for j in range(len(os.listdir('./preprocess_Result/'))): | for j in range(len(os.listdir('./preprocess_Result/'))): | ||||
| file_name_sof = rst_path + "ISBI_test_bs_1_" + str(j) + "_0" + ".bin" | 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" | file_name_arg = rst_path + "ISBI_test_bs_1_" + str(j) + "_1" + ".bin" | ||||
| @@ -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'], | _, valid_dataset = create_dataset(data_dir, 1, 1, False, cross_valid_ind, False, do_crop=cfg['crop'], | ||||
| img_size=cfg['img_size']) | img_size=cfg['img_size']) | ||||
| labels_list = [] | |||||
| for i, data in enumerate(valid_dataset): | for i, data in enumerate(valid_dataset): | ||||
| file_name = "ISBI_test_bs_1_" + str(i) + ".bin" | file_name = "ISBI_test_bs_1_" + str(i) + ".bin" | ||||
| file_path = result_path + file_name | file_path = result_path + file_name | ||||
| data[0].asnumpy().tofile(file_path) | data[0].asnumpy().tofile(file_path) | ||||
| labels_list.append(data[1].asnumpy()) | |||||
| np.save("./label.npy", labels_list) | |||||
| class CellNucleiDataset: | class CellNucleiDataset: | ||||
| """ | """ | ||||
| @@ -69,7 +69,6 @@ int main(int argc, char **argv) { | |||||
| mindspore::Graph graph; | mindspore::Graph graph; | ||||
| Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph); | Serialization::Load(FLAGS_mindir_path, ModelType::kMindIR, &graph); | ||||
| if (!FLAGS_precision_mode.empty()) { | if (!FLAGS_precision_mode.empty()) { | ||||
| ascend310->SetPrecisionMode(FLAGS_precision_mode); | ascend310->SetPrecisionMode(FLAGS_precision_mode); | ||||
| } | } | ||||
| @@ -83,7 +82,7 @@ int main(int argc, char **argv) { | |||||
| Model model; | Model model; | ||||
| Status ret = model.Build(GraphCell(graph), context); | Status ret = model.Build(GraphCell(graph), context); | ||||
| if (ret != kSuccess) { | if (ret != kSuccess) { | ||||
| std::cout << "EEEEEEEERROR Build failed." << std::endl; | |||||
| std::cout << "ERROR Build failed." << std::endl; | |||||
| return 1; | return 1; | ||||
| } | } | ||||
| @@ -350,8 +350,7 @@ std::vector<std::string> SampleProcess::GetModelExecCostTimeInfo() { | |||||
| " ms of infer_count " << infer_cnt << std::endl; | " ms of infer_count " << infer_cnt << std::endl; | ||||
| result.emplace_back(timeCost.str()); | 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; | std::stringstream totalTimeCost; | ||||
| totalTimeCost << "total inference cost time: "<< totalCostTime << " ms; count " << infer_cnt << std::endl; | totalTimeCost << "total inference cost time: "<< totalCostTime << " ms; count " << infer_cnt << std::endl; | ||||
| result.emplace_back(totalTimeCost.str()); | result.emplace_back(totalTimeCost.str()); | ||||
| @@ -157,8 +157,7 @@ DIR *Utils::OpenDir(std::string dir_name) { | |||||
| return nullptr; | return nullptr; | ||||
| } | } | ||||
| DIR *dir; | |||||
| dir = opendir(real_path.c_str()); | |||||
| DIR *dir = opendir(real_path.c_str()); | |||||
| if (dir == nullptr) { | if (dir == nullptr) { | ||||
| std::cout << "Can not open dir " << dir_name << std::endl; | std::cout << "Can not open dir " << dir_name << std::endl; | ||||
| return nullptr; | return nullptr; | ||||