You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+87-42Lines changed: 87 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,34 +16,89 @@
16
16
## Documentation
17
17
Detail documentation regarding the code base can be found in the [GitPages](https://cielal.github.io/torch-staintools/).
18
18
19
+
## Citation
20
+
If this toolkit helps you in your publication, please feel free to cite with the following bibtex entry:
21
+
```bibtex
22
+
@software{zhou_2024_10453807,
23
+
author = {Zhou, Yufei},
24
+
title = {CielAl/torch-staintools: V1.0.3 Release},
25
+
month = jan,
26
+
year = 2024,
27
+
publisher = {Zenodo},
28
+
version = {v1.0.3},
29
+
doi = {10.5281/zenodo.10453807},
30
+
url = {https://doi.org/10.5281/zenodo.10453807}
31
+
}
32
+
```
33
+
19
34
## Description
20
35
* Stain Normalization (Reinhard, Macenko, and Vahadane) for pytorch. Input tensors (fit and transform) must be in shape of `NxCxHxW`, with value scaled to [0, 1] in format of torch.float32.
21
36
* Stain Augmentation using Macenko and Vahadane as stain extraction.
22
37
* Fast normalization/augmentation on GPU with stain matrices caching.
23
-
* Simulate the workflow in [StainTools library](https://github.com/Peter554/StainTools) but use the Iterative Shrinkage Thresholding Algorithm (ISTA), or optionally, the coordinate descent (CD) to solve the dictionary learning for stain matrix/concentration computation in Vahadane or Macenko (stain concentration only) algorithm. The implementation of ISTA and CD are derived from Cédric Walker's [torchvahadane](https://github.com/cwlkr/torchvahadane)
38
+
* Simulate the workflow in [StainTools library](https://github.com/Peter554/StainTools) but use the Iterative Shrinkage Thresholding Algorithm (ISTA), or optionally, the coordinate descent (CD) to solve the dictionary learning for stain matrix computation in Vahadane or Macenko (stain concentration only) algorithm. The implementation of ISTA and CD are derived from Cédric Walker's [torchvahadane](https://github.com/cwlkr/torchvahadane)
39
+
* Stain Concentration is solved via factorization of `Stain_Matrix x Concentration = Optical_Density`. For efficient sparse solution and more robust outcomes, ISTA can be applied. Alternatively, Least Square solver (LS) from `torch.linalg.lstsq` might be applied for faster non-sparse solution.
24
40
* No SPAMS requirement (which is a dependency in StainTools).
25
41
26
42
<br />
27
43
28
-
#### Sample Output of TorchStainTools
44
+
#### Sample Output of Torch-StainTools Normalization
| CD (`concentration_method='cd'`) | 29.3s | 4.87 |
84
+
| LS (`concentration_method='ls'`) | 0.221 |**0.097**|
85
+
| StainTools (SPAMS) | 16.6 | N/A |
86
+
87
+
88
+
## Use Cases and Tips
35
89
* For details, follow the example in demo.py
36
90
* Normalizers are wrapped as `torch.nn.Module`, working similarly to a standalone neural network. This means that for a workflow involving dataloader with multiprocessing, the normalizer
37
91
(Note that CUDA has poor support in multiprocessing, and therefore it may not be the best practice to perform GPU-accelerated on-the-fly stain transformation in pytorch's dataset/dataloader)
38
92
93
+
*`concentration_method='ls'` (i.e., `torch.linalg.lstsq`) can be efficient for batches of many smaller input (e.g., `256x256`) in terms of width and height. However, it may fail on GPU for a single larger input image (width and height). This happens even if the
94
+
the total number of pixels of the image is fewer than the aforementioned batch of multiple smaller input. Therefore, `concentration_method='ls'` could be suitable to deal with huge amount of small images in batches on the fly.
39
95
40
96
```python
41
97
import cv2
42
98
import torch
43
99
from torchvision.transforms import ToTensor
44
-
from torchvision.transforms.functional import convert_image_dtype
45
-
from torch_staintools.normalizer.factory import NormalizerBuilder
46
-
from torch_staintools.augmentor.factory import AugmentorBuilder
100
+
from torch_staintools.normalizer import NormalizerBuilder
101
+
from torch_staintools.augmentor import AugmentorBuilder
The next time `Normalizer` or `Augmentor` process the images, the corresponding stain matrices will be queried and fetched from cache if they are stored already, rather than recomputing from scratch.
134
213
135
214
136
-
## Benchmark
137
-
* Use the sample images under ./test_images (size `2500x2500x3`). Mean was computed from 7 runs (1 loop per run) using
138
-
timeit. Comparison between torch_stain_tools in CPU/GPU mode, as well as that of the StainTools Implementation.
139
-
140
-
### Transformation
141
-
142
-
| Method | CPU[s]| GPU[s]| StainTool[s]|
143
-
|:---------|:-------|:-------|:-------------|
144
-
| Vahadane | 119 | 7.5 | 20.9 |
145
-
| Macenko | 5.57 | 0.479 | 20.7 |
146
-
| Reinhard | 0.840 | 0.024 | 0.414 |
147
-
148
-
### Fitting
149
-
| Method | CPU[s]| GPU[s]| StainTool[s]|
150
-
|:---------|:-------|:-------|:-------------|
151
-
| Vahadane | 132 | 8.40 | 19.1 |
152
-
| Macenko | 6.99 | 0.064 | 20.0 |
153
-
| Reinhard | 0.422 | 0.011 | 0.076 |
154
-
155
-
## Citation
156
-
If this toolkit helps you in your publication, please feel free to cite with the following bibtex entry:
157
-
```bibtex
158
-
@software{zhou_2024_10453807,
159
-
author = {Zhou, Yufei},
160
-
title = {CielAl/torch-staintools: V1.0.3 Release},
161
-
month = jan,
162
-
year = 2024,
163
-
publisher = {Zenodo},
164
-
version = {v1.0.3},
165
-
doi = {10.5281/zenodo.10453807},
166
-
url = {https://doi.org/10.5281/zenodo.10453807}
167
-
}
168
-
```
169
-
170
215
## Acknowledgments
171
216
* Some codes are derived from [torchvahadane](https://github.com/cwlkr/torchvahadane), [torchstain](https://github.com/EIDOSLAB/torchstain), and [StainTools](https://github.com/Peter554/StainTools)
172
217
* Sample images in the demo and ReadMe.md are selected from [The Cancer Genome Atlas Program(TCGA)](https://www.cancer.gov/ccg/research/genome-sequencing/tcga) dataset.
0 commit comments