Esempio n. 1
0
 def predict(img_path, evaluator, cfg, debug=False):
     # detect objects in single image
     regressed_rois, cls_probs = evaluator.process_image(img_path)
     bboxes, labels, scores = od.filter_results(regressed_rois, cls_probs,
                                                cfg)
     # visualize detections on image
     if debug:
         od.visualize_results(img_path,
                              bboxes,
                              labels,
                              scores,
                              cfg,
                              store_to_path=img_path + "o.jpg")
     # write detection results to output
     fg_boxes = np.where(labels > 0)
     result = []
     for i in fg_boxes[0]:
         # print (cfg["DATA"].CLASSES)
         # print(labels)
         result.append({
             'label': cfg["DATA"].CLASSES[labels[i]],
             'score': '%.3f' % (scores[i]),
             'box': [int(v) for v in bboxes[i]]
         })
     return result
def evaluate_model(eval_model, detector_name):
    cfg = get_configuration(detector_name)
    cfg['NUM_CHANNELS'] = 3
    print("Map file = ", cfg["DATA"].CLASS_MAP_FILE)
    cfg["DATA"].CLASSES = parse_class_map_file(
        os.path.join("Steer_Bad_Relevant_output", cfg["DATA"].CLASS_MAP_FILE))
    cfg["DATA"].NUM_CLASSES = len(cfg["DATA"].CLASSES)

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

    fg_boxes = np.where(labels > 0)
    print("#bboxes: before nms: {}, after nms: {}, foreground: {}".format(
        len(regressed_rois), len(bboxes), len(fg_boxes[0])))
    for i in fg_boxes[0]:
        print("{:<12} (label: {:<2}), score: {:.3f}, box: {}".format(
            cfg["DATA"].CLASSES[labels[i]], labels[i], scores[i],
            [int(v) for v in bboxes[i]]))

    od.visualize_results(img_path,
                         bboxes,
                         labels,
                         scores,
                         cfg,
                         store_to_path="Steer_Bad_Relevant_output/output.jpg")
Esempio n. 3
0
def test_detection_demo(device_id):
    if cntk_device(device_id).type() != DeviceKind_GPU:
        pytest.skip('test only runs on GPU')  # it runs very slow in CPU
    try_set_default_device(cntk_device(device_id))

    from prepare_test_data import prepare_Grocery_data, prepare_alexnet_v0_model
    grocery_path = prepare_Grocery_data()
    prepare_alexnet_v0_model()

    from FastRCNN.install_data_and_model import create_grocery_mappings
    create_grocery_mappings(grocery_path)

    from DetectionDemo import get_configuration
    import utils.od_utils as od

    cfg = get_configuration('FasterRCNN')
    cfg["CNTK"].FORCE_DETERMINISTIC = True
    cfg["CNTK"].DEBUG_OUTPUT = False
    cfg["CNTK"].MAKE_MODE = False
    cfg["CNTK"].FAST_MODE = False
    cfg.CNTK.E2E_MAX_EPOCHS = 3
    cfg.CNTK.RPN_EPOCHS = 2
    cfg.CNTK.FRCN_EPOCHS = 2
    cfg.IMAGE_WIDTH = 400
    cfg.IMAGE_HEIGHT = 400
    cfg["CNTK"].TRAIN_E2E = True
    cfg.USE_GPU_NMS = False
    cfg.VISUALIZE_RESULTS = False
    cfg["DATA"].MAP_FILE_PATH = grocery_path

    externalData = 'CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY' in os.environ
    if externalData:
        extPath = os.environ['CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY']
        cfg['BASE_MODEL_PATH'] = os.path.join(extPath, "PreTrainedModels",
                                              "AlexNet", "v1",
                                              "AlexNet_ImageNet_Caffe.model")
    else:
        cfg['BASE_MODEL_PATH'] = os.path.join(
            abs_path,
            *"../../../../PretrainedModels/AlexNet_ImageNet_Caffe.model".split(
                "/"))

    # train and test
    eval_model = od.train_object_detector(cfg)
    eval_results = od.evaluate_test_set(eval_model, cfg)

    meanAP = np.nanmean(list(eval_results.values()))
    print('meanAP={}'.format(meanAP))
    assert meanAP > 0.01

    # detect objects in single image
    img_path = os.path.join(grocery_path, "testImages",
                            "WIN_20160803_11_28_42_Pro.jpg")
    regressed_rois, cls_probs = od.evaluate_single_image(
        eval_model, img_path, cfg)
    bboxes, labels, scores = od.filter_results(regressed_rois, cls_probs, cfg)
    assert bboxes.shape[0] == labels.shape[0]
Esempio n. 4
0
def test_detection_demo(device_id):
    if cntk_device(device_id).type() != DeviceKind_GPU:
        pytest.skip('test only runs on GPU')  # it runs very slow in CPU
    try_set_default_device(cntk_device(device_id))

    from prepare_test_data import prepare_Grocery_data, prepare_alexnet_v0_model
    grocery_path = prepare_Grocery_data()
    prepare_alexnet_v0_model()

    from FastRCNN.install_data_and_model import create_grocery_mappings
    create_grocery_mappings(grocery_path)

    from DetectionDemo import get_configuration
    import utils.od_utils as od

    cfg = get_configuration('FasterRCNN')
    cfg["CNTK"].FORCE_DETERMINISTIC = True
    cfg["CNTK"].DEBUG_OUTPUT = False
    cfg["CNTK"].MAKE_MODE = False
    cfg["CNTK"].FAST_MODE = False
    cfg.CNTK.E2E_MAX_EPOCHS = 3
    cfg.CNTK.RPN_EPOCHS = 2
    cfg.CNTK.FRCN_EPOCHS = 2
    cfg.IMAGE_WIDTH = 400
    cfg.IMAGE_HEIGHT = 400
    cfg["CNTK"].TRAIN_E2E = True
    cfg.USE_GPU_NMS = False
    cfg.VISUALIZE_RESULTS = False
    cfg["DATA"].MAP_FILE_PATH = grocery_path

    externalData = 'CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY' in os.environ
    if externalData:
        extPath = os.environ['CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY']
        cfg['BASE_MODEL_PATH'] = os.path.join(extPath, "PreTrainedModels", "AlexNet", "v1", "AlexNet_ImageNet_Caffe.model")
    else:
        cfg['BASE_MODEL_PATH'] = os.path.join(abs_path, *"../../../../PretrainedModels/AlexNet_ImageNet_Caffe.model".split("/"))

    # train and test
    eval_model = od.train_object_detector(cfg)
    eval_results = od.evaluate_test_set(eval_model, cfg)

    meanAP = np.nanmean(list(eval_results.values()))
    print('meanAP={}'.format(meanAP))
    assert meanAP > 0.01

    # detect objects in single image
    img_path = os.path.join(grocery_path, "testImages", "WIN_20160803_11_28_42_Pro.jpg")
    regressed_rois, cls_probs = od.evaluate_single_image(eval_model, img_path, cfg)
    bboxes, labels, scores = od.filter_results(regressed_rois, cls_probs, cfg)
    assert bboxes.shape[0] == labels.shape[0]
Esempio n. 5
0
    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
Esempio n. 6
0
def run(input_df):
    print(str(input_df))
    
    startTime = dt.datetime.now()

    # convert input back to image and save to disk
    base64ImgString = input_df['image base64 string'][0]
    imgPath = base64ToImage(base64ImgString, workingDir)

    # load configuration
    cfg = getConfiguration(detectorName)

    # load model
    od.prepareOnly_object_detector(cfg)
    eval_model = load_model(os.path.join(workingDir), pretrainnedModelName))

    # score image
    regressed_rois, cls_probs = od.evaluate_single_image(eval_model, imgPath, cfg)
    bboxes, labels, scores = od.filter_results(regressed_rois, cls_probs, cfg)

    return toJson(bboxes, labels, scores)
Esempio n. 7
0
def get_result(filename, eval_model):
    names = filename.split('/')
    name = names[len(names) - 1]
    print(name)
    data_path = os.environ['AZUREML_NATIVE_SHARE_DIRECTORY'] + '/output'
    if os.path.isdir(data_path) == False:
        os.makedirs(data_path)
    img_path = ''
    if load_file_from_blob(os.environ['STORAGE_ACCOUNT_NAME'], \
                        os.environ['TESTIMAGESCONTAINER'], filename, data_path + '/' + name) is True:
        print(name)
    img_path = data_path + '/' + name
    print(img_path)
    #print('detector_name: {}'.format(os.environ["DETECTOR_NAME"]))
    #print('img_path: {}'.format(img_path))
    cfg = get_configuration('FasterRCNN')  #os.environ["DETECTOR_NAME"])

    # detect objects in single image
    regressed_rois, cls_probs = evaluate_single_image(eval_model, img_path,
                                                      cfg)
    bboxes, labels, scores = 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(
        len(regressed_rois), len(bboxes), len(fg_boxes[0])))
    result = []
    for i in fg_boxes[0]:
        #print("{:<12} (label: {:<2}), score: {:.3f}, box: {}".format(
        #cfg["DATA"].CLASSES[labels[i]], labels[i], scores[i], [int(v) for v in bboxes[i]]))
        result.append({
            'label': cfg["DATA"].CLASSES[labels[i]],
            'score': '%.3f' % (scores[i]),
            'box': [int(v) for v in bboxes[i]]
        })

    return json.dumps(result)
    eval_model = od.train_object_detector(cfg)
    eval_results = od.evaluate_test_set(eval_model, cfg)

    # write AP results to output
    for class_name in eval_results:
        print('AP for {:>15} = {:.4f}'.format(class_name,
                                              eval_results[class_name]))
    print('Mean AP = {:.4f}'.format(np.nanmean(list(eval_results.values()))))

    # detect objects in single image
    img_path = os.path.join(
        os.path.dirname(os.path.abspath(__file__)),
        r"../DataSets/Grocery/testImages/WIN_20160803_11_28_42_Pro.jpg")
    regressed_rois, cls_probs = od.evaluate_single_image(
        eval_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(
        len(regressed_rois), len(bboxes), len(fg_boxes[0])))
    for i in fg_boxes[0]:
        print("{:<12} (label: {:<2}), score: {:.3f}, box: {}".format(
            cfg["DATA"].CLASSES[labels[i]], labels[i], scores[i],
            [int(v) for v in bboxes[i]]))

    # visualize detections on image
    od.visualize_results(img_path, bboxes, labels, scores, cfg)

    # measure inference time
    od.measure_inference_time(eval_model, img_path, cfg, num_repetitions=100)
Esempio n. 9
0
if __name__ == '__main__':
    # Currently supported detectors: 'FastRCNN', 'FasterRCNN'
    args = sys.argv
    detector_name = get_detector_name(args)
    cfg = get_configuration(detector_name)

    # train and test
    eval_model = od.train_object_detector(cfg)
    eval_results = od.evaluate_test_set(eval_model, cfg)

    # write AP results to output
    for class_name in eval_results: print('AP for {:>15} = {:.4f}'.format(class_name, eval_results[class_name]))
    print('Mean AP = {:.4f}'.format(np.nanmean(list(eval_results.values()))))

    # detect objects in single image
    img_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), r"../DataSets/Grocery/testImages/WIN_20160803_11_28_42_Pro.jpg")
    regressed_rois, cls_probs = od.evaluate_single_image(eval_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(len(regressed_rois), len(bboxes), len(fg_boxes[0])))
    for i in fg_boxes[0]: print("{:<12} (label: {:<2}), score: {:.3f}, box: {}".format(
                                cfg["DATA"].CLASSES[labels[i]], labels[i], scores[i], [int(v) for v in bboxes[i]]))

    # visualize detections on image
    od.visualize_results(img_path, bboxes, labels, scores, cfg)

    # measure inference time
    od.measure_inference_time(eval_model, img_path, cfg, num_repetitions=100)