From 759a9fb2c1d8aaaca974248658aa4e2534b9331d Mon Sep 17 00:00:00 2001 From: Adam Czyzewski Date: Wed, 5 Sep 2018 11:55:57 +0000 Subject: [PATCH 1/2] Fix compatibility issues. --- README.md | 2 +- inference.py | 22 ++--- inference_video_file.py | 174 ++++++++++++++++++++++++++++++++++++++++ install.sh | 4 + 4 files changed, 190 insertions(+), 12 deletions(-) create mode 100755 inference_video_file.py create mode 100755 install.sh diff --git a/README.md b/README.md index e8a9707..7e17b78 100755 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ To run inference on a directory of image files (`demo/*.jpg` in this example), y First, place `inference.py` file in Detectron `tools` directory and `visualize.py` file in Detectron `lib/utils` directory. Then, run the following command: ``` -python2 tools/infererence.py \ +python2 tools/inference.py \ --cfg configs/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml \ --output-dir /tmp/detectron-visualizations \ --image-ext jpg \ diff --git a/inference.py b/inference.py index 2160d0a..5ccd692 100755 --- a/inference.py +++ b/inference.py @@ -37,15 +37,15 @@ from caffe2.python import workspace -from core.config import assert_and_infer_cfg -from core.config import cfg -from core.config import merge_cfg_from_file -from utils.timer import Timer -import core.test_engine as infer_engine -import datasets.dummy_datasets as dummy_datasets -import utils.c2 as c2_utils -import utils.logging -import utils.vis as vis_utils +from detectron.core.config import assert_and_infer_cfg +from detectron.core.config import cfg +from detectron.core.config import merge_cfg_from_file +from detectron.utils.timer import Timer +import detectron.core.test_engine as infer_engine +import detectron.datasets.dummy_datasets as dummy_datasets +import detectron.utils.c2 as c2_utils +import detectron.utils.logging as log_utils +import detectron.utils.vis as vis_utils @@ -100,7 +100,7 @@ def main(args): cfg.TEST.WEIGHTS = args.weights cfg.NUM_GPUS = 1 assert_and_infer_cfg() - model = infer_engine.initialize_model_from_cfg() + model = infer_engine.initialize_model_from_cfg(args.weights) dummy_coco_dataset = dummy_datasets.get_coco_dataset() if os.path.isdir(args.im_or_folder): @@ -155,6 +155,6 @@ def main(args): if __name__ == '__main__': workspace.GlobalInit(['caffe2', '--caffe2_log_level=0']) - utils.logging.setup_logging(__name__) + log_utils.setup_logging(__name__) args = parse_args() main(args) diff --git a/inference_video_file.py b/inference_video_file.py new file mode 100755 index 0000000..1f23340 --- /dev/null +++ b/inference_video_file.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python2 + +# Copyright (c) 2017-present, Facebook, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +############################################################################## + +"""Perform a simple inference on a single image or all images with a certain extension +(e.g., .jpg) in a folder. + +Original source: https://github.com/facebookresearch/Detectron/blob/master/tools/infer_simple.py +""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + +from collections import defaultdict +import argparse +import cv2 # NOQA (Must import before importing caffe2 due to bug in cv2) +import glob +import logging +import os +import sys +import time + +from caffe2.python import workspace + +from detectron.core.config import assert_and_infer_cfg +from detectron.core.config import cfg +from detectron.core.config import merge_cfg_from_file +from detectron.utils.timer import Timer +import detectron.core.test_engine as infer_engine +import detectron.datasets.dummy_datasets as dummy_datasets +import detectron.utils.c2 as c2_utils +import detectron.utils.logging as log_utils +import detectron.utils.vis as vis_utils + +c2_utils.import_detectron_ops() + +# OpenCL may be enabled by default in OpenCV3; disable it because it's not +# thread safe and causes unwanted GPU memory allocations. +cv2.ocl.setUseOpenCL(False) + +def parse_args(): + parser = argparse.ArgumentParser(description='End-to-end inference') + parser.add_argument( + '--cfg', + dest='cfg', + help='cfg model file (/path/to/model_config.yaml)', + default=None, + type=str + ) + parser.add_argument( + '--wts', + dest='weights', + help='weights model file (/path/to/model_weights.pkl)', + default=None, + type=str + ) + parser.add_argument( + '--output-dir', + dest='output_dir', + help='directory for visualization pdfs (default: /tmp/infer_simple)', + default='/tmp/infer_simple', + type=str + ) + + parser.add_argument( + '--file', + dest='file', + help='source video file', + default='video.mp4', + type=str + ) + + parser.add_argument( + '--image-ext', + dest='image_ext', + help='image file name extension (default: jpg)', + default='jpg', + type=str + ) + parser.add_argument( + 'im_or_folder', help='image or folder of images', default=None + ) + if len(sys.argv) == 1: + parser.print_help() + sys.exit(1) + return parser.parse_args() + + +def main(args): + logger = logging.getLogger(__name__) + merge_cfg_from_file(args.cfg) + cfg.TEST.WEIGHTS = args.weights + cfg.NUM_GPUS = 1 + assert_and_infer_cfg() + model = infer_engine.initialize_model_from_cfg(args.weights) + dummy_coco_dataset = dummy_datasets.get_coco_dataset() + + if os.path.isdir(args.im_or_folder): + im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext) + else: + im_list = [args.im_or_folder] + + """ + Add support for webcam + """ + + # Set source + cam = cv2.VideoCapture(args.file) + frame_id = 0 + + while cam.isOpened(): + + # Fetch image from file + _, im = cam.read() + + timers = defaultdict(Timer) + t = time.time() + + with c2_utils.NamedCudaScope(0): + cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( + model, im, None, timers=timers + ) + + logger.info('Inference time: {:.3f}s'.format(time.time() - t)) + + for k, v in timers.items(): + logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) + + if frame_id == 0: + logger.info( + ' \ Note: inference on the first image will be slower than the ' + 'rest (caches and auto-tuning need to warm up)' + ) + + im_name = "{}_frame_{}".format(args.file.split('/')[-1][:-4], str(frame_id).zfill(5)) + frame_id += 1 + + vis_utils.vis_one_image( + im[:, :, ::-1], # BGR -> RGB for visualization + im_name, + args.output_dir, + cls_boxes, + cls_segms, + cls_keyps, + dataset=dummy_coco_dataset, + box_alpha=0.3, + show_class=True, + thresh=0.7, + kp_thresh=2, + ext='jpg' # default is PDF, but we want JPG. + ) + + cv2.destroyAllWindows() + +if __name__ == '__main__': + workspace.GlobalInit(['caffe2', '--caffe2_log_level=0']) + log_utils.setup_logging(__name__) + args = parse_args() + main(args) diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..6117078 --- /dev/null +++ b/install.sh @@ -0,0 +1,4 @@ +DETECTRON_HOME=~/detectron +cp ./inference.py $DETECTRON_HOME/tools/ +cp ./inference_video_file.py $DETECTRON_HOME/tools/ +cp ./visualize.py $DETECTRON_HOME/detectron/utils From af66c10e5cfc093454af61d326fd660e43fbc572 Mon Sep 17 00:00:00 2001 From: Adam Czyzewski Date: Thu, 6 Sep 2018 07:51:50 +0000 Subject: [PATCH 2/2] Fix cache issue. --- inference_video_file.py | 77 ++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/inference_video_file.py b/inference_video_file.py index 1f23340..e36bd23 100755 --- a/inference_video_file.py +++ b/inference_video_file.py @@ -46,6 +46,7 @@ import detectron.utils.c2 as c2_utils import detectron.utils.logging as log_utils import detectron.utils.vis as vis_utils +from detectron.utils.io import cache_url c2_utils.import_detectron_ops() @@ -107,6 +108,8 @@ def main(args): cfg.TEST.WEIGHTS = args.weights cfg.NUM_GPUS = 1 assert_and_infer_cfg() + + args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE) model = infer_engine.initialize_model_from_cfg(args.weights) dummy_coco_dataset = dummy_datasets.get_coco_dataset() @@ -128,43 +131,47 @@ def main(args): # Fetch image from file _, im = cam.read() - timers = defaultdict(Timer) - t = time.time() - - with c2_utils.NamedCudaScope(0): - cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( - model, im, None, timers=timers + try: + timers = defaultdict(Timer) + t = time.time() + + with c2_utils.NamedCudaScope(0): + cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all( + model, im, None, timers=timers + ) + + logger.info('Inference time: {:.3f}s'.format(time.time() - t)) + + for k, v in timers.items(): + logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) + + if frame_id == 0: + logger.info( + ' \ Note: inference on the first image will be slower than the ' + 'rest (caches and auto-tuning need to warm up)' + ) + + im_name = "{}_frame_{}".format(args.file.split('/')[-1][:-4], str(frame_id).zfill(5)) + frame_id += 1 + + vis_utils.vis_one_image( + im[:, :, ::-1], # BGR -> RGB for visualization + im_name, + args.output_dir, + cls_boxes, + cls_segms, + cls_keyps, + dataset=dummy_coco_dataset, + box_alpha=0.3, + show_class=True, + thresh=0.7, + kp_thresh=2, + ext='png' # default is PDF, but we want JPG. ) - logger.info('Inference time: {:.3f}s'.format(time.time() - t)) - - for k, v in timers.items(): - logger.info(' | {}: {:.3f}s'.format(k, v.average_time)) - - if frame_id == 0: - logger.info( - ' \ Note: inference on the first image will be slower than the ' - 'rest (caches and auto-tuning need to warm up)' - ) - - im_name = "{}_frame_{}".format(args.file.split('/')[-1][:-4], str(frame_id).zfill(5)) - frame_id += 1 - - vis_utils.vis_one_image( - im[:, :, ::-1], # BGR -> RGB for visualization - im_name, - args.output_dir, - cls_boxes, - cls_segms, - cls_keyps, - dataset=dummy_coco_dataset, - box_alpha=0.3, - show_class=True, - thresh=0.7, - kp_thresh=2, - ext='jpg' # default is PDF, but we want JPG. - ) - + except: + break + cv2.destroyAllWindows() if __name__ == '__main__':