forked from ramiro050/lazy-tensor-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazytensor_maskrcnn_example.py
71 lines (51 loc) · 2.2 KB
/
lazytensor_maskrcnn_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Runs the MaskRCNN on input image with Lazy Tensor Core/TorchScript backend.
Requirements to run example:
- `opencv-python` Python package
- `lazy_tensor_core` Python package
For information on how to obtain the `lazy_tensor_core` Python package,
see here:
https://github.com/pytorch/pytorch/blob/lazy_tensor_staging/lazy_tensor_core/QUICKSTART.md
- `maskrcnn_benchmark` Python package
For information on how to obtain the this Python package, see here:
https://github.com/facebookresearch/maskrcnn-benchmark/blob/main/INSTALL.md
To run the example, make sure `/path/to/maskrcnn-benchmark/demo` and
`/path/to/pytorch/lazy_tensor_core` is in your PYTHONPATH. Then, run
python lazytensor_maskrcnn_example.py img_path
where `img_path` is the path to the sample image to run the model on.
The output of this example can be found in
`lazytensor_maskrcnn_example_output.txt`
"""
import argparse
import pathlib
from maskrcnn_benchmark.config import cfg
from predictor import COCODemo
import lazy_tensor_core as ltc
from lazy_tensor_core.debug import metrics
import cv2
ltc._LAZYC._ltc_init_ts_backend()
def setup_argparse() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description='Run MaskRCNN on Lazy Tensor Core')
parser.add_argument('img_path', type=pathlib.Path,
help='Path to image to run model on')
parser.add_argument('maskrcnn_path', type=pathlib.Path,
help='Path to `maskrcnn-benchmark` repo. ' \
'Used to get configuration information.')
return parser
def main():
args = setup_argparse().parse_args()
print('Loading image...')
image = cv2.imread(str(args.img_path))
rel_config_file = 'configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml'
config_file = args.maskrcnn_path / rel_config_file
cfg.merge_from_file(config_file)
cfg.merge_from_list(["MODEL.DEVICE", "lazy"])
coco_demo = COCODemo(cfg, min_image_size=50,
confidence_threshold=0.7)
print('Running model on image...')
coco_demo.run_on_opencv_image(image)
print('Metrics Report:')
print(metrics.metrics_report())
if __name__ == '__main__':
main()