Esempio n. 1
0
def preprocess_voc():
    """Resizes all VOC images to 300x300 and saves them into .ppm files.

    This script assumes all images fetched to network in batches have size 300x300,
    so in this function we preproceess all VOC images to fit that format.
    """
    voc_root = PATHS.get_voc_dir_path()
    voc_jpegs = glob.glob(os.path.join(voc_root, 'JPEGImages', '*.jpg'))
    voc_ppms = glob.glob(os.path.join(voc_root, 'PPMImages', '*.ppm'))

    # Check if preprocessing is needed by comparing
    # image names between JPEGImages and PPMImages
    voc_jpegs_basenames = \
        [os.path.splitext(os.path.basename(p))[0] for p in voc_jpegs]
    voc_ppms_basenames = \
        [os.path.splitext(os.path.basename(p))[0] for p in voc_ppms]
    # If lists are not the same, preprocessing is needed
    if sorted(voc_jpegs_basenames) != sorted(voc_ppms_basenames):
        print("Preprocessing VOC dataset. It may take few minutes.")
        # Make PPMImages directory if it doesn't exist
        voc_ppms_path = PATHS.get_voc_ppm_img_path()
        if not os.path.exists(os.path.dirname(voc_ppms_path)):
            os.makedirs(os.path.dirname(voc_ppms_path))
        # For each .jpg file, make a resized
        # .ppm copy to fit model input expectations
        for voc_jpeg_path in voc_jpegs:
            voc_jpeg_basename = os.path.basename(voc_jpeg_path)
            voc_ppm_path = voc_ppms_path.format(
                os.path.splitext(voc_jpeg_basename)[0])
            if not os.path.exists(voc_ppm_path):
                img_pil = Image.open(voc_jpeg_path)
                img_pil = img_pil.resize(size=(ModelData.get_input_width(),
                                               ModelData.get_input_height()),
                                         resample=Image.BILINEAR)
                img_pil.save(voc_ppm_path)
Esempio n. 2
0
def extract_voc_data_if_needed():
    if os.path.exists(PATHS.get_voc_dir_path()):
        return
    voc_archive_path = PATHS.get_data_file_path('VOCtest_06-Nov-2007.tar')
    print("Unpacking {}".format(voc_archive_path))
    with tarfile.open(voc_archive_path, "r") as tar:
        tar.extractall(path=PATHS.get_sample_root())
    print("Unpacking done!")
Esempio n. 3
0
def parse_commandline_arguments():
    """Parses command line arguments and adjusts internal data structures."""

    # Define script command line arguments
    parser = argparse.ArgumentParser(description='Run object detection evaluation on VOC2007 dataset.')
    parser.add_argument('inference_backend', metavar='INFERENCE_BACKEND',
        type=str, choices=['tensorrt', 'tensorflow'], default='tensorrt', nargs='?',
        help='inference backend to run evaluation with')
    parser.add_argument('-p', '--precision', type=int, choices=[32, 16], default=32,
        help='desired TensorRT float precision to build an engine with')
    parser.add_argument('-b', '--max_batch_size', type=int, default=64,
        help='max TensorRT engine batch size')
    parser.add_argument('-f', '--force_inference', action='store_true',
        help='force model inference even if detections exist')
    parser.add_argument('-w', '--workspace_dir',
        help='sample workspace directory')
    parser.add_argument('-fc', '--flatten_concat',
        help='path of built FlattenConcat plugin')
    parser.add_argument('-voc', '--voc_dir',
        help='VOC2007 root directory')

    # Parse arguments passed
    args = parser.parse_args()

    # Adjust global Paths data structure
    adjust_paths(args)

    # Verify Paths after adjustments. This also exits script if verification fails
    PATHS.verify_all_paths(should_verify_voc=True)

    # Fetch directory to save inference results to, create it if it doesn't exist
    trt_engine_datatype = None
    trt_engine_path = None
    if args.inference_backend == 'tensorrt':
        # In case of TensorRT we also fetch engine data type and engine path
        trt_engine_datatype = TRT_PRECISION_TO_DATATYPE[args.precision]
        trt_engine_path = PATHS.get_engine_path(trt_engine_datatype,
            args.max_batch_size)
        if not os.path.exists(os.path.dirname(trt_engine_path)):
            os.makedirs(os.path.dirname(trt_engine_path))
        results_dir = PATHS.get_voc_model_detections_path('tensorrt',
            trt_engine_datatype)
    elif args.inference_backend == 'tensorflow':
        results_dir = PATHS.get_voc_model_detections_path('tensorflow')
    if not os.path.exists(results_dir):
        os.makedirs(results_dir)

    # Return parsed arguments for further functions to use
    parsed = {
        'inference_backend': args.inference_backend,
        'max_batch_size': args.max_batch_size,
        'force_inference': args.force_inference,
        'results_dir': results_dir,
        'trt_engine_path': trt_engine_path,
        'trt_engine_datatype': trt_engine_datatype
    }
    return parsed
def main():
    # Parse arguments
    args = parse_commandline_arguments()

    # Loading FlattenConcat plugin library using CDLL has a side
    # effect of loading FlattenConcat plugin into internal TensorRT
    # PluginRegistry data structure. This will be needed when parsing
    # network into UFF, since some operations will need to use this plugin
    try:
        ctypes.CDLL(PATHS.get_flatten_concat_plugin_path())
    except:
        print("Error: {}\n{}\n{}".format(
            "Could not find {}".format(PATHS.get_flatten_concat_plugin_path()),
            "Make sure you have compiled FlattenConcat custom plugin layer",
            "For more details, check README.md"))
        sys.exit(1)

    # build engine
    trt_inference_wrapper = inference_utils.TRTInference(
        args.engine_path, args.model_file, trt.DataType.HALF, 1)

    inference_time = []
    cnt = 0
    cap = cv2.VideoCapture(args.video_file)

    start = datetime.now()
    success, frame = cap.read()

    while success:
        cnt += 1
        frame = preprocess_frame(frame)

        start_infer = datetime.now()
        trt_inference_wrapper.infer_numpy(frame)
        end_infer = datetime.now()

        inference_time.append(
            (end_infer - start_infer).total_seconds() * 1000.0)
        success, frame = cap.read()
    end = datetime.now()

    time_delta = (end - start).total_seconds()
    fps = cnt / time_delta

    print(
        "==============================================================================="
    )
    print("Process time (exclude load model time): {time:.2f}s".format(
        time=time_delta))
    print("Total #frames: {cnt}".format(cnt=cnt))
    print("Process FPS (exclude load model time): {time:.2f}".format(time=fps))
    print("Average inference time is: {time:.2f}ms".format(
        time=np.mean(inference_time)))
    print(
        "==============================================================================="
    )
Esempio n. 5
0
def main():
    # Parse command line arguments
    args = parse_commandline_arguments()

    # Loading FlattenConcat plugin library using CDLL has a side
    # effect of loading FlattenConcat plugin into internal TensorRT
    # PluginRegistry data structure. This will be needed when parsing
    # network into UFF, since some operations will need to use this plugin
    try:
        ctypes.CDLL(PATHS.get_flatten_concat_plugin_path())
    except:
        print(
            "Error: {}\n{}\n{}".format(
                "Could not find {}".format(PATHS.get_flatten_concat_plugin_path()),
                "Make sure you have compiled FlattenConcat custom plugin layer",
                "For more details, check README.md"
            )
        )
        sys.exit(1)

    # Fetch .uff model path, convert from .pb
    # if needed, using prepare_ssd_model
    ssd_model_uff_path = PATHS.get_model_uff_path(MODEL_NAME)
    if not os.path.exists(ssd_model_uff_path):
        model_utils.prepare_ssd_model(MODEL_NAME)

    # Set up all TensorRT data structures needed for inference
    trt_inference_wrapper = inference_utils.TRTInference(
        args.trt_engine_path, ssd_model_uff_path,
        trt_engine_datatype=args.trt_engine_datatype,
        batch_size=args.max_batch_size)

    # Start measuring time
    inference_start_time = time.time()

    # Get TensorRT SSD model output
    detection_out, keep_count_out = \
        trt_inference_wrapper.infer(args.input_img_path)

    # Make PIL.Image for drawing bounding boxes and
    # let analyze_prediction() draw them based on model output
    img_pil = Image.open(args.input_img_path)
    prediction_fields = len(TRT_PREDICTION_LAYOUT)
    for det in range(int(keep_count_out[0])):
        analyze_prediction(detection_out, det * prediction_fields, img_pil)

    # Output total [img load + inference + drawing bboxes] time
    print("Total time taken for one image: {} ms\n".format(
        int(round((time.time() - inference_start_time) * 1000))))

    # Save output image and output path
    img_pil.save(args.output)
    print("Saved output image to: {}".format(args.output))
def parse_commandline_arguments():
    """Parses command line arguments and adjusts internal data structures."""

    # Define script command line arguments
    parser = argparse.ArgumentParser(
        description='Run object detection inference on input image.')
    parser.add_argument('input_img_path',
                        metavar='INPUT_IMG_PATH',
                        help='an image file to run inference on')
    parser.add_argument(
        '-p',
        '--precision',
        type=int,
        choices=[32, 16],
        default=32,
        help='desired TensorRT float precision to build an engine with')
    parser.add_argument('-b',
                        '--max_batch_size',
                        type=int,
                        default=1,
                        help='max TensorRT engine batch size')
    parser.add_argument('-w',
                        '--workspace_dir',
                        help='sample workspace directory')
    parser.add_argument('-fc',
                        '--flatten_concat',
                        help='path of built FlattenConcat plugin')

    # Parse arguments passed
    args = parser.parse_args()

    # Set FlattenConcat TRT plugin path and
    # workspace dir path if passed by user
    if args.flatten_concat:
        PATHS.set_flatten_concat_plugin_path(args.flatten_concat)
    if args.workspace_dir:
        PATHS.set_workspace_dir_path(args.workspace_dir)
    if not os.path.exists(PATHS.get_workspace_dir_path()):
        os.makedirs(PATHS.get_workspace_dir_path())

    # Verify Paths after adjustments. This also exits script if verification fails
    PATHS.verify_all_paths()

    # Fetch TensorRT engine path and datatype
    trt_engine_datatype = TRT_PRECISION_TO_DATATYPE[args.precision]
    trt_engine_path = PATHS.get_engine_path(trt_engine_datatype,
                                            args.max_batch_size)
    if not os.path.exists(os.path.dirname(trt_engine_path)):
        os.makedirs(os.path.dirname(trt_engine_path))

    parsed = {
        'input_img_path': args.input_img_path,
        'max_batch_size': args.max_batch_size,
        'trt_engine_datatype': trt_engine_datatype,
        'trt_engine_path': trt_engine_path
    }
    return parsed
def do_python_eval(results_dir):
    cachedir = PATHS.get_voc_annotation_cache_path()
    aps = []
    for i, cls in enumerate(voc_utils.VOC_CLASSES_LIST):
        filename = get_voc_results_file_template(cls, results_dir)
        rec, prec, ap = voc_eval(
           filename,
           PATHS.get_voc_image_set_path(),
           cls, cachedir,
           ovthresh=0.5)
        aps += [ap]
        print('AP for {} = {:.4f}'.format(cls, ap))
    print('Mean AP = {:.4f}'.format(np.mean(aps)))
Esempio n. 8
0
def _extract_model(silent=False):
    """Extract model from Tensorflow model zoo.

    Args:
        silent (bool): if False, writes progress messages to stdout
    """
    maybe_print(not silent, "Preparing pretrained model")
    model_dir = PATHS.get_models_dir_path()
    maybe_mkdir(model_dir)
    model_archive_path = PATHS.get_data_file_path(
        'ssd_inception_v2_coco_2017_11_17.tar.gz')
    maybe_print(not silent, "Unpacking {}".format(model_archive_path))
    with tarfile.open(model_archive_path, "r:gz") as tar:
        tar.extractall(path=model_dir)
    maybe_print(not silent, "Model ready")
Esempio n. 9
0
def main():
    # Parse command line arguments
    args = parse_commandline_arguments()

    # Fetch .uff model path
    ssd_model_uff_path = PATHS.get_model_uff_path(MODEL_NAME)
    # Set up all TensorRT data structures needed for inference
    trt_inference_wrapper = TRTInference(
        args.trt_engine_path,
        ssd_model_uff_path,
        trt_engine_datatype=args.trt_engine_datatype,
        batch_size=args.max_batch_size)

    # Start measuring time
    inference_start_time = time.time()

    # Get TensorRT SSD model output
    detection_out, keep_count_out = \
        trt_inference_wrapper.infer(args.input_img_path)

    # Make PIL.Image for drawing bounding boxes and
    # let analyze_prediction() draw them based on model output
    img_pil = Image.open(args.input_img_path)
    prediction_fields = len(TRT_PREDICTION_LAYOUT)
    for det in range(int(keep_count_out[0])):
        analyze_prediction(detection_out, det * prediction_fields, img_pil)

    # Output total [img load + inference + drawing bboxes] time
    print("Total time taken for one image: {} ms\n".format(
        int(round((time.time() - inference_start_time) * 1000))))

    # Save output image and output path
    img_pil.save(args.output)
    print("Saved output image to: {}".format(args.output))
Esempio n. 10
0
def main():
    # Parse command line arguments
    args = parse_commandline_arguments()

    # Fetch .uff model path
    ssd_model_uff_path = PATHS.get_model_uff_path(MODEL_NAME)
    # convert from .pb if needed, using prepare_ssd_model
    if not os.path.exists(ssd_model_uff_path):
        model_utils.prepare_ssd_model(MODEL_NAME)
Esempio n. 11
0
    def __init__(self, proto, model, labelmap_file, gpu_mode, trt_mode, batch_size, resolution, precision):

        self.batch_size = batch_size

        TRT_PRECISION_TO_DATATYPE = {
            16: trt.DataType.HALF,
            32: trt.DataType.FLOAT
        }
        trt_engine_datatype = TRT_PRECISION_TO_DATATYPE[precision]

        tyolo_model_path = model
        tyolo_deploy_path = proto

        trt_engine_path = PATHS.get_engine_path(trt_engine_datatype, batch_size)
        print("trt_engine_path:", trt_engine_path)
        try:
            os.makedirs(os.path.dirname(trt_engine_path))
        except:
            pass

        # override native resolution with command line or prototxt
        try:
            import re
            if resolution != "":
                wh = resolution.split('x')
                w = int(wh[0])
                h = int(wh[1])

                f = open(tyolo_deploy_path, 'r')
                contents = f.read()
                f.close()
                contents = re.sub('(?<=input: "data"\ninput_shape {\n  dim: 1\n  dim: 3\n  dim: )[0-9]+', str(h), contents)
                contents = re.sub('(?<=input: "data"\ninput_shape {\n  dim: 1\n  dim: 3\n  dim: ' + str(h) + '\n  dim: )[0-9]+', str(w), contents)
                f = open(tyolo_deploy_path, 'w')
                f.write(contents)
                f.close()
            else:
                f = open(tyolo_deploy_path, 'r')
                contents = f.read()
                f.close()
                dim = re.search('(?<=input: "data"\ninput_shape {\n  dim: 1\n  dim: 3\n  dim: )[0-9]+', contents)
                h = contents[dim.span()[0]:dim.span()[1]]
                dim = re.search('(?<=input: "data"\ninput_shape {\n  dim: 1\n  dim: 3\n  dim: ' + h + '\n  dim: )[0-9]+', contents)
                w = int(contents[dim.span()[0]:dim.span()[1]])
                h = int(h)

            import utils.model as model_utils
            model_utils.ModelData.INPUT_SHAPE = (3, h, w)
        except Exception as e:
            import logging
            logging.info('Bad resolution, using defaults')

        # Set up all TensorRT data structures needed for inference
        self.trt_inference_wrapper = inference_utils.TRTInference(
            tyolo_deploy_path, trt_engine_path, tyolo_model_path,
            trt_engine_datatype=trt_engine_datatype,
            batch_size=batch_size)
Esempio n. 12
0
def prepare_ssd_model(model_name="ssd_inception_v2_coco_2017_11_17", silent=False):
    """Downloads pretrained object detection model and converts it to UFF.

    The model is downloaded from Tensorflow object detection model zoo.
    Currently only ssd_inception_v2_coco_2017_11_17 model is supported
    due to model_to_uff() using logic specific to that network when converting.

    Args:
        model_name (str): chosen object detection model
        silent (bool): if True, writes progress messages to stdout
    """
    if model_name != "ssd_inception_v2_coco_2017_11_17":
        raise NotImplementedError(
            "Model {} is not supported yet".format(model_name))
    download_model(model_name, silent)
    ssd_pb_path = PATHS.get_model_pb_path(model_name)
    ssd_uff_path = PATHS.get_model_uff_path(model_name)
    model_to_uff(ssd_pb_path, ssd_uff_path, silent)
Esempio n. 13
0
def download_model(model_name, silent=False):
    """Downloads model_name from Tensorflow model zoo.

    Args:
        model_name (str): chosen object detection model
        silent (bool): if True, writes progress messages to stdout
    """
    maybe_print(not silent, "Preparing pretrained model")
    model_dir = PATHS.get_models_dir_path()
    maybe_mkdir(model_dir)
    model_url = PATHS.get_model_url(model_name)
    model_archive_path = os.path.join(model_dir, "{}.tar.gz".format(model_name))
    download_file(model_url, model_archive_path, silent)
    maybe_print(not silent, "Download complete\nUnpacking {}".format(model_archive_path))
    with tarfile.open(model_archive_path, "r:gz") as tar:
        tar.extractall(path=model_dir)
    maybe_print(not silent, "Extracting complete\nRemoving {}".format(model_archive_path))
    os.remove(model_archive_path)
    maybe_print(not silent, "Model ready")
Esempio n. 14
0
def parse_commandline_arguments():
    """Parses command line arguments and adjusts internal data structures."""

    # Define script command line arguments
    parser = argparse.ArgumentParser(
        description='Run object detection inference on input image.')
    parser.add_argument('input_img_path',
                        metavar='INPUT_IMG_PATH',
                        help='an image file to run inference on')
    parser.add_argument(
        '-p',
        '--precision',
        type=int,
        choices=[32, 16],
        default=32,
        help='desired TensorRT float precision to build an engine with')
    parser.add_argument('-b',
                        '--max_batch_size',
                        type=int,
                        default=1,
                        help='max TensorRT engine batch size')
    parser.add_argument('-w',
                        '--workspace_dir',
                        help='sample workspace directory')
    parser.add_argument("-o",
                        "--output",
                        help="path of the output file",
                        default=os.path.join(PATHS.get_sample_root(),
                                             "image_inferred.jpg"))

    # Parse arguments passed
    args = parser.parse_args()

    # Set workspace dir path if passed by user
    if args.workspace_dir:
        PATHS.set_workspace_dir_path(args.workspace_dir)

    try:
        os.makedirs(PATHS.get_workspace_dir_path())
    except:
        pass

    # Verify Paths after adjustments. This also exits script if verification fails
    PATHS.verify_all_paths()

    # Fetch TensorRT engine path and datatype
    args.trt_engine_datatype = TRT_PRECISION_TO_DATATYPE[args.precision]
    args.trt_engine_path = PATHS.get_engine_path(args.trt_engine_datatype,
                                                 args.max_batch_size)
    try:
        os.makedirs(os.path.dirname(args.trt_engine_path))
    except:
        pass

    return args
def main():

    # Parse command line arguments
    args = parse_commandline_arguments()

    # Fetch .uff model path, convert from .pb
    # if needed, using prepare_ssd_model
    ssd_model_uff_path = PATHS.get_model_uff_path(MODEL_NAME)
    if not os.path.exists(ssd_model_uff_path):
        model_utils.prepare_ssd_model(MODEL_NAME)

    # Set up all TensorRT data structures needed for inference
    trt_inference_wrapper = inference_utils.TRTInference(
        args.trt_engine_path,
        ssd_model_uff_path,
        trt_engine_datatype=args.trt_engine_datatype,
        calib_dataset=args.calib_dataset,
        batch_size=args.max_batch_size)

    print("TRT ENGINE PATH", args.trt_engine_path)

    if args.camera == True:
        print('Running webcam:', args.camera)
        # Define the video stream
        cap = cv2.VideoCapture(
            0)  # Change only if you have more than one webcams

        # Loop for running inference on frames from the webcam
        while True:
            # Read frame from camera (and expand its dimensions to fit)
            ret, image_np = cap.read()

            # Actually run inference
            detection_out, keep_count_out = trt_inference_wrapper.infer_webcam(
                image_np)

            # Overlay the bounding boxes on the image
            # let analyze_prediction() draw them based on model output
            img_pil = Image.fromarray(image_np)
            prediction_fields = len(TRT_PREDICTION_LAYOUT)
            for det in range(int(keep_count_out[0])):
                analyze_prediction(detection_out, det * prediction_fields,
                                   img_pil)
            final_img = np.asarray(img_pil)

            # Display output
            cv2.imshow('object detection', final_img)

            if cv2.waitKey(25) & 0xFF == ord('q'):
                cv2.destroyAllWindows()
                break
Esempio n. 16
0
def main():
    # Parse command line arguments
    #解析命令行参数,具体参考本文件下的实现
    args = parse_commandline_arguments()

    # Fetch .uff model path, convert from .pb
    # if needed, using prepare_ssd_model
    #获取相应的uff模型的路径
    #get_model_uff_path参考utils/paths.py下的实现
    ssd_model_uff_path = PATHS.get_model_uff_path(MODEL_NAME)
    #如果不存在uff模型
    if not os.path.exists(ssd_model_uff_path):
        #从pb模型完成到uff模型的转换
        #prepare_ssd_model参考utils/model.py下的实现
        model_utils.prepare_ssd_model(MODEL_NAME)

    # Set up all TensorRT data structures needed for inference
    #建立推理所需要的数据结构
    #具体参考utils/inference.py下的实现
    trt_inference_wrapper = inference_utils.TRTInference(
        args.trt_engine_path, ssd_model_uff_path,
        trt_engine_datatype=args.trt_engine_datatype,
        batch_size=args.max_batch_size)

    # Start measuring time
    inference_start_time = time.time()

    # Get TensorRT SSD model output
    #获取相应的推理输出
    #参考utils/inference.py
    detection_out, keep_count_out = \
        trt_inference_wrapper.infer(args.input_img_path)

    # Make PIL.Image for drawing bounding boxes and
    # let analyze_prediction() draw them based on model output
    img_pil = Image.open(args.input_img_path)
    prediction_fields = len(TRT_PREDICTION_LAYOUT)
    for det in range(int(keep_count_out[0])):
        #analyze_prediction参考本文件的实现
        analyze_prediction(detection_out, det * prediction_fields, img_pil)

    # Output total [img load + inference + drawing bboxes] time
    print("Total time taken for one image: {} ms\n".format(
        int(round((time.time() - inference_start_time) * 1000))))

    # Save output image and output path
    #保存相应的结果
    img_pil.save(args.output)
    print("Saved output image to: {}".format(args.output))
Esempio n. 17
0
def main():
    # Parse command line arguments
    args = parse_commandline_arguments()

    # Loading FlattenConcat plugin library using CDLL has a side
    # effect of loading FlattenConcat plugin into internal TensorRT
    # PluginRegistry data structure. This will be needed when parsing
    # network into UFF, since some operations will need to use this plugin
    try:
        ctypes.CDLL(PATHS.get_flatten_concat_plugin_path())
    except:
        print(
            "Error: {}\n{}\n{}".format(
                "Could not find {}".format(PATHS.get_flatten_concat_plugin_path()),
                "Make sure you have compiled FlattenConcat custom plugin layer",
                "For more details, check README.md"
            )
        )
        sys.exit(1)

    input_shape = (3, 300, 300) if not args.input_shape else args.input_shape
    print("[SSD Mobilenet V2 Converter] Converting {input_file} to UFF format...".format(input_file=args.input_file))

    if args.model_name == "ssd_mobilenet_v2_coco":
        model_utils.model_to_uff(args.input_file, args.output_path,
                                 preprocess_func=model_utils.ssd_mobilenet_v2_unsupported_nodes_to_plugin_nodes,
                                 input_shape=input_shape)
    elif args.model_name == "ssd_mobilenet_v1_coco":
        model_utils.model_to_uff(args.input_file, args.output_path,
                                 preprocess_func=model_utils.ssd_mobilenet_v1_unsupported_nodes_to_plugin_nodes,
                                 input_shape=input_shape)
    else:
        raise ValueError("Got unsupported model: {model_name}".format(model_name=args.model_name))

    print("[SSD Mobilenet V2 Converter] Convert succeed, output is saved to {output_path}"
          .format(output_path=args.output_path))
def adjust_paths(args):
    """Adjust all file/directory paths, arguments passed by user.

    During script launch, user can pass several arguments to the script
    (e.g. --workspace_dir, --voc_dir), that define where script will look
    for the files needed for execution. This function adjusts internal
    Paths Python datastructure to accomodate for changes from defaults
    requested by user through appropriate command line arguments.

    Args:
        args (argparse.Namespace): parsed user arguments
    """
    if args.voc_dir:
        PATHS.set_voc_dir_path(args.voc_dir)
    if args.flatten_concat:
        PATHS.set_flatten_concat_plugin_path(args.flatten_concat)
    if args.workspace_dir:
        PATHS.set_workspace_dir_path(args.workspace_dir)
    if not os.path.exists(PATHS.get_workspace_dir_path()):
        os.makedirs(PATHS.get_workspace_dir_path())
Esempio n. 19
0
def read_voc_annotations(annotations_dir, image_numbers):
    if not os.path.isdir(annotations_dir):
        os.makedirs(annotations_dir)
    annotations_file = os.path.join(annotations_dir, 'annots.pkl')
    if not os.path.isfile(annotations_file):
        # If annotations were not present, compute them
        detections = {}
        for i, image_num in enumerate(image_numbers):
            detections[image_num] = parse_voc_annotation_xml(
                PATHS.get_voc_annotation_path().format(image_num))
            if i % 100 == 0:
                print('Reading annotation for {:d}/{:d}'.format(
                   i + 1, len(image_numbers)))
        # Save
        print('Saving cached annotations to {:s}'.format(annotations_file))
        with open(annotations_file, 'wb') as f:
            pickle.dump(detections, f)
    else:
        # If annotations were present, load them
        with open(annotations_file, 'rb') as f:
            detections = pickle.load(f)
    return detections
Esempio n. 20
0
def parse_commandline_arguments():
    """Parses command line arguments and adjusts internal data structures."""

    # Define script command line arguments
    parser = argparse.ArgumentParser(
        description='Run object detection inference on input image.')
    parser.add_argument('-w',
                        '--workspace_dir',
                        help='sample workspace directory')
    parser.add_argument(
        '-d',
        '--data',
        help=
        "Specify the data directory where it is saved in. $TRT_DATA_DIR will be overwritten by this argument."
    )

    args, _ = parser.parse_known_args()

    data_dir = os.environ.get('TRT_DATA_DIR',
                              None) if args.data is None else args.data
    if data_dir is None:
        raise ValueError(
            "Data directory must be specified by either `-d $DATA` or environment variable $TRT_DATA_DIR."
        )
    PATHS.set_data_dir_path(data_dir)

    # Set workspace dir path if passed by user
    if args.workspace_dir:
        PATHS.set_workspace_dir_path(args.workspace_dir)

    try:
        os.makedirs(PATHS.get_workspace_dir_path())
    except:
        pass

    # Verify Paths after adjustments. This also exits script if verification fails
    PATHS.verify_all_paths()

    return args
    skip_inference = should_skip_inference(parsed)
    # And if inference will not be skipped, then we
    # create files to store its results in
    detection_files = {}
    if not skip_inference:
        for voc_class in VOC_CLASSES:
            detection_files[voc_class] = open(
                os.path.join(parsed['results_dir'],
                             'det_test_{}.txt'.format(voc_class)), 'w')

    # Loading FlattenConcat plugin library using CDLL has a side
    # effect of loading FlattenConcat plugin into internal TensorRT
    # PluginRegistry data structure. This will be needed when parsing
    # network into UFF, since some operations will need to use this plugin
    try:
        ctypes.CDLL(PATHS.get_flatten_concat_plugin_path())
    except FileNotFoundError:
        print("Error: {}\n{}\n{}".format(
            "Could not find {}".format(PATHS.get_flatten_concat_plugin_path()),
            "Make sure you have compiled FlattenConcat custom plugin layer",
            "For more details, check README.md"))
        sys.exit(1)

    # Fetch frozen model .pb path...
    ssd_model_pb_path = PATHS.get_model_pb_path(MODEL_NAME)
    # ...and .uff path, if needed (converting .pb to .uff if not already done)
    if parsed['inference_backend'] == 'tensorrt':
        ssd_model_uff_path = PATHS.get_model_uff_path(MODEL_NAME)
        if not os.path.exists(ssd_model_uff_path):
            model_utils.prepare_ssd_model(MODEL_NAME)
