From dbf160423135c9f69a4923567ac6e5963812b282 Mon Sep 17 00:00:00 2001 From: liaoxingyu Date: Tue, 25 May 2021 15:55:43 +0800 Subject: [PATCH] fix model deploy problems --- demo/README.md | 17 +++++++++++++---- demo/demo.py | 4 ++-- demo/run_demo.sh | 3 --- tools/deploy/README.md | 10 ++++++---- tools/deploy/caffe_export.py | 10 ++++------ tools/deploy/caffe_inference.py | 2 +- tools/deploy/onnx_export.py | 11 +++++++---- tools/deploy/trt_export.py | 7 +++---- tools/deploy/trt_inference.py | 2 +- 9 files changed, 37 insertions(+), 29 deletions(-) delete mode 100644 demo/run_demo.sh diff --git a/demo/README.md b/demo/README.md index 572e51f..dc4daee 100644 --- a/demo/README.md +++ b/demo/README.md @@ -2,9 +2,18 @@ We provide a command line tool to run a simple demo of builtin models. -You can run this command to get cosine similarites between different images +You can run this command to get rank visualization results by cosine similarites between different images. -```bash -cd demo/ -sh run_demo.sh +```shell script +python3 demo/visualize_result.py --config-file logs/dukemtmc/mgn_R50-ibn/config.yaml \ +--parallel --vis-label --dataset-name 'DukeMTMC' --output logs/mgn_duke_vis \ +--opts MODEL.WEIGHTS logs/dukemtmc/mgn_R50-ibn/model_final.pth +``` + +You can also run this command to extract image features. + +```shell script +python3 demo/demo.py --config-file logs/dukemtmc/sbs_R50/config.yaml \ +--parallel --input tools/deploy/test_data/*.jpg --output sbs_R50_feat \ +--opts MODEL.WEIGHTS logs/dukemtmc/sbs_R50/model_final.pth ``` \ No newline at end of file diff --git a/demo/demo.py b/demo/demo.py index 80a1c09..23405b6 100644 --- a/demo/demo.py +++ b/demo/demo.py @@ -14,7 +14,7 @@ import numpy as np import tqdm from torch.backends import cudnn -sys.path.append('..') +sys.path.append('.') from fastreid.config import get_cfg from fastreid.utils.logger import setup_logger @@ -86,4 +86,4 @@ if __name__ == '__main__': img = cv2.imread(path) feat = demo.run_on_image(img) feat = feat.numpy() - np.save(os.path.join(args.output, os.path.basename(path) + '.npy'), feat) + np.save(os.path.join(args.output, os.path.basename(path).split('.')[0] + '.npy'), feat) diff --git a/demo/run_demo.sh b/demo/run_demo.sh deleted file mode 100644 index 6f3f280..0000000 --- a/demo/run_demo.sh +++ /dev/null @@ -1,3 +0,0 @@ -python demo/visualize_result.py --config-file logs/dukemtmc/mgn_R50-ibn/config.yaml \ ---parallel --vis-label --dataset-name 'DukeMTMC' --output logs/mgn_duke_vis \ ---opts MODEL.WEIGHTS logs/dukemtmc/mgn_R50-ibn/model_final.pth diff --git a/tools/deploy/README.md b/tools/deploy/README.md index c5bbe48..137d240 100644 --- a/tools/deploy/README.md +++ b/tools/deploy/README.md @@ -18,14 +18,16 @@ This is a tiny example for converting fastreid-baseline in `meta_arch` to Caffe 1. Run `caffe_export.py` to get the converted Caffe model, ```bash - python caffe_export.py --config-file root-path/market1501/bagtricks_R50/config.yml --name baseline_R50 --output outputs/caffe_model --opts MODEL.WEIGHTS root-path/logs/market1501/bagtricks_R50/model_final.pth + python tools/deploy/caffe_export.py --config-file configs/market1501/bagtricks_R50/config.yml --name baseline_R50 --output caffe_R50_model --opts MODEL.WEIGHTS logs/market1501/bagtricks_R50/model_final.pth ``` - then you can check the Caffe model and prototxt in `outputs/caffe_model`. + then you can check the Caffe model and prototxt in `./caffe_R50_model`. -2. Change `prototxt` following next two steps: +2. Change `prototxt` following next three steps: - 1) Modify `avg_pooling` in `baseline_R50.prototxt` + 1) Modify `MaxPooling` in `baseline_R50.prototxt` and delete `ceil_mode: false`. + + 2) Add `avg_pooling` in `baseline_R50.prototxt` ```prototxt layer { diff --git a/tools/deploy/caffe_export.py b/tools/deploy/caffe_export.py index 46142b0..503c6fd 100644 --- a/tools/deploy/caffe_export.py +++ b/tools/deploy/caffe_export.py @@ -10,7 +10,7 @@ import sys import torch -sys.path.append('../../') +sys.path.append('.') import pytorch_to_caffe from fastreid.config import get_cfg @@ -20,8 +20,8 @@ from fastreid.utils.checkpoint import Checkpointer from fastreid.utils.logger import setup_logger # import some modules added in project like this below -# sys.path.append('../projects/FastCls') -# from fastcls import * +# sys.path.append("projects/PartialReID") +# from partialreid import * setup_logger(name='fastreid') logger = logging.getLogger("fastreid.caffe_export") @@ -29,7 +29,6 @@ logger = logging.getLogger("fastreid.caffe_export") def setup_cfg(args): cfg = get_cfg() - # add_cls_config(cfg) cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() @@ -69,8 +68,7 @@ if __name__ == '__main__': cfg.defrost() cfg.MODEL.BACKBONE.PRETRAIN = False - if cfg.MODEL.HEADS.POOL_LAYER == 'FastGlobalAvgPool': - cfg.MODEL.HEADS.POOL_LAYER = 'GlobalAvgPool' + cfg.MODEL.HEADS.POOL_LAYER = "Identity" cfg.MODEL.BACKBONE.WITH_NL = False model = build_model(cfg) diff --git a/tools/deploy/caffe_inference.py b/tools/deploy/caffe_inference.py index ca2a7a6..ec6882a 100644 --- a/tools/deploy/caffe_inference.py +++ b/tools/deploy/caffe_inference.py @@ -91,5 +91,5 @@ if __name__ == "__main__": net.blobs["blob1"].data[...] = image feat = net.forward()["output"] feat = normalize(feat[..., 0, 0], axis=1) - np.save(os.path.join(args.output, path.replace('.jpg', '.npy').split('/')[-1]), feat) + np.save(os.path.join(args.output, os.path.basename(path).split('.')[0] + '.npy'), feat) diff --git a/tools/deploy/onnx_export.py b/tools/deploy/onnx_export.py index 81572cb..108c2de 100644 --- a/tools/deploy/onnx_export.py +++ b/tools/deploy/onnx_export.py @@ -4,6 +4,7 @@ @contact: sherlockliao01@gmail.com """ +import logging import os import argparse import io @@ -15,7 +16,7 @@ import torch from onnxsim import simplify from torch.onnx import OperatorExportTypes -sys.path.append('../../') +sys.path.append('.') from fastreid.config import get_cfg from fastreid.modeling.meta_arch import build_model @@ -24,10 +25,12 @@ from fastreid.utils.checkpoint import Checkpointer from fastreid.utils.logger import setup_logger # import some modules added in project like this below -# sys.path.append('../../projects/FastDistill') +# sys.path.append("projects/FastDistill") # from fastdistill import * -logger = setup_logger(name='onnx_export') + +setup_logger(name="fastreid") +logger = logging.getLogger("fastreid.onnx_export") def setup_cfg(args): @@ -149,7 +152,7 @@ if __name__ == '__main__': model.eval() logger.info(model) - inputs = torch.randn(args.batch_size, 3, cfg.INPUT.SIZE_TEST[0], cfg.INPUT.SIZE_TEST[1]) + inputs = torch.randn(args.batch_size, 3, cfg.INPUT.SIZE_TEST[0], cfg.INPUT.SIZE_TEST[1]).to(model.device) onnx_model = export_onnx_model(model, inputs) model_simp, check = simplify(onnx_model) diff --git a/tools/deploy/trt_export.py b/tools/deploy/trt_export.py index ea518ab..5f569e5 100644 --- a/tools/deploy/trt_export.py +++ b/tools/deploy/trt_export.py @@ -12,12 +12,11 @@ import tensorrt as trt from trt_calibrator import FeatEntropyCalibrator -sys.path.append('../../') +sys.path.append('.') -from fastreid.utils.logger import setup_logger -from fastreid.utils.file_io import PathManager +from fastreid.utils.logger import setup_logger, PathManager -logger = setup_logger(name='trt_export') +logger = setup_logger(name="trt_export") def get_parser(): diff --git a/tools/deploy/trt_inference.py b/tools/deploy/trt_inference.py index ea7d547..1f9ea84 100644 --- a/tools/deploy/trt_inference.py +++ b/tools/deploy/trt_inference.py @@ -197,4 +197,4 @@ if __name__ == "__main__": # the model expects RGB inputs cvt_img = img[:, :, ::-1] feat = trt.inference_on_images([cvt_img]) - np.save(os.path.join(args.output, os.path.basename(img_path) + '.npy'), feat[0]) + np.save(os.path.join(args.output, os.path.basename(img_path).split('.')[0] + '.npy'), feat)