NVIDIA / DALI

A GPU-accelerated library containing highly optimized building blocks and an execution engine for data processing to accelerate deep learning training and inference applications.
https://docs.nvidia.com/deeplearning/dali/user-guide/docs/index.html
Apache License 2.0
5.18k stars 621 forks source link

The shape of tensor obtained by nvidia.dali reads the image is rotated #2660

Open miaomiaoma0703 opened 3 years ago

miaomiaoma0703 commented 3 years ago

The shape of tensor obtained by nvidia.dali reads the image is rotated, because the rotated jpeg file has orientation info, dali considers the rotation head information of the jpeg file, but opencv does not consider it。 code: dali:

class ImageDecoderPipeline(Pipeline):
    def __init__(self, image_dir, batch_size, num_threads, device_id):
        super(ImageDecoderPipeline, self).__init__(batch_size, num_threads, device_id, seed = seed)
        self.input = ops.FileReader(file_root = image_dir)
        if torch.cuda.is_available():
            self.decode = ops.ImageDecoder(device = 'mixed', output_type = types.RGB)  #mixed
            # self.rotate = ops.Rotate(device="gpu", angle = 0.0)
        else:
            self.decode = ops.ImageDecoder(device='cpu', output_type=types.RGB)  # mixed

    def define_graph(self):
        jpegs, labels = self.input()
        images = self.decode(jpegs)
        return (images, labels)

opencv: input = cv2.imread(file_path)

the jpeg file: 5b214adc0fa1b96ad61322f8e0e72ea0

the head info: {"size":2534018,"format":"jpeg","width":3000,"height":4000,"colorModel":"ycbcr","orientation":"Right-top"}

obtain by dali and opencv: dali: image opencv: image

JanuszL commented 3 years ago

Hi, Orientation is just metadata that is used by viewers to rotate the image before the displaying, but the image is encoded as it is. DALI doesn't use metadata to adjust the image after the decoding. If you want to apply the Exif orientation to the actual image I would recommend using imagemagic with -auto-orient option to adjust your image first and then pass it to DALI.

miaomiaoma0703 commented 3 years ago

Hi, The fact is that the part of the image decoded with the dali will be rotated (not the same as the one opened with the viewing software), but opencv will not cause the rotation. I suspect it is a bug in the dali library. The dali decoding code as above.

JanuszL commented 3 years ago

Both viewers and OpenCV apply optional rotation operation based on the Exif metadata - https://github.com/opencv/opencv/blob/master/modules/imgcodecs/src/loadsave.cpp#L912. In the case of DALI, we are not doing such an operation. We will add it to our ToDo list, and we will be more than happy to review and accept any PR introducing such change from the community.

mohanksriram commented 3 years ago

Hi @JanuszL, As you suggested, I will be starting work on this issue. OpenCV has its own parser file here: https://github.com/opencv/opencv/blob/master/modules/imgcodecs/src/exif.hpp I have two questions:

JanuszL commented 3 years ago

Hi @mohankumarSriram: