Giter VIP home page Giter VIP logo

medicalnet's Introduction

MedicalNet

This repository contains a Pytorch implementation of Med3D: Transfer Learning for 3D Medical Image Analysis. Many studies have shown that the performance on deep learning is significantly affected by volume of training data. The MedicalNet project aggregated the dataset with diverse modalities, target organs, and pathologies to to build relatively large datasets. Based on this dataset, a series of 3D-ResNet pre-trained models and corresponding transfer-learning training code are provided.

License

MedicalNet is released under the MIT License (refer to the LICENSE file for detailso).

Citing MedicalNet

If you use this code or pre-trained models, please cite the following:

    @article{chen2019med3d,
        title={Med3D: Transfer Learning for 3D Medical Image Analysis},
        author={Chen, Sihong and Ma, Kai and Zheng, Yefeng},
        journal={arXiv preprint arXiv:1904.00625},
        year={2019}
    }

Update(2019/07/30)

We uploaded 4 pre-trained models based on more datasets (23 datasets).

Model name             : parameters settings
resnet_10_23dataset.pth: --model resnet --model_depth 10 --resnet_shortcut B
resnet_18_23dataset.pth: --model resnet --model_depth 18 --resnet_shortcut A
resnet_34_23dataset.pth: --model resnet --model_depth 34 --resnet_shortcut A
resnet_50_23dataset.pth: --model resnet --model_depth 50 --resnet_shortcut B

We transferred the above pre-trained models to the multi-class segmentation task (left lung, right lung and background) on Visceral dataset. The results are as follows:

Network Pretrain LungSeg(Dice)
3D-ResNet10 Train from scratch 69.31%
MedicalNet 96.56%
3D-ResNet18 Train from scratch 70.89%
MedicalNet 94.68%
3D-ResNet34 Train from scratch 75.25%
MedicalNet 94.14%
3D-ResNet50 Train from scratch 52.94%
MedicalNet 89.25%

Contents

  1. Requirements
  2. Installation
  3. Demo
  4. Experiments
  5. TODO
  6. Acknowledgement

Requirements

  • Python 3.7.0
  • PyTorch-0.4.1
  • CUDA Version 9.0
  • CUDNN 7.0.5

Installation

  • Install Python 3.7.0
  • pip install -r requirements.txt

Demo

  • Structure of data directories
MedicalNet is used to transfer the pre-trained model to other datasets (here the MRBrainS18 dataset is used as an example).
MedicalNet/
    |--datasets/:Data preprocessing module
    |   |--brains18.py:MRBrainS18 data preprocessing script
    |	|--models/:Model construction module
    |   |--resnet.py:3D-ResNet network build script
    |--utils/:tools
    |   |--logger.py:Logging script
    |--toy_data/:For CI test
    |--data/:Data storage module
    |   |--MRBrainS18/:MRBrainS18 dataset
    |	|   |--images/:source image named with patient ID
    |	|   |--labels/:mask named with patient ID
    |   |--train.txt: training data lists
    |   |--val.txt: validation data lists
    |--pretrain/:Pre-trained models storage module
    |--model.py: Network processing script
    |--setting.py: Parameter setting script
    |--train.py: MRBrainS18 training demo script
    |--test.py: MRBrainS18 testing demo script
    |--requirement.txt: Dependent library list
    |--README.md
  • Network structure parameter settings
Model name   : parameters settings
resnet_10.pth: --model resnet --model_depth 10 --resnet_shortcut B
resnet_18.pth: --model resnet --model_depth 18 --resnet_shortcut A
resnet_34.pth: --model resnet --model_depth 34 --resnet_shortcut A
resnet_50.pth: --model resnet --model_depth 50 --resnet_shortcut B
resnet_101.pth: --model resnet --model_depth 101 --resnet_shortcut B
resnet_152.pth: --model resnet --model_depth 152 --resnet_shortcut B
resnet_200.pth: --model resnet --model_depth 200 --resnet_shortcut B
  • After successfully completing basic installation, you'll be ready to run the demo.
  1. Clone the MedicalNet repository
git clone https://github.com/Tencent/MedicalNet
  1. Download data & pre-trained models (Google Drive or Tencent Weiyun)

    Unzip and move files

mv MedicalNet_pytorch_files.zip MedicalNet/.
cd MedicalNet
unzip MedicalNet_pytorch_files.zip
  1. Run the training code (e.g. 3D-ResNet-50)
python train.py --gpu_id 0 1    # multi-gpu training on gpu 0,1
or
python train.py --gpu_id 0    # single-gpu training on gpu 0
  1. Run the testing code (e.g. 3D-ResNet-50)
python test.py --gpu_id 0 --resume_path trails/models/resnet_50_epoch_110_batch_0.pth.tar --img_list data/val.txt

Experiments

  • Computational Cost
GPU:NVIDIA Tesla P40
Network Paramerers (M) Running time (s)
3D-ResNet10 14.36 0.18
3D-ResNet18 32.99 0.19
3D-ResNet34 63.31 0.22
3D-ResNet50 46.21 0.21
3D-ResNet101 85.31 0.29
3D-ResNet152 117.51 0.34
3D-ResNet200 126.74 0.45
  • Performance
Visualization of the segmentation results of our approach vs. the comparison ones after the same training epochs. 
It has demonstrated that the efficiency for training convergence and accuracy based on our MedicalNet pre-trained models.

Results of transfer MedicalNet pre-trained models to lung segmentation (LungSeg) and pulmonary nodule classification (NoduleCls) with Dice and accuracy evaluation metrics, respectively.
Network Pretrain LungSeg(Dice) NoduleCls(accuracy)
3D-ResNet10 Train from scratch 71.30% 79.80%
MedicalNet 87.16% 86.87%
3D-ResNet18 Train from scratch 75.22% 80.80%
MedicalNet 87.26% 88.89%
3D-ResNet34 Train from scratch 76.82% 83.84%
MedicalNet 89.31% 89.90%
3D-ResNet50 Train from scratch 71.75% 84.85%
MedicalNet 93.31% 89.90%
3D-ResNet101 Train from scratch 72.10% 81.82%
MedicalNet 92.79% 90.91%
3D-ResNet152 Train from scratch 73.29% 73.74%
MedicalNet 92.33% 90.91%
3D-ResNet200 Train from scratch 71.29% 76.77%
MedicalNet 92.06% 90.91%

TODO

  • 3D-ResNet series pre-trained models
  • Transfer learning training code
  • Training with multi-gpu
  • 3D efficient pre-trained models(e.g., 3D-MobileNet, 3D-ShuffleNet)
  • 2D medical pre-trained models
  • Pre-trained MedicalNet models based on more medical dataset

Acknowledgement

We thank 3D-ResNets-PyTorch and MRBrainS18 which we build MedicalNet refer to this releasing code and the dataset.

Contribution

If you want to contribute to MedicalNet, be sure to review the contribution guidelines

medicalnet's People

Contributors

cshwhale 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  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

medicalnet's Issues

Question about multi-modality medical image.

Dear author:
Thank you for your solid research, we have benefited from the project a lot.
We know that in many cases, the medical image dataset always has many modalities (or channels) like brats2018, etc. My question is how to convert the single modality data pre-train weight to a multi-modality pre-train? Is that a good way to replace the input channel of the first convolution layer, and fine-tune the network?
Thanks, looking forward to your reply.

Google download of pre-trained network download requires access request

Not sure if it is intentional but when I click on the pre-trained network link to download the data from Google it says that I have to request access. This is based on the link in the new README as of 4 days ago. Should that link be open publicly?

The old link gets further but comes to
image

Do you have a pre-trained model with stride (4, 8, 16, 32)?

I find the pre-trained resnet model's stride is (4, 16, 16, 16). But i want to detect cancer by detection rather than segmentation.So i want to a (4, 8, 16, 32) stride model to build a standard FPN backbone. Because the parameters of the pre-trained model are fixed, I can't modify the base layers in the network. Do you have this type of pre-trained model there? Think you very much

Missing key(s) in state_dict error when testing the pretrained models

Hi,

thank you for sharing your excellent work!

I wanna test your pre-trained lung segmentation model (this release) with my CT data, and I encountered the following error when running "python test.py --gpu_id 0 --resume_path pretrain/resnet_50_23dataset.pth --img_list data/val.txt":

...
loading pretrained model pretrain/resnet_50_23dataset.pth
Traceback (most recent call last):
  File "test.py", line 90, in <module>
    net.load_state_dict(checkpoint['state_dict'])
  File "_<my_conda_dir>_/envs/torch3.7.0/lib/python3.7/site-packages/torch/nn/modules/module.py", line 719, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for DataParallel:
        Missing key(s) in state_dict: "module.conv_seg.0.weight", "module.conv_seg.0.bias", "module.conv_seg.1.weight", "module.conv_seg.1.bias", "module.conv_seg.1.running_mean", "module.conv_seg.1.running_var", "module.conv_seg.3.weight", "module.conv_seg.4.weight", "module.conv_seg.4.bias", "module.conv_seg.4.running_mean", "module.conv_seg.4.running_var", "module.conv_seg.6.weight".

The same error persists when I replace "--resume_path pretrain/resnet_50_23dataset.pth" with "--resume_path pretrain/resnet_50.pth". It does not work by adding "--model resnet --model_depth 50 --resnet_shortcut B".

However, it works when I use another pre-trained model instead: "--resume_path trails/resnet_50_epoch_200_batch_0.pth.tar".

Would appreciate your advice on how to make the pre-trained models work. My environment:

  • Python 3.7.0
  • PyTorch-0.4.1 (installed by conda)
  • CUDA Version 9.0 (conda package)

Thanks!
Wei

Will the train code in LIDC dataset be released later?

I'd like to transfer the pretrained model to a tumor classification task.

So i am wondering if there is any Global Average Pooling layers? for there are two many feature maps in the last residual module (512 in resnet 34 and 1024 in resnet 50). I am concerned about overfitting.

Assertion is always true, perhaps remove parentheses?

flake8 testing of https://github.com/Tencent/MedicalNet on Python 3.7.1

$ flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics

./datasets/brains18.py:58:13: F631 assertion is always true, perhaps remove parentheses?
            assert(img_array.shape ==  mask_array.shape, "img shape:{} is not equal to mask shape:{}".format(img_array.shape, mask_array.shape))
            ^
1     F631 assertion is always true, perhaps remove parentheses?
1

Question about the data dimension

Thanks for providing the code and this is really a great work.

I have one question about the data dimension in the code, which confuses me.
The get_data function in nibabel exports the data in [x, y, z] and from your code it looks like the depth corresponds to the z-dimension. However, in the resize function in the dataloader brains18.py, it maps the depth to the first element of the data.shape.

def __resize_data__(self, data):
    """
    Resize the data to the input size
    """ 
    [depth, height, width] = data.shape
    scale = [self.input_D*1.0/depth, self.input_H*1.0/height, self.input_W*1.0/width]  

Can you clarify a little bit here, or am I mis-understand something?

Test after training leads to 0.0 dice

I trained my model for 2000 epochs. Loss value decreased with every batch. However when i use test.py with the same training data, i obtain 0.0 dice.

mean dice for class-1 is 0.0
mean dice for class-2 is 0.0

What could be the reason for this?

question about co-training models

In your paper. you mentioned the networks co-trained in 3DSeg-8 dataset, including Brats. Would like lease more detail about co-training? In other words, Brats dataset contains T1, T2, T1c, Flair (4 different weighted sequences) for each patient. And the annotation is also based on all 4 sequences. Therefore, the input is supposed to be 4 channels. However, the input is usually only one channel for CT dataset. So, how did you deal with this situation? re-initializing first conv layer?

train on custom data

Is it possible to train on custom data?

Data format is image.nii.gz, segmentation.nii.gz

About brats dataset

Brats dataset (reference 19 in your paper) is a multimodal dataset. How do you convert from the four modalities to a single modality (one single channel)?

Will you release the 3D VGG pre-trained model later?

VGG is essential for computing perceptual loss. A pre-trained VGG model would helpful for synthesis task, e.g. providing an efficient evaluation or guiding the image enhancement.

P.S. Can you tell me which tool could generate such fancy gif images in your project?

When will you release the 2D pretrained model?

It is a great work that you release these wonderful pretrained models. But mostly the medical images we can get in touch with are 2D images and I have seen you listed "2D medical pre-trained model" in the to-do list. So when will you release these models and could you please give us more information about how you trained these 2D models? Thanks a lot!

Some minor typos in the paper...

Hi, thanks for your awesome work!
But there are some minor typos in the paper...

  • In Section3.3,

屏幕快照 2019-07-29 16 19 51

It should be "except"...

  • In Section4.1,

屏幕快照 2019-07-29 16 21 41

It should be "Fig.4"...

  • In Section4.2,

屏幕快照 2019-07-29 16 23 07

屏幕快照 2019-07-29 16 23 35

There are some repeated sentences...

question about test, thanks you

(MedicalNet) xinyu@xinyu-MS-7B89:~/MedicalNet-master$ python test.py --gpu_id 0 --resume_path trails/models/resnet_50_epoch_110_batch_0.pth.tar --img_list data/val.txt
/home/xinyu/MedicalNet-master/models/resnet.py:173: UserWarning: nn.init.kaiming_normal is now deprecated in favor of nn.init.kaiming_normal_.
m.weight = nn.init.kaiming_normal(m.weight, mode='fan_out')
Processing 2 datas
./data/MRBrainS18/images/070.nii.gz
./data/MRBrainS18/images/1.nii.gz
THCudaCheck FAIL file=/pytorch/aten/src/THC/THCGeneral.cpp line=383 error=11 : invalid argument
Traceback (most recent call last):
File "test.py", line 92, in
masks = test(data_loader, net, img_names, sets)
File "test.py", line 67, in test
mask = ndimage.interpolation.zoom(mask, scale, order=1)
File "/home/xinyu/anaconda3/lib/python3.6/site-packages/scipy/ndimage/interpolation.py", line 591, in zoom
input = numpy.asarray(input)
File "/home/xinyu/anaconda3/lib/python3.6/site-packages/numpy/core/_asarray.py", line 85, in asarray
return array(a, dtype, copy=False, order=order)
File "/home/xinyu/anaconda3/lib/python3.6/site-packages/torch/tensor.py", line 458, in array
return self.numpy()
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

你好,我在测试的时候出现了一个问题,请问您知道这是为什么吗

(MedicalNet) xinyu@xinyu-MS-7B89:~/MedicalNet-master$ python test.py --gpu_id 0 --resume_path trails/models/resnet_50_epoch_199_batch_0.pth.tar --img_list data/val.txt
/home/xinyu/MedicalNet-master/models/resnet.py:173: UserWarning: nn.init.kaiming_normal is now deprecated in favor of nn.init.kaiming_normal_.
m.weight = nn.init.kaiming_normal(m.weight, mode='fan_out')
Processing 1 datas
/home/xinyu/MedicalNet-master/data/MRBrainS18/images/imaging4.nii.gz
THCudaCheck FAIL file=/pytorch/aten/src/THC/THCGeneral.cpp line=383 error=11 : invalid argument
Traceback (most recent call last):
File "test.py", line 92, in
masks = test(data_loader, net, img_names, sets)
File "test.py", line 67, in test
mask = ndimage.interpolation.zoom(mask, scale, order=1)
File "/home/xinyu/anaconda3/lib/python3.6/site-packages/scipy/ndimage/interpolation.py", line 591, in zoom
input = numpy.asarray(input)
File "/home/xinyu/anaconda3/lib/python3.6/site-packages/numpy/core/_asarray.py", line 85, in asarray
return array(a, dtype, copy=False, order=order)
File "/home/xinyu/anaconda3/lib/python3.6/site-packages/torch/tensor.py", line 458, in array
return self.numpy()
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

Maybe an error in the code?

image

Hi, I use your dataloader to load the BrainS18 dataset, and I found that maybe an error ? as shown above, the input data's shape is HWD, but the resize scale is computed by DHW ?

question about dice

Hi,
As the complexity of the model increases, the dice coefficient decreases using more datasets (23 datasets) pretrained model.
image
However using other datasets (8 datasets?) pretrained model, as the complexity of the model increases, the dice coefficient increase .
image
This seems too strange,can you give more details or some Explanation?

question about loss function

Hi, I'm confused about the loss function in the train phase.The loss function you have used is cross entropy Loss and the tensors inputted are different shape.Maybe something wrong? Meanwhile i found others have encountered the same problem that net perform well in the train phase,it's loss value is small,but dice value in the test phase maybe 0.

cuda error: out of memory

could you please tell me how many graphic memories are required? can i use my 6gb memories to run this project

Why lr *100 when load pretrained model?

Thank you for your work.

I wonder why lr*100 and i have found the rate of convergence could get higher with original lr. Thanks a lot.

{ 'params': parameters['base_parameters'], 'lr': sets.learning_rate },
{ 'params': parameters['new_parameters'], 'lr': sets.learning_rate*100 }

Mean dice coming out to be 0

Hey i have trained Medical Net on custom data, but even though the loss approaches 0,When i test the mean dice for every class is 0(there are 3 total classes).

But this result is only after i trained for 4 epochs.

Any suggestions?

Thanks in advance.

Question: is it wrong to calculate the variable 'avg_batch_time'

train.py:

batch_id_sp = epoch * batches_per_epoch
...
avg_batch_time = (time.time() - train_time_sp) / (1 + batch_id_sp)

'1 + batch_id_sp' is not the number of batches that have completed the training at present..

Yeah, it won't affect the training process, but the real-time training information from 'log.INFO()' is something wrong. At the first epoch, it's easy to find that's wrong info because 'avg_batch_time' is calculated with the wrong batches '1+batch_id_sp'. The correct variable of batches is '1+batch_id_sp+batch_id'.

Another question:
image
As above, I see the author updated the file three days before, but the variables of epoch are different when it's used to save the model's paramerters and load them later. ---> 'ecpoch' & 'epoch'..

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.