Esempio n. 22
0
import numpy as np
import scipy.io as scio
import sys
import os
import argparse
import pickle
from sklearn import metrics
import json
from utils.util import score_smoothing
from utils.paths import PATHS

sys.path.append('../')

DATA_DIR = PATHS.get_dataset_dir_path()

# normalize scores in each sub video
NORMALIZE = True

# number of history frames, since in prediction based method,
# the first 4 frames can not be predicted, so that
# the first 4frames are undecidable, we just ignore the first 4 frames
DECIDABLE_IDX = 2


def parser_args():
    parser = argparse.ArgumentParser(
        description='evaluating the model, computing the roc/auc.')

    parser.add_argument('-f',
                        '--file',
                        type=str,
def parse_commandline_arguments():
    """Parses command line arguments and adjusts internal data structures."""

    # Define script command line arguments
    parser = argparse.ArgumentParser(
        description='Run object detection inference on input image.')
    parser.add_argument('--input_img_path',
                        metavar='INPUT_IMG_PATH',
                        help='an image file to run inference on')
    parser.add_argument(
        '-p',
        '--precision',
        type=int,
        choices=[32, 16, 8],
        default=32,
        help='desired TensorRT float precision to build an engine with')
    parser.add_argument('-b',
                        '--max_batch_size',
                        type=int,
                        default=1,
                        help='max TensorRT engine batch size')
    parser.add_argument('-w',
                        '--workspace_dir',
                        help='sample workspace directory')
    parser.add_argument('-fc',
                        '--flatten_concat',
                        help='path of built FlattenConcat plugin')
    parser.add_argument('-d',
                        '--calib_dataset',
                        default='../VOCdevkit/VOC2007/JPEGImages',
                        help='path to the calibration dataset')
    parser.add_argument('-c',
                        '--camera',
                        default=True,
                        help='if True, will run webcam application')

    # Parse arguments passed
    args = parser.parse_args()

    # Set FlattenConcat TRT plugin path and
    # workspace dir path if passed by user
    if args.flatten_concat:
        PATHS.set_flatten_concat_plugin_path(args.flatten_concat)
    if args.workspace_dir:
        PATHS.set_workspace_dir_path(args.workspace_dir)

    try:
        os.makedirs(PATHS.get_workspace_dir_path())
    except:
        pass

    # Verify Paths after adjustments. This also exits script if verification fails
    PATHS.verify_all_paths()

    # Fetch TensorRT engine path and datatype
    args.trt_engine_datatype = TRT_PRECISION_TO_DATATYPE[args.precision]
    args.trt_engine_path = PATHS.get_engine_path(args.trt_engine_datatype,
                                                 args.max_batch_size)
    try:
        os.makedirs(os.path.dirname(args.trt_engine_path))
    except:
        pass

    return args
Esempio n. 24
0
from sklearn.externals import joblib
import sys
from scripts import inference
from models.CAE import CAE_encoder
from utils import util
import os
import argparse
import numpy as np
import pickle
import time
from utils import evaluate
from utils.paths import PATHS

sys.path.append("../")

prefix = PATHS.get_dataset_dir_path()


def arg_parse():
    parser = argparse.ArgumentParser()
    parser.add_argument("-g",
                        "--gpu",
                        type=str,
                        default="0",
                        help="Use which gpu?")
    parser.add_argument("-d",
                        "--dataset",
                        type=str,
                        help="Train on which dataset")
    parser.add_argument("-b",
                        "--bn",
Esempio n. 25
0
def main():
    # Parse command line arguments
    parsed = parse_commandline_arguments()

    # Check if inference should be skipped (if model inference
    # results are already computed, we don't need to recompute
    # them for VOC mAP computation)
    skip_inference = should_skip_inference(parsed)
    # And if inference will not be skipped, then we
    # create files to store its results in
    detection_files = {}
    if not skip_inference:
        for voc_class in VOC_CLASSES:
            detection_files[voc_class] = open(
                os.path.join(parsed['results_dir'],
                             'det_test_{}.txt'.format(voc_class)), 'w')

    # Fetch frozen model .pb path...
    ssd_model_pb_path = PATHS.get_model_pb_path(MODEL_NAME)
    # ...and .uff path, if needed (converting .pb to .uff if not already done)
    if parsed['inference_backend'] == 'tensorrt':
        ssd_model_uff_path = PATHS.get_model_uff_path(MODEL_NAME)

    # This block of code sets up and performs inference, if needed
    if not skip_inference:
        # Preprocess VOC dataset if necessary by resizing images
        preprocess_voc()

        # Fetch image list and input .ppm files path
        with open(PATHS.get_voc_image_set_path(), 'r') as f:
            voc_image_numbers = f.readlines()
            voc_image_numbers = [line.strip() for line in voc_image_numbers]
        voc_image_path = PATHS.get_voc_ppm_img_path()

        # Tensorflow and TensorRT paths are a little bit different,
        # so we must treat each one individually
        if parsed['inference_backend'] == 'tensorrt':
            # TRTInference initialization initializes
            # all TensorRT structures, creates engine if it doesn't
            # already exist and finally saves it to file for future uses
            from utils.inference_trt import TRTInference
            trt_inference_wrapper = TRTInference(parsed['trt_engine_path'],
                                                 ssd_model_uff_path,
                                                 parsed['trt_engine_datatype'],
                                                 parsed['max_batch_size'])
            # Outputs from TensorRT are handled differently than
            # outputs from Tensorflow, that's why we use another
            # function to produce the detections from them
            produce_tensorrt_detections(detection_files, trt_inference_wrapper,
                                        parsed['max_batch_size'],
                                        voc_image_numbers, voc_image_path)
        elif parsed['inference_backend'] == 'tensorflow':
            # In case of Tensorflow all we need to
            # initialize inference is frozen model...
            from utils.inference_tf import TensorflowInference
            tf_inference_wrapper = TensorflowInference(ssd_model_pb_path)
            # ...and after initializing it, we can
            # proceed to producing detections
            produce_tensorflow_detections(detection_files,
                                          tf_inference_wrapper,
                                          parsed['max_batch_size'],
                                          voc_image_numbers, voc_image_path)

    # Flush detection to files to make sure evaluation is correct
    for key in detection_files:
        detection_files[key].flush()

    # Do mAP computation based on saved detections
    voc_mAP_utils.do_python_eval(parsed['results_dir'])

    # Close detection files, they are not needed anymore
    for key in detection_files:
        detection_files[key].close()
Esempio n. 26
0
def parse_commandline_arguments():
    """Parses command line arguments and adjusts internal data structures."""

    # Define script command line arguments
    parser = argparse.ArgumentParser(
        description='Run object detection evaluation on VOC2007 dataset.')
    parser.add_argument('inference_backend',
                        metavar='INFERENCE_BACKEND',
                        type=str,
                        choices=['tensorrt', 'tensorflow'],
                        default='tensorrt',
                        nargs='?',
                        help='inference backend to run evaluation with')
    parser.add_argument(
        '-p',
        '--precision',
        type=int,
        choices=[32, 16],
        default=32,
        help='desired TensorRT float precision to build an engine with')
    parser.add_argument('-b',
                        '--max_batch_size',
                        type=int,
                        default=64,
                        help='max TensorRT engine batch size')
    parser.add_argument('-f',
                        '--force_inference',
                        action='store_true',
                        help='force model inference even if detections exist')
    parser.add_argument('-w',
                        '--workspace_dir',
                        help='sample workspace directory')
    parser.add_argument(
        '-d',
        '--data',
        help=
        "Specify the data directory where it is saved in. $TRT_DATA_DIR will be overwritten by this argument."
    )

    args, _ = parser.parse_known_args()

    data_dir = os.environ.get('TRT_DATA_DIR',
                              None) if args.data is None else args.data
    if data_dir is None:
        raise ValueError(
            "Data directory must be specified by either `-d $DATA` or environment variable $TRT_DATA_DIR."
        )

    adjust_paths(args, data_dir)

    extract_voc_data_if_needed()
    # Verify Paths after adjustments. This also exits script if verification fails
    PATHS.verify_all_paths(should_verify_voc=True)

    # Fetch directory to save inference results to, create it if it doesn't exist
    trt_engine_datatype = None
    trt_engine_path = None
    if args.inference_backend == 'tensorrt':
        # In case of TensorRT we also fetch engine data type and engine path
        trt_engine_datatype = TRT_PRECISION_TO_DATATYPE[args.precision]
        trt_engine_path = PATHS.get_engine_path(trt_engine_datatype,
                                                args.max_batch_size)
        if not os.path.exists(os.path.dirname(trt_engine_path)):
            os.makedirs(os.path.dirname(trt_engine_path))
        results_dir = PATHS.get_voc_model_detections_path(
            'tensorrt', trt_engine_datatype)
    elif args.inference_backend == 'tensorflow':
        results_dir = PATHS.get_voc_model_detections_path('tensorflow')
    if not os.path.exists(results_dir):
        os.makedirs(results_dir)

    # Return parsed arguments for further functions to use
    parsed = {
        'inference_backend': args.inference_backend,
        'max_batch_size': args.max_batch_size,
        'force_inference': args.force_inference,
        'results_dir': results_dir,
        'trt_engine_path': trt_engine_path,
        'trt_engine_datatype': trt_engine_datatype
    }
    return parsed
Esempio n. 27
0
    # Check if inference should be skipped (if model inference
    # results are already computed, we don't need to recompute
    # them for VOC mAP computation)
    skip_inference = should_skip_inference(parsed)
    # And if inference will not be skipped, then we
    # create files to store its results in
    detection_files = {}
    if not skip_inference:
        for voc_class in VOC_CLASSES:
            detection_files[voc_class] = open(
                os.path.join(parsed['results_dir'],
                             'det_test_{}.txt'.format(voc_class)), 'w')

    # Fetch frozen model .pb path...
    ssd_model_pb_path = PATHS.get_model_pb_path(MODEL_NAME)
    # ...and .uff path, if needed (converting .pb to .uff if not already done)
    if parsed['inference_backend'] == 'tensorrt':
        ssd_model_uff_path = PATHS.get_model_uff_path(MODEL_NAME)
        if not os.path.exists(ssd_model_uff_path):
            model_utils.prepare_ssd_model(MODEL_NAME)

    # This block of code sets up and performs inference, if needed
    if not skip_inference:
        # Preprocess VOC dataset if necessary by resizing images
        preprocess_voc()

        # Fetch image list and input .ppm files path
        with open(PATHS.get_voc_image_set_path(), 'r') as f:
            voc_image_numbers = f.readlines()
            voc_image_numbers = [line.strip() for line in voc_image_numbers]
Esempio n. 28
0
import random
from utils import util
import sklearn.svm as svm
from sklearn.cluster import KMeans
from sklearn.externals import joblib
from models import CAE
from sklearn.linear_model import SGDClassifier
from sklearn.multiclass import OneVsRestClassifier

from cyvlfeat.kmeans import kmeans, kmeans_quantize
from utils.paths import PATHS

sys.path.append('../')
tf.logging.set_verbosity(tf.logging.DEBUG)

summary_save_path_pre = PATHS.get_logs_dir_path()
svm_save_dir = PATHS.get_model_svm_dir_path()
model_save_path_pre = PATHS.get_model_cae_dir_path()

prefix = PATHS.get_sample_root()

batch_size = 64
learning_rate = [1e-3, 1e-4]
lr_decay_epochs = [100]
epochs = 200
'''
The Author said that the model may be better when 90-d one-hot embedding, representing the object class in COCO, 
add to the feature vector, which is can be activated by '--class_add'
'''

Esempio n. 29
0
def main(max_time, min_time, sum_time, num):

    # Parse command line arguments
    args = parse_commandline_arguments()
    outputdir_path = args.outpath
    inputdir_path = args.inpath
    # Fetch .uff model path, convert from .pb
    # if needed, using prepare_ssd_model
    ssd_model_uff_path = PATHS.get_model_uff_path(MODEL_NAME)
    if not os.path.exists(ssd_model_uff_path):
        model_utils.prepare_ssd_model(MODEL_NAME)

    # Set up all TensorRT data structures needed for inference
    trt_inference_wrapper = inference_utils.TRTInference(
        args.trt_engine_path,
        ssd_model_uff_path,
        trt_engine_datatype=args.trt_engine_datatype,
        calib_dataset=args.calib_dataset,
        batch_size=args.max_batch_size)
    ARGS = 3
    print("TRT ENGINE PATH", args.trt_engine_path)

    if args.camera == True:
        #if True:
        #print('Running webcam:', args.camera)
        # Define the video stream
        #cap = cv2.VideoCapture(0)
        #cap = cv2.VideoCapture('animal.webm')  # Change only if you have more than one webcams
        cap = cv2.VideoCapture(
            inputdir_path)  # Change only if you have more than one webcams
        if (cap.isOpened() == False):
            print("Error opening video stream or file")
            exit()
        else:
            print("success!")
        # Default resolutions of the frame are obtained.The default resolutions are system dependent.

# We convert the resolutions from float to integer.
        frame_width = int(cap.get(3))
        frame_height = int(cap.get(4))
        fps = cap.get(cv2.CAP_PROP_FPS)

        # Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
        out = cv2.VideoWriter(str(outputdir_path + "/output.avi"),
                              cv2.VideoWriter_fourcc(*'XVID'), fps,
                              (frame_width, frame_height))

        # Loop for running inference on frames from the webcam
        while True:
            # Read frame from camera (and expand its dimensions to fit)
            ret, image_np = cap.read()
            if not ret:
                print("Video played.")
                break
            # Actually run inference
            cur, detection_out, keep_count_out = trt_inference_wrapper.infer_webcam(
                image_np)
            max_time = max(max_time, cur)
            min_time = min(min_time, cur)
            sum_time += cur
            num += 1

            # Overlay the bounding boxes on the image
            # let analyze_prediction() draw them based on model output
            img_pil = Image.fromarray(image_np)
            prediction_fields = len(TRT_PREDICTION_LAYOUT)
            for det in range(int(keep_count_out[0])):
                analyze_prediction(detection_out, det * prediction_fields,
                                   img_pil)
            final_img = np.asarray(img_pil)

            out.write(final_img)
        return outputdir_path, max_time, min_time, sum_time, num
Esempio n. 30
0
    def __init__(self,
                 trt_engine_path,
                 uff_model_path,
                 trt_engine_datatype=trt.DataType.FLOAT,
                 batch_size=1):
        """Initializes TensorRT objects needed for model inference.

        Args:
            trt_engine_path (str): path where TensorRT engine should be stored
            uff_model_path (str): path of .uff model
            trt_engine_datatype (trt.DataType):
                requested precision of TensorRT engine used for inference
            batch_size (int): batch size for which engine
                should be optimized for
        """

        # We first load all custom plugins shipped with TensorRT,
        # some of them will be needed during inference
        trt.init_libnvinfer_plugins(TRT_LOGGER, '')

        # Initialize runtime needed for loading TensorRT engine from file
        self.trt_runtime = trt.Runtime(TRT_LOGGER)
        # TRT engine placeholder
        self.trt_engine = None

        # Display requested engine settings to stdout
        print("TensorRT inference engine settings:")
        print("  * Inference precision - {}".format(trt_engine_datatype))
        print("  * Max batch size - {}\n".format(batch_size))

        # If engine is not cached, we need to build it
        if not os.path.exists(trt_engine_path):
            # This function uses supplied .uff file
            # alongside with UffParser to build TensorRT
            # engine. For more details, check implmentation

            # Set up for calibration
            if trt_engine_datatype == trt.DataType.INT8:
                with open(PATHS.get_voc_image_set_path(), 'r') as f:
                    voc_image_numbers = f.readlines()
                    voc_image_numbers = [
                        line.strip() for line in voc_image_numbers
                    ]
                total_imgs = len(voc_image_numbers)
                voc_names = []

                calibration_cache = "ssd_calibration_eval.cache"

                for n in range(total_imgs):
                    voc_names.append(voc_image_numbers[n] + ".jpg")

                _, calib_data = common.find_sample_data(
                    description="Runs a ResNet50 network in Int8 mode",
                    subfolder="JPEGImages",
                    find_files=voc_names)
                calib = VOCEntropyCalibrator(calib_data,
                                             total_imgs,
                                             cache_file=calibration_cache)

            self.trt_engine = engine_utils.build_engine(
                uff_model_path,
                calib,
                TRT_LOGGER,
                trt_engine_datatype=trt_engine_datatype,
                batch_size=batch_size)
            # Save the engine to file
            engine_utils.save_engine(self.trt_engine, trt_engine_path)

        # If we get here, the file with engine exists, so we can load it
        if not self.trt_engine:
            print("Loading cached TensorRT engine from {}".format(
                trt_engine_path))
            self.trt_engine = engine_utils.load_engine(self.trt_runtime,
                                                       trt_engine_path)

        # This allocates memory for network inputs/outputs on both CPU and GPU
        self.inputs, self.outputs, self.bindings, self.stream = \
            engine_utils.allocate_buffers(self.trt_engine)

        # Execution context is needed for inference
        self.context = self.trt_engine.create_execution_context()

        # Allocate memory for multiple usage [e.g. multiple batch inference]
        input_volume = trt.volume(model_utils.ModelData.INPUT_SHAPE)
        self.numpy_array = np.zeros(
            (self.trt_engine.max_batch_size, input_volume))