Git Product home page Git Product logo

pytorch-medical-segmentation's Introduction

Pytorch Medical Segmentation

Read Chinese Introduction:Here!

Notes

We are planning a major update to the code in the near future, so if you have any suggestions, please feel free to email me or mention them in the issue.

Recent Updates

  • 2021.1.8 The train and test codes are released.
  • 2021.2.6 A bug in dice was fixed with the help of Shanshan Li.
  • 2021.2.24 A video tutorial was released(https://www.bilibili.com/video/BV1gp4y1H7kq/).
  • 2021.5.16 A bug in Unet3D implement was fixed.
  • 2021.5.16 The metric code is released.
  • 2021.6.24 All parameters can be adjusted in hparam.py.
  • 2021.7.7 Now you can refer medical classification in Pytorch-Medical-Classification
  • 2022.5.15 Now you can refer semi-supervised learning on medical segmentation in SSL-For-Medical-Segmentation
  • 2022.5.17 We update the training and inference code and fix some bugs.

Requirements

  • pytorch1.7
  • torchio<=0.18.20
  • python>=3.6

Notice

  • You can modify hparam.py to determine whether 2D or 3D segmentation and whether multicategorization is possible.
  • We provide algorithms for almost all 2D and 3D segmentation.
  • This repository is compatible with almost all medical data formats(e.g. nii.gz, nii, mhd, nrrd, ...), by modifying fold_arch in hparam.py of the config. I would like you to convert both the source and label images to the same type before using them, where labels are marked with 1, not 255.
  • If you want to use a multi-category program, please modify the corresponding codes by yourself. I cannot identify your specific categories.
  • Whether in 2D or 3D, this project is processed using patch. Therefore, images do not have to be strictly the same size. In 2D, however, you should set the patch large enough.

Prepare Your Dataset

Example1

if your source dataset is :

source_dataset
├── source_1.mhd
├── source_1.zraw
├── source_2.mhd
├── source_2.zraw
├── source_3.mhd
├── source_3.zraw
├── source_4.mhd
├── source_4.zraw
└── ...

and your label dataset is :

label_dataset
├── label_1.mhd
├── label_1.zraw
├── label_2.mhd
├── label_2.zraw
├── label_3.mhd
├── label_3.zraw
├── label_4.mhd
├── label_4.zraw
└── ...

then your should modify fold_arch as *.mhd, source_train_dir as source_dataset and label_train_dir as label_dataset in hparam.py

Example2

if your source dataset is :

source_dataset
├── 1
    ├── source_1.mhd
    ├── source_1.zraw
├── 2
    ├── source_2.mhd
    ├── source_2.zraw
├── 3
    ├── source_3.mhd
    ├── source_3.zraw
├── 4
    ├── source_4.mhd
    ├── source_4.zraw
└── ...

and your label dataset is :

label_dataset
├── 1
    ├── label_1.mhd
    ├── label_1.zraw
├── 2
    ├── label_2.mhd
    ├── label_2.zraw
├── 3
    ├── label_3.mhd
    ├── label_3.zraw
├── 4
    ├── label_4.mhd
    ├── label_4.zraw
└── ...

then your should modify fold_arch as */*.mhd, source_train_dir as source_dataset and label_train_dir as label_dataset in hparam.py

Training

  • without pretrained-model
set hparam.train_or_test to 'train'
python main.py
  • with pretrained-model
set hparam.train_or_test to 'train'
python main.py -k True

Inference

  • testing
set hparam.train_or_test to 'test'
python main.py

Examples

Tutorials

Done

Network

  • 2D
  • 3D

Metric

  • metrics.py to evaluate your results

TODO

  • dataset
  • benchmark
  • nnunet

By The Way

This project is not perfect and there are still many problems. If you are using this project and would like to give the author some feedbacks, you can send Me an email.

Acknowledgements

This repository is an unoffical PyTorch implementation of Medical segmentation in 3D and 2D and highly based on MedicalZooPytorch and torchio. Thank you for the above repo. The project is done with the supervisions of Prof. Ruoxiu Xiao, Prof. Shuang Song and Dr. Cheng Chen. Thank you to Youming Zhang, Daiheng Gao, Jie Zhang, Xing Tao, Weili Jiang and Shanshan Li for all the help I received.

pytorch-medical-segmentation's People

Contributors

lykzsm avatar montaellis avatar tomguluson92 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytorch-medical-segmentation's Issues

train

Can this project not be trained except for data files in *.mhd format?
image
image

validation transform is not applied

Hi there,

I found your work very inspiring, and I made my own segmentation model follwing your idea. But I found a problem in the validation step.

In your main.py file, line 456:

for i,subj in enumerate(test_dataset.subjects):
...

I think in the enumerate parentheses, it should not be test_dataset.subjects because the the subjects object is the raw image list without preprocessing (aka transforms in the data_function.py). Maybe here should be:

for i,subj in enumerate(test_dataset.valid_set):
...

Because valid_set(also there may be a typo that you use training_set in test_loader) is the one with transforms(normalization) applied.

Maybe my comprehension is not correct and you have your own thoughts, so could you please share your idea on this step? Thank u

医学图像分割数据集

新入门医学影像分割小白,请问您有医学影像目标分割的数据集吗,在网上找了一段时间,但是都没有找到合适的香瓜的数据集,如果方便的话希望分享以下,谢谢🙏

数据集划分

请问一下你的数据集是怎么划分的,训练集、验证集、测试集?

How to train 2D images with multi-classes?

Hello, dear author!
Thank you for your amazing job!
Could you tell me how to train 2D images with multi-classes (assume num_classes=5)?
Where should I modify? (expect "mode = '2d', in_class = 3, out_class = 5" in hparam.py)
Thank you in advance!

3D数据集在loss函数上出现维度匹配

作者你好,首先很感谢您在github上提供的代码。
下面是我在训练3D CT数据出现的问题:

训练时在运行到main.py里的260行,也就是loss=criterion_ce(outputs,y.argmax(dim=1)) +criterion_dice(outputs,y.argmax(dim=1))出错,我进去看后,发现是predict和target维度不一致,以图片大小为646496,batchs=4,channel=2为例,predict为(4,2,64,64,96),target为(4,128,64,96),缺少channel维度,使得dice损失函数无法运行,同时self._one_hot_encoder函数使得第二个数翻倍,也就是叠加了,我加入target = torch.unsqueeze(target,1)这行代码后,成功进行训练,但tensorboard中显示loss在缓慢下降,但dice直接下降到0。

D:\ProgramData\miniconda3\envs\pytorch\python.exe E:/python_learning/Pytorch-Medical-Segmentation-master/main.py
epoch:1
Batch: 0/5 epoch 1

Traceback (most recent call last):
File "E:\python_learning\Pytorch-Medical-Segmentation-master\main.py", line 582, in
train()
File "E:\python_learning\Pytorch-Medical-Segmentation-master\main.py", line 265, in train
loss = criterion_ce(outputs, y.argmax(dim=1)) + criterion_dice(outputs, y.argmax(dim=1))
File "D:\ProgramData\miniconda3\envs\pytorch\lib\site-packages\torch\nn\modules\module.py", line 1194, in _call_impl
return forward_call(*input, **kwargs)
File "E:\python_learning\Pytorch-Medical-Segmentation-master\loss_function.py", line 141, in forward
assert inputs.size() == target.size(), 'predict & target shape do not match'
AssertionError: predict & target shape do not match

Process finished with exit code 1

非常感谢您的回复

如何让模型可以处理大于1的标签?

现在的模型训练后只是把脊椎的所有节都分成一个颜色,我的训练集标签是每相邻节骨头都是不一样的颜色,我也需要这样的推理效果,请问应该怎么处理啊?

我用更新后的代码预测时出现了错误

我用您更新后的代码重新训练了模型,在预测时出现了这样的错误:
File "main.py", line 519, in test
aggregator_1.add_batch(labels, locations)
File "/home/anran/.conda/envs/seg/lib/python3.6/site-packages/torchio/data/inference/aggregator.py", line 131, in add_batch
self.patch_overlap,
File "/home/anran/.conda/envs/seg/lib/python3.6/site-packages/torchio/data/inference/aggregator.py", line 82, in crop_batch
diff = patch_shape - crop_shape
ValueError: operands could not be broadcast together with shapes (2,) (3,)

请问要怎么解决

bug

RuntimeError: More than one value for "spacing" found in subject images:
{'label': (1.0468800067901611, 1.0468800067901611, 2.5),
'source': (0.712891, 0.712891, 2.49998)}
image

metrics.py加载数据目录的构成

你好,我的数据是3d的,后缀nii.gz,请问metrics.py加载数据目录的构成应该是什么样的?
应该没问题,我是在这一行出错的,for i,subj in enumerate(training_set.subjects):
报错为:
File "/home/carrie/PycharmProjects/Pytorch_Medical_Segmentation/metrics.py", line 55, in
for i,subj in enumerate(training_set.subjects):#training_set.subjects
AttributeError: 'SubjectsDataset' object has no attribute 'subjects'
可以再readme里加一个metrics运行示例吗?
可以了,这样改 for i,subj in enumerate(training_set._subjects):#少了一个下划线

main.py第250行loss = criterion(outputs, y)-->loss = criterion(logits, y)

你好,很感谢您的代码,我预测效果很差,多次修改损失函数都没有效果,看代码发现这里有点疑问,请问这里是不是错了?
man.py代码:
logits = torch.sigmoid(outputs)
labels = logits.clone()
labels[labels>0.5] = 1
labels[labels<=0.5] = 0
loss = criterion(outputs, y)#有问题?应该为loss = criterion(logits, y)

the prediction results are all black

I fixed the errors in the model prediction in the same way as you, but now the prediction results are all black. What's the problem. What I did was a dichotomy. During the test, there was such a warning:
image

模型训练效果较差

我使用这个版本的 ResUNet 在我的数据上进行训练可以得到0.78左右的 Dice 值. 我使用您的模型对同样的数据进行训练模型无法进行收敛 Dice 值始终处在0.1以下. 我在对模型进行检查后仍未能发现问题.

这是我训练的 log. 其中红色线为2DResUNet, 蓝色线为3D模型.
image

我原始的数据类型为 .dcm 类型, 使用脚本转为 .nii.gz 格式. 我使用 dicompyler 对转后的 .nii.gz 进行检查未发现数据存在问题.

希望能够得到您的回复!

size of input会随着batchsize的改变而改变

使用3d unet,用的brats2020的数据集,python main.py后,targetsize和inputsize会出现维度不匹配的问题,inputsize是[(2,2,32,32,32)],targetsize是[(2,64,32,32)];我把target.reshape([2,2,32,32,32])后成功运行,但是后续调整batsize的的大小,程序又会报错,RuntimeError: shape '[2, 2, 32, 32, 32]' is invalid for input of size 524288,就是inputsize会随着batchsize的改变而改变,这是什么原因呢,因为一般batchsize不就是可以随意改变的吗

hi, question about the image size of .nrrd format.

Thanks for your github.

I'm testing with .nrrd format.
however, there is an issue where the resulting image is enlarged.
(ex: input size:256x256x256 --> result : 128x128x128)
Also, the image is enlarged to 32-bit or greater precision.
What part of main.py should I edit?

训练相关

您好!有一个关于训练结果的问题想要请教一下。
我使用的网上下载的胰腺数据集,原数据和标签都是 nii.gz 格式的,因为是3d数据,所以用的是3d的网络来跑的。是在本地的计算机上跑的代码。默认设置的epoch是100
参数设置如下:
image
但是我训练到后面,loss值是在下降,但是dice值就一直是0了,false_positive_rate以及false_negative_rate也很不正常,如下所示:
image
想请问原因可以能为什么呢?是否和epoch数有关?因为我看您的原代码是500000,我设置100已经需要跑一段时间了。
其他的参数是否有要根据数据的三维尺寸调整的呢?
感谢回复!

很好的项目,对TensorBoard有个疑问

我看了下可能因为数据量比较大 一个epoch 就收敛的差不多了 但是我查看TensorBoard显示dice false_negtive_rate false_positive_rate 都没结果 是我的姿势不对嘛。
image

Sharing models through Hugging Face Hub

Hi MontaEllis team!

The Medical Segmentation project is fantastic. I see you host and share models with your server. Would you be interested in sharing your models in the Hugging Face Hub?

This integration would allow you to freely download/upload models and make your work more accessible and visible to the rest of the ML community. We can help you set up a MontaEllis organization (examples, Facebook AI y Stanford NLP).

Creating the repos and adding new models should be a relatively straightforward process. This is a step-by-step guide explaining the process in case you're interested. Please let us know if you would be interested and if you have any questions.

Some of the benefits of sharing your models through the Hub would be:

  • Presence in the HF Hub might lower the entry of barrier to your medical models and increase their visibility.
    • Repos provide valuable metadata about their tasks, languages, metrics, etc. that make them discoverable
  • versioning, commit history, and diffs.
  • multiple features from TensorBoard visualizations, PapersWithCode integration, and more.

Additionally, we have a library to access repositories programmatically (both downloading pre-trained models and pushing, with many nice things such as filtering, caching, etc.). If we want to try out this integration, I would suggest you add one or two models manually and then use the huggingface_hub library to implement downloading those models programmatically from pytorch-medical-segmentation. To read more about it, you might want to check our documentation to read more about it.

Relevant references:

Happy to hear your thoughts,

Omar and the Hugging Face team (cc @osanseviero @abidlabs )

prepare data

你好 请问在哪里添加预处理的代码 我没找到您预处理的代码。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.