Beispiel #1
0
def test_box_encoder_cpu():

    pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=None)
    test_box_shape = [20, 4]

    def get_boxes():
        out = [
            (np.random.randint(0, 255, size=test_box_shape, dtype=np.uint8) /
             255).astype(dtype=np.float32) for _ in range(batch_size)
        ]
        return out

    test_lables_shape = [20, 1]

    def get_lables():
        out = [
            np.random.randint(0, 255, size=test_lables_shape, dtype=np.int32)
            for _ in range(batch_size)
        ]
        return out

    boxes = fn.external_source(source=get_boxes)
    lables = fn.external_source(source=get_lables)
    processed, _ = fn.box_encoder(boxes, lables, anchors=coco_anchors())
    pipe.set_outputs(processed)
    pipe.build()
    for _ in range(3):
        pipe.run()
Beispiel #2
0
def create_coco_pipeline(default_boxes, args):
    try:
        shard_id = torch.distributed.get_rank()
        num_shards = torch.distributed.get_world_size()
    except RuntimeError:
        shard_id = 0
        num_shards = 1

    images, bboxes, labels = fn.readers.coco(
        file_root=args.train_coco_root,
        annotations_file=args.train_annotate,
        skip_empty=True,
        shard_id=shard_id,
        num_shards=num_shards,
        ratio=True,
        ltrb=True,
        random_shuffle=False,
        shuffle_after_epoch=True,
        name="Reader")

    crop_begin, crop_size, bboxes, labels = fn.random_bbox_crop(
        bboxes,
        labels,
        device="cpu",
        aspect_ratio=[0.5, 2.0],
        thresholds=[0, 0.1, 0.3, 0.5, 0.7, 0.9],
        scaling=[0.3, 1.0],
        bbox_layout="xyXY",
        allow_no_crop=True,
        num_attempts=50)
    images = fn.image_decoder_slice(images,
                                    crop_begin,
                                    crop_size,
                                    device="mixed",
                                    output_type=types.RGB)
    flip_coin = fn.random.coin_flip(probability=0.5)
    images = fn.resize(images,
                       resize_x=300,
                       resize_y=300,
                       min_filter=types.DALIInterpType.INTERP_TRIANGULAR)

    saturation = fn.uniform(range=[0.5, 1.5])
    contrast = fn.uniform(range=[0.5, 1.5])
    brightness = fn.uniform(range=[0.875, 1.125])
    hue = fn.uniform(range=[-0.5, 0.5])

    images = fn.hsv(images, dtype=types.FLOAT, hue=hue,
                    saturation=saturation)  # use float to avoid clipping and
    # quantizing the intermediate result
    images = fn.brightness_contrast(
        images,
        contrast_center=128,  # input is in float, but in 0..255 range
        dtype=types.UINT8,
        brightness=brightness,
        contrast=contrast)

    dtype = types.FLOAT16 if args.fp16 else types.FLOAT

    bboxes = fn.bb_flip(bboxes, ltrb=True, horizontal=flip_coin)
    images = fn.crop_mirror_normalize(
        images,
        crop=(300, 300),
        mean=[0.485 * 255, 0.456 * 255, 0.406 * 255],
        std=[0.229 * 255, 0.224 * 255, 0.225 * 255],
        mirror=flip_coin,
        dtype=dtype,
        output_layout="CHW",
        pad_output=False)

    bboxes, labels = fn.box_encoder(bboxes,
                                    labels,
                                    criteria=0.5,
                                    anchors=default_boxes.as_ltrb_list())

    labels = labels.gpu()
    bboxes = bboxes.gpu()

    return images, bboxes, labels
Beispiel #3
0
def test_box_encoder_cpu():
    def coco_anchors():
        anchors = []

        fig_size = 300
        feat_sizes = [38, 19, 10, 5, 3, 1]
        feat_count = len(feat_sizes)
        steps = [8., 16., 32., 64., 100., 300.]
        scales = [21., 45., 99., 153., 207., 261., 315.]
        aspect_ratios = [[2], [2, 3], [2, 3], [2, 3], [2], [2]]

        fks = []
        for step in steps:
            fks.append(fig_size / step)

        anchor_idx = 0
        for idx in range(feat_count):
            sk1 = scales[idx] / fig_size
            sk2 = scales[idx + 1] / fig_size
            sk3 = sqrt(sk1 * sk2)

            all_sizes = [[sk1, sk1], [sk3, sk3]]

            for alpha in aspect_ratios[idx]:
                w = sk1 * sqrt(alpha)
                h = sk1 / sqrt(alpha)
                all_sizes.append([w, h])
                all_sizes.append([h, w])

            for sizes in all_sizes:
                w, h = sizes[0], sizes[1]

                for i in range(feat_sizes[idx]):
                    for j in range(feat_sizes[idx]):
                        cx = (j + 0.5) / fks[idx]
                        cy = (i + 0.5) / fks[idx]

                        cx = max(min(cx, 1.), 0.)
                        cy = max(min(cy, 1.), 0.)
                        w = max(min(w, 1.), 0.)
                        h = max(min(h, 1.), 0.)

                        anchors.append(cx - 0.5 * w)
                        anchors.append(cy - 0.5 * h)
                        anchors.append(cx + 0.5 * w)
                        anchors.append(cy + 0.5 * h)

                        anchor_idx = anchor_idx + 1
        return anchors

    pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=None)
    test_box_shape = [20, 4]

    def get_boxes():
        out = [
            (np.random.randint(0, 255, size=test_box_shape, dtype=np.uint8) /
             255).astype(dtype=np.float32) for _ in range(batch_size)
        ]
        return out

    test_lables_shape = [20, 1]

    def get_lables():
        out = [
            np.random.randint(0, 255, size=test_lables_shape, dtype=np.int32)
            for _ in range(batch_size)
        ]
        return out

    boxes = fn.external_source(source=get_boxes)
    lables = fn.external_source(source=get_lables)
    processed, _ = fn.box_encoder(boxes, lables, anchors=coco_anchors())
    pipe.set_outputs(processed)
    pipe.build()
    for _ in range(3):
        pipe.run()
def box_encoder_pipeline(get_boxes, get_labels):
    boxes = fn.external_source(source=get_boxes)
    labels = fn.external_source(source=get_labels)
    out = fn.box_encoder(boxes, labels, anchors=coco_anchors())
    return tuple(out)