提交 db86eefd 编写于 作者: J jiangjiajun

some details modify

上级 56f76f5f
......@@ -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)
### 参数
......
......@@ -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(
......
......@@ -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:
......
......@@ -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))
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='./')
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册