BiomedSciAI / histocartography

A standardized Python API with necessary preprocessing, machine learning and explainability tools to facilitate graph-analytics in computational pathology.
GNU Affero General Public License v3.0
241 stars 57 forks source link

Vahadane StainNormalizer raises error #24

Closed NeelKanwal closed 2 years ago

NeelKanwal commented 2 years ago

Hi,

I am trying to run Macenko and Vahadane stain normalizer on my datasets.

The dataset has separate folders for I am trying to make the list of files, initialize the VahadaneStainNormalizer instance and call the _normalize_image(img) method. It works in the beginning but stops suddenly after some time and leaves this error. I have tried on different datasets but the error is the same.

Images are in PNG, I am loading them using PIL, converting them to RGB, and making ndarray. I do not understand where NaN or inf values might be appearing.

Traceback (most recent call last): File "normalizer.py", line 58, in norm_img = normalization._normalize_image(target) File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/histocartography/preprocessing/stain_normalizers.py", line 498, in _normalize_image input_image, stain_matrix_source File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/histocartography/preprocessing/stain_normalizers.py", line 103, in _get_concentrations stain_matrix.T, optical_density.T, rcond=-1)[0].T File "<__array_function__ internals>", line 6, in lstsq File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/numpy/linalg/linalg.py", line 2306, in lstsq x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj) File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/numpy/linalg/linalg.py", line 100, in _raise_linalgerror_lstsq raise LinAlgError("SVD did not converge in Linear Least Squares") numpy.linalg.LinAlgError: SVD did not converge in Linear Least Squares

Can someone please suggest here?

guillaumejaume commented 2 years ago

Hi,

Can you try to follow this example instead https://github.com/histocartography/histocartography/blob/main/examples/stain_normalization.py. Let me know if you still get the same error.

Guillaume

NeelKanwal commented 2 years ago

Thanks for a quick reply. I am also using png files but they are RGBA(32bit).

In order to make it similar to example dataset, I load then and convert to RGB (24bit). While running it on two other datasets, I am getting different errors from linalg.

Here are both traces.

Traceback (most recent call last): File "", line 1, in File "/home/neel/.pycharm_helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "/home/neel/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/home/neel/IVMSP/normalizer.py", line 69, in sav_img.save(os.path.join(new_path, "training", "artifact_free", img)) File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/histocartography/pipeline.py", line 138, in process return self._process(*args, **kwargs) File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/histocartography/preprocessing/stain_normalizers.py", line 164, in _process normalized_image = self._normalize_image(standardized_image) File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/histocartography/preprocessing/stain_normalizers.py", line 498, in _normalize_image input_image, stain_matrix_source File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/histocartography/preprocessing/stain_normalizers.py", line 103, in _get_concentrations stain_matrix.T, optical_density.T, rcond=-1)[0].T File "<__array_function__ internals>", line 6, in lstsq File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/numpy/linalg/linalg.py", line 2259, in lstsq x, resids, rank, s = gufunc(a, b, rcond, signature=signature, extobj=extobj) ValueError: On entry to DLASCL parameter number 4 had an illegal value

Traceback (most recent call last): File "", line 1, in File "/home/neel/.pycharm_helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "/home/neel/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/home/neel/IVMSP/normalizer.py", line 58, in norm_img = normalization.process(target) File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/histocartography/pipeline.py", line 138, in process return self._process(*args, kwargs) File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/histocartography/preprocessing/stain_normalizers.py", line 164, in _process normalized_image = self._normalize_image(standardized_image) File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/histocartography/preprocessing/stain_normalizers.py", line 496, in _normalize_image stain_matrix_source = self._get_stain_matrix(input_image) File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/histocartography/preprocessing/stain_normalizers.py", line 480, in _get_stain_matrix dictionary = dict_learner.fit_transform(optical_density.T).T File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/sklearn/base.py", line 699, in fit_transform return self.fit(X, fit_params).transform(X) File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/sklearn/decomposition/_dict_learning.py", line 1343, in fit X = self._validate_data(X) File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/sklearn/base.py", line 421, in _validate_data X = check_array(X, *check_params) File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/sklearn/utils/validation.py", line 63, in inner_f return f(args, **kwargs) File "/home/neel/miniconda3/envs/DKL/lib/python3.6/site-packages/sklearn/utils/validation.py", line 737, in check_array context)) ValueError: Found array with 0 feature(s) (shape=(3, 0)) while a minimum of 1 is required.

I have changed NumPy version also. Note: I am not using the target image and leaving the default stain matrix. (I do not know a reference for that particular stain concentration.)

Neel