コード例 #1
0
ファイル: DetectionDemo_test.py プロジェクト: Tak-Au/CNTK
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]
コード例 #2
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)
コード例 #3
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)
    # 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
コード例 #5
0
ファイル: DetectionDemo.py プロジェクト: AllanYiin/CNTK
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)