Example #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
Example #2
0
def test_constant_promotion_mixed():
    filename = os.path.join(jpeg_folder, "241", "cute-4074304_1280.jpg")
    file_contents = np.fromfile(filename, dtype=np.uint8)
    pipe = Pipeline(1, 3, 0)
    with pipe:
        jpegs, _ = fn.readers.file(files=[filename])
        from_reader = fn.image_decoder(jpegs, device="mixed")
        from_constant = fn.image_decoder(file_contents, device="mixed")
        pipe.set_outputs(from_constant, from_reader)
    pipe.build()
    from_reader, from_constant = pipe.run()
    check_batch(from_reader, from_constant, 1)
Example #3
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
Example #4
0
def test_separated_exec_setup():
    batch_size = 128
    pipe = Pipeline(batch_size=batch_size,
                    num_threads=3,
                    device_id=None,
                    prefetch_queue_depth={
                        "cpu_size": 5,
                        "gpu_size": 3
                    })
    inputs, labels = fn.caffe_reader(path=caffe_dir, shard_id=0, num_shards=1)
    images = fn.image_decoder(inputs, output_type=types.RGB)
    images = fn.resize(images, resize_x=224, resize_y=224)
    images_cpu = fn.dump_image(images, suffix="cpu")
    pipe.set_outputs(images, images_cpu)

    pipe.build()
    out = pipe.run()
    assert (out[0].is_dense_tensor())
    assert (out[1].is_dense_tensor())
    assert (out[0].as_tensor().shape() == out[1].as_tensor().shape())
    a_raw = out[0]
    a_cpu = out[1]
    for i in range(batch_size):
        t_raw = a_raw.at(i)
        t_cpu = a_cpu.at(i)
        assert (np.sum(np.abs(t_cpu - t_raw)) == 0)
Example #5
0
def mnist_pipeline(num_threads,
                   path,
                   device,
                   device_id=0,
                   shard_id=0,
                   num_shards=1,
                   seed=0):
    pipeline = Pipeline(BATCH_SIZE, num_threads, device_id, seed)
    with pipeline:
        jpegs, labels = fn.readers.caffe2(path=path,
                                          random_shuffle=True,
                                          shard_id=shard_id,
                                          num_shards=num_shards)
        images = fn.image_decoder(jpegs,
                                  device='mixed' if device == 'gpu' else 'cpu',
                                  output_type=types.GRAY)
        if device == 'gpu':
            labels = labels.gpu()
        images = fn.crop_mirror_normalize(images,
                                          dtype=types.FLOAT,
                                          mean=[0.],
                                          std=[255.],
                                          output_layout="CHW")

        pipeline.set_outputs(images, labels)

    return pipeline
