drfcn_detector.py
Go to the documentation of this file.
00001 # --------------------------------------------------------
00002 # Deformable Convolutional Networks
00003 # Copyright (c) 2017 Microsoft
00004 # Licensed under The Apache-2.0 License [see LICENSE for details]
00005 # Written by Yi Li, Haocheng Zhang
00006 # --------------------------------------------------------
00007 import os
00008 import sys
00009 import numpy as np
00010 import argparse
00011 import logging
00012 import pprint
00013 import cv2
00014 
00015 import rospkg
00016 
00017 # TODO: When the code in setup.py starts working, then make sure to remove this
00018 # paths hack as well
00019 def add_path(path):
00020     if path not in sys.path:
00021         sys.path.insert(0, path)
00022 rospack = rospkg.RosPack()
00023 LIB_PATH = os.path.join(rospack.get_path('rail_object_detector'), 'libs', 'drfcn')
00024 add_path(LIB_PATH)
00025 
00026 # Start importing mxnet
00027 from utils.image import resize, transform
00028 from cfg.drfcn_config import config, update_config
00029 
00030 # get config
00031 os.environ['PYTHONUNBUFFERED'] = '1'
00032 os.environ['MXNET_CUDNN_AUTOTUNE_DEFAULT'] = '0'
00033 os.environ['MXNET_ENABLE_GPU_P2P'] = '0'
00034 update_config(os.path.join(LIB_PATH, 'cfg', 'rfcn_coco_demo.yaml'))
00035 
00036 # actually import mxnet
00037 import mxnet as mx
00038 from core.tester import im_detect, Predictor
00039 from symbols import *
00040 from utils.load_model import load_param
00041 from utils.show_boxes import show_boxes
00042 from utils.tictoc import tic, toc
00043 from nms.nms import py_nms_wrapper, cpu_nms_wrapper, gpu_nms_wrapper
00044 
00045 
00046 class Detector:
00047     def __init__(
00048         self,
00049         model_path=os.path.join(LIB_PATH, 'model', 'rfcn_dcn_coco'),
00050         hd_images=False
00051     ):
00052         # IF we are guaranteed QHD / HD, we can remove scaling and resizing and be a bit faster
00053         # if hd_images:
00054         #     config.SCALES = [(1080, 1820)]
00055         # else:
00056         #     config.SCALES = [(540, 960)]
00057         pprint.pprint(config)
00058         config.symbol = 'resnet_v1_101_rfcn_dcn'
00059         sym_instance = eval(config.symbol + '.' + config.symbol)()
00060         sym = sym_instance.get_symbol(config, is_train=False)
00061 
00062         # set up class names
00063         self.num_classes = 81
00064         self.classes = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat',
00065                    'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse',
00066                    'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
00067                    'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove',
00068                    'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon',
00069                    'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut',
00070                    'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse',
00071                    'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book',
00072                    'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
00073 
00074         # get predictor
00075         self.data_names = ['data', 'im_info']
00076         self.label_names = []
00077         data = []
00078         sam = np.zeros((config.SCALES[0][0], config.SCALES[0][1], 3))
00079         im, im_scale = resize(sam, config.SCALES[0][0], config.SCALES[0][1], stride=config.network.IMAGE_STRIDE)
00080         im_tensor = transform(im, config.network.PIXEL_MEANS)
00081         im_info = np.array([[im_tensor.shape[2], im_tensor.shape[3], im_scale]], dtype=np.float32)
00082         data.append({'data': im_tensor, 'im_info': im_info})
00083         data = [[mx.nd.array(data[i][name]) for name in self.data_names] for i in xrange(len(data))]
00084 
00085         max_data_shape = [[('data', (1, 3, max([v[0] for v in config.SCALES]), max([v[1] for v in config.SCALES])))]]
00086         provide_data = [[(k, v.shape) for k, v in zip(self.data_names, data[i])] for i in xrange(len(data))]
00087         provide_label = [None for i in xrange(len(data))]
00088         arg_params, aux_params = load_param(model_path, 0, process=True)
00089         self.predictor = Predictor(sym, self.data_names, self.label_names,
00090                                    context=[mx.gpu(0)], max_data_shapes=max_data_shape,
00091                                    provide_data=provide_data, provide_label=provide_label,
00092                                    arg_params=arg_params, aux_params=aux_params)
00093         self.nms = gpu_nms_wrapper(config.TEST.NMS, 0)
00094 
00095     def detect_objects(self, input_image):
00096         data = []
00097         target_size = config.SCALES[0][0]
00098         max_size = config.SCALES[0][1]
00099         im, im_scale = resize(input_image, target_size, max_size, stride=config.network.IMAGE_STRIDE)
00100         im_tensor = transform(input_image, config.network.PIXEL_MEANS)
00101         im_info = np.array([[im_tensor.shape[2], im_tensor.shape[3], im_scale]], dtype=np.float32)
00102         data.append({'data': im_tensor, 'im_info': im_info})
00103         data = [[mx.nd.array(data[i][name]) for name in self.data_names] for i in xrange(len(data))]
00104 
00105         data_batch = mx.io.DataBatch(data=[data[0]], label=[], pad=0, index=0,
00106                                      provide_data=[[(k, v.shape) for k, v in zip(self.data_names, data[0])]],
00107                                      provide_label=[None])
00108         scales = [im_scale]
00109         scores, boxes, data_dict = im_detect(self.predictor, data_batch, self.data_names, scales, config)
00110         boxes = boxes[0].astype('f')
00111         scores = scores[0].astype('f')
00112         dets_nms = []
00113         for j in range(1, scores.shape[1]):
00114             cls_scores = scores[:, j, np.newaxis]
00115             cls_boxes = boxes[:, 4:8] if config.CLASS_AGNOSTIC else boxes[:, j * 4:(j + 1) * 4]
00116             cls_dets = np.hstack((cls_boxes, cls_scores))
00117             keep = self.nms(cls_dets)
00118             cls_dets = cls_dets[keep, :]
00119             cls_dets = cls_dets[cls_dets[:, -1] > 0.7, :]
00120             cls_dets[:4] *= im_scale
00121             dets_nms.append(cls_dets)
00122         # im = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
00123         # show_boxes(im, dets_nms, self.classes, 1)
00124         return dets_nms
00125 


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:30