DALI icon indicating copy to clipboard operation
DALI copied to clipboard

16 bit gray scale Image read error

Open sniperHJJ opened this issue 11 months ago • 1 comments

Describe the question.

My loader is configured as follows.

However, KeyError: <DALIDataType.UINT16: 1> is occurring.

torch 1.12.1+cu113 nvidia-dali-cuda110 1.36.0 I'm using the above version

class ExternalInputIterator(object):
    ....
  def __next__(self):
      source_batch = []
      target_batch = []
      name_batch = []

      if self.i >= self.n:
          self.__iter__()
          raise StopIteration

      for _ in range(self.batch_size):
          filename = self.source_files[self.i % self.n]
          # print("self.i : ", self.i, ", filename : ", filename)

          source_batch.append(np.fromfile(os.path.join(self.source_root, filename), dtype=np.uint8))  # we can use numpy
          target_batch.append(np.fromfile(os.path.join(self.target_root, filename), dtype=np.uint8))  # we can use numpy
          # name_batch.append(filename)
          name_batch.append(np.fromstring(filename, dtype=np.uint8))
          self.i += 1

      return (source_batch, target_batch, name_batch)

  def __len__(self):
      return self.data_set_len

  next = __next__


def ExternalSourcePipeline(batch_size, num_threads, device_id, external_data, mode):
  pipe = Pipeline(batch_size, num_threads, device_id)
  with pipe:
      source_img, target_img, filename = fn.external_source(source=external_data, num_outputs=3)

      source_images = fn.experimental.decoders.image(
          source_img, device='mixed', output_type=types.GRAY, dtype=types.UINT16)
      target_images = fn.experimental.decoders.image(
          source_img, device='mixed', output_type=types.GRAY, dtype=types.UINT16)

      ...
      ...

      pipe.set_outputs(source_images, target_images, filename)
  return pipe

Check for duplicates

  • [X] I have searched the open bugs/issues and have found no duplicates for this bug report

sniperHJJ avatar Mar 28 '24 00:03 sniperHJJ

Hi @JunHyunjae

I've check your code above and it produces valid results. I had to modify it a little bit to make it run:

from nvidia.dali import Pipeline, fn, types
import numpy as np

class ExternalInputIterator(object):
    def __init__(self, batch_size):
        self.batch_size = batch_size
        self.source_files = [
            'DALI_extra/db/single/jpeg/450/bobsled-683993_1280.jpg',
            'DALI_extra/db/single/jpeg/450/bobsled-643397_1280.jpg',
            'DALI_extra/db/single/jpeg/450/bobsled-683995_1280.jpg',
            'DALI_extra/db/single/jpeg/312/grasshopper-4357907_1280.jpg',
            'DALI_extra/db/single/jpeg/312/grasshopper-4357903_1280.jpg',
            'DALI_extra/db/single/jpeg/312/cricket-1345065_1280.jpg',
            'DALI_extra/db/single/jpeg/372/baboon-3089012_1280.jpg',
            'DALI_extra/db/single/jpeg/372/monkey-653705_1920.jpg',
            'DALI_extra/db/single/jpeg/372/baboon-174073_1280.jpg',
        ]
    def __iter__(self):
        self.i = 0
        self.n = self.batch_size
        return self

    def __next__(self):
        source_batch = []
        target_batch = []
        name_batch = []

        if self.i >= self.n:
            self.__iter__()
            raise StopIteration

        for _ in range(self.batch_size):
            filename = self.source_files[self.i % self.n]
            # print("self.i : ", self.i, ", filename : ", filename)

            source_batch.append(np.fromfile(filename, dtype=np.uint8))  # we can use numpy
            target_batch.append(np.fromfile(filename, dtype=np.uint8))  # we can use numpy
            # name_batch.append(filename)
            name_batch.append(np.fromstring(filename, dtype=np.uint8))
            self.i += 1

        return (source_batch, target_batch, name_batch)

    def __len__(self):
        return self.data_set_len

    next = __next__

class ExternalSourcePipeline(Pipeline):
    def __init__(self, batch_size, num_threads, device_id):
        super(ExternalSourcePipeline, self).__init__(batch_size, num_threads, device_id)
        self.eii = ExternalInputIterator(batch_size)
        self.iterator = iter(self.eii)

    def define_graph(self):
        source = fn.external_source(device="cpu", name="source_images")
        target = fn.external_source(device="cpu", name="target_images")
        filename = fn.external_source(device="cpu", name="filename")
        source_images = fn.experimental.decoders.image(
            source, device='mixed', output_type=types.GRAY, dtype=types.UINT16)
        target_images = fn.experimental.decoders.image(
            target, device='mixed', output_type=types.GRAY, dtype=types.UINT16)
        return (source_images, target_images, filename)

    def iter_setup(self):
        source_images, target_images, filename = next(self.iterator)
        self.feed_input("source_images", source_images)
        self.feed_input("target_images", target_images)
        self.feed_input("filename", filename)

p = ExternalSourcePipeline(1, 1, 0)
p.build()
print(p.run())

Producing:

(TensorListGPU(
    [[[[16705]
       [16191]
       ...
       [ 6425]
       [ 5911]]

      [[15420]
       [15934]
       ...
       [ 3341]
       [ 3855]]

      ...

      [[54998]
       [55769]
       ...
       [61937]
       [61680]]

      [[54998]
       [56026]
       ...
       [61937]
       [61937]]]],
    dtype=DALIDataType.UINT16,
    layout="HWC",
    num_samples=1,
    shape=[(988, 1280, 1)]), TensorListGPU(
    [[[[16705]
       [16191]
       ...
       [ 6425]
       [ 5911]]

      [[15420]
       [15934]
       ...
       [ 3341]
       [ 3855]]

      ...

      [[54998]
       [55769]
       ...
       [61937]
       [61680]]

      [[54998]
       [56026]
       ...
       [61937]
       [61937]]]],
    dtype=DALIDataType.UINT16,
    layout="HWC",
    num_samples=1,
    shape=[(988, 1280, 1)]), TensorListCPU(
    [[ 47 104 111 109 101  47 106  97 110 116 111 110  47 103 105 116  47
       68  65  76  73  95 101 120 116 114  97  47 100  98  47 115 105 110
      103 108 101  47 106 112 101 103  47  52  53  48  47  98 111  98 115
      108 101 100  45  54  56  51  57  57  51  95  49  50  56  48  46 106
      112 103]],
    dtype=DALIDataType.UINT8,
    num_samples=1,
    shape=[(70,)]))

If you still find issues with your code, please attach a full script that we can run on our end.

jantonguirao avatar Apr 03 '24 08:04 jantonguirao