Example #6
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)
Example #7
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)
Example #8
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()
Example #9
0
def get_pipeline(batch_size, tile, ratio, angle):
  pipe = Pipeline(batch_size, 4, None)
  with pipe:
    input, _ = fn.readers.file(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
Example #10
0
 def image_decoder_pipe(max_batch_size, input_data, device):
     pipe = Pipeline(batch_size=max_batch_size, num_threads=4, device_id=0)
     encoded = fn.external_source(source=input_data,
                                  cycle=False,
                                  device='cpu')
     decoded = fn.image_decoder(encoded, device=device)
     pipe.set_outputs(decoded)
     return pipe
Example #11
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()
Example #12
0
 def make_pipe(self):
     log.debug("making pipe")
     self.pipe = Pipeline(batch_size=self.batch_size,
                          num_threads=2,
                          device_id=self.device_id,
                          prefetch_queue_depth=self.prefetch)
     with self.pipe:
         self.files = fn.external_source()
         images = fn.image_decoder(self.files, device=self.device)
         self.pipe.set_outputs(images)
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
Example #14
0
def get_random_pipeline(batch_size):
  pipe = Pipeline(batch_size, 4, None)
  with pipe:
    input, _ = fn.readers.file(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
Example #15
0
File: main.py Project: xvdp/DALI
def create_dali_pipeline(data_dir,
                         crop,
                         size,
                         shard_id,
                         num_shards,
                         dali_cpu=False,
                         is_training=True):
    images, labels = fn.readers.file(file_root=data_dir,
                                     shard_id=shard_id,
                                     num_shards=num_shards,
                                     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()
    return images, labels
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
Example #17
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)
Example #18
0
def test_compose_change_device():
    batch_size = 3
    pipe = Pipeline(batch_size, 1, 0)

    size = fn.uniform(shape=2, range=(300,500))
    c = ops.Compose([
        ops.ImageDecoder(device="cpu"),
        ops.Resize(size=size, device="gpu")
    ])
    files, labels = fn.caffe_reader(path=caffe_db_folder, seed=1)
    pipe.set_outputs(c(files), fn.resize(fn.image_decoder(files).gpu(), size=size))

    pipe.build()
    out = pipe.run()
    assert isinstance(out[0], dali.backend.TensorListGPU)
    test_utils.check_batch(out[0], out[1], batch_size=batch_size)
Example #19
0
def ExternalSourcePipeline(params, num_threads, device_id, external_date,
                           seed):
    pipe = Pipeline(params.batch_size, num_threads, device_id, seed=seed)
    with pipe:
        jpegs, labels = fn.external_source(source=external_date, num_outputs=2)
        images = fn.image_decoder(jpegs, device="mixed", output_type=types.RGB)
        images = fn.resize(images, resize_x=224, resize_y=224)
        images = fn.cast(images, dtype=types.UINT8) / 255
        images = fn.normalize(images,
                              axes=[0, 1],
                              mean=params.mean,
                              stddev=params.std,
                              device='gpu',
                              batch=False)
        output = fn.transpose(images, perm=[2, 0, 1], device='gpu')
        pipe.set_outputs(output, labels)
    return pipe
Example #20
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.readers.file(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
Example #21
0
def get_pipeline(batch_size,
                 num_threads,
                 device,
                 device_id=0,
                 shard_id=0,
                 num_shards=1):
    test_data_root = os.environ['DALI_EXTRA_PATH']
    file_root = os.path.join(test_data_root, 'db', 'coco_dummy', 'images')
    annotations_file = os.path.join(test_data_root, 'db', 'coco_dummy',
                                    'instances.json')

    pipe = Pipeline(batch_size, num_threads, device_id)
    with pipe:
        jpegs, _, _, image_ids = fn.coco_reader(
            file_root=file_root,
            annotations_file=annotations_file,
            shard_id=shard_id,
            num_shards=num_shards,
            ratio=False,
            image_ids=True)
        images = fn.image_decoder(
            jpegs,
            device=('mixed' if device == 'gpu' else 'cpu'),
            output_type=types.RGB)
        images = fn.resize(images,
                           resize_x=224,
                           resize_y=224,
                           interp_type=types.INTERP_LINEAR)
        images = fn.crop_mirror_normalize(images,
                                          dtype=types.FLOAT,
                                          mean=[128., 128., 128.],
                                          std=[1., 1., 1.])
        if device == 'gpu':
            image_ids = image_ids.gpu()
        ids_reshaped = fn.reshape(image_ids, shape=[1, 1])
        ids_int16 = fn.cast(image_ids, dtype=types.INT16)

        pipe.set_outputs(images, ids_reshaped, ids_int16)

    return pipe
Example #22
0
def setup_dali(
    input_name='DALI_INPUT_0',
    image_dim=[896, 1536],
    batch_size=1,
    num_threads=4,
    device='cpu',
    device_id=0,
    output_dir='./out/',
):

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

    with pipeline:
        data = fn.external_source(name=input_name, device="cpu")
        # 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)

    os.makedirs(os.path.dirname(output_dir), exist_ok=True)
    pipeline.serialize(filename=os.path.join(output_dir, 'model.dali'))
Example #23
0
def load_tfrecord(directory, batch_size, training):
    tfrecord = []
    tfrecord_idx = []
    for f in os.listdir(directory):
        fullpath = os.path.join(directory, f)
        if not os.path.isfile(fullpath):
            continue

        if f.endswith(".tfrecord"):
            tfrecord.append(fullpath)

        if f.endswith(".idx"):
            tfrecord_idx.append(fullpath)

    tfrecord.sort()
    tfrecord_idx.sort()

    pipe = Pipeline(batch_size=batch_size, num_threads=32, device_id=0)
    with pipe:
        inputs = fn.tfrecord_reader(
            path=tfrecord,
            index_path=tfrecord_idx,
            features={
                "frame_one":
                tfrec.FixedLenFeature((), tfrec.string, ""),
                "frame_two":
                tfrec.FixedLenFeature((), tfrec.string, ""),
                "frame_three":
                tfrec.FixedLenFeature((), tfrec.string, ""),
                "frame_four":
                tfrec.FixedLenFeature((), tfrec.string, ""),
                "plus_one_position":
                tfrec.FixedLenFeature([3], tfrec.float32, 0.0),
                "plus_one_orientation":
                tfrec.FixedLenFeature([3], tfrec.float32, 0.0),
                "plus_two_position":
                tfrec.FixedLenFeature([3], tfrec.float32, 0.0),
                "plus_two_orientation":
                tfrec.FixedLenFeature([3], tfrec.float32, 0.0),
                "plus_three_position":
                tfrec.FixedLenFeature([3], tfrec.float32, 0.0),
                "plus_three_orientation":
                tfrec.FixedLenFeature([3], tfrec.float32, 0.0),
                "speed":
                tfrec.FixedLenFeature([], tfrec.float32, 0.0),
            })
        frame1 = inputs["frame_one"]
        frame1 = fn.image_decoder(frame1,
                                  device="mixed",
                                  output_type=types.RGB)
        # frame1 = fn.resize(frame1, device="gpu", resize_shorter=256.)
        frame1 = fn.crop_mirror_normalize(frame1,
                                          device="gpu",
                                          dtype=types.FLOAT,
                                          mean=[0., 0., 0.],
                                          std=[1., 1., 1.])
        frame1 = fn.transpose(frame1, device="gpu", perm=[1, 2, 0])

        frame2 = inputs["frame_two"]
        frame2 = fn.image_decoder(frame2,
                                  device="mixed",
                                  output_type=types.RGB)
        # frame2 = fn.resize(frame2, device="gpu", resize_shorter=256.)
        frame2 = fn.crop_mirror_normalize(frame2,
                                          device="gpu",
                                          dtype=types.FLOAT,
                                          mean=[0., 0., 0.],
                                          std=[1., 1., 1.])
        frame2 = fn.transpose(frame2, device="gpu", perm=[1, 2, 0])

        position = inputs["plus_one_position"].gpu()
        orientation = inputs["plus_one_orientation"].gpu()
        speed = inputs["speed"].gpu()

        image = fn.cat(frame1, frame2, device="gpu", axis=2)
        pose = fn.cat(position, orientation, device="gpu", axis=0)
        pipe.set_outputs(image, pose, speed)

    # Define shapes and types of the outputs
    shapes = ((batch_size, 480, 640), (batch_size, 6), (batch_size))
    dtypes = (tf.float32, tf.float32)

    # Create dataset
    return dali_tf.DALIDataset(pipeline=pipe,
                               batch_size=batch_size,
                               output_shapes=shapes,
                               output_dtypes=dtypes,
                               device_id=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
Example #25
0
def get_dali_pipeline(tfrec_filenames,
                      tfrec_idx_filenames,
                      height,
                      width,
                      shard_id,
                      num_gpus,
                      dali_cpu=True,
                      training=True):

    inputs = fn.readers.tfrecord(path=tfrec_filenames,
                                 index_path=tfrec_idx_filenames,
                                 random_shuffle=training,
                                 shard_id=shard_id,
                                 num_shards=num_gpus,
                                 initial_fill=10000,
                                 features={
                                     'image/encoded':
                                     tfrec.FixedLenFeature((), tfrec.string,
                                                           ""),
                                     'image/class/label':
                                     tfrec.FixedLenFeature([1], tfrec.int64,
                                                           -1),
                                     'image/class/text':
                                     tfrec.FixedLenFeature([], tfrec.string,
                                                           ''),
                                     'image/object/bbox/xmin':
                                     tfrec.VarLenFeature(tfrec.float32, 0.0),
                                     'image/object/bbox/ymin':
                                     tfrec.VarLenFeature(tfrec.float32, 0.0),
                                     'image/object/bbox/xmax':
                                     tfrec.VarLenFeature(tfrec.float32, 0.0),
                                     'image/object/bbox/ymax':
                                     tfrec.VarLenFeature(tfrec.float32, 0.0)
                                 })

    decode_device = "cpu" if dali_cpu else "mixed"
    resize_device = "cpu" if dali_cpu else "gpu"
    if training:
        images = fn.image_decoder_random_crop(inputs["image/encoded"],
                                              device=decode_device,
                                              output_type=types.RGB,
                                              random_aspect_ratio=[0.75, 1.25],
                                              random_area=[0.05, 1.0],
                                              num_attempts=100)
        images = fn.resize(images,
                           device=resize_device,
                           resize_x=width,
                           resize_y=height)
    else:
        images = fn.image_decoder(inputs["image/encoded"],
                                  device=decode_device,
                                  output_type=types.RGB)
        # Make sure that every image > 224 for CropMirrorNormalize
        images = fn.resize(images, device=resize_device, resize_shorter=256)

    images = fn.crop_mirror_normalize(images.gpu(),
                                      dtype=types.FLOAT,
                                      crop=(height, width),
                                      mean=[123.68, 116.78, 103.94],
                                      std=[58.4, 57.12, 57.3],
                                      output_layout="HWC",
                                      mirror=fn.random.coin_flip())
    labels = inputs["image/class/label"].gpu()

    labels -= 1  # Change to 0-based (don't use background class)
    return images, labels
Example #26
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()



from nvidia.dali.plugin.pytorch import DALIClassificationIterator
dali_iter = DALIClassificationIterator([pipe], -1)
Example #27
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)
    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)),
                                saturation=fn.uniform(range=(0.1, 2)))