def evaluate_single_image(model, img_path, cfg):
    """
    Computes detection results for the given model on the provided image
    :param model: the model
    :param img_path: the path to the image
    :param cfg: the configuration
    :return:
        regressed_rois - the predicted bounding boxes
        cls_probs - class probabilities per bounding box
    """

    detector_name = _get_detector_name(cfg)
    regressed_rois = None
    cls_probs = None
    print("detecting objects in image {}".format(img_path))
    if detector_name == 'FastRCNN':
        from FastRCNN.FastRCNN_eval import FastRCNN_Evaluator
        evaluator = FastRCNN_Evaluator(model, cfg)
        regressed_rois, cls_probs = evaluator.process_image(img_path)
    elif detector_name == 'FasterRCNN':
        from FasterRCNN.FasterRCNN_eval import FasterRCNN_Evaluator
        evaluator = FasterRCNN_Evaluator(model, cfg)
        regressed_rois, cls_probs = evaluator.process_image(img_path)
    else:
        print('Unknown detector: {}'.format(detector_name))

    return regressed_rois, cls_probs
def measure_inference_time(model, img_path, cfg, num_repetitions=100):
    """
    Computes detection results for the given model on the provided image
    :param model: the model
    :param img_path: the path to the image
    :param cfg: the configuration
    :return:
        regressed_rois - the predicted bounding boxes
        cls_probs - class probabilities per bounding box
    """

    detector_name = _get_detector_name(cfg)
    print(
        "Measuring inference time (seconds per image) as average over {} runs".
        format(num_repetitions))
    if detector_name == 'FastRCNN':
        from FastRCNN.FastRCNN_eval import FastRCNN_Evaluator
        evaluator = FastRCNN_Evaluator(model, cfg)
    elif detector_name == 'FasterRCNN':
        from FasterRCNN.FasterRCNN_eval import FasterRCNN_Evaluator
        evaluator = FasterRCNN_Evaluator(model, cfg)
    else:
        print('Unknown detector: {}'.format(detector_name))
        return

    from time import time
    start = time()
    for i in range(num_repetitions):
        _, _ = evaluator.process_image(img_path)
    total = time() - start
    print("seconds per image: {:2f} (total for {} images: {:2f})".format(
        total / num_repetitions, num_repetitions, total))
    def load(self):
        prepare(self.cfg, use_arg_parser=False)
        print("Loading existing model from %s" % self.model_file)
        self.eval_model = load_model(self.model_file)
        from FasterRCNN.FasterRCNN_eval import FasterRCNN_Evaluator
        self.evaluator = FasterRCNN_Evaluator(self.eval_model, self.cfg)

        # Loading en_zh dictionary
        if os.path.exists(self.en_zh_file):
            with open(
                    self.en_zh_file,
                    'r',
                    encoding='utf-8',
            ) as f:
                for line in f.readlines():
                    en_name, zh_name = line.split(',', 2)
                    self.en_zh_dict[en_name.lower()] = zh_name.replace(
                        '\n', '')
        return
Beispiel #4
0
    def run_faster_rcnn():
        print("Running training")
        base_folder = os.path.dirname(os.path.abspath(__file__))
        sys.path.append(os.path.join(base_folder, "FasterRCNN"))
        from cntk import load_model
        from FasterRCNN_train import prepare
        from FasterRCNN_eval import compute_test_set_aps
        from FasterRCNN.FasterRCNN_eval import FasterRCNN_Evaluator
        import numpy as np
        import json

        cfg = get_configuration()
        prepare(cfg, False)

        cfg["DATA"].NUM_TEST_IMAGES = args.num_test
        cfg["CNTK"].MAKE_MODE = True
        cfg["CNTK"].VISUALIZE_RESULTS = True
        if args.gpu:
            cfg["CNTK"].USE_GPU_NMS = True
        else:
            cfg["CNTK"].USE_GPU_NMS = False
        if not (args.conf_threshold is None):
            cfg.RESULTS_NMS_CONF_THRESHOLD = args.conf_threshold

        trained_model = load_model(args.model_path)
        eval_results = compute_test_set_aps(trained_model, cfg)

        for class_name in eval_results:
            print('Average precision (AP) for {:>15} = {:.4f}'.format(
                class_name, eval_results[class_name]))
        print('Mean average precision (AP) = {:.4f}'.format(
            np.nanmean(list(eval_results.values()))))

        if cfg["CNTK"].VISUALIZE_RESULTS:
            num_eval = min(cfg["DATA"].NUM_TEST_IMAGES, 100)
            results_folder = os.path.join(cfg.OUTPUT_PATH, cfg["DATA"].DATASET)
            evaluator = FasterRCNN_Evaluator(trained_model, cfg)
            plot_test_set_results(evaluator, num_eval, results_folder, cfg)

        with open(
                r"/cntk/Examples/Image/Detection/FasterRCNN/Output/custom_images_output.json",
                "w+") as resultFile:
            print(
                "Bounding boxes written to /cntk/Examples/Image/Detection/FasterRCNN/Output/custom_images_output.json"
            )
            resultFile.write(json.dumps(json_output))
class FRCNN_Model:
    def __init__(self, model_type):
        self.cfg = merge_configs([
            detector_cfg, network_cfg, dataset_cfg, {
                'DETECTOR': 'FasterRCNN'
            }
        ])
        self.name = "TNC_faster_rcnn_eval_AlexNet_e2e_native.model"
        self.model_type = ''
        self.model_path = ''
        self.model_file = ''
        self.en_zh_file = ''
        self.en_zh_dict = {}
        self.eval_model = None
        self.evaluator = None
        self.set_model_type(model_type)
        return

    def set_model_type(self, model_type):
        self.model_type = model_type
        self.model_path = os.path.join(
            os.path.join(os.path.dirname(__file__), 'models'), model_type)
        if not os.path.exists(self.model_path):
            os.makedirs(self.model_path)
        self.model_file = os.path.join(self.model_path, self.name)
        self.cfg['DATA'].MAP_FILE_PATH = self.model_path
        self.cfg['DATA'].CLASS_MAP_FILE = os.path.join(
            self.model_path, os.path.basename(self.cfg['DATA'].CLASS_MAP_FILE))
        self.en_zh_file = os.path.join(self.model_path, 'en_zh.txt')
        return

    def get_model_type(self):
        return self.model_type

    def get_cntk_version(self):
        return cntk.__version__

    def get_model_file(self):
        return self.model_file

    def get_class_map_file(self):
        return self.cfg['DATA'].CLASS_MAP_FILE

    def get_en_zh_map_file(self):
        return self.en_zh_file

    def load(self):
        prepare(self.cfg, use_arg_parser=False)
        print("Loading existing model from %s" % self.model_file)
        self.eval_model = load_model(self.model_file)
        from FasterRCNN.FasterRCNN_eval import FasterRCNN_Evaluator
        self.evaluator = FasterRCNN_Evaluator(self.eval_model, self.cfg)

        # Loading en_zh dictionary
        if os.path.exists(self.en_zh_file):
            with open(
                    self.en_zh_file,
                    'r',
                    encoding='utf-8',
            ) as f:
                for line in f.readlines():
                    en_name, zh_name = line.split(',', 2)
                    self.en_zh_dict[en_name.lower()] = zh_name.replace(
                        '\n', '')
        return

    def predict(self, image_buf, lang='en'):
        predictions = self._eval_single_image(image_buf)

        # Translated to zh
        if lang == 'zh':
            for p in predictions:
                tag = p['Tag'].lower()
                if tag in self.en_zh_dict:
                    p['Tag'] = self.en_zh_dict[tag]
        return predictions

    # Evaluates a single image using the provided model
    def _eval_single_image(self, img_buf):
        regressed_rois, cls_probs = self.evaluator.process_image_mem(img_buf)
        bboxes, labels, scores = od.filter_results(regressed_rois, cls_probs,
                                                   self.cfg)

        # write detection results to output
        fg_boxes = np.where(labels > 0)
        print("#bboxes: before nms: {}, after nms: {}, foreground: {}".format(
            len(regressed_rois), len(bboxes), len(fg_boxes[0])))
        fg = fg_boxes[0]
        predictions = []
        if len(fg) == 0:
            print("Nothing found in current image.")
            predictions.append({
                "TagId": 0,
                "Tag": 'Empty',
                "Probability": 1.0,
                "Region": {
                    "Left": 0.0,
                    "Top": 0.0,
                    "Width": 0.0,
                    "Height": 0.0
                }
            })
        else:
            for i in fg:
                print("{:<12} (label: {:<2}), score: {:.3f}, box: {}".format(
                    self.cfg["DATA"].CLASSES[labels[i]], labels[i], scores[i],
                    [int(v) for v in bboxes[i]]))
                left, top, right, bottom = [int(v) for v in bboxes[i]]
                predictions.append({
                    "TagId": np.asscalar(labels[i]),
                    "Tag": self.cfg["DATA"].CLASSES[labels[i]],
                    "Probability": np.asscalar(scores[i]),
                    "Region": {
                        "Left": float(left / _image_width),
                        "Top": float(top / _image_height),
                        "Width": float((right - left) / _image_width),
                        "Height": float((bottom - top) / _image_height)
                    }
                })
        return predictions
