コード例 #1
0
    def define_graph(self):
        inputs, bboxes, labels, polygons, vertices = fn.readers.coco(
                                            file_root=self.file_root,
                                            annotations_file=self.annotation_file,
                                            skip_empty=True,
                                            shard_id=self.share_id,
                                            num_shards=self.num_gpus,
                                            ratio=True,
                                            ltrb=True,
                                            polygon_masks = True,
                                            random_shuffle=self.random_shuffle,
                                            shuffle_after_epoch=self.shuffle_after_epoch,
                                            name="Reader")

        input_shape = fn.slice(fn.cast(fn.peek_image_shape(inputs), dtype=types.INT32), 0, 2, axes=[0])
        h = fn.slice(input_shape, 0, 1, axes = [0], dtype=types.FLOAT)
        w = fn.slice(input_shape, 1, 1, axes = [0], dtype=types.FLOAT)
        short_side = math.min(w, h)        
        scale = fn.random.uniform(range=[0.3, 1.])
        crop_side = fn.cast(math.ceil(scale * short_side), dtype=types.INT32)    
        crop_shape = fn.cat(crop_side, crop_side)
        anchor_rel, shape_rel, bboxes, labels, bbox_indices = fn.random_bbox_crop(
                        bboxes,
                        labels,
                        input_shape=input_shape,
                        crop_shape=crop_shape,
                        shape_layout="HW",
                        thresholds=[0.],            # No minimum intersection-over-union, for demo purposes
                        allow_no_crop=False,        # No-crop is disallowed, for demo purposes 
                        seed=-1,                    # Fixed random seed for deterministic results
                        bbox_layout="xyXY",         # left, top, right, back
                        output_bbox_indices=True,   # Output indices of the filtered bounding boxes
                        total_num_attempts=1024,
        )
        polygons, vertices = fn.segmentation.select_masks(
            bbox_indices, polygons, vertices
        )
        images = fn.decoders.image_slice(
            inputs, anchor_rel, shape_rel, normalized_anchor=False, normalized_shape=False, device='mixed'
        )
        images = fn.color_space_conversion(images, image_type=types.RGB, output_type=types.BGR)
        MT_1_vertices = fn.transforms.crop(
            to_start=(0.0, 0.0), to_end=fn.cat(w, h)
        )    
        MT_2_vertices = fn.transforms.crop(
            from_start=anchor_rel, from_end=(anchor_rel + shape_rel),
            to_start=(0.0, 0.0), to_end=(1., 1.)
        )    
        vertices = fn.coord_transform(fn.coord_transform(vertices, MT=MT_1_vertices), MT=MT_2_vertices)    
        targets = fn.cat( bboxes, fn.reshape(vertices, shape=[-1, 10]), axis=1)

        interp_methods = [types.INTERP_LINEAR, types.INTERP_CUBIC, types.INTERP_LANCZOS3, types.INTERP_GAUSSIAN, types.INTERP_NN, types.INTERP_TRIANGULAR]
        interp_method = fn.random.uniform(values=[int(x) for x in interp_methods], dtype=types.INT32)
        interp_method = fn.reinterpret(interp_method, dtype=types.INTERP_TYPE)
        images = fn.resize(images, dtype=types.FLOAT, size=self.input_dim, interp_type=interp_method)

        labels = labels.gpu()
        targets = targets.gpu()
        return (images, targets, labels)
コード例 #2
0
 def crop_fn(self, img, lbl):
     center = fn.segmentation.random_mask_pixel(lbl, foreground=fn.coin_flip(probability=self.oversampling))
     crop_anchor = self.slice_fn(center, 1, self.dim) - self.crop_shape // 2
     adjusted_anchor = math.max(0, crop_anchor)
     max_anchor = self.slice_fn(fn.shapes(lbl), 1, self.dim) - self.crop_shape
     crop_anchor = math.min(adjusted_anchor, max_anchor)
     img = fn.slice(img.gpu(), crop_anchor, self.crop_shape, axis_names=self.axis_name, out_of_bounds_policy="pad")
     lbl = fn.slice(lbl.gpu(), crop_anchor, self.crop_shape, axis_names=self.axis_name, out_of_bounds_policy="pad")
     return img, lbl
コード例 #3
0
 def crop_fn(self, img, lbl):
     center = fn.segmentation.random_mask_pixel(lbl, foreground=fn.coin_flip(probability=self.oversampling, **self.aug_seed_kwargs),
                                                **self.aug_seed_kwargs)
     crop_anchor = self.slice_fn(center) - self.crop_shape // 2
     adjusted_anchor = math.max(0, crop_anchor)
     max_anchor = self.slice_fn(fn.shapes(lbl)) - self.crop_shape
     crop_anchor = math.min(adjusted_anchor, max_anchor)
     img = fn.slice(img, crop_anchor, self.crop_shape, axis_names="DHW", out_of_bounds_policy="pad")
     lbl = fn.slice(lbl, crop_anchor, self.crop_shape, axis_names="DHW", out_of_bounds_policy="pad")
     return img, lbl
コード例 #4
0
def check_random_mask_pixel(ndim=2, batch_size=3,
                            min_extent=20, max_extent=50):
    pipe = dali.pipeline.Pipeline(batch_size=batch_size, num_threads=4, device_id=0, seed=1234)
    with pipe:
        # Input mask
        in_shape_dims = [fn.cast(fn.random.uniform(range=(min_extent, max_extent + 1)),
                                 dtype=types.INT32) for _ in range(ndim)]
        in_shape = fn.stack(*in_shape_dims)
        in_mask = fn.cast(fn.random.uniform(range=(0, 2), shape=in_shape), dtype=types.INT32)

        #  > 0
        fg_pixel1 = fn.segmentation.random_mask_pixel(in_mask, foreground=1)
        #  >= 0.99
        fg_pixel2 = fn.segmentation.random_mask_pixel(in_mask, foreground=1, threshold=0.99)
        #  == 2
        fg_pixel3 = fn.segmentation.random_mask_pixel(in_mask, foreground=1, value=2)

        rnd_pixel = fn.segmentation.random_mask_pixel(in_mask, foreground=0)

        coin_flip = fn.random.coin_flip(probability=0.7)
        fg_biased = fn.segmentation.random_mask_pixel(in_mask, foreground=coin_flip)

        # Demo purposes: Taking a random pixel and produce a valid anchor to feed slice
        # We want to force the center adjustment, thus the large crop shape
        crop_shape = in_shape - 2
        anchor = fn.cast(fg_pixel1, dtype=types.INT32) - crop_shape // 2
        anchor = math.min(math.max(0, anchor), in_shape - crop_shape)
        out_mask = fn.slice(in_mask, anchor, crop_shape, axes=tuple(range(ndim)))

    pipe.set_outputs(in_mask, fg_pixel1, fg_pixel2, fg_pixel3, rnd_pixel, coin_flip, fg_biased,
                     anchor, crop_shape, out_mask)
    pipe.build()
    for iter in range(3):
        outputs = pipe.run()
        for idx in range(batch_size):
            in_mask = outputs[0].at(idx)
            fg_pixel1 = outputs[1].at(idx).tolist()
            fg_pixel2 = outputs[2].at(idx).tolist()
            fg_pixel3 = outputs[3].at(idx).tolist()
            rnd_pixel = outputs[4].at(idx).tolist()
            coin_flip = outputs[5].at(idx).tolist()
            fg_biased = outputs[6].at(idx).tolist()
            anchor = outputs[7].at(idx).tolist()
            crop_shape = outputs[8].at(idx).tolist()
            out_mask = outputs[9].at(idx)

            assert in_mask[tuple(fg_pixel1)] > 0
            assert in_mask[tuple(fg_pixel2)] > 0.99
            assert in_mask[tuple(fg_pixel3)] == 2
            assert in_mask[tuple(fg_biased)] > 0 or not coin_flip

            for d in range(ndim):
                assert 0 <= anchor[d] and anchor[d] + crop_shape[d] <= in_mask.shape[d]
            assert out_mask.shape == tuple(crop_shape)
コード例 #5
0
def pipeline_arithm_ops_cpu(source):
    data = fn.external_source(source=source, layout="HWC")
    processed = (data * 2,
                 data + 2,
                 data - 2,
                 data / 2,
                 data // 2,
                 data ** 2,
                 data == 2,
                 data != 2,
                 data < 2,
                 data <= 2,
                 data > 2,
                 data >= 2,
                 data & 2,
                 data | 2,
                 data ^ 2,
                 dmath.abs(data),
                 dmath.fabs(data),
                 dmath.floor(data),
                 dmath.ceil(data),
                 dmath.pow(data, 2),
                 dmath.fpow(data, 1.5),
                 dmath.min(data, 2),
                 dmath.max(data, 50),
                 dmath.clamp(data, 10, 50),
                 dmath.sqrt(data),
                 dmath.rsqrt(data),
                 dmath.cbrt(data),
                 dmath.exp(data),
                 dmath.exp(data),
                 dmath.log(data),
                 dmath.log2(data),
                 dmath.log10(data),
                 dmath.sin(data),
                 dmath.cos(data),
                 dmath.tan(data),
                 dmath.asin(data),
                 dmath.acos(data),
                 dmath.atan(data),
                 dmath.atan2(data, 3),
                 dmath.sinh(data),
                 dmath.cosh(data),
                 dmath.tanh(data),
                 dmath.asinh(data),
                 dmath.acosh(data),
                 dmath.atanh(data))
    return processed
コード例 #6
0
ファイル: test_dali_cpu_only.py プロジェクト: hannahaih/DALI
def test_arithm_ops_cpu():
    pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=None)
    data = fn.external_source(source=get_data, layout="HWC")
    processed = [
        data * 2, data + 2, data - 2, data / 2, data // 2, data**2, data == 2,
        data != 2, data < 2, data <= 2, data > 2, data >= 2, data & 2,
        data | 2, data ^ 2,
        dmath.abs(data),
        dmath.fabs(data),
        dmath.floor(data),
        dmath.ceil(data),
        dmath.pow(data, 2),
        dmath.fpow(data, 1.5),
        dmath.min(data, 2),
        dmath.max(data, 50),
        dmath.clamp(data, 10, 50),
        dmath.sqrt(data),
        dmath.rsqrt(data),
        dmath.cbrt(data),
        dmath.exp(data),
        dmath.exp(data),
        dmath.log(data),
        dmath.log2(data),
        dmath.log10(data),
        dmath.sin(data),
        dmath.cos(data),
        dmath.tan(data),
        dmath.asin(data),
        dmath.acos(data),
        dmath.atan(data),
        dmath.atan2(data, 3),
        dmath.sinh(data),
        dmath.cosh(data),
        dmath.tanh(data),
        dmath.asinh(data),
        dmath.acosh(data),
        dmath.atanh(data)
    ]
    pipe.set_outputs(*processed)
    pipe.build()
    for _ in range(3):
        pipe.run()
コード例 #7
0
                                ("cpu", "const", "const"),
                                ("gpu", "const", "const"),
                                ("gpu", "cpu", "cpu_scalar"),
                                ("cpu_scalar", "cpu_scalar", "cpu_scalar")]

bench_ternary_input_kinds = [("cpu", "cpu", "cpu"), ("gpu", "gpu", "gpu"),
                             ("cpu", "const", "const"),
                             ("gpu", "const", "const"),
                             ("cpu", "cpu", "const"), ("gpu", "gpu", "const")]

unary_operations = [((lambda x: +x), "+"), ((lambda x: -x), "-")]

sane_operations = [
    ((lambda x, y: x + y), "+"), ((lambda x, y: x - y), "-"),
    ((lambda x, y: x * y), "*"),
    (((lambda x, y: math.min(x, y)), (lambda x, y: np.minimum(x, y))), "min"),
    (((lambda x, y: math.max(x, y)), (lambda x, y: np.maximum(x, y))), "max")
]

bitwise_operations = [((lambda x, y: x & y), "&"), ((lambda x, y: x | y), "|"),
                      ((lambda x, y: x ^ y), "^")]

comparisons_operations = [
    ((lambda x, y: x == y), "=="),
    ((lambda x, y: x != y), "!="),
    ((lambda x, y: x < y), "<"),
    ((lambda x, y: x <= y), "<="),
    ((lambda x, y: x > y), ">"),
    ((lambda x, y: x >= y), ">="),
]
コード例 #8
0
    ((lambda x: math.acos(x)), (lambda x: np.arccos(x)), "acos", one_range, 1e-6),
    ((lambda x: math.atan(x)), (lambda x: np.arctan(x)), "atan", default_range, 1e-6),
    ((lambda x: math.sinh(x)), (lambda x: np.sinh(x)), "sinh", default_range, 1e-6),
    ((lambda x: math.cosh(x)), (lambda x: np.cosh(x)), "cosh", default_range, 1e-6),
    ((lambda x: math.tanh(x)), (lambda x: np.tanh(x)), "tanh", default_range, 1e-6),
    ((lambda x: math.asinh(x)), (lambda x: np.arcsinh(x)), "asinh", limited_range, 1e-6),
    ((lambda x: math.acosh(x)), (lambda x: np.arccosh(x)), "acosh", pos_range, 1e-6),
    ((lambda x: math.atanh(x)), (lambda x: np.arctanh(x)), "atanh", one_range, 1e-6)]


sane_operations = [((lambda x, y: x + y), "+", default_range),
                   ((lambda x, y: x - y), "-", default_range),
                   ((lambda x, y: x * y), "*", default_range),
                   (((lambda x, y: x ** y), sane_pow), "**", pow_range),
                   (((lambda x, y: math.pow(x, y)), sane_pow), "pow", pow_range),
                   (((lambda x, y: math.min(x, y)), (lambda x, y: np.minimum(x, y))), "min", default_range),
                   (((lambda x, y: math.max(x, y)), (lambda x, y: np.maximum(x, y))), "max", default_range)]

floaty_operations = [(((lambda x, y: x / y), (lambda x, y: x / y)), "/", default_range),
                     (((lambda x, y: math.fpow(x, y)), sane_pow), "fpow", pow_range),
                     (((lambda x, y: math.atan2(x, y)), (lambda x, y: np.arctan2(x, y))), "atan2", default_range)]

bitwise_operations = [((lambda x, y: x & y), "&"), ((lambda x, y: x | y), "|"),
                      ((lambda x, y: x ^ y), "^")]

comparisons_operations = [((lambda x, y: x == y), "=="), ((lambda x, y: x != y), "!="),
                          ((lambda x, y: x < y), "<"), ((lambda x, y: x <= y), "<="),
                          ((lambda x, y: x > y), ">"), ((lambda x, y: x >= y), ">="),]

# The observable behaviour for hi < lo is the same as numpy
ternary_operations = [(((lambda v, lo, hi: math.clamp(v, lo, hi)), (lambda v, lo, hi: np.clip(v, lo, hi))), "clamp")]
コード例 #9
0
     1e-6),
    ((lambda x: math.asinh(x)), (lambda x: np.arcsinh(x)), "asinh",
     limited_range, 1e-6),
    ((lambda x: math.acosh(x)), (lambda x: np.arccosh(x)), "acosh", pos_range,
     1e-6),
    ((lambda x: math.atanh(x)), (lambda x: np.arctanh(x)), "atanh", one_range,
     1e-6)
]

sane_operations = [((lambda x, y: x + y), "+", default_range),
                   ((lambda x, y: x - y), "-", default_range),
                   ((lambda x, y: x * y), "*", default_range),
                   (((lambda x, y: x**y), sane_pow), "**", pow_range),
                   (((lambda x, y: math.pow(x, y)), sane_pow), "pow",
                    pow_range),
                   (((lambda x, y: math.min(x, y)),
                     (lambda x, y: np.minimum(x, y))), "min", default_range),
                   (((lambda x, y: math.max(x, y)),
                     (lambda x, y: np.maximum(x, y))), "max", default_range)]

floaty_operations = [
    (((lambda x, y: x / y), (lambda x, y: x / y)), "/", default_range),
    (((lambda x, y: math.fpow(x, y)), sane_pow), "fpow", pow_range),
    (((lambda x, y: math.atan2(x, y)), (lambda x, y: np.arctan2(x, y))),
     "atan2", default_range)
]

bitwise_operations = [((lambda x, y: x & y), "&"), ((lambda x, y: x | y), "|"),
                      ((lambda x, y: x ^ y), "^")]

comparisons_operations = [