GNU General Public License v3.0

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

深度分析 GitHub 项目 “BMIRDS/deepslide”

项目背景与痛点

“BMIRDS/deepslide” 是基于 Nature Scientific Reports 论文《使用深度神经网络对切除肺腺癌滑片中的组织病理学模式进行病理学家水平的分类》的源代码实现。该项目通过滑动窗口框架对高分辨率全切片图像进行分类,这是当前在医学图像分析领域中尤为重要的问题。在人工病理诊断过程中,对于大量高分辨率的组织病理切片图像处理非常耗时耗力,甚至会出现遗漏或误诊的风险。因此,开发基于深度学习的图像处理系统,对快速、精确地进行病理切片图像分类的需求日益增大。该系统在自动化、提高诊断效率的同时,可减少人为因素导致的误诊风险,进一步推动病理学与机器学习的深度融合。

技术核心揭秘

该项目采用了深度神经网络模型,通过滑动窗口技术对全切片图像进行分割、分类。具体来说,BMIRDS/deepslide 对输入图像结合预处理手段(如图像增强和归一化)进行高效处理,然后通过滑动窗口方法对图像进行分割,并经历一系列深度卷积神经网络层以提取图像特征。最后,通过全连接层对这些特征进行分类。其关键技术栈包括 TensorFlow,它在深度学习领域有着极其丰富的资源和强大的计算加速资源。

根据项目特点,我们可以推测出项目的关键工作流程如下:

graph TD A[输入全切片图像] --> B[图像预处理] B --> C{"滑动窗口分割"} C -->|"窗口移动完成"| D[图像分类] C -->|"继续移动"| B D --> E[输出分类结果] subgraph "深度学习框架" B --> D[图像分类] end

这个工作流的主要数据来源是高分辨率的全切片组织病理学图像。数据流涉及到图像预处理,滑动窗口技术,以及深度神经网络分类。

功能亮点与差异

BMIRDS/deepslide 项目在病理切片图像分类上有着明显的技术优势。不仅继承了深度学习模型的强大性能,还可以灵活调整滑动窗口大小和步幅,使系统能在特定任务需求下提供最优效果。这一点在样本稀缺、数据分裂严重的情况下尤为重要,使模型能够有效利用有限的训练样本提高分类准确性。

应用场景与落地建议

在实际应用环境中,用户需要谨慎设置数据预处理参数,根据任务需求适当调整滑动窗口参数,以保障模型能在资源受限的情况下仍能获得不错的性能。此项目适合应用于需要进行病理学高分辨率切片图像分类的场合,例如临床医学中的形态学分析和诊断辅助决策系统中。同时,该系统的输出可以直接与医院的电子病历系统进行集成,实现病理学分析的自动化和智能化。

综合评价

BMIRDS/deepslide 项目凭借其在病理学切片分类任务中的高效表现,展现了深度学习与医学图像分析技术的高度结合。作为一个强大的全切片图像分类工具,它在功能强大同时,也在技术实现上展现出高水准的设计思想,无疑为未来病理学研究的自动化和智能化开辟了新路径。不过,在面对多样化的数据类型和任务需求时,可能是其灵活性和适应性稍显欠缺,这也是本项目未来发展的一个重要方向。

关于

GitHub 原始描述(英文)

Code for the Nature Scientific Reports paper "Pathologist-level classification of histologic patterns on resected lung adenocarcinoma slides with deep neural networks." A sliding window framework for classification of high resolution whole-slide images, often microscopy or histopathology images.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
817
价值分
356
活跃状态
活跃

语言占比

Python
Shell

README

DeepSlide: A Sliding Window Framework for Classification of High Resolution Microscopy Images (Whole-Slide Images)

This repository is a sliding window framework for classification of high resolution whole-slide images, often called microscopy or histopathology images. This is also the code for the paper Pathologist-level Classification of Histologic Patterns on Resected Lung Adenocarcinoma Slides with Deep Neural Networks. For a practical guide and implementation tips, see the Medium post Classification of Histopathology Images with Deep Learning: A Practical Guide.

We have made 143 digitized high-resolution histology slides of lung adenocarcinoma in the test set and their predominant subtypes according to the consensus opinion of three pathologists at Dartmouth-Hitchcock Medical Center publicly available. More information about this dataset and instructions on how to download are provided on the dataset webpage.

For questions about our code, please open an issue on this code repository.

alt text

Requirements

conda env create --file setup/conda_env.yaml

This command creates a conda environment called ‘deepslide_env’ with Python 3.9 and PyTorch with CUDA 11.3. Please modify the environment file(s) for other versions.

In addition, install_openslide.sh installs dependencies of OpenSlide package in Ubuntu. For other platforms, please visit to the OpenSlide’s official website for more information.

Usage

Take a look at code/config.py before you begin to get a feel for what parameters can be changed.

1. Train-Val-Test Split:

Splits the data into a validation and test set. Default validation whole-slide images (WSI) per class is 20 and test images per class is 30. You can change these numbers by changing the --val_wsi_per_class and --test_wsi_per_class flags at runtime. You can skip this step if you did a custom split (for example, you need to split by patients).

python code/1_split.py

If you do not want to duplicate the data, append --keep_orig_copy False to the above command.

Inputs: all_wsi

Outputs: wsi_train, wsi_val, wsi_test, labels_train.csv, labels_val.csv, labels_test.csv

Note that all_wsi must contain subfolders of images labeled by class. For instance, if your two classes are a and n, you must have a/*.jpg with the images in class a and n/*.jpg with images in class n.

If you already have a patch-based preprocessed dataset, you may skip to Stage 3 for model training. Please make sure that at least:

  1. all_wsi has a folder for each class as a placeholder (they can be empty).

  2. Both train_folder/train and train_folder/val folders contain a folder for each class and each slide that belongs to its partition. The slide folder should contain at least one patch extracted from the slide (e.g., train_folder/train/<class_name>/<slide_name>/<patch_file>).

  3. Review code/config.py and make appropriate/necessary changes for your dataset.

Example

python code/1_split.py --val_wsi_per_class 10 --test_wsi_per_class 20

2. Data Processing

  • Generate patches for the training set.
  • Balance the class distribution for the training set.
  • Generate patches for the validation set.
  • Generate patches by folder for WSI in the validation set.
  • Generate patches by folder for WSI in the testing set.
python code/2_process_patches.py

Note that this will take up a significant amount of space. Change --num_train_per_class to be smaller if you wish not to generate as many windows. If your histopathology images are H&E-stained, whitespace will automatically be filtered. Turn this off using the option --type_histopath False. Default overlapping area is 1/3 for test slides. Use 1 or 2 if your images are very large; you can also change this using the --slide_overlap option.

Inputs: wsi_train, wsi_val, wsi_test

Outputs: train_folder (fed into model for training), patches_eval_train (for validation, sorted by WSI), patches_eval_test (for testing, sorted by WSI)

Example

python code/2_process_patches.py --num_train_per_class 20000 --slide_overlap 2

3. Model Training

CUDA_VISIBLE_DEVICES=0 python code/3_train.py

We recommend using ResNet-18 if you are training on a relatively small histopathology dataset. You can change hyperparameters using the argparse flags. There is an option to retrain from a previous checkpoint. Model checkpoints are saved by default every epoch in checkpoints.

Inputs: train_folder

Outputs: checkpoints, logs

Example

CUDA_VISIBLE_DEVICES=0 python code/3_train.py --batch_size 32 --num_epochs 100 --save_interval 5

4. Testing on WSI

Run the model on all the patches for each WSI in the validation and test set.

CUDA_VISIBLE_DEVICES=0 python code/4_test.py

We automatically choose the model with the best validation accuracy. You can also specify your own. You can change the thresholds used in the grid search by specifying the threshold_search variable in code/config.py.

Inputs: patches_eval_val, patches_eval_test

Outputs: preds_val, preds_test

Example

CUDA_VISIBLE_DEVICES=0 python code/4_test.py --auto_select False

5. Searching for Best Thresholds

The simplest way to make a whole-slide inference is to choose the class with the most patch predictions. We can also implement thresholding on the patch level to throw out noise. To find the best thresholds, we perform a grid search. This function will generate csv files for each WSI with the predictions for each patch.

python code/5_grid_search.py

Inputs: preds_val, labels_val.csv

Outputs: inference_val

Example

python code/5_grid_search.py --preds_val different_labels_val.csv

6. Visualization

A good way to see what the network is looking at is to visualize the predictions for each class.

python code/6_visualize.py

Inputs: wsi_val, preds_val

Outputs: vis_val

You can change the colors in colors in code/config.py

alt text

Example

python code/6_visualize.py --vis_test different_vis_test_directory

7. Final Testing

Do the final testing to compute the confusion matrix on the test set.

python code/7_final_test.py

Inputs: preds_test, labels_test.csv, inference_val and labels_val (for the best thresholds)

Outputs: inference_test and confusion matrix to stdout

Example

python code/7_final_test.py --labels_test different_labels_test.csv

Best of luck.

Quick Run

If you want to run all code and change the default parameters in code/config.py, run

sh code/run_all.sh

and change the desired flags on each line of the code/run_all.sh script.

Pre-Processing Scripts

See code/z_preprocessing for some code to convert images from svs into jpg. This uses OpenSlide and takes a while. How much you want to compress images will depend on the resolution that they were originally scanned, but a guideline that has worked for us is 3-5 MB per WSI.

Known Issues and Limitations

  • Only 1 GPU supported.
  • Should work, but not tested on Windows.
  • In cases where no crops are found for an image, empty directories are created. Current workaround uses try and except statements to catch errors.
  • Image reading code expects colors to be in the RGB space. Current workaround is to keep first 3 channels.
  • This code will likely work better when the labels are at the tissue level. It will still work for the entire WSI, but results may vary.

Still not working? Consider the following…

  • Ask a pathologist to look at your visualizations.
  • Make your own heuristic for aggregating patch predictions to determine the WSI-level classification. Often, a slide thats 20% abnormal and 80% normal should be classified as abnormal.
  • If each WSI can have multiple types of lesions/labels, you may need to annotate bounding boxes around these.
  • Did you pre-process your images? If you used raw .svs files that are more than 1GB in size, its likely that the patches are way too zoomed in to see any cell structures.
  • If you have less than 10 WSI per class in the training set, obtain more.
  • Feel free to view our end-to-end attention-based model in JAMA Network Open: https://jamanetwork.com/journals/jamanetworkopen/fullarticle/2753982.

Future Work

  • Contributions to this repository are welcome.
  • Code for generating patches on the fly instead of storing them in memory for training and testing would save a lot of disk space.
  • If you have issues, please post in the issues section and we will do our best to help.

Citations

DeepSlide is an open-source library and is licensed under the GNU General Public License (v3). If you are using this library please cite:

Jason Wei, Laura Tafe, Yevgeniy Linnik, Louis Vaickus, Naofumi Tomita, Saeed Hassanpour, "Pathologist-level Classification of Histologic Patterns on Resected Lung Adenocarcinoma Slides with Deep Neural Networks", Scientific Reports;9:3358 (2019).

评论

暂无评论