Beispiel #6
0
            })
        return result

    # from ObjectDetector import predict, get_configuration

    input_path = args.input
    output_path = args.output
    json_output_path = args.json_output
    model_path = args.model
    model = load_model(model_path)
    FRCNN_DIM_W = model.arguments[0].shape[1]
    FRCNN_DIM_H = model.arguments[0].shape[2]
    labels_count = model.cls_pred.shape[1]
    model_classes = get_classes_description(model_path, labels_count)
    cfg = get_configuration(model_classes)
    evaluator = FasterRCNN_Evaluator(model, cfg)

    if (output_path is None and json_output_path is None):
        parser.error("No directory output path or json output path specified")

    if (output_path is not None) and not os.path.exists(output_path):
        os.makedirs(output_path)

    if os.path.isdir(input_path):
        import glob
        file_paths = sorted(glob.glob(os.path.join(input_path, '*.jpg')),
                            key=numerical_sort)
    else:
        file_paths = [input_path]

    vott_classes = {model_classes[i]: i for i in range(len(model_classes))}
Beispiel #7
0

if __name__ == '__main__':
    # Get Configuration
    cfg = get_configuration()

    # train
    trained_model = od.train_object_detector(cfg)
    # test model with some images
    eval_results = od.evaluate_test_set(trained_model, cfg)

    # Plot results on test set images
    if cfg.VISUALIZE_RESULTS:
        num_eval = min(cfg["DATA"].NUM_TEST_IMAGES, 100)
        results_folder = os.path.join(cfg.OUTPUT_PATH, cfg["DATA"].DATASET)
        evaluator = FasterRCNN_Evaluator(trained_model, cfg)
        plot_test_set_results(evaluator, num_eval, results_folder, cfg)

    if cfg.STORE_EVAL_MODEL_WITH_NATIVE_UDF:
        store_eval_model_with_native_udf(trained_model, cfg)

    # detect objects in single image
    img_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                            r"DataSets/NO3310/testImages/img30.jpg")
    regressed_rois, cls_probs = od.evaluate_single_image(
        trained_model, img_path, cfg)
    bboxes, labels, scores = od.filter_results(regressed_rois, cls_probs, cfg)

    # write detection results to output
    fg_boxes = np.where(labels > 0)
    print("#bboxes: before nms: {}, after nms: {}, foreground: {}".format(
        }])


print("\nPython version: " + str(sys.version) + ", CNTK version: " +
      __version__)
startTime = dt.datetime.now()

# load configuration
print("Loading Prometheus configuration:", detectorName)
cfg = getConfiguration(detectorName)

# load model
print("Loading Neural Network: ", pretrainnedModelName)
od.prepareOnly_object_detector(cfg)
eval_model = load_model(os.path.join(workingDir, pretrainnedModelName))
evaluator = FasterRCNN_Evaluator(eval_model, cfg)

cap = cv2.VideoCapture(1)

while (True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Prepare to detect
    regressed_rois = None
    cls_probs = None

    regressed_rois, cls_probs = evaluator.process_image_raw(frame)
    bboxes, labels, scores = od.filter_results(regressed_rois, cls_probs, cfg)

    # Add text label and network score to the video captue