VincentStimper / mclahe

NumPy and Tensorflow implementation of the Multidimensional Contrast Limited Adaptive Histogram Equalization (MCLAHE) procedure

Home Page:https://ieeexplore.ieee.org/document/8895993

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Need to clear tf session if looping through images

jameybrowning opened this issue · comments

Thanks for the code. Just a few notes in case anyone else runs into the same problems. I needed to add a statement to clear the session when I was using mclahe to loop through thousands of 3D images on a CPU. Otherwise, memory usage expands and code slows to a crawl. Also, the default kernel size is 1/8 of data dimensions. For a 3D image with one dimension less than 8, I got an error. I needed to change kernel size to ceiling of 1/8 image dims. These fixes are shown below.

for i, full_path in enumerate(full_paths):
    os.makedirs(paths_new[i], exist_ok = True)
    print(i)
    img_arr, meta_data = load(full_path)
    dims = np.shape(img_arr)
    img_arr_eq = mc.mclahe(img_arr, kernel_size = [int(np.ceil(dims[0]/8)), int(np.ceil(dims[1]/8)), int(np.ceil(dims[2]/8))])
    save(img_arr_eq, full_paths_new[i], meta_data)
    tf.keras.backend.clear_session()

Yes, saved me days! Thank you, this tf.keras.backend.clear_session() does the magic.

hi,
Can we use this code for 3d volumetric images?

@SidGul, yes, it works for arbitrary dimensional images.