From c1889553627398a7c64594cd5692c427d865dd72 Mon Sep 17 00:00:00 2001 From: Artur Jurgas Date: Mon, 28 Oct 2024 13:00:15 +0100 Subject: [PATCH] Documentation and examples. --- .gitignore | 5 +- README.md | 58 +- .../replace_with_classification_weights | 0 data/models/replace_with_segmentation_weights | 0 .../annotations/dataset_name/test_image.xml | 1249 ++++ .../dataset_name/replace_with_test_image | 0 .../segmentations/dataset_name/test_image.xml | 5442 +++++++++++++++++ libhaa/__init__.py | 4 - libhaa/scripts/__init__.py | 4 - libhaa/scripts/build_collection.py | 5 +- libhaa/scripts/generate_dataset.py | 18 +- libhaa/scripts/segment.py | 15 +- libhaa/segmentation/dataloader.py | 2 +- pyproject.toml | 3 +- 14 files changed, 6780 insertions(+), 25 deletions(-) create mode 100644 data/models/replace_with_classification_weights create mode 100644 data/models/replace_with_segmentation_weights create mode 100644 data/test_data/annotations/dataset_name/test_image.xml create mode 100644 data/test_data/images/dataset_name/replace_with_test_image create mode 100644 data/test_data/segmentations/dataset_name/test_image.xml diff --git a/.gitignore b/.gitignore index 9fd3c25..2ef8532 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ libhaa.egg-info **/__pycache__ -data \ No newline at end of file +data/test_augmented +data/test_collection +**/*.tiff +**/*.tar \ No newline at end of file diff --git a/README.md b/README.md index e7d5875..69e6149 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,14 @@ Finally, the `libhaa` library is installable by running the following command: pip install -e . ``` -Additional options are available for installation, such as installing the library with the `classification` or `segmentation` modules. To do so, first install [PyTorch](https://pytorch.org/get-started/locally/) and then run the following command. You can choose to instal one of the following options: +Additional options are available after installation, such as installing the library with the `classification` or `segmentation` modules. To do so, first make sure you have installed the base package, then install [PyTorch](https://pytorch.org/get-started/locally/) and then run the following command. You can choose to instal one of the following options: - both modules by running the command with the `all` options. - only one module with either `histo-seg` or `histo-class` option. ```bash -# (e.g.) conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia +# (e.g.) conda install pytorch==2.2.2 torchvision==0.17.2 torchaudio==2.2.2 pytorch-cuda=12.1 -c pytorch -c nvidia +# After this you can choose to install optional dependencies: pip install -e .[all] # or [histo-seg] or [histo-class] ``` @@ -60,7 +61,58 @@ generates patches of training data from the chosen pyramid level of images from - `cut-patches-inference`: generates patches from a single WSI for inference of the classification model available in the [releases page](https://github.com/Jarartur/HistopathologyAugmentationResearch/releases/tag/classification). -Run `command -h` to learn about their available arguments. More detailed step-by-step guide will come in the near future. A simplified classification inference script is currently being worked on. +## Typical workflow + +To follow along download `test_image.tiff` from the releases page and replace the file `data/test_data/images/dataset_name/replace_with_test_image`. + +### Building a collection + +First to build our artifact collection we run +```bash +build-collection --wsi-path data\test_data\images --annotations-path data\test_data\annotations --save-path data\test_collection --relative-paths +``` + +- `--wsi-path` specifies our image directory. +- `--annotations-path` specifies our annotation directories. Keep in mind that if we were to keep our annotations inside the image folder we can just supply the same path as to `--wsi-path`. +- `--save-path` specifies where to save the collection. +- `--relative-paths` tells our program to include the `dataset_name` folder when searching for annotations. If you had a folder structure where each of the image is inside a subfolder (like here) but the annotations are aggregated in a single folder then do not use this option. + +This will create an artifact library in `data\test_collection`. It will consist of folders of artifact types and in each folder there will be artifact images and their annotation. + +### Segmenting images + +Now before augmenting our images we also need to generate segmentations for our images, so the program will know where to place each artifact. To do so, first download the segmentation model weights and place them, e.g., in the `data/models` directory. *Do not extract the downloaded file.* Now we run: +```bash +segment --wsi-path data\test_data\images --model-weights data\models\weights_v11.07.2023.tar --save-path data\test_data\segmentations --openslide-level 4 --device 'cuda' +``` + +- `--wsi-path` specifies our image directory. +- `--model-weights` specifies our weights path. +- `--save-path` specifies the save folder. Any subfolders in `--wsi-path` will be propagated here. +- `--openslide-level` specifies the pyramid level to load. Running `segment -h` will give you recommended values for datasets used in the study. +- `--device` can be a cpu or cuda. You can also specify which GPU to use by, e.g., `cuda:2`. + +In our case this will create a folder `data\test_data\segmentations\dataset_name` with a `test_image.xml` file in it contianing the segmentation in ASAP format. + +### Augmenting a dataset + +Now we will augment our image with new artifacts. + +```bash +generate-dataset --wsi-path data\test_data\images --segmentations-path data\test_data\segmentations --artifact-collection-path data\test_collection --save-path data\test_augmented --root-annotation data\test_data\annotations +``` + +- `--wsi-path` specifies our image directory. +- `--segmentations-path` specifies our previously generated segmentations. +- `--artifact-collection-path` specifies our previously generated artifact collection. +- `--save-path` specifies the save folder. +- `--root-annotation` (optional). With this argument you can supply already present annotations. The new, augmented annotations will be merged with the existing ones. + +This will generate a `data\test_augmented\dataset_name` folder containing our augmented image. You can inspect the before and after with [ASAP](https://computationalpathologygroup.github.io/ASAP/) software. Keep in mind not all already present artifacts in this particular image are annotated from the beginning as it is a simple example. + +### Classification + +This section is still under construction and will be available in the near future. All code required to run this step is in `model_training/classification` folder but a simplified script will be made for inference. # Citing diff --git a/data/models/replace_with_classification_weights b/data/models/replace_with_classification_weights new file mode 100644 index 0000000..e69de29 diff --git a/data/models/replace_with_segmentation_weights b/data/models/replace_with_segmentation_weights new file mode 100644 index 0000000..e69de29 diff --git a/data/test_data/annotations/dataset_name/test_image.xml b/data/test_data/annotations/dataset_name/test_image.xml new file mode 100644 index 0000000..ac943b3 --- /dev/null +++ b/data/test_data/annotations/dataset_name/test_image.xml @@ -0,0 +1,1249 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/data/test_data/images/dataset_name/replace_with_test_image b/data/test_data/images/dataset_name/replace_with_test_image new file mode 100644 index 0000000..e69de29 diff --git a/data/test_data/segmentations/dataset_name/test_image.xml b/data/test_data/segmentations/dataset_name/test_image.xml new file mode 100644 index 0000000..f128f49 --- /dev/null +++ b/data/test_data/segmentations/dataset_name/test_image.xml @@ -0,0 +1,5442 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/libhaa/__init__.py b/libhaa/__init__.py index f1d3e72..e69de29 100644 --- a/libhaa/__init__.py +++ b/libhaa/__init__.py @@ -1,4 +0,0 @@ -import libhaa.classification as classification -import libhaa.scripts as scripts -import libhaa.segmentation as segmentation -import libhaa.base as base diff --git a/libhaa/scripts/__init__.py b/libhaa/scripts/__init__.py index aa36f84..e69de29 100644 --- a/libhaa/scripts/__init__.py +++ b/libhaa/scripts/__init__.py @@ -1,4 +0,0 @@ -import libhaa.scripts.build_collection as build_collection -import libhaa.scripts.cut_patches as cut_patches -import libhaa.scripts.segment as segment -import libhaa.scripts.generate_dataset as generate_dataset \ No newline at end of file diff --git a/libhaa/scripts/build_collection.py b/libhaa/scripts/build_collection.py index 182d682..48e5bad 100644 --- a/libhaa/scripts/build_collection.py +++ b/libhaa/scripts/build_collection.py @@ -1,10 +1,9 @@ import itertools from pathlib import Path -from typing import Dict, Generator, Tuple, List +from typing import Generator, Tuple, List -from libhaa.base.io import AnnotationCollection, Artifact, ImageWSI, Annotation, AnnotatedWSI +from libhaa.base.io import AnnotationCollection, Artifact, ImageWSI, AnnotatedWSI from libhaa.base.config import WSI_EXTs, ANNOTATION_EXTs -from libhaa.base.parsing import match_ext2class import uuid from tqdm import tqdm diff --git a/libhaa/scripts/generate_dataset.py b/libhaa/scripts/generate_dataset.py index 32a548d..cbee1c4 100644 --- a/libhaa/scripts/generate_dataset.py +++ b/libhaa/scripts/generate_dataset.py @@ -9,13 +9,12 @@ from libhaa.base.io import ( AnnotatedWSI, - ImageWSI, - AnnotationCollection, ArtifactsCollection, ) from libhaa.base.config import ARTIFACT_LIBRARY_ANNOTATION_EXT, WSI_EXTs -from libhaa.base.parsing import match_ext2class +from sys import platform +import gc def main( root_wsi: Path, root_segmentation: Path, artifact_collection: Path, save_root: Path, skip_existing: bool = True, root_annotation = None @@ -35,9 +34,16 @@ def main( pbar.set_description(f"{case.wsi_path.name} already exists, skipping...") continue - proc = mp.Process(target=execute_then_release, args=(case, Artifacts)) - proc.start() - proc.join() + if ( + platform == "win32" + ): # sorry windows users, I don't know how to make this work as Artifacts are not pickable. + execute_then_release(case, Artifacts) + del case + gc.collect() + else: + proc = mp.Process(target=execute_then_release, args=(case, Artifacts)) + proc.start() + proc.join() def execute_then_release(case, Artifacts): diff --git a/libhaa/scripts/segment.py b/libhaa/scripts/segment.py index 9d977bf..59f6b4c 100644 --- a/libhaa/scripts/segment.py +++ b/libhaa/scripts/segment.py @@ -6,7 +6,11 @@ import itertools def main( - root_wsi: Path, model_weights_path: Path, save_root: Path, openslide_level: int = 3 + root_wsi: Path, + model_weights_path: Path, + save_root: Path, + openslide_level: int = 3, + device: str = "cpu", ): ''' openslide_level: int, default 3 @@ -14,7 +18,7 @@ def main( for ACROBAT: 3-4 for BigPicture: 6-7 ''' - segmod = SegmentationModule(model_weights_path, False, device="cuda") + segmod = SegmentationModule(model_weights_path, False, device=device) for wsi_path, save_path in wsi_iterator(root_wsi=root_wsi, save_root=save_root): segmod.inference_wsi(wsi_path, openslide_level, save_path) @@ -64,6 +68,13 @@ def cli(): for BigPicture: 6-7 """, ) + parser.add_argument( + "--device", + type=str, + choices=["cpu", "cuda"], + default="cpu", # Default value if none is provided + help="Specify the device to use (cpu or cuda). Default is cpu.", + ) args = parser.parse_args() main( diff --git a/libhaa/segmentation/dataloader.py b/libhaa/segmentation/dataloader.py index 24fa4f0..86e68de 100644 --- a/libhaa/segmentation/dataloader.py +++ b/libhaa/segmentation/dataloader.py @@ -128,7 +128,7 @@ def aggregate_whole_wsi( patch_overlap, ) - patch_loader = tc.utils.data.DataLoader(grid_sampler, batch_size=batch_size) + patch_loader = tio.SubjectsLoader(grid_sampler, batch_size=batch_size) aggregator = tio.inference.GridAggregator(grid_sampler) for patches_batch in tqdm(patch_loader, desc="WSI processing"): diff --git a/pyproject.toml b/pyproject.toml index 79aded8..310f76e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,8 +29,9 @@ dependencies = [ "ruamel.yaml", # "pyvips-binary", # not working with openslideload "pyvips", - "openslide-bin", + # "openslide-bin", # will work after openslide-python 1.4 will be released "openslide-python", + "tqdm" ]