From db86eefd6102fa9ca59fea4fcdd799a88ea8edd9 Mon Sep 17 00:00:00 2001 From: jiangjiajun Date: Sat, 11 Jul 2020 06:49:50 +0000 Subject: [PATCH] some details modify --- docs/apis/slim.md | 2 +- paddlex/cv/models/base.py | 36 ++++++++++++--------- paddlex/cv/models/load_model.py | 6 ++-- paddlex/cv/models/utils/pretrain_weights.py | 23 +++++++++---- tutorials/interpret/lime.py | 9 +----- 5 files changed, 42 insertions(+), 34 deletions(-) diff --git a/docs/apis/slim.md b/docs/apis/slim.md index 38420a7..23c2d5e 100755 --- a/docs/apis/slim.md +++ b/docs/apis/slim.md @@ -9,7 +9,7 @@ paddlex.slim.cal_params_sensitivities(model, save_file, eval_dataset, batch_size 1. 获取模型中可裁剪卷积Kernel的名称。 2. 计算每个可裁剪卷积Kernel不同裁剪率下的敏感度。 【注意】卷积的敏感度是指在不同裁剪率下评估数据集预测精度的损失,通过得到的敏感度,可以决定最终模型需要裁剪的参数列表和各裁剪参数对应的裁剪率。 -[查看使用示例](https://github.com/PaddlePaddle/PaddleX/blob/develop/tutorials/compress/classification/cal_sensitivities_file.py#L33) [查看裁剪教程](../tutorials/compress/classification.md) +[查看使用示例](https://github.com/PaddlePaddle/PaddleX/blob/develop/tutorials/compress/classification/cal_sensitivities_file.py#L33) ### 参数 diff --git a/paddlex/cv/models/base.py b/paddlex/cv/models/base.py index ff7b5cc..399a670 100644 --- a/paddlex/cv/models/base.py +++ b/paddlex/cv/models/base.py @@ -192,22 +192,31 @@ class BaseAPI: if self.model_type == 'classifier': if pretrain_weights not in ['IMAGENET']: logging.warning( - "Pretrain_weights for classifier should be defined as directory path or parameter file or 'IMAGENET' or None, but it is {}, so we force to set it as 'IMAGENET'". + "Path of pretrain_weights('{}') is not exists!". format(pretrain_weights)) + logging.warning( + "Pretrain_weights will be forced to set as 'IMAGENET', if you don't want to use pretrain weights, set pretrain_weights=None." + ) pretrain_weights = 'IMAGENET' elif self.model_type == 'detector': if pretrain_weights not in ['IMAGENET', 'COCO']: logging.warning( - "Pretrain_weights for detector should be defined as directory path or parameter file or 'IMAGENET' or 'COCO' or None, but it is {}, so we force to set it as 'IMAGENET'". + "Path of pretrain_weights('{}') is not exists!". format(pretrain_weights)) + logging.warning( + "Pretrain_weights will be forced to set as 'IMAGENET', if you don't want to use pretrain weights, set pretrain_weights=None." + ) pretrain_weights = 'IMAGENET' elif self.model_type == 'segmenter': if pretrain_weights not in [ 'IMAGENET', 'COCO', 'CITYSCAPES' ]: logging.warning( - "Pretrain_weights for segmenter should be defined as directory path or parameter file or 'IMAGENET' or 'COCO' or 'CITYSCAPES', but it is {}, so we force to set it as 'IMAGENET'". + "Path of pretrain_weights('{}') is not exists!". format(pretrain_weights)) + logging.warning( + "Pretrain_weights will be forced to set as 'IMAGENET', if you don't want to use pretrain weights, set pretrain_weights=None." + ) pretrain_weights = 'IMAGENET' if hasattr(self, 'backbone'): backbone = self.backbone @@ -237,8 +246,8 @@ class BaseAPI: logging.info( "Load pretrain weights from {}.".format(pretrain_weights), use_color=True) - paddlex.utils.utils.load_pretrain_weights( - self.exe, self.train_prog, pretrain_weights, fuse_bn) + paddlex.utils.utils.load_pretrain_weights(self.exe, self.train_prog, + pretrain_weights, fuse_bn) # 进行裁剪 if sensitivities_file is not None: import paddleslim @@ -342,9 +351,7 @@ class BaseAPI: logging.info("Model saved in {}.".format(save_dir)) def export_inference_model(self, save_dir): - test_input_names = [ - var.name for var in list(self.test_inputs.values()) - ] + test_input_names = [var.name for var in list(self.test_inputs.values())] test_outputs = list(self.test_outputs.values()) with fluid.scope_guard(self.scope): if self.__class__.__name__ == 'MaskRCNN': @@ -382,8 +389,7 @@ class BaseAPI: # 模型保存成功的标志 open(osp.join(save_dir, '.success'), 'w').close() - logging.info("Model for inference deploy saved in {}.".format( - save_dir)) + logging.info("Model for inference deploy saved in {}.".format(save_dir)) def train_loop(self, num_epochs, @@ -510,13 +516,11 @@ class BaseAPI: eta = ((num_epochs - i) * total_num_steps - step - 1 ) * avg_step_time if time_eval_one_epoch is not None: - eval_eta = ( - total_eval_times - i // save_interval_epochs - ) * time_eval_one_epoch + eval_eta = (total_eval_times - i // save_interval_epochs + ) * time_eval_one_epoch else: - eval_eta = ( - total_eval_times - i // save_interval_epochs - ) * total_num_steps_eval * avg_step_time + eval_eta = (total_eval_times - i // save_interval_epochs + ) * total_num_steps_eval * avg_step_time eta_str = seconds_to_hms(eta + eval_eta) logging.info( diff --git a/paddlex/cv/models/load_model.py b/paddlex/cv/models/load_model.py index e6bbac0..7c9aa26 100644 --- a/paddlex/cv/models/load_model.py +++ b/paddlex/cv/models/load_model.py @@ -26,6 +26,8 @@ from paddlex.cv.transforms import build_transforms, build_transforms_v1 def load_model(model_dir, fixed_input_shape=None): model_scope = fluid.Scope() + if not osp.exists(model_dir): + logging.error("model_dir '{}' is not exists!".format(model_dir)) if not osp.exists(osp.join(model_dir, "model.yml")): raise Exception("There's not model.yml in {}".format(model_dir)) with open(osp.join(model_dir, "model.yml")) as f: @@ -101,8 +103,8 @@ def load_model(model_dir, fixed_input_shape=None): model.model_type, info['Transforms'], info['BatchTransforms']) model.eval_transforms = copy.deepcopy(model.test_transforms) else: - model.test_transforms = build_transforms( - model.model_type, info['Transforms'], to_rgb) + model.test_transforms = build_transforms(model.model_type, + info['Transforms'], to_rgb) model.eval_transforms = copy.deepcopy(model.test_transforms) if '_Attributes' in info: diff --git a/paddlex/cv/models/utils/pretrain_weights.py b/paddlex/cv/models/utils/pretrain_weights.py index b50cdf0..5fbfd27 100644 --- a/paddlex/cv/models/utils/pretrain_weights.py +++ b/paddlex/cv/models/utils/pretrain_weights.py @@ -144,8 +144,7 @@ def get_pretrain_weights(flag, class_name, backbone, save_dir): logging.warning(warning_info.format(class_name, flag, 'IMAGENET')) flag = 'IMAGENET' elif class_name == 'FastSCNN': - logging.warning( - warning_info.format(class_name, flag, 'CITYSCAPES')) + logging.warning(warning_info.format(class_name, flag, 'CITYSCAPES')) flag = 'CITYSCAPES' elif flag == 'CITYSCAPES': model_name = '{}_{}'.format(class_name, backbone) @@ -168,8 +167,7 @@ def get_pretrain_weights(flag, class_name, backbone, save_dir): logging.warning(warning_info.format(class_name, flag, 'COCO')) flag = 'COCO' elif class_name == 'FastSCNN': - logging.warning( - warning_info.format(class_name, flag, 'CITYSCAPES')) + logging.warning(warning_info.format(class_name, flag, 'CITYSCAPES')) flag = 'CITYSCAPES' if flag == 'IMAGENET': @@ -196,8 +194,14 @@ def get_pretrain_weights(flag, class_name, backbone, save_dir): # paddlex.utils.download_and_decompress(url, path=new_save_dir) # return osp.join(new_save_dir, fname) try: + logging.info( + "Connecting PaddleHub server to get pretrain weights...") hub.download(backbone, save_path=new_save_dir) except Exception as e: + logging.error( + "Couldn't download pretrain weight, you can download it manualy from {} (decompress the file if it is a compressed file), and set pretrain weights by your self". + format(image_pretrain[backbone]), + exit=False) if isinstance(e, hub.ResourceNotFoundError): raise Exception("Resource for backbone {} not found".format( backbone)) @@ -224,8 +228,14 @@ def get_pretrain_weights(flag, class_name, backbone, save_dir): # paddlex.utils.download_and_decompress(url, path=new_save_dir) # return osp.join(new_save_dir, fname) try: + logging.info( + "Connecting PaddleHub server to get pretrain weights...") hub.download(backbone, save_path=new_save_dir) except Exception as e: + logging.error( + "Couldn't download pretrain weight, you can download it manualy from {} (decompress the file if it is a compressed file), and set pretrain weights by your self". + format(url), + exit=False) if isinstance(hub.ResourceNotFoundError): raise Exception("Resource for backbone {} not found".format( backbone)) @@ -238,6 +248,5 @@ def get_pretrain_weights(flag, class_name, backbone, save_dir): "Unexpected error, please make sure paddlehub >= 1.6.2") return osp.join(new_save_dir, backbone) else: - raise Exception( - "pretrain_weights need to be defined as directory path or 'IMAGENET' or 'COCO' or 'Cityscapes' (download pretrain weights automatically)." - ) + logging.error("Path of retrain weights '{}' is not exists!".format( + flag)) diff --git a/tutorials/interpret/lime.py b/tutorials/interpret/lime.py index ae862aa..8028b50 100644 --- a/tutorials/interpret/lime.py +++ b/tutorials/interpret/lime.py @@ -1,8 +1,3 @@ -import os -# 选择使用0号卡 -os.environ['CUDA_VISIBLE_DEVICES'] = '0' - -import os.path as osp import paddlex as pdx # 下载和解压Imagenet果蔬分类数据集 @@ -18,6 +13,4 @@ model = pdx.load_model('mini_imagenet_veg_mobilenetv2') # 可解释性可视化 pdx.interpret.lime( - 'mini_imagenet_veg/mushroom/n07734744_1106.JPEG', - model, - save_dir='./') + 'mini_imagenet_veg/mushroom/n07734744_1106.JPEG', model, save_dir='./') -- GitLab