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.16k stars 622 forks source link

[QUESTION] Image histogram example #4549

Open xevolesi opened 1 year ago

xevolesi commented 1 year ago

Hi, thank you very much for you work, DALI is really a great library. DALI accelerated my model training by almost 19 times compared to another augmentation library and it's really impressive. Although I'm still missing a few things. For example, i want to use custom augmentation and i need to calculate image histogram in the following way (I need it for Otsu thresholding):

import cv2
import numpy as np

IMAGE_PATH = "image.jpeg"

if __name__ == "__main__":
    image = cv2.imread(IMAGE_PATH)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ...
    # Find normalized_histogram, and its cumulative distribution function
    hist = cv2.calcHist([image], [0], None, [256], [0, 256])
    hist_norm = hist.ravel() / hist.sum()
    Q = hist_norm.cumsum()

Could you please guide me? I've tried something like this:

def dali_calc_hist(decoded_images: DALITensorList, image_sizes: DALITensorList):
       gray = fn.color_space_conversion(
            decoded_images,
            image_type=dali_types.RGB,
            output_type=dali_types.GRAY,
        )
        hist = [0] * 256
        cumsum = [0] * 256
        n = image_size[0] * image_size[1]
        _sum = fn.constant(idata=[0], dtype=dali_types.FLOAT)
        for gray_lvl in range(256):
            hist[gray_lvl] = fn.reductions.sum(
                fn.cast(gray == gray_lvl, dtype=dali_types.UINT8)
            ) / n
            _sum = _sum + hist[gray_lvl]
            cumsum[gray_lvl] = _sum
        return hist, cumsum

but i really feel that i'm doing something wrong, because actually in hist and cumsum i have nested DataNodes.

JanuszL commented 1 year ago

Hi @xevolesi,

I'm sorry to say it is not possible now in DALI as it lacks a histogram operator. There was some work towards it https://github.com/NVIDIA/DALI/pull/3502 but it is currently suspended due to other priorities. In the meantime you can look at https://github.com/CVCUDA/CV-CUDA plans to include this operator in the foreseeable future.