GithubHelp home page GithubHelp logo

pymic_examples's Introduction

PyMIC_examples

PyMIC is a PyTorch-based toolkit for medical image computing with annotation-efficient deep learning. Here we provide a set of examples to show how it can be used for image classification and segmentation tasks. For annotation efficient learning, we show examples of Semi-Supervised Learning (SSL), Weakly Supervised Learning (WSL) and Noisy Label Learning (NLL), respectively. For beginners, you can follow the examples by just editting the configuration files for model training, testing and evaluation. For advanced users, you can easily develop your own modules, such as customized networks and loss functions.

Install PyMIC

The released version of PyMIC (v0.4.0) is required for these examples, and it can be installed by:

pip install PYMIC==0.4.0

To use the latest development version, you can download the source code here, and install it by:

python setup.py install

Data

The datasets for the examples can be downloaded from Google Drive or Baidu Disk (extraction code: xlwg). Extract the files to PyMIC_data after downloading.

List of Examples

Currently we provide the following examples in this repository:

Catetory Example Remarks
Classification AntBee Finetuning a resnet18 for Ant and Bee classification
Classification CHNCXR Finetuning restnet18 and vgg16 for normal/tuberculosis X-ray image classification
Fully supervised segmentation JSRT Using a 2D UNet for lung segmentation from chest X-ray images
Fully supervised segmentation JSRT2 Using a customized network and loss function for the JSRT dataset
Fully supervised segmentation Fetal_HC Using a 2D UNet for fetal head segmentation from 2D ultrasound images
Fully supervised segmentation Prostate Using a 3D UNet for prostate segmentation from 3D MRI
Semi-supervised segmentation seg_ssl/ACDC Semi-supervised methods for heart structure segmentation using 2D CNNs
Semi-supervised segmentation seg_ssl/AtriaSeg Semi-supervised methods for left atrial segmentation using 3D CNNs
Weakly-supervised segmentation seg_wsl/ACDC Segmentation of heart structure with scrible annotations
Noisy label learning seg_nll/JSRT Comparing different NLL methods for learning from noisy labels

Useful links

pymic_examples's People

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

Watchers

 avatar  avatar

pymic_examples's Issues

a doubt about using CopleNet in PyMIC

Hi, thanks for your sharing for the excellent work!

I have read the PYMIC paper and the CopleNet paper, and have a question here:
Can I use CopleNet in PyMIC to train 3D medical image segmentation with noisy labels , which 3D Volume is processed directly instead of using 2D slices ?

Looking forward to your reply.
Best wishes.

Why 'post_process = KeepLargestComponent' didn't work in testing process.

I use Unet2d to segment Myo in CMR. I want to keep the largest region. But it did't work in testing process.
Here is my train_val.cfg and test.cfg files.

[dataset]
# tensor type (float or double)
tensor_type = float
task_type = seg
supervise_type = fully_sup
root_dir  = MyoPS_data_dir/data_preprocessed
train_csv = config/data/foldi_train.csv
valid_csv = config/data/foldi_valid.csv
test_csv  = config/data/foldi_valid.csv
modal_num = 1
# batch size
train_batch_size = 18
num_workder = 8

# data transforms
train_transform = [Pad, RandomRotate, RandomCrop, RandomFlip, NormalizeWithMeanStd, GammaCorrection, GaussianNoise, LabelConvert, LabelToProbability]
valid_transform = [NormalizeWithMeanStd, Pad, LabelConvert, LabelToProbability]
test_transform  = [NormalizeWithMeanStd, Pad]

Pad_output_size = [8, 256, 256]
Pad_ceil_mode   = False

RandomRotate_angle_range_d = [-30, 30]
RandomRotate_angle_range_h = None
RandomRotate_angle_range_w = None

RandomCrop_output_size = [6, 224, 224]
RandomCrop_foreground_focus = False
RandomCrop_foreground_ratio = None
Randomcrop_mask_label       = None

RandomFlip_flip_depth  = False
RandomFlip_flip_height = True
RandomFlip_flip_width  = True

NormalizeWithMeanStd_channels = [0]

GammaCorrection_channels  = [0]
GammaCorrection_gamma_min = 0.7
GammaCorrection_gamma_max = 1.5

GaussianNoise_channels = [0]
GaussianNoise_mean     = 0
GaussianNoise_std      = 0.05
GaussianNoise_probability = 0.5

LabelConvert_source_list = [0,1,2]
LabelConvert_target_list = [0,1,1]

LabelToProbability_class_num = 2

[network]
# this section gives parameters for network
# the keys may be different for different networks

# type of network
net_type = UNet2D

# number of class, required for segmentation task
class_num     = 2
in_chns       = 1
feature_chns  = [16, 32, 64, 128, 256]
dropout       = [0, 0, 0.5, 0.5, 0.5]
bilinear      = True
multiscale_pred = False

[training]
# list of gpus
gpus        = [0]

loss_type   = [CrossEntropyLoss, DiceLoss]
loss_weight = [1.0, 1.0]

# for optimizers
optimizer     = Adam
learning_rate = 1e-3
momentum      = 0.9
weight_decay  = 1e-5

# for lr schedular (MultiStepLR)
lr_scheduler  = MultiStepLR
lr_gamma      = 0.5
lr_milestones = [3000, 6000, 9000, 12000]

ckpt_save_dir = model/unet2d/fold_i
ckpt_save_prefix = model

# start iter
iter_start = 0
iter_max   = 16000
iter_valid = 100
iter_save  = [4000, 8000, 12000, 15000, 15200, 15400, 15600, 15800, 16000]

[testing]
# gpu
gpus = [0]
# checkpoint mode can be [0-latest, 1-best, 2-specified]
ckpt_mode         = 1
output_dir        = result/unet2d
post_process      = KeepLargestComponent

sliding_window_enable = True
sliding_window_size   = [6, 224, 224]
sliding_window_stride = [6, 224, 224]
[dataset]
# tensor type (float or double)
tensor_type = float

task_type = seg
root_dir  = projects/RenJi811/data_raw
test_csv  = config/data/data_test.csv

# modality number
modal_num = 1

# data transforms
test_transform  = [NormalizeWithMeanStd, Pad]

NormalizeWithMeanStd_channels = [0]
Pad_output_size = [6,244,244]

[network]
# this section gives parameters for network
# the keys may be different for different networks

# type of network
net_type = UNet2D

# number of class, required for segmentation task
class_num     = 2
in_chns       = 1
feature_chns  = [16, 32, 64, 128, 256]
dropout       = [0, 0, 0.5, 0.5, 0.5]
bilinear      = True
multiscale_pred = False

[training]
# nothing here

[testing]
# list of gpus
gpus   = [0]

# checkpoint mode can be [0-latest, 1-best, 2-specified, 3-multiple ckpt ensemble]
ckpt_mode         = 3
ckpt_name         = [model/unet2d/fold_1/fold_1_15000.pt, model/unet2d/fold_2/fold_2_13300.pt, model/unet2d/fold_3/fold_3_13500.pt, model/unet2d/fold_4/fold_4_12900.pt, model/unet2d/fold_5/fold_5_12500.pt]
output_dir        = result/unet2d_test/

post_process = KeepLargestComponent
KeepLargestComponent_mode = 2

label_source  = [0,1]
label_target  = [0,3]

sliding_window_enable = True
sliding_window_size   = [6, 224, 224]
sliding_window_stride = [6, 224, 224]

How to set number_workers?

Hello, teacher! During implementation, the dataloader defaults to number_ workers is 16, which cannot be run on windows. I want to know how to change the NW value. Thank you

a problem about training

Thanks for your work, I need your help.
I just follow readme, but there is a problem when running pymic_train config/unet.cfg
I didn't modify any configuration or code, there is stack:

Traceback (most recent call last): File "\\?\C:\Users\91119\anaconda3\envs\krl\Scripts\pymic_train-script.py", line 33, in <module> sys.exit(load_entry_point('PYMIC==0.4.0', 'console_scripts', 'pymic_train')()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\91119\anaconda3\envs\krl\Lib\site-packages\pymic-0.4.0-py3.11.egg\pymic\net_run\train.py", line 103, in main File "C:\Users\91119\anaconda3\envs\krl\Lib\site-packages\pymic-0.4.0-py3.11.egg\pymic\net_run\agent_abstract.py", line 316, in run File "C:\Users\91119\anaconda3\envs\krl\Lib\site-packages\pymic-0.4.0-py3.11.egg\pymic\net_run\agent_seg.py", line 333, in train_valid File "C:\Users\91119\anaconda3\envs\krl\Lib\site-packages\torch\utils\data\dataloader.py", line 438, in __iter__ return self._get_iterator() ^^^^^^^^^^^^^^^^^^^^ File "C:\Users\91119\anaconda3\envs\krl\Lib\site-packages\torch\utils\data\dataloader.py", line 386, in _get_iterator return _MultiProcessingDataLoaderIter(self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\91119\anaconda3\envs\krl\Lib\site-packages\torch\utils\data\dataloader.py", line 1039, in __init__ w.start() ^^^^^^^^^^^^^^^^^^ File "C:\Users\91119\anaconda3\envs\krl\Lib\multiprocessing\popen_spawn_win32.py", line 94, in __init__ reduction.dump(process_obj, to_child) File "C:\Users\91119\anaconda3\envs\krl\Lib\multiprocessing\reduction.py", line 60, in dump ForkingPickler(file, protocol).dump(obj) AttributeError: Can't pickle local object 'NetRunAgent.create_dataset.<locals>.worker_init_fn' Traceback (most recent call last): File "<string>", line 1, in <module> File "C:\Users\91119\anaconda3\envs\krl\Lib\multiprocessing\spawn.py", line 122, in spawn_main exitcode = _main(fd, parent_sentinel) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\91119\anaconda3\envs\krl\Lib\multiprocessing\spawn.py", line 132, in _main self = reduction.pickle.load(from_parent) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EOFError: Ran out of input

How to train

First of all, thank you for your work, and secondly, I would like to ask how to solve this error during training。

Data preprocess

This is impressive work!!! I want to run an example for semi-superior segmentation, however I can't find any preprocessed data in this repository. How can I process my own data for this work?

About training my own data

Really exciting work! I would like to know how to use your PyMIC to complete the semi-supervised learning of my own CT data, can you make a demo?

About the data

Hello, teacher! I saw the readme mentioned that the data is under the PyMIC_data/ACDC/preprocess directory, but I didn't find it. Can you provide a download link? Thank you.

KeyError: 'gaussiannoise_channels'

Hi, I was trying to follow the semi-supervised example (dataset AtriaSeg). When I ran the following command I got an error.

 pymic_train config/unet3d_r10_em.cfg
torch.backends.cuda.matmul.allow_tf32 = True by default.
  This value defaults to True when PyTorch version in [1.7, 1.11] and may affect precision.
  See https://docs.monai.io/en/latest/precision_accelerating.html#precision-and-accelerating
dataset tensor_type float
dataset task_type seg
dataset supervise_type semi_sup
dataset root_dir ../../PyMIC_data/AtriaSeg/TrainingSet_crop/
dataset train_csv config/data/image_train_r10_lab.csv
dataset train_csv_unlab config/data/image_train_r10_unlab.csv
dataset valid_csv config/data/image_valid.csv
dataset test_csv config/data/image_test.csv
dataset train_batch_size 2
dataset train_batch_size_unlab 2
dataset train_transform ['RandomCrop', 'RandomFlip', 'NormalizeWithMeanStd', 'GammaCorrection', 'GaussianNoise', 'LabelToProbability']

dataset train_transform_unlab ['RandomCrop', 'RandomFlip', 'NormalizeWithMeanStd', 'GammaCorrection', 'GaussianNoise']
dataset valid_transform ['NormalizeWithMeanStd', 'LabelToProbability']
dataset test_transform ['NormalizeWithMeanStd']
dataset randomcrop_output_size [72, 96, 112]
dataset randomcrop_foreground_focus False
dataset randomcrop_foreground_ratio None
dataset randomcrop_mask_label None
dataset randomflip_flip_depth False
dataset randomflip_flip_height True
dataset randomflip_flip_width True
dataset normalizewithmeanstd_channels [0]
dataset gammacorrection_channels [0]
dataset gammacorrection_gamma_min 0.7
dataset gammacorrection_gamma_max 1.5
network net_type UNet3D
network class_num 2
network in_chns 1
network feature_chns [32, 64, 128, 256]
network dropout [0.0, 0.0, 0.5, 0.5]
network trilinear True
network multiscale_pred False
training gpus [1]
training loss_type ['DiceLoss', 'CrossEntropyLoss']
training loss_weight [0.5, 0.5]
training optimizer Adam
training learning_rate 0.001
training momentum 0.9
training weight_decay 1e-05
training lr_scheduler ReduceLROnPlateau
training lr_gamma 0.5
training reducelronplateau_patience 2000
training early_stop_patience 5000
training ckpt_save_dir model/unet3d_r10_em
training iter_max 20000
training iter_valid 100
training iter_save [1000, 20000]
semi_supervised_learning method_name EntropyMinimization
semi_supervised_learning regularize_w 0.1
semi_supervised_learning rampup_start 1000
semi_supervised_learning rampup_end 15000
testing gpus [1]
testing ckpt_mode 1
testing output_dir result/unet3d_r10_em
testing post_process None
testing sliding_window_enable False
dataset tensor_type = float
dataset task_type = seg
dataset supervise_type = semi_sup
dataset root_dir = ../../PyMIC_data/AtriaSeg/TrainingSet_crop/
dataset train_csv = config/data/image_train_r10_lab.csv
dataset train_csv_unlab = config/data/image_train_r10_unlab.csv
dataset valid_csv = config/data/image_valid.csv
dataset test_csv = config/data/image_test.csv
dataset train_batch_size = 2
dataset train_batch_size_unlab = 2
dataset train_transform = ['RandomCrop', 'RandomFlip', 'NormalizeWithMeanStd', 'GammaCorrection', 'GaussianNoise', 'LabelToProbability']
dataset train_transform_unlab = ['RandomCrop', 'RandomFlip', 'NormalizeWithMeanStd', 'GammaCorrection', 'GaussianNoise']
dataset valid_transform = ['NormalizeWithMeanStd', 'LabelToProbability']
dataset test_transform = ['NormalizeWithMeanStd']
dataset randomcrop_output_size = [72, 96, 112]
dataset randomcrop_foreground_focus = False
dataset randomcrop_foreground_ratio = None
dataset randomcrop_mask_label = None
dataset randomflip_flip_depth = False
dataset randomflip_flip_height = True
dataset randomflip_flip_width = True
dataset normalizewithmeanstd_channels = [0]
dataset gammacorrection_channels = [0]
dataset gammacorrection_gamma_min = 0.7
dataset gammacorrection_gamma_max = 1.5
dataset labeltoprobability_class_num = 2
network net_type = UNet3D
network class_num = 2
network in_chns = 1
network feature_chns = [32, 64, 128, 256]
network dropout = [0.0, 0.0, 0.5, 0.5]
network trilinear = True
network multiscale_pred = False
training gpus = [1]
training loss_type = ['DiceLoss', 'CrossEntropyLoss']
training loss_weight = [0.5, 0.5]
training optimizer = Adam
training learning_rate = 0.001
training momentum = 0.9
training weight_decay = 1e-05
training lr_scheduler = ReduceLROnPlateau
training lr_gamma = 0.5
training reducelronplateau_patience = 2000
training early_stop_patience = 5000
training ckpt_save_dir = model/unet3d_r10_em
training iter_max = 20000
training iter_valid = 100
training iter_save = [1000, 20000]
semi_supervised_learning method_name = EntropyMinimization
semi_supervised_learning regularize_w = 0.1
semi_supervised_learning rampup_start = 1000
semi_supervised_learning rampup_end = 15000
testing gpus = [1]
testing ckpt_mode = 1
testing output_dir = result/unet3d_r10_em
testing post_process = None
testing sliding_window_enable = False

********** Semi Supervised Learning **********

deterministric is true
Traceback (most recent call last):
  File "/gpu_home/bori/miniconda3/envs/pymic3/bin/pymic_train", line 8, in <module>
    sys.exit(main())
  File "/gpu_home/bori/miniconda3/envs/pymic3/lib/python3.9/site-packages/pymic/net_run/train.py", line 95, in main
    agent.run()
  File "/gpu_home/bori/miniconda3/envs/pymic3/lib/python3.9/site-packages/pymic/net_run/agent_abstract.py", line 311, in run
    self.create_dataset()
  File "/gpu_home/bori/miniconda3/envs/pymic3/lib/python3.9/site-packages/pymic/net_run/semi_sup/ssl_abstract.py", line 64, in create_dataset
    super(SSLSegAgent, self).create_dataset()
  File "/gpu_home/bori/miniconda3/envs/pymic3/lib/python3.9/site-packages/pymic/net_run/agent_abstract.py", line 247, in create_dataset
    self.train_set = self.get_stage_dataset_from_config('train')
  File "/gpu_home/bori/miniconda3/envs/pymic3/lib/python3.9/site-packages/pymic/net_run/agent_seg.py", line 61, in get_stage_dataset_from_config
    one_transform = self.transform_dict[name](transform_param)
  File "/gpu_home/bori/miniconda3/envs/pymic3/lib/python3.9/site-packages/pymic/transform/intensity.py", line 103, in __init__
    self.channels = params['GaussianNoise_channels'.lower()]
KeyError: 'gaussiannoise_channels'

I had to updgrade pytorch to a newer one due to my cuda version 11.6. Not it is:
torch==1.11.0+cu113
torchvision==0.12.0+cu113
pymic==0.4.0

and Python is 3.9

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.