Skip to content

Instantly share code, notes, and snippets.

@mauigna06
Last active February 7, 2025 16:32
Show Gist options
  • Select an option

  • Save mauigna06/133567c0e8c9134920bf737d6d2608bb to your computer and use it in GitHub Desktop.

Select an option

Save mauigna06/133567c0e8c9134920bf737d6d2608bb to your computer and use it in GitHub Desktop.
parenchyma of the kidney, kidney neoplasms, kidney arteries, kidney veins, kidney ureters
{
"imports": [
"$import glob",
"$import os"
],
"bundle_root": ".",
"ckpt_dir": "$@bundle_root + '/models'",
"output_dir": "$@bundle_root + '/eval'",
"dataset_dir": "$@bundle_root + '/data'",
"images": "$[{'artery':a, 'vein':b, 'excret':c }for a,b,c in zip(glob.glob(@dataset_dir + '/*/12.nii.gz'), glob.glob(@dataset_dir + '/*/22-.nii.gz'), glob.glob(@dataset_dir + '/*/32-.nii.gz'))]",
"labels": "$[]",
"device": "$torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')",
"network_def": {
"_target_": "SegResNet",
"in_channels": 3,
"out_channels": 6,
"init_filters": 32,
"upsample_mode": "deconv",
"dropout_prob": 0.2,
"norm_name": "group",
"blocks_down": [
1,
2,
2,
4
],
"blocks_up": [
1,
1,
1
]
},
"network": "$@network_def.to(@device)",
"preprocessing": {
"_target_": "Compose",
"transforms": [
{
"_target_": "LoadImaged",
"keys": [
"artery",
"vein",
"excret"
],
"image_only": false
},
{
"_target_": "EnsureChannelFirstd",
"keys": [
"artery",
"vein",
"excret"
]
},
{
"_target_": "Orientationd",
"keys": [
"artery",
"vein",
"excret"
],
"axcodes": "LPS"
},
{
"_target_": "Spacingd",
"keys": [
"artery",
"vein",
"excret"
],
"pixdim": [
0.8,
0.8,
0.8
],
"mode": "bilinear"
},
{
"_target_": "scripts.my_transforms.ConcatImages",
"keys_merge": [
"artery",
"vein",
"excret"
],
"keys_out": "image"
},
{
"_target_": "ScaleIntensityRanged",
"keys": "image",
"a_min": -1000,
"a_max": 1000,
"b_min": 0.0,
"b_max": 1.0,
"clip": true
},
{
"_target_": "EnsureTyped",
"keys": "image"
}
]
},
"dataset": {
"_target_": "Dataset",
"data": "@images",
"transform": "@preprocessing"
},
"dataloader": {
"_target_": "DataLoader",
"dataset": "@dataset",
"batch_size": 1,
"shuffle": false,
"num_workers": 4
},
"inferer": {
"_target_": "SlidingWindowInferer",
"roi_size": [
96,
96,
96
],
"sw_batch_size": 4,
"overlap": 0.25
},
"postprocessing": {
"_target_": "Compose",
"transforms": [
{
"_target_": "Invertd",
"transform": "$@preprocessing",
"device": "@device",
"keys": "pred",
"orig_keys": "artery",
"meta_keys": "pred_meta_dict",
"nearest_interp": false,
"to_tensor": true
},
{
"_target_": "Activationsd",
"keys": "pred",
"softmax": false,
"sigmoid": true
},
{
"_target_": "AsDiscreted",
"keys": "pred",
"threshold": 0.5
},
{
"_target_": "scripts.my_transforms.MergeClassesd",
"keys": "pred"
},
{
"_target_": "SaveImaged",
"keys": "pred",
"meta_keys": "pred_meta_dict",
"data_root_dir": "@dataset_dir",
"output_dir": "@output_dir"
}
]
},
"handlers": [
{
"_target_": "CheckpointLoader",
"load_path": "$@ckpt_dir + '/model.pt'",
"load_dict": {
"model": "@network"
},
"strict": "True"
},
{
"_target_": "StatsHandler",
"iteration_log": false
}
],
"evaluator": {
"_target_": "SupervisedEvaluator",
"device": "@device",
"val_data_loader": "@dataloader",
"network": "@network",
"inferer": "@inferer",
"postprocessing": "@postprocessing",
"val_handlers": "@handlers",
"amp": false
},
"inference": [
"$setattr(torch.backends.cudnn, 'benchmark', True)",
"$@evaluator.run()"
]
}
#--------------------------------
# commands on this part need to be executed one by one
curl -fsSL https://pyenv.run | bash
# execute the following line by line
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc
touch ~/.bash_profile
# execute the following line by line
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init - bash)"' >> ~/.bash_profile
exec "$SHELL" # you need to restart the shell
#--------------------------------
MYMONAIDIR=/home/$USER/my_monai
MYPYENVNAME=kidney_bundle2
MYPYENVVERSION=3.10.12
pyenv install $MYPYENVVERSION
pyenv virtualenv $MYPYENVVERSION $MYPYENVNAME
mkdir -p $MYMONAIDIR
cd $MYMONAIDIR
pyenv activate $MYPYENVNAME
# pyenv local $MYPYENVNAME # this line does not work
pip install "monai[all]"
python -m monai.bundle download "renalStructures_CECT_segmentation" --bundle_dir "bundles/"
cd bundles/renalStructures_CECT_segmentation
python -m monai.bundle run download_data --meta_file configs/metadata.json --config_file "['configs/train.json', 'configs/evaluate.json']"
rm -f ./data/*/merged.nii.gz
# please overwrite the configs/inference.json with the other file in this gist
python -m monai.bundle run inference --meta_file configs/metadata.json --config_file configs/inference.json
# check segmentation results on eval folder
@mauigna06

Copy link
Copy Markdown
Author

Ok. Now it should work for batch inference.

Please have in mind that you need to achieve a folder structure like this for inference:

    "images": [
        {"artery": "./data/patient1/12.nii.gz", "vein": "./data/patient1/22-.nii.gz", "excret": "./data/patient1/32-.nii.gz"},
        {"artery": "./data/patient2/12.nii.gz", "vein": "./data/patient2/22-.nii.gz", "excret": "./data/patient2/32-.nii.gz"},
        {"artery": "./data/patient3/12.nii.gz", "vein": "./data/patient3/22-.nii.gz", "excret": "./data/patient3/32-.nii.gz"}
    ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment