예제 #1
0
def check_gaussian_blur(batch_size, sigma, window_size, op_type="cpu"):
    decoder_device = "cpu" if op_type == "cpu" else "mixed"
    pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=0)
    with pipe:
        input, _ = fn.file_reader(file_root=images_dir,
                                  shard_id=0,
                                  num_shards=1)
        decoded = fn.image_decoder(input,
                                   device=decoder_device,
                                   output_type=types.RGB)
        blurred = fn.gaussian_blur(decoded,
                                   device=op_type,
                                   sigma=sigma,
                                   window_size=window_size)
        pipe.set_outputs(blurred, decoded)
    pipe.build()

    for _ in range(test_iters):
        result, input = pipe.run()
        if op_type == "gpu":
            result = result.as_cpu()
            input = input.as_cpu()
        input = to_batch(input, batch_size)
        baseline_cv = [gaussian_cv(img, sigma, window_size) for img in input]
        check_batch(result, baseline_cv, batch_size, max_allowed_error=1)
예제 #2
0
def check_generic_gaussian_blur(
        batch_size, sigma, window_size, shape, layout, axes, op_type="cpu", in_dtype=np.uint8,
        out_dtype=types.NO_TYPE, random_shape=True):
    pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=0)
    min_shape = None if random_shape else shape
    data = RandomlyShapedDataIterator(batch_size, min_shape=min_shape, max_shape=shape, dtype=in_dtype)
    # Extract the numpy type from DALI, we can have float32 or the same as input
    if out_dtype == types.NO_TYPE:
        result_type = in_dtype
    elif dali_type(in_dtype) == out_dtype:
        result_type = in_dtype
    else:
        result_type = np.float32
    with pipe:
        input = fn.external_source(data, layout=layout)
        if op_type == "gpu":
            input = input.gpu()
        blurred = fn.gaussian_blur(input, device=op_type, sigma=sigma,
                                   window_size=window_size, dtype=out_dtype)
        pipe.set_outputs(blurred, input)
    pipe.build()

    for _ in range(test_iters):
        result, input = pipe.run()
        if op_type == "gpu":
            result = result.as_cpu()
            input = input.as_cpu()
        input = to_batch(input, batch_size)
        skip_axes = count_skip_axes(layout)
        baseline = [
            gaussian_baseline(img, sigma, window_size, axes, skip_axes, dtype=result_type)
            for img in input]
        max_error = 1 if result_type != np.float32 else 1e-04
        check_batch(result, baseline, batch_size, max_allowed_error=max_error, expected_layout=layout)
예제 #3
0
def check_gaussian_blur_output(batch_size, sigma, window_size, op_type="cpu"):
    decoder_device = "cpu" if op_type == "cpu" else "mixed"
    pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=0)
    with pipe:
        input, _ = fn.file_reader(file_root=images_dir,
                                  shard_id=0,
                                  num_shards=1)
        decoded = fn.image_decoder(input,
                                   device=decoder_device,
                                   output_type=types.RGB)
        blurred = fn.gaussian_blur(decoded,
                                   device=op_type,
                                   sigma=sigma,
                                   window_size=window_size)
        normalized = fn.crop_mirror_normalize(blurred,
                                              device=op_type,
                                              dtype=types.FLOAT,
                                              output_layout="HWC",
                                              mean=[128.0, 128.0, 128.0],
                                              std=[100.0, 100.0, 100.0])
        pipe.set_outputs(normalized)
    pipe.build()

    for _ in range(3):
        result = pipe.run()
 def pipeline():
     outputs = fn.external_source(  # noqa F841
         source=sample_cb_source,
         batch=False,
         num_outputs=num_outputs)
     data = fn.random.uniform(range=(0, 255), shape=(300, 100, 3))
     img = fn.reshape(data, layout="HWC")
     return fn.gaussian_blur(img, window_size=3)
예제 #5
0
def check_per_sample_gaussian_blur(batch_size,
                                   sigma_dim,
                                   window_size_dim,
                                   shape,
                                   layout,
                                   axes,
                                   op_type="cpu"):
    pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=0)
    data = RandomlyShapedDataIterator(batch_size, max_shape=shape)
    with pipe:
        if sigma_dim is not None:
            sigma = fn.random.uniform(range=[0.5, 3], shape=[sigma_dim])
            sigma_arg = sigma
        else:
            # placeholder, so we can return something
            sigma = fn.random.coin_flip(probability=0)
            sigma_arg = None

        if window_size_dim is not None:
            window_radius = fn.random.uniform(range=[5, 10],
                                              shape=[window_size_dim])
            window_size = fn.cast(window_radius, dtype=types.INT32) * 2 + 1
            window_arg = window_size
        else:
            window_size = fn.random.coin_flip(probability=0)
            window_arg = None

        input = fn.external_source(data, layout=layout)
        if op_type == "gpu":
            input = input.gpu()
        blurred = fn.gaussian_blur(input,
                                   device=op_type,
                                   sigma=sigma_arg,
                                   window_size=window_arg)
        pipe.set_outputs(blurred, input, sigma, window_size)
    pipe.build()

    for _ in range(test_iters):
        result, input, sigma, window_size = pipe.run()
        if op_type == "gpu":
            result = result.as_cpu()
            input = input.as_cpu()
        input = to_batch(input, batch_size)
        sigma = to_batch(sigma, batch_size)
        window_size = to_batch(window_size, batch_size)
        baseline = []
        for i in range(batch_size):
            sigma_arg = sigma[i] if sigma is not None else None
            window_arg = window_size[i] if window_size_dim is not None else None
            skip_axes = count_skip_axes(layout)
            baseline.append(
                gaussian_baseline(input[i], sigma_arg, window_arg, axes,
                                  skip_axes))
        check_batch(result,
                    baseline,
                    batch_size,
                    max_allowed_error=1,
                    expected_layout=layout)
 def pipeline():
     outputs = fn.external_source(source=sources[source_type],
                                  num_outputs=num_outputs,
                                  batch=source_type != "sample_cb_source")
     assert len(outputs) == num_outputs
     utilized_outputs = (out for out, is_used in zip(outputs, usage_mask)
                         if is_used)
     return tuple(
         fn.gaussian_blur(out, window_size=3) for out in utilized_outputs)
예제 #7
0
def get_gaussian_pipe(batch_size, sigma, window_size, op_type):
    pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=0)
    with pipe:
        input, _ = fn.readers.file(file_root=images_dir, shard_id=0, num_shards=1)
        decoded = fn.decoders.image(input, device="cpu", output_type=types.RGB)
        if op_type == "gpu":
            decoded = decoded.gpu()
        blurred = fn.gaussian_blur(decoded, device=op_type, sigma=sigma, window_size=window_size)
        pipe.set_outputs(blurred, decoded)
    return pipe
예제 #8
0
 def blur_fn(self, img):
     img_blured = fn.gaussian_blur(img, sigma=fn.uniform(range=(0.5, 1.5)))
     return self.random_augmentation(0.15, img_blured, img)
        labels += [index] * args.bs

    W, H = int(args.sz * 120), int(args.sz * 400)
    ImageBytes = W * H * 3 * 4

    TestingPipe = Pipeline(batch_size=args.bs, num_threads=4, device_id=0)
    with TestingPipe:
        files, labels = fn.file_reader(files=DataList, labels=labels)
        images = fn.image_decoder(files, device='cpu', use_fast_idct=True)
        images = fn.resize(images.gpu(),
                           device='gpu',
                           bytes_per_sample_hint=ImageBytes,
                           size=(H, W))
        images = fn.gaussian_blur(images,
                                  device='gpu',
                                  bytes_per_sample_hint=ImageBytes,
                                  sigma=fn.uniform(range=(0.1, 2)),
                                  window_size=11)
        images = fn.color_twist(images,
                                device='gpu',
                                bytes_per_sample_hint=ImageBytes,
                                brightness=fn.uniform(range=(0.5, 1.5)),
                                contrast=fn.uniform(range=(0.5, 2.5)),
                                saturation=fn.uniform(range=(0.1, 2)))
        images = fn.cast(images,
                         device='gpu',
                         bytes_per_sample_hint=ImageBytes,
                         dtype=DALIDataType.FLOAT)
        images = fn.normalize(
            images,
            device='gpu',
예제 #10
0
 def pipeline():
     blob = fn.random.uniform(range=[0, 1], shape=(3, 200, 100))
     image = fn.reshape(blob, layout="CHW")
     per_channel = np.array([3, 5, 7])
     return fn.gaussian_blur(image, window_size=fn.per_frame(per_channel))
예제 #11
0
def train_pipeline(cfg: TrainLoaderConfig):

    jpeg, label = fn.readers.file(
        file_root=ROOT_DATA_DIR + "/train/",
        random_shuffle=True,
        shard_id=env_rank(),
        num_shards=env_world_size(),
        name="Reader",
    )
    image = fn.decoders.image_random_crop(
        jpeg,
        device="mixed",
        random_aspect_ratio=[0.75, 1.25],
        random_area=[cfg.min_area, 1.0],
        num_attempts=100,
        output_type=types.RGB,
    )

    image_tr = fn.resize(image,
                         device="gpu",
                         size=cfg.image_size,
                         interp_type=types.INTERP_TRIANGULAR)
    if cfg.random_interpolation:
        image_cub = fn.resize(image,
                              device="gpu",
                              size=cfg.image_size,
                              interp_type=types.INTERP_CUBIC)
        image = mix(fn.random.coin_flip(probability=0.5), image_cub, image_tr)
    else:
        image = image_tr

    if cfg.blur_prob > 0:
        blur_image = fn.gaussian_blur(
            image,
            device="gpu",
            window_size=11,
            sigma=fn.random.uniform(range=[0.5, 1.1]))
        image = mix(
            fn.random.coin_flip(probability=cfg.blur_prob, dtype=types.BOOL),
            blur_image, image)

    if cfg.color_twist_prob > 0:
        image_ct = fn.color_twist(
            image,
            device="gpu",
            contrast=fn.random.uniform(range=[0.7, 1.3]),
            brightness=fn.random.uniform(range=[0.7, 1.3]),
            hue=fn.random.uniform(range=[-20, 20]),  # in degrees
            saturation=fn.random.uniform(range=[0.7, 1.3]),
        )
        image = mix(
            fn.random.coin_flip(probability=cfg.color_twist_prob,
                                dtype=types.BOOL), image_ct, image)

    if cfg.gray_prob > 0:
        grayscale_coin = fn.cast(
            fn.random.coin_flip(probability=cfg.gray_prob), dtype=types.FLOAT)
        image = fn.hsv(image, device="gpu", saturation=grayscale_coin)

    if cfg.re_prob:  # random erasing
        image_re = fn.erase(
            image,
            device="gpu",
            anchor=fn.random.uniform(range=(0.0, 1), shape=cfg.re_count * 2),
            shape=fn.random.uniform(range=(0.05, 0.25),
                                    shape=cfg.re_count * 2),
            axis_names="HW",
            fill_value=DATA_MEAN,
            normalized_anchor=True,
            normalized_shape=True,
        )
        image = mix(
            fn.random.coin_flip(probability=cfg.re_prob, dtype=types.BOOL),
            image_re, image)

    image = fn.crop_mirror_normalize(
        image,
        device="gpu",
        crop=(cfg.image_size, cfg.image_size),
        mirror=fn.random.coin_flip(probability=0.5),
        mean=DATA_MEAN,
        std=DATA_STD,
        dtype=types.FLOAT,
        output_layout=types.NCHW,
    )
    label = fn.one_hot(label, num_classes=cfg.num_classes).gpu()
    return image, label
예제 #12
0
 def blur_fn(self, img):
     img_blured = fn.gaussian_blur(img, sigma=fn.uniform(range=(0.5, 1.5), **self.aug_seed_kwargs),**self.aug_seed_kwargs)
     return self.random_augmentation(RAND_AUG_PROB, img_blured, img)