未验证 提交 83253af0 编写于 作者: T Tingquan Gao 提交者: GitHub

[Cherry-pick] Some fixes in HubServing and whl. (#1056)

* Fix params format

* Add preprocess in HubServing

* Fix printing prediction

* Supplement some fixes
上级 b6f71606
......@@ -33,8 +33,10 @@ def get_default_confg():
"enable_benchmark": False
},
'PostProcess': {
'name': 'Topk',
'topk': 5,
'class_id_map_file': './utils/imagenet1k_label_list.txt'
'main_indicator': 'Topk',
'Topk': {
'topk': 5,
'class_id_map_file': './utils/imagenet1k_label_list.txt'
}
}
}
\ No newline at end of file
}
......@@ -15,7 +15,7 @@ hubserving/clas/
### 1. 准备环境
```shell
# 安装paddlehub,请安装2.0版本
pip3 install paddlehub==2.0.0b1 --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install paddlehub==2.1.0 --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple
```
### 2. 下载推理模型
......@@ -128,8 +128,12 @@ python hubserving/test_hubserving.py server_url image_path
`http://[ip_address]:[port]/predict/[module_name]`
- **image_path**:测试图像路径,可以是单张图片路径,也可以是图像集合目录路径。
- **batch_size**:[**可选**] 以`batch_size`大小为单位进行预测,默认为`1`。
- **resize_short**:[**可选**] 预处理时,按短边调整大小,默认为`256`。
- **crop_size**:[**可选**] 预处理时,居中裁剪的大小,默认为`224`。
- **normalize**:[**可选**] 预处理时,是否进行`normalize`,默认为`True`。
- **to_chw**:[**可选**] 预处理时,是否调整为`CHW`顺序,默认为`True`。
**注意**:如果使用`Transformer`系列模型,如`DeiT_***_384`, `ViT_***_384`等,请注意模型的输入数据尺寸。需要指定`--resize_short=384 --resize=384`。
**注意**:如果使用`Transformer`系列模型,如`DeiT_***_384`, `ViT_***_384`等,请注意模型的输入数据尺寸,需要指定`--resize_short=384 --crop_size=384`。
访问示例:
......
......@@ -15,7 +15,7 @@ hubserving/clas/
### 1. Prepare the environment
```shell
# Install version 2.0 of PaddleHub
pip3 install paddlehub==2.0.0b1 --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install paddlehub==2.1.0 --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple
```
### 2. Download inference model
......@@ -126,9 +126,13 @@ Two required parameters need to be passed to the script:
`http://[ip_address]:[port]/predict/[module_name]`
- **image_path**: Test image path, can be a single image path or an image directory path
- **batch_size**: [**Optional**] batch_size. Default by `1`.
- **resize_short**: [**Optional**] In preprocessing, resize according to short size. Default by `256`
- **crop_size**: [**Optional**] In preprocessing, centor crop size. Default by `224`
- **normalize**: [**Optional**] In preprocessing, whether to do `normalize`. Default by `True`
- **to_chw**: [**Optional**] In preprocessing, whether to transpose to `CHW`. Default by `True`
**Notice**:
If you want to use `Transformer series models`, such as `DeiT_***_384`, `ViT_***_384`, etc., please pay attention to the input size of model, and need to set `--resize_short=384`, `--resize=384`.
If you want to use `Transformer series models`, such as `DeiT_***_384`, `ViT_***_384`, etc., please pay attention to the input size of model, and need to set `--resize_short=384`, `--crop_size=384`.
**Eg.**
```shell
......
......@@ -32,30 +32,59 @@ from utils import config
from utils.encode_decode import np_to_b64
from python.preprocess import create_operators
preprocess_config = [{
'ResizeImage': {
'resize_short': 256
}
}, {
'CropImage': {
'size': 224
}
}, {
'NormalizeImage': {
'scale': 0.00392157,
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'order': ''
}
}, {
'ToCHWImage': None
}]
def get_args():
def str2bool(v):
return v.lower() in ("true", "t", "1")
parser = argparse.ArgumentParser()
parser.add_argument("--server_url", type=str)
parser.add_argument("--image_file", type=str)
parser.add_argument("--batch_size", type=int, default=1)
parser.add_argument("--resize_short", type=int, default=256)
parser.add_argument("--crop_size", type=int, default=224)
parser.add_argument("--normalize", type=str2bool, default=True)
parser.add_argument("--to_chw", type=str2bool, default=True)
return parser.parse_args()
class PreprocessConfig(object):
def __init__(self,
resize_short=256,
crop_size=224,
normalize=True,
to_chw=True):
self.config = [{
'ResizeImage': {
'resize_short': resize_short
}
}, {
'CropImage': {
'size': crop_size
}
}]
if normalize:
self.config.append({
'NormalizeImage': {
'scale': 0.00392157,
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'order': ''
}
})
if to_chw:
self.config.append({'ToCHWImage': None})
def __call__(self):
return self.config
def main(args):
image_path_list = get_image_list(args.image_file)
headers = {"Content-type": "application/json"}
preprocess_ops = create_operators(preprocess_config)
preprocess_ops = create_operators(
PreprocessConfig(args.resize_short, args.crop_size, args.normalize,
args.to_chw)())
cnt = 0
predict_time = 0
......@@ -113,14 +142,10 @@ def main(args):
for number, result_list in enumerate(preds):
all_score += result_list["scores"][0]
result_str = ""
for i in range(len(result_list["class_ids"])):
result_str += "{}: {:.2f}\t".format(
result_list["class_ids"][i],
result_list["scores"][i])
pred_str = ", ".join(
[f"{k}: {result_list[k]}" for k in result_list])
logger.info(
f"File:{img_name_list[number]}, The result(s): {result_str}"
f"File:{img_name_list[number]}, The result(s): {pred_str}"
)
finally:
......@@ -136,10 +161,5 @@ def main(args):
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--server_url", type=str)
parser.add_argument("--image_file", type=str)
parser.add_argument("--batch_size", type=int, default=1)
args = parser.parse_args()
args = get_args()
main(args)
......@@ -279,8 +279,13 @@ def args_cfg():
"--save_dir",
type=str,
help="The directory to save prediction results as pre-label.")
parser.add_argument("--resize_short", type=int, default=256, help="")
parser.add_argument("--crop_size", type=int, default=224, help="")
parser.add_argument(
"--resize_short",
type=int,
default=256,
help="Resize according to short size.")
parser.add_argument(
"--crop_size", type=int, default=224, help="Centor crop size.")
args = parser.parse_args()
return vars(args)
......@@ -407,7 +412,7 @@ class PaddleClas(object):
Args:
model_name: The model name supported by PaddleClas, default by None. If specified, override config.
inference_model_dir: The directory that contained model file and params file to be used, default by None. If specified, override config.
use_gpu: Wheather use GPU, default by None. If specified, override config.
use_gpu: Whether use GPU, default by None. If specified, override config.
batch_size: The batch size to pridict, default by None. If specified, override config.
topk: Return the top k prediction results with the highest score.
"""
......@@ -459,7 +464,7 @@ class PaddleClas(object):
Args:
input_data (str | NumPy.array): The path of image, or the directory containing images, or the URL of image from Internet.
print_pred (bool, optional): Wheather print the prediction result. Defaults to False.
print_pred (bool, optional): Whether print the prediction result. Defaults to False.
Raises:
ImageTypeError: Illegal input_data.
......@@ -506,12 +511,12 @@ class PaddleClas(object):
preds = self.cls_predictor.postprocess(outputs,
img_path_list)
if print_pred and preds:
for nu, pred in enumerate(preds):
for pred in preds:
filename = pred.pop("file_name")
pred_str = ", ".join(
[f"{k}: {pred[k]}" for k in pred])
print(
f"filename: {img_path_list[nu]}, top-{topk}, {pred_str}"
)
f"filename: {filename}, top-{topk}, {pred_str}")
img_list = []
img_path_list = []
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册