From 1dc8393364b783f61b579545090b6270adec65df Mon Sep 17 00:00:00 2001 From: GregChu Date: Tue, 30 Jul 2019 13:47:01 -0700 Subject: [PATCH 1/4] adding MMDetection inference module --- .../images/detectors/mm_detector.py | 131 ++++++++++++++++++ multivitamin/version.py | 2 +- requirements.txt | 4 +- 3 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 multivitamin/applications/images/detectors/mm_detector.py diff --git a/multivitamin/applications/images/detectors/mm_detector.py b/multivitamin/applications/images/detectors/mm_detector.py new file mode 100644 index 0000000..9adc777 --- /dev/null +++ b/multivitamin/applications/images/detectors/mm_detector.py @@ -0,0 +1,131 @@ +"""MMDetection detector +https://github.com/open-mmlab/mmdetection/ +""" +import os +import sys +import glog as log +import numpy as np +import traceback + +glog_level = os.environ.get("GLOG_minloglevel", None) + +if glog_level is None: + os.environ["GLOG_minloglevel"] = "1" + log.info("GLOG_minloglevel isn't set. Setting level to 1 (info)") + log.info( + "\nGLOG_minloglevel levels are...\n\ + 0 -- Debug\n\ + 1 -- Info\n\ + 2 -- Warning\n\ + 3 -- Error" + ) + +try: + import mmcv +except ImportError: + raise ImportError("package: mmcv not found") + +try: + from mmdet.apis import init_detector, inference_detector +except ImportError: + raise ImportError("module: mmdet.apis not found") + +from multivitamin.module import ImagesModule +from multivitamin.data.response.utils import ( + crop_image_from_bbox_contour, + compute_box_area +) +from multivitamin.data.response.dtypes import ( + create_bbox_contour_from_points, + Region, + Property, +) +from multivitamin.applications.utils import load_idmap + + +class MMDetector(ImagesModule): + def __init__( + self, + server_name, + version, + config_file, + model_file, + confidence_min=0.3, + prop_type=None, + prop_id_map=None, + module_id_map=None, + gpuid=0, + ): + """Inference module for https://github.com/open-mmlab/mmdetection/ + + Args: + server_name (str): module name + version (str): version + config_file (str): path to config_file + model_file (str): path to model_file + confidence_min (float, optional): Defaults to 0.3. + prop_type (str, optional): Defaults to None. + prop_id_map (dict, optional): Defaults to None. + module_id_map (dict, optional): Defaults to None. + gpuid (int, optional): Defaults to 0. + """ + super().__init__( + server_name, + version, + prop_type=prop_type, + prop_id_map=prop_id_map, + module_id_map=module_id_map, + ) + self.confidence_min = confidence_min + if not self.prop_type: + self.prop_type = "object" + + log.info(f"Loading config_file: {config_file} and model_file: {model_file}") + cfge = mmcv.Config.fromfile(config_file) + log.debug(f"config: {cfge}") + self.model = None + if not (os.path.exists(config_file) and os.path.exists(model_file)): + raise ValueError("config_file and/or model_file does not exist") + try: + self.model = init_detector(config_file, model_file, device="cuda:{}".format(gpuid)) + except: + raise ValueError("Could not init_detector") + + def process_images(self, images, tstamps, prev_regions=None): + for frame, tstamp in zip(images, tstamps): + predictions = inference_detector(self.model, frame) + + for pred_idx, pred in enumerate(predictions): + try: + for xmin, ymin, xmax, ymax, confidence in pred: + if confidence < self.confidence_min: + continue + label = self.model.CLASSES[pred_idx] + + xmin_n = xmin / frame.shape[1] + xmax_n = xmax / frame.shape[1] + ymin_n = ymin / frame.shape[0] + ymax_n = ymax / frame.shape[0] + + contour = create_bbox_contour_from_points( + xmin_n, ymin_n, xmax_n, ymax_n, bound=True + ) + + area = compute_box_area(contour) + + prop = Property( + confidence=float(confidence), + confidence_min=self.confidence_min, + ver=self.version, + server=self.name, + value=label, + property_type=self.prop_type, + fraction=area, + ) + + self.response.append_region( + t=tstamp, region=Region(contour=contour, props=[prop]) + ) + + except Exception: + log.error(traceback.format_exc()) diff --git a/multivitamin/version.py b/multivitamin/version.py index c547f7e..ea3f6c7 100644 --- a/multivitamin/version.py +++ b/multivitamin/version.py @@ -1 +1 @@ -__version__ = "1.3.34" +__version__ = "1.3.35" diff --git a/requirements.txt b/requirements.txt index 856f7fb..0354dff 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ botocore==1.12.86 certifi chardet==3.0.4 click==6.7 -Cython==0.28.3 +Cython==0.27.3 decorator==4.3.0 docopt==0.6.2 docutils==0.14 @@ -23,7 +23,7 @@ jmespath==0.9.3 MarkupSafe==1.0 more-itertools==4.2.0 networkx==2.1 -numpy==1.14.5 +numpy>=1.16.0 pandas==0.23.1 Pillow==5.1.0 pipreqs==0.4.9 From cb4e861f78b961a702564f871d3d25ce92c32ea4 Mon Sep 17 00:00:00 2001 From: GregChu Date: Tue, 13 Aug 2019 15:09:35 -0700 Subject: [PATCH 2/4] tool to push messages to sqs --- tools/push_sqs.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 tools/push_sqs.py diff --git a/tools/push_sqs.py b/tools/push_sqs.py new file mode 100644 index 0000000..5685916 --- /dev/null +++ b/tools/push_sqs.py @@ -0,0 +1,21 @@ +import sys +import argparse + +import boto3 + +sqs = boto3.resource('sqs') +a = argparse.ArgumentParser() +a.add_argument("--queue_name") +a.add_argument("--request_txt") +args = a.parse_args() + +reqs = [] +with open(args.request_txt) as rf: + reqs = rf.readlines() + +reqs = [r.strip() for r in reqs] +queue = sqs.get_queue_by_name(QueueName=args.queue_name) +for req in reqs: + resp = queue.send_message(MessageBody=req) + print(resp) + From 2f0e5835056e458f992b4641e0f02ebcd1aad05d Mon Sep 17 00:00:00 2001 From: Javier Molina Date: Thu, 19 Sep 2019 13:01:10 -0700 Subject: [PATCH 3/4] dummy forward pass in the creation --- multivitamin/applications/images/detectors/mm_detector.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/multivitamin/applications/images/detectors/mm_detector.py b/multivitamin/applications/images/detectors/mm_detector.py index 9adc777..90015bf 100644 --- a/multivitamin/applications/images/detectors/mm_detector.py +++ b/multivitamin/applications/images/detectors/mm_detector.py @@ -88,6 +88,11 @@ def __init__( raise ValueError("config_file and/or model_file does not exist") try: self.model = init_detector(config_file, model_file, device="cuda:{}".format(gpuid)) + dummy_image= np.zeros((720, 1280, 3), dtype = "uint8") + images=[dummy_image] + tstamps=[0.0] + self.process_images(images, tstamps) + log.info('dummy forward pass successful') except: raise ValueError("Could not init_detector") From d3b85214e5cecf74a51df46a714f0254a46c43fb Mon Sep 17 00:00:00 2001 From: Javier Molina Date: Thu, 19 Sep 2019 13:10:04 -0700 Subject: [PATCH 4/4] Upticking version --- multivitamin/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/multivitamin/version.py b/multivitamin/version.py index ea3f6c7..4c7fd2f 100644 --- a/multivitamin/version.py +++ b/multivitamin/version.py @@ -1 +1 @@ -__version__ = "1.3.35" +__version__ = "1.3.36"