예제 #1
0
def create_dali_pipeline(batch_size, num_threads, device_id, data_dir):
    files = []
    with open(join(data_dir, "file_list.txt"), "r") as f:
        files = [line.rstrip() for line in f if line is not '']

    shuffle(files)

    img_files = []
    seg_files = []

    for prefix in files:
        img_files.append(join(data_dir, "leftImg8bit/train", prefix + "_leftImg8bit.png"))
        seg_files.append(join(data_dir, "gtFine/train", prefix + "_gtFine_labelIds.png"))

    pipeline = Pipeline(batch_size, num_threads, device_id, seed=12 + device_id)
    with pipeline:
        imgs, _ = fn.file_reader(files=img_files,
                                 shard_id=0, num_shards=1,
                                 random_shuffle=False, pad_last_batch=True)

        segs, _ = fn.file_reader(files=seg_files,
                                 shard_id=0, num_shards=1,
                                 random_shuffle=False, pad_last_batch=True)

        dali_device = 'gpu'
        decoder_device = 'mixed'
        # device_memory_padding = 211025920 if decoder_device == 'mixed' else 0
        # host_memory_padding = 140544512 if decoder_device == 'mixed' else 0
        device_memory_padding = 0
        host_memory_padding = 0

        imgs = fn.image_decoder(imgs, device=decoder_device, output_type=types.RGB,
                                device_memory_padding=device_memory_padding,
                                host_memory_padding=host_memory_padding,
                                hybrid_huffman_threshold=250000)

        segs = fn.image_decoder(segs, device=decoder_device, output_type=types.GRAY,
                                 device_memory_padding=device_memory_padding,
                                 host_memory_padding=host_memory_padding,
                                 hybrid_huffman_threshold=250000)

        imgs = fn.crop_mirror_normalize(imgs, device=dali_device,
                                        crop=(512, 512),
                                        dtype=types.FLOAT,
                                        mean=[0.485 * 255, 0.456 * 255, 0.406 * 255],
                                        std=[0.229 * 255, 0.224 * 255, 0.225 * 255],
                                        output_layout="CHW")

        segs = fn.crop(segs, device=dali_device,
                       dtype=types.UINT8, crop=(512, 512))

        pipeline.set_outputs(imgs, segs)

    return pipeline
예제 #2
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)
예제 #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()
예제 #4
0
def _test_reader_files_arg(use_root, use_labels, shuffle):
    root = g_root
    fnames = g_files
    if not use_root:
        fnames = [os.path.join(root, f) for f in fnames]
        root = None

    lbl = None
    if use_labels:
        lbl = [10000 + i for i in range(len(fnames))]

    batch_size = 3
    pipe = Pipeline(batch_size, 1, 0)
    files, labels = fn.file_reader(file_root=root,
                                   files=fnames,
                                   labels=lbl,
                                   random_shuffle=shuffle)
    pipe.set_outputs(files, labels)
    pipe.build()

    num_iters = (len(fnames) + 2 * batch_size) // batch_size
    for i in range(num_iters):
        out_f, out_l = pipe.run()
        for j in range(batch_size):
            contents = bytes(out_f.at(j)).decode('utf-8')
            label = out_l.at(j)[0]
            index = label - 10000 if use_labels else label
            assert contents == ref_contents(fnames[index])
예제 #5
0
def get_pipeline(folder="train", custom_reader=None):
    pipe = Pipeline(batch_size=64, num_threads=1, device_id=1)

    if custom_reader:
        raw_files, labels = custom_reader
    else:
        raw_files, labels = fn.file_reader(file_root="%s" % folder,
                                           random_shuffle=True)

    decode = fn.image_decoder(raw_files, device="mixed", output_type=types.GRAY)
    resize = fn.resize(decode, device="gpu", image_type=types.RGB,
                       interp_type=types.INTERP_LINEAR, resize_x=WIDTH, resize_y=HEIGHT)

    hsv = fn.hsv(resize, hue=fn.uniform(range=(-10, 10)), saturation=fn.uniform(range=(-.5, .5)),
                 value=fn.uniform(range=(0.9, 1.2)), device="gpu", dtype=types.UINT8)
    bc = fn.brightness_contrast(hsv, device="gpu", brightness=fn.uniform(range=(.9, 1.1)))

    cmn = fn.crop_mirror_normalize(bc, device="gpu", output_dtype=types.FLOAT,
                                   output_layout=types.NHWC,
                                   image_type=types.GRAY,
                                   mean=[255 // 2],
                                   std=[255 // 2])

    rot = fn.rotate(cmn, angle=fn.uniform(range=(-40, 40)), device="gpu", keep_size=True)

    tpose = fn.transpose(rot, perm=(2, 0, 1), device="gpu")  # Reshaping to a format PyTorch likes

    pipe.set_outputs(tpose, labels)
    pipe.build()

    dali_iter = DALIClassificationIterator([pipe], -1)

    return dali_iter
예제 #6
0
def run_decode(data_path, out_type):
    batch_size = 4
    pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=0)
    input, _ = fn.file_reader(file_root=data_path,
                              shard_id=0,
                              num_shards=1,
                              name="reader")
    decoded = fn.image_decoder(input, output_type=types.RGB)
    decoded_shape = fn.shapes(decoded)
    raw_shape = fn.peek_image_shape(input, type=out_type)
    pipe.set_outputs(decoded, decoded_shape, raw_shape)
    pipe.build()
    samples = 0
    length = pipe.reader_meta(name="reader")['epoch_size']
    while samples < length:
        samples += batch_size
        (images, decoded_shape, raw_shape) = pipe.run()
        for i in range(batch_size):
            # as we are asking for a particular color space it may differ from the source image, so don't compare it
            image = images.at(i)
            shape_type = dali_types_to_np(out_type)
            for d in range(len(image.shape) - 1):
                assert image.shape[d] == decoded_shape.at(
                    i)[d], "{} vs {}".format(image.shape[d],
                                             decoded_shape.at(i)[d])
                assert image.shape[d] == raw_shape.at(i)[d], "{} vs {}".format(
                    image.shape[d],
                    raw_shape.at(i)[d])
                assert raw_shape.at(
                    i)[d].dtype == shape_type, "{} vs {}".format(
                        raw_shape.at(i)[d].dtyp, shape_type)
예제 #7
0
def test_image_decoder_cpu():
    pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=None)
    input, _ = fn.file_reader(file_root=images_dir, shard_id=0, num_shards=1)
    decoded = fn.image_decoder(input, output_type=types.RGB)
    pipe.set_outputs(decoded)
    pipe.build()
    for _ in range(3):
        pipe.run()
예제 #8
0
def test_audio_decoder_cpu():
    pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=None)
    input, _ = fn.file_reader(file_root=audio_dir, shard_id=0, num_shards=1)
    decoded, _ = fn.audio_decoder(input)
    pipe.set_outputs(decoded)
    pipe.build()
    for _ in range(3):
        pipe.run()
예제 #9
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.file_reader(file_root=images_dir, shard_id=0, num_shards=1)
        decoded = fn.image_decoder(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
예제 #10
0
def reference_pipeline(flip_vertical,
                       flip_horizontal,
                       ref_batch_size=max_batch_size):
    pipeline = Pipeline(ref_batch_size, num_threads, device_id)
    with pipeline:
        data, _ = fn.file_reader(file_root=images_dir)
        img = fn.image_decoder(data)
        flipped = fn.flip(img,
                          horizontal=flip_horizontal,
                          vertical=flip_vertical)
        pipeline.set_outputs(flipped, img)
    return pipeline
예제 #11
0
def get_pipeline(batch_size, tile, ratio, angle):
    pipe = Pipeline(batch_size, 4, None)
    with pipe:
        input, _ = fn.file_reader(file_root=img_dir)
        decoded = fn.image_decoder(input, device='cpu', output_type=types.RGB)
        grided = fn.grid_mask(decoded,
                              device='cpu',
                              tile=tile,
                              ratio=ratio,
                              angle=angle)
        pipe.set_outputs(grided, decoded)
    return pipe
예제 #12
0
파일: main.py 프로젝트: yutiansut/DALI
def create_dali_pipeline(batch_size, num_threads, device_id, data_dir, crop, size,
                       shard_id, num_shards, dali_cpu=False, is_training=True):
    pipeline = Pipeline(batch_size, num_threads, device_id, seed=12 + device_id)
    with pipeline:
        images, labels = fn.file_reader(file_root=data_dir,
                                        shard_id=args.local_rank,
                                        num_shards=args.world_size,
                                        random_shuffle=is_training,
                                        pad_last_batch=True,
                                        name="Reader")
        dali_device = 'cpu' if dali_cpu else 'gpu'
        decoder_device = 'cpu' if dali_cpu else 'mixed'
        device_memory_padding = 211025920 if decoder_device == 'mixed' else 0
        host_memory_padding = 140544512 if decoder_device == 'mixed' else 0
        if is_training:
            images = fn.image_decoder_random_crop(images,
                                                  device=decoder_device, output_type=types.RGB,
                                                  device_memory_padding=device_memory_padding,
                                                  host_memory_padding=host_memory_padding,
                                                  random_aspect_ratio=[0.8, 1.25],
                                                  random_area=[0.1, 1.0],
                                                  num_attempts=100)
            images = fn.resize(images,
                               device=dali_device,
                               resize_x=crop,
                               resize_y=crop,
                               interp_type=types.INTERP_TRIANGULAR)
            mirror = fn.random.coin_flip(probability=0.5)
        else:
            images = fn.image_decoder(images,
                                      device=decoder_device,
                                      output_type=types.RGB)
            images = fn.resize(images,
                               device=dali_device,
                               size=size,
                               mode="not_smaller",
                               interp_type=types.INTERP_TRIANGULAR)
            mirror = False

        images = fn.crop_mirror_normalize(images.gpu(),
                                          dtype=types.FLOAT,
                                          output_layout="CHW",
                                          crop=(crop, crop),
                                          mean=[0.485 * 255,0.456 * 255,0.406 * 255],
                                          std=[0.229 * 255,0.224 * 255,0.225 * 255],
                                          mirror=mirror)
        labels = labels.gpu()
        pipeline.set_outputs(images, labels)
    return pipeline
예제 #13
0
def setup_dali(
    image_file='/mnt/data/DATASETS/samples/images/image_110.jpg',
    image_dim=[800, 1600],
    batch_size=1,
    num_threads=4,
    device='mixed',
    device_id=0,
    output_dir='./out/',
):

    os.makedirs(os.path.dirname(output_dir), exist_ok=True)

    pipeline = dali.pipeline.Pipeline(batch_size=batch_size,
                                      num_threads=num_threads,
                                      device_id=device_id)

    with pipeline:
        data, _ = fn.file_reader(files=[image_file])
        # image preprocess
        images = fn.image_decoder(data, device=device)
        images = fn.resize(images,
                           size=image_dim,
                           mode="not_larger",
                           max_size=image_dim)
        images = fn.pad(images,
                        fill_value=0,
                        shape=[image_dim[0], image_dim[1], 1])
        images = fn.transpose(images, perm=[2, 0, 1])
        images = fn.cast(images, dtype=dali.types.FLOAT)
        images = images / 255.
        # input shape
        input_shape = np.float32((image_dim[0], image_dim[1], 1))
        # original shape
        shapes = fn.peek_image_shape(data)
        shapes = fn.cast(shapes, dtype=dali.types.FLOAT)
        # gather outputs
        out = [images, input_shape, shapes]
        pipeline.set_outputs(*out)

    pipeline.build()
    output = pipeline.run()
    img = output[0].at(0) if device == 'cpu' else output[0].as_cpu().at(0)

    img = img.transpose(1, 2, 0)  # HWC
    img = img[:, :, ::-1]  # BGR
    print(img)
    quit()
    cv2.imwrite(os.path.join(output_dir, 'dali_image.jpg'), img)
예제 #14
0
def get_random_pipeline(batch_size):
    pipe = Pipeline(batch_size, 4, None)
    with pipe:
        input, _ = fn.file_reader(file_root=img_dir)
        decoded = fn.image_decoder(input, device='cpu', output_type=types.RGB)
        tile = fn.cast(fn.uniform(range=(50, 200), shape=[1]),
                       dtype=types.INT32)
        ratio = fn.uniform(range=(0.3, 0.7), shape=[1])
        angle = fn.uniform(range=(-math.pi, math.pi), shape=[1])
        grided = fn.grid_mask(decoded,
                              device='cpu',
                              tile=tile,
                              ratio=ratio,
                              angle=angle)
        pipe.set_outputs(grided, decoded, tile, ratio, angle)
    return pipe
예제 #15
0
def test_file_reader_relpath():
    batch_size = 3
    rel_root = os.path.relpath(g_root, os.getcwd())
    fnames = [os.path.join(rel_root, f) for f in g_files]

    pipe = Pipeline(batch_size, 1, 0)
    files, labels = fn.file_reader(files=fnames, random_shuffle=True)
    pipe.set_outputs(files, labels)
    pipe.build()

    num_iters = (len(fnames) + 2 * batch_size) // batch_size
    for i in range(num_iters):
        out_f, out_l = pipe.run()
        for j in range(batch_size):
            contents = bytes(out_f.at(j)).decode('utf-8')
            index = out_l.at(j)[0]
            assert contents == ref_contents(fnames[index])
예제 #16
0
def test_image_decoder_slice_cpu():
    anch_shape = [1, 2]

    def get_anchors():
        out = [(np.random.randint(1, 128, size=anch_shape, dtype=np.uint8) /
                255).astype(dtype=np.float32) for _ in range(batch_size)]
        return out

    def get_shape():
        out = [(np.random.randint(1, 128, size=anch_shape, dtype=np.uint8) /
                255).astype(dtype=np.float32) for _ in range(batch_size)]
        return out

    pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=None)
    input, _ = fn.file_reader(file_root=images_dir, shard_id=0, num_shards=1)
    anchors = fn.external_source(source=get_anchors)
    shape = fn.external_source(source=get_shape)
    processed = fn.image_decoder_slice(input, anchors, shape)
    pipe.set_outputs(processed)
    pipe.build()
    for _ in range(3):
        pipe.run()
예제 #17
0
def test_file_reader_relpath_file_list():
    batch_size = 3
    fnames = g_files

    list_file = os.path.join(g_root, "list.txt")
    with open(list_file, "w") as f:
        for i, name in enumerate(fnames):
            f.write("{0} {1}\n".format(name, 10000 - i))

    pipe = Pipeline(batch_size, 1, 0)
    files, labels = fn.file_reader(file_list=list_file, random_shuffle=True)
    pipe.set_outputs(files, labels)
    pipe.build()

    num_iters = (len(fnames) + 2 * batch_size) // batch_size
    for i in range(num_iters):
        out_f, out_l = pipe.run()
        for j in range(batch_size):
            contents = bytes(out_f.at(j)).decode('utf-8')
            label = out_l.at(j)[0]
            index = 10000 - label
            assert contents == ref_contents(fnames[index])
예제 #18
0
def get_pipeline(batch_size=4,
                 in_size=None,
                 out_size=None,
                 even_paste_count=False,
                 k=4,
                 dtype=types.UINT8,
                 no_intersections=True,
                 full_input=False,
                 in_anchor_top_left=False,
                 out_anchor_top_left=False):
    pipe = Pipeline(batch_size=batch_size,
                    num_threads=4,
                    device_id=types.CPU_ONLY_DEVICE_ID)
    with pipe:
        input, _ = fn.file_reader(file_root=img_dir)
        decoded = fn.image_decoder(input, device='cpu', output_type=types.RGB)
        resized = fn.resize(decoded, resize_x=in_size[1], resize_y=in_size[0])
        in_idx_l, in_anchors_l, shapes_l, out_anchors_l = prepare_cuts(
            k, batch_size, in_size, out_size, even_paste_count,
            no_intersections, full_input, in_anchor_top_left,
            out_anchor_top_left)
        in_idx = fn.external_source(lambda: in_idx_l)
        in_anchors = fn.external_source(lambda: in_anchors_l)
        shapes = fn.external_source(lambda: shapes_l)
        out_anchors = fn.external_source(lambda: out_anchors_l)
        kwargs = {"in_ids": in_idx, "output_size": out_size, "dtype": dtype}

        if not full_input:
            kwargs["shapes"] = shapes

        if not in_anchor_top_left:
            kwargs["in_anchors"] = in_anchors

        if not out_anchor_top_left:
            kwargs["out_anchors"] = out_anchors

        pasted = fn.multi_paste(resized, **kwargs)
        pipe.set_outputs(pasted, resized)
    return pipe, in_idx_l, in_anchors_l, shapes_l, out_anchors_l
예제 #19
0
def pipeline_runtime(flip_vertical, flip_horizontal):
    data, _ = fn.file_reader(file_root=images_dir)
    img = fn.image_decoder(data)
    flipped = fn.flip(img, horizontal=flip_horizontal, vertical=flip_vertical)
    return flipped, img
예제 #20
0
str(human_readable.loc[0])




from nvidia.dali.pipeline import Pipeline
import nvidia.dali.ops as ops
import nvidia.dali.types as types
import matplotlib.pylab as plt
import nvidia.dali.fn as fn

import nvidia.dali.types as types

pipe = Pipeline(batch_size = 64, num_threads = 1, device_id = 0)

raw_files, labels = fn.file_reader(file_root = "data/resized", random_shuffle = True)

decode = fn.image_decoder(raw_files, device = "mixed", output_type = types.GRAY)
resize = fn.resize(decode, device = "gpu", image_type = types.GRAY,
                                interp_type = types.INTERP_LINEAR, resize_x=WIDTH, resize_y=HEIGHT)
cmn = fn.crop_mirror_normalize(resize, device="gpu",output_dtype=types.FLOAT,
                                                          output_layout=types.NCHW,
                                                        image_type=types.GRAY,
                                                        mean=[ 255//2],
                                                        std=[255//2])

pipe.set_outputs(cmn, labels)
pipe.build()


예제 #21
0
 def pipeline_duplicated_arg(max_streams):
     data, _ = fn.file_reader(file_root=images_dir)
     return data + max_streams
예제 #22
0
 def ref_pipeline(val):
     data, _ = fn.file_reader(file_root=images_dir)
     return data + val
예제 #23
0
def check_mixed_op_bad_device(device_id):
    pipe = Pipeline(batch_size=batch_size, num_threads=4, device_id=device_id)
    input, _ = fn.file_reader(file_root=images_dir, shard_id=0, num_shards=1)
    decoded = fn.image_decoder(input, device="mixed", output_type=types.RGB)
    pipe.set_outputs(decoded)
    assert_raises(RuntimeError, pipe.build)
    args = parser.parse_args()

    DataList = []
    labels = []
    Ids = [1]
    for index in range(1, 1001):
        DataList += ['../Downloads/test/images/image_%04d.png' % index
                     ] * args.bs
        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)),