def test_read_annotations_wrong_y2():
    with pytest.raises(ValueError):
        try:
            csv_generator._read_annotations(csv_str('a.png,0,1,2,a,a'), {'a': 1})
        except ValueError as e:
            assert str(e).startswith("line 1: malformed y2:")
            raise
def test_read_annotations_wrong_class():
    with pytest.raises(ValueError):
        try:
            csv_generator._read_annotations(csv_str('a.png,0,1,2,3,g'), {'a': 1})
        except ValueError as e:
            assert str(e).startswith("line 1: unknown class name:")
            raise
示例#3
0
def test_read_annotations_wrong_y2():
    with pytest.raises(ValueError):
        try:
            csv_generator._read_annotations(csv_str('a.png,0,1,2,a,a'),
                                            {'a': 1})
        except ValueError as e:
            assert str(e).startswith("line 1: malformed y2:")
            raise
示例#4
0
def test_read_annotations_wrong_class():
    with pytest.raises(ValueError):
        try:
            csv_generator._read_annotations(csv_str('a.png,0,1,2,3,g'),
                                            {'a': 1})
        except ValueError as e:
            assert str(e).startswith("line 1: unknown class name:")
            raise
def test_read_annotations_invalid_bb_x():
    with pytest.raises(ValueError):
        try:
            csv_generator._read_annotations(csv_str('a.png,1,2,1,3,g'), {'a': 1})
        except ValueError as e:
            assert str(e).startswith("line 1: x2 (1) must be higher than x1 (1)")
            raise
    with pytest.raises(ValueError):
        try:
            csv_generator._read_annotations(csv_str('a.png,9,2,5,3,g'), {'a': 1})
        except ValueError as e:
            assert str(e).startswith("line 1: x2 (5) must be higher than x1 (9)")
            raise
示例#6
0
def test_read_annotations_empty_image():
    # Check that images without annotations are parsed.
    assert csv_generator._read_annotations(csv_str('a.png,,,,,\nb.png,,,,,'),
                                           {'a': 1}) == {
                                               'a.png': [],
                                               'b.png': []
                                           }

    # Check that lines without annotations don't clear earlier annotations.
    assert csv_generator._read_annotations(
        csv_str('a.png,0,1,2,3,a\na.png,,,,,'), {'a': 1}) == {
            'a.png': [annotation(0, 1, 2, 3, 'a')]
        }
def test_read_annotations_invalid_bb_y():
    with pytest.raises(ValueError):
        try:
            csv_generator._read_annotations(csv_str('a.png,1,2,3,2,a'), {'a': 1})
        except ValueError as e:
            assert str(e).startswith("line 1: y2 (2) must be higher than y1 (2)")
            raise
    with pytest.raises(ValueError):
        try:
            csv_generator._read_annotations(csv_str('a.png,1,8,3,5,a'), {'a': 1})
        except ValueError as e:
            assert str(e).startswith("line 1: y2 (5) must be higher than y1 (8)")
            raise
示例#8
0
def test_read_annotations_invalid_bb_y():
    with pytest.raises(ValueError):
        try:
            annotations = csv_generator._read_annotations(
                csv_str('a.png,1,2,3,2,a'), {'a': 1})
        except ValueError as e:
            assert str(e).startswith(
                "line 1: y2 (2) must be higher than y1 (2)")
            raise
    with pytest.raises(ValueError):
        try:
            annotations = csv_generator._read_annotations(
                csv_str('a.png,1,8,3,5,a'), {'a': 1})
        except ValueError as e:
            assert str(e).startswith(
                "line 1: y2 (5) must be higher than y1 (8)")
            raise
示例#9
0
def test_read_annotations_invalid_bb_x():
    with pytest.raises(ValueError):
        try:
            annotations = csv_generator._read_annotations(
                csv_str('a.png,1,2,1,3,g'), {'a': 1})
        except ValueError as e:
            assert str(e).startswith(
                "line 1: x2 (1) must be higher than x1 (1)")
            raise
    with pytest.raises(ValueError):
        try:
            annotations = csv_generator._read_annotations(
                csv_str('a.png,9,2,5,3,g'), {'a': 1})
        except ValueError as e:
            assert str(e).startswith(
                "line 1: x2 (5) must be higher than x1 (9)")
            raise
def test_read_annotations_wrong_format():
    classes = {'a': 1, 'b': 2, 'c': 4, 'd': 10}
    with pytest.raises(ValueError):
        try:
            csv_generator._read_annotations(csv_str('a.png,1,2,3,a'), classes)
        except ValueError as e:
            assert str(e).startswith("line 1: format should be")
            raise

    with pytest.raises(ValueError):
        try:
            csv_generator._read_annotations(csv_str(
                'a.png,0,1,2,3,a' '\n'
                'a.png,1,2,3,a'   '\n'
            ), classes)
        except ValueError as e:
            assert str(e).startswith("line 2: format should be")
            raise
示例#11
0
def test_read_annotations_wrong_format():
    classes = {'a': 1, 'b': 2, 'c': 4, 'd': 10}
    with pytest.raises(ValueError):
        try:
            csv_generator._read_annotations(csv_str('a.png,1,2,3,a'), classes)
        except ValueError as e:
            assert str(e).startswith("line 1: format should be")
            raise

    with pytest.raises(ValueError):
        try:
            csv_generator._read_annotations(
                csv_str('a.png,0,1,2,3,a'
                        '\n'
                        'a.png,1,2,3,a'
                        '\n'), classes)
        except ValueError as e:
            assert str(e).startswith("line 2: format should be")
            raise
def test_read_annotations_multiple():
    classes = {'a': 1, 'b': 2, 'c': 4, 'd': 10}
    annotations = csv_generator._read_annotations(csv_str(
        'a.png,0,1,2,3,a'     '\n'
        'b.png,4,5,6,7,b'     '\n'
        'a.png,8,9,10,11,c'   '\n'
    ), classes)
    assert annotations == {
        'a.png': [
            annotation(0, 1,  2,  3, 'a'),
            annotation(8, 9, 10, 11, 'c'),
        ],
        'b.png': [annotation(4, 5, 6, 7, 'b')],
    }
def test_read_annotations():
    classes = {'a': 1, 'b': 2, 'c': 4, 'd': 10}
    annotations = csv_generator._read_annotations(csv_str(
        'a.png,0,1,2,3,a'     '\n'
        'b.png,4,5,6,7,b'     '\n'
        'c.png,8,9,10,11,c'   '\n'
        'd.png,12,13,14,15,d' '\n'
    ), classes)
    assert annotations == {
        'a.png': [annotation( 0,  1,  2,  3, 'a')],
        'b.png': [annotation( 4,  5,  6,  7, 'b')],
        'c.png': [annotation( 8,  9, 10, 11, 'c')],
        'd.png': [annotation(12, 13, 14, 15, 'd')],
    }
示例#14
0
def test_read_annotations_multiple():
    classes = {'a': 1, 'b': 2, 'c': 4, 'd': 10}
    annotations = csv_generator._read_annotations(
        csv_str('a.png,0,1,2,3,a'
                '\n'
                'b.png,4,5,6,7,b'
                '\n'
                'a.png,8,9,10,11,c'
                '\n'), classes)
    assert annotations == {
        'a.png': [
            annotation(0, 1, 2, 3, 'a'),
            annotation(8, 9, 10, 11, 'c'),
        ],
        'b.png': [annotation(4, 5, 6, 7, 'b')],
    }
示例#15
0
def test_read_annotations():
    classes = {'a': 1, 'b': 2, 'c': 4, 'd': 10}
    annotations = csv_generator._read_annotations(
        csv_str('a.png,0,1,2,3,a'
                '\n'
                'b.png,4,5,6,7,b'
                '\n'
                'c.png,8,9,10,11,c'
                '\n'
                'd.png,12,13,14,15,d'
                '\n'), classes)
    assert annotations == {
        'a.png': [annotation(0, 1, 2, 3, 'a')],
        'b.png': [annotation(4, 5, 6, 7, 'b')],
        'c.png': [annotation(8, 9, 10, 11, 'c')],
        'd.png': [annotation(12, 13, 14, 15, 'd')],
    }
示例#16
0
def main(args=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)

    # make sure keras is the minimum required version
    check_keras_version()

    # optionally choose specific GPU
    if args.gpu:
        os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
    keras.backend.tensorflow_backend.set_session(get_session())

    # make save path if it doesn't exist
    if args.save_path is not None and not os.path.exists(args.save_path):
        os.makedirs(args.save_path)

    # optionally load config parameters
    if args.config:
        args.config = read_config_file(args.config)

    # create the generator
    #generator = create_generator(args)

    # optionally load anchor parameters
    anchor_params = None
    if args.config and 'anchor_parameters' in args.config:
        anchor_params = parse_anchor_parameters(args.config)

    # load the model
    print('Loading model, this may take a second...')
    model = models.load_model(args.model, backbone_name=args.backbone)

    # optionally convert the model
    if args.convert_model:
        model = models.convert_model(model, anchor_params=anchor_params)

    # print model summary
    print(model.summary())

    print("annotations", args.annotations)
    print("classes", args.classes)
    print("min", args.image_min_side)
    print("max", args.image_max_side)
    print("configs", args.config)
    iou_threshold = args.iou_threshold
    score_threshold = args.score_threshold
    max_detections = args.max_detections

    image_names = []
    image_data = {}

    # Take base_dir from annotations file if not explicitly specified.

    # parse the provided class file
    try:
        with _open_for_csv(args.classes) as file:
            classes = _read_classes(csv.reader(file, delimiter=','))
    except ValueError as e:
        raise_from(
            ValueError('invalid CSV class file: {}: {}'.format(
                args.classes, e)), None)

    labels = {}
    for key, value in classes.items():
        labels[value] = key

    # csv with img_path, x1, y1, x2, y2, class_name
    try:
        with _open_for_csv(args.annotations) as file:
            file_annotations = _read_annotations(
                csv.reader(file, delimiter=','), classes)
    except ValueError as e:
        raise_from(
            ValueError('invalid CSV annotations file: {}: {}'.format(
                args.annotations, e)), None)
    image_names = list(file_annotations.keys())

    num_classes = len(labels)

    all_detections = [[None for i in range(num_classes) if i in labels]
                      for j in range(len(image_names))]
    for image_index in range(len(image_names)):
        """ Load annotations for an image_index.
		"""
        path = file_annotations[image_names[image_index]]
        annotations = {'labels': np.empty((0, )), 'bboxes': np.empty((0, 4))}

        for idx, annot in enumerate(
                file_annotations[image_names[image_index]]):
            for key, value in classes.items():
                if annot['class'] == key:
                    break

            annotations['labels'] = np.concatenate(
                (annotations['labels'], [value]))
            annotations['bboxes'] = np.concatenate((annotations['bboxes'], [[
                float(annot['x1']),
                float(annot['y1']),
                float(annot['x2']),
                float(annot['y2']),
            ]]))

    f = []
    for label in range(num_classes):
        for cls in classes:
            if classes[cls] == label:
                print('class', cls)
                break
        f.append(
            open(
                "results/comp4_det_" +
                args.annotations.split("/")[-1].split(".")[0] + "_" + cls +
                ".txt", 'w'))

    for i in range(0, len(image_names)):
        print('image num', i)
        file_name = image_names[i]

        # load image
        image = read_image_bgr(file_name)
        image = preprocess_image(image)
        image, scale = resize_image(image)
        boxes, scores, labels = model.predict_on_batch(
            np.expand_dims(image, axis=0))
        boxes /= scale

        # select indices which have a score above the threshold
        indices = np.where(scores[0, :] > score_threshold)[0]

        # select those scores
        scores = scores[0][indices]

        # find the order with which to sort the scores
        scores_sort = np.argsort(-scores)[:max_detections]

        # select detections
        image_boxes = boxes[0, indices[scores_sort], :]
        image_scores = scores[scores_sort]
        image_labels = labels[0, indices[scores_sort]]
        image_detections = np.concatenate([
            image_boxes,
            np.expand_dims(image_scores, axis=1),
            np.expand_dims(image_labels, axis=1)
        ],
                                          axis=1)
        # copy detections to all_detections
        for label in range(num_classes):
            if not label in labels:
                continue
            dets = image_detections[image_detections[:, -1] == label, :-1]
            for cls in classes:
                if classes[cls] == label:
                    print('class', cls)
                    break
            for scr in dets:
                f[label].write(
                    file_name.split("/")[-1].split(".")[0] + " " +
                    str(scr[4]) + " " + str(scr[0]) + " " + str(scr[1]) + " " +
                    str(scr[2]) + " " + str(scr[3]) + "\n")

    for label in range(num_classes):
        f[label].close()
def test_read_annotations_empty_image():
    # Check that images without annotations are parsed.
    assert csv_generator._read_annotations(csv_str('a.png,,,,,\nb.png,,,,,'), {'a': 1}) == {'a.png': [], 'b.png': []}

    # Check that lines without annotations don't clear earlier annotations.
    assert csv_generator._read_annotations(csv_str('a.png,0,1,2,3,a\na.png,,,,,'), {'a': 1}) == {'a.png': [annotation(0, 1,  2,  3, 'a')]}