Skip to content

Commit

Permalink
back to resample of nnunet + separete custom no resampling + rewrite …
Browse files Browse the repository at this point in the history
…scripts
  • Loading branch information
Roman Fitzjalen authored and Roman Fitzjalen committed Jun 19, 2024
1 parent 17705c3 commit 9b8642d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export nnUNet_results="/bluemind/nnunet/trains/nnUNet_results"
Use [generate_json](scripts/generate_json.ipynb) for this.

4. #### Use scripts for training.
There are several scripts written by me (Roma) for datgaset [preprocessing](scripts/preprocess_data.sh), [training](scripts/train.sh), [finetuning](scripts/finetune.sh) and [predicting](scripts/predict.py).
There are several scripts written by me (Roma) for datgaset [preprocessing](scripts/preprocess.sh), [training](scripts/train.sh), [finetuning](scripts/finetune.sh).

### READ NNUUNET [README](readme.md) FOR MORE INFORMATION
17 changes: 10 additions & 7 deletions configs/mosaic_planner.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@
"3d_fullres_no_resampling": {
"inherits_from": "3d_fullres",
"resampling_fn_data": "no_resampling_data_or_seg_to_shape",
"resampling_fn_data_kwargs": {},
"resampling_fn_seg": "no_resampling_data_or_seg_to_shape",
"resampling_fn_seg_kwargs": {},
"resampling_fn_probabilities": "no_resampling_data_or_seg_to_shape",
"data_identifier": "3d_fullres_no_resampling"
"resampling_fn_probabilities_kwargs": {},
"data_identifier": "nnUNetPlans_3d_fullres_NoRsmp"
},
"3d_fullres_mosaic_spacing_highres_NoRsmp": {
"3d_fullres_highres_NoRsmp": {
"inherits_from": "3d_fullres_no_resampling",
"spacing": [1.0, 1.0, 1.0],
"data_identifier": "3d_fullres_mosaic_spacing_highres_NoRsmp"
"data_identifier": "nnUNetPlans_3d_fullres_highres_NoRsmp"
},
"3d_fullres_mosaic_spacing_stdres_NoRsmp": {
"3d_fullres_stdres_NoRsmp": {
"inherits_from": "3d_fullres_no_resampling",
"spacing": [1.5, 1.5, 1.5],
"data_identifier": "3d_fullres_mosaic_spacing_stdres_NoRsmp"
"data_identifier": "nnUNetPlans_3d_fullres_stdres_NoRsmp"
},
"3d_fullres_mosaic_spacing_lowres_NoRsmp": {
"3d_fullres_lowres_NoRsmp": {
"inherits_from": "3d_fullres_no_resampling",
"spacing": [3.0, 3.0, 3.0],
"data_identifier": "3d_fullres_mosaic_spacing_lowres_NoRsmp"
"data_identifier": "nnUNetPlans_3d_fullres_lowres_NoRsmp"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -544,19 +544,19 @@ def plan_experiment(self):

plans['configurations']['3d_fullres_no_resampling'] = mosaic_planner['3d_fullres_no_resampling']

plans['configurations']['3d_fullres_mosaic_spacing_highres_NoRsmp'] = mosaic_planner['3d_fullres_mosaic_spacing_highres_NoRsmp']
plans['configurations']['3d_fullres_highres_NoRsmp'] = mosaic_planner['3d_fullres_highres_NoRsmp']
print('3D fullres Mosaic HighRes spacing No Resampling U-Net configuration:')
print(plans['configurations']['3d_fullres_mosaic_spacing_highres_NoRsmp'])
print(plans['configurations']['3d_fullres_highres_NoRsmp'])
print()

plans['configurations']['3d_fullres_mosaic_spacing_stdres_NoRsmp'] = mosaic_planner['3d_fullres_mosaic_spacing_stdres_NoRsmp']
plans['configurations']['3d_fullres_stdres_NoRsmp'] = mosaic_planner['3d_fullres_stdres_NoRsmp']
print('3D fullres Mosaic StdRes spacing No Resampling U-Net configuration:')
print(plans['configurations']['3d_fullres_mosaic_spacing_stdres_NoRsmp'])
print(plans['configurations']['3d_fullres_stdres_NoRsmp'])
print()

plans['configurations']['3d_fullres_mosaic_spacing_lowres_NoRsmp'] = mosaic_planner['3d_fullres_mosaic_spacing_lowres_NoRsmp']
plans['configurations']['3d_fullres_lowres_NoRsmp'] = mosaic_planner['3d_fullres_lowres_NoRsmp']
print('3D fullres Mosaic LowRes spacing No Resampling U-Net configuration:')
print(plans['configurations']['3d_fullres_mosaic_spacing_lowres_NoRsmp'])
print(plans['configurations']['3d_fullres_lowres_NoRsmp'])
print()

self.plans = plans
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,12 @@ def _keygen(patch_size, strides):
'spacing': spacing,
'normalization_schemes': normalization_schemes,
'use_mask_for_norm': mask_is_used_for_norm,
#### MOSAIC CHAGES ####
# 'resampling_fn_data': resampling_data.__name__,
# 'resampling_fn_seg': resampling_seg.__name__,
# 'resampling_fn_data_kwargs': resampling_data_kwargs,
# 'resampling_fn_seg_kwargs': resampling_seg_kwargs,
# 'resampling_fn_probabilities': resampling_softmax.__name__,
# 'resampling_fn_probabilities_kwargs': resampling_softmax_kwargs,
#### MOSAIC CHAGES ####
'resampling_fn_data': 'no_resampling_data_or_seg_to_shape',
'resampling_fn_seg': 'no_resampling_data_or_seg_to_shape',
'resampling_fn_data_kwargs': {},
'resampling_fn_seg_kwargs': {},
'resampling_fn_probabilities': 'no_resampling_data_or_seg_to_shape',
'resampling_fn_probabilities_kwargs': {},
'resampling_fn_data': resampling_data.__name__,
'resampling_fn_seg': resampling_seg.__name__,
'resampling_fn_data_kwargs': resampling_data_kwargs,
'resampling_fn_seg_kwargs': resampling_seg_kwargs,
'resampling_fn_probabilities': resampling_softmax.__name__,
'resampling_fn_probabilities_kwargs': resampling_softmax_kwargs,
'architecture': architecture_kwargs
}
return plan
Expand Down
12 changes: 8 additions & 4 deletions scripts/preprocess.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,38 @@
echo "Mosaic Preprocessor. Be sure you have a dataset.json file in the raw directory!"
read -p "Enter dataset id: " id
read -p "Enter resolution (low | std | high): " res
read -p "Enter configuration (press Enter to set automatically): " config
read -p "Enter planner (press Enter to set automatically): " planner
read -p "Enter GPU memory (press Enter to set automatically): " gpu_mem
read -p "Enter number of processes (1-20, press Enter to use default 4): " np
read -p "Enter number of processes (1-20, press Enter to use default 8): " np

# Устанавливаем количество процессов по умолчанию, если не указано
[[ -z "$np" ]] && np=4
[[ -z "$np" ]] && np=8

declare -a spacing

# Настройка разрешения, планировщика и автоматическое определение памяти
# Настройка разрешения, планировщика, автоматическое определение памяти и конфигурации флага
case "$res" in
low)
echo -e "\nLow resolution selected. Setting spacing at 3.0 3.0 3.0"
spacing=(3.0 3.0 3.0)
[[ -z "$planner" ]] && planner="nnUNetPlannerResEncM"
[[ -z "$gpu_mem" ]] && gpu_mem=12
[[ -z "$config" ]] && config="3d_fullres_lowres_NoRsmp"
;;
std)
echo -e "\nStandard resolution selected. Setting spacing at 1.5 1.5 1.5"
spacing=(1.5 1.5 1.5)
[[ -z "$planner" ]] && planner="nnUNetPlannerResEncL"
[[ -z "$gpu_mem" ]] && gpu_mem=24
[[ -z "$config" ]] && config="3d_fullres_stdres_NoRsmp"
;;
high)
echo -e "\nHigh resolution selected. Setting spacing at 1.0 1.0 1.0"
spacing=(1.0 1.0 1.0)
[[ -z "$planner" ]] && planner="nnUNetPlannerResEncXL"
[[ -z "$gpu_mem" ]] && gpu_mem=40
[[ -z "$config" ]] && config="3d_fullres_highres_NoRsmp"
;;
*)
echo -e "\nInvalid resolution option."
Expand All @@ -39,7 +43,7 @@ case "$res" in
esac

nnUNetv2_plan_and_preprocess -d "$id" \
-c 3d_fullres \
-c "$config" \
-pl "$planner" \
-overwrite_target_spacing "${spacing[@]}" \
-gpu_memory_target "$gpu_mem" \
Expand Down
49 changes: 38 additions & 11 deletions scripts/train.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,52 @@

echo "Train model from scratch. Be sure that you have already created the dataset.json file!"
read -p "Enter dataset id: " id
read -p "Enter train config (Press enter to set default 3d_fullres): " conf
read -p "Enter plans identifier (Press enter to set nnUNetPlans): " p
echo "Select train config:
0 - 3d_fullres
1 - 3d_fullres_lowres_NoRsmp
2 - 3d_fullres_stdres_NoRsmp
3 - 3d_fullres_highres_NoRsmp"
read -p "Enter train config number (Press enter to set default 0 - 3d_fullres): " conf_num
echo "Select trainer:
0 - nnUNetTrainer
1 - nnUNetTrainerNoMirroring
2 - nnUNetTrainer_250epochs_NoMirroring
3 - nnUNetTrainer_500epochs_NoMirroring
4 - nnUNetTrainer_750epochs_NoMirroring
5 - nnUNetTrainer_2000epochs_NoMirroring
6 - nnUNetTrainer_4000epochs_NoMirroring
7 - nnUNetTrainer_8000epochs_NoMirroring"
read -p "Enter trainer number: " trainer_num
read -p "Enter train fold ([0-4] or all usually): " fold
read -p "Enter trainer (nnUNetTrainer, nnUNetTrainerNoMirroring, nnUNetTrainer_Xepochs_NoMirroring etc.): " trainer

# Устанавливаем значение по умолчанию для конфигурации и плана, если они не указаны
[[ -z "$conf" ]] && conf="3d_fullres"
[[ -z "$p" ]] && p="nnUNetPlans"
case "$conf_num" in
0) conf="3d_fullres" ;;
1) conf="3d_fullres_lowres_NoRsmp" ;;
2) conf="3d_fullres_stdres_NoRsmp" ;;
3) conf="3d_fullres_highres_NoRsmp" ;;
*) conf="3d_fullres" ;;
esac

plans="nnUNetPlans"

case "$trainer_num" in
0) trainer="nnUNetTrainer" ;;
1) trainer="nnUNetTrainerNoMirroring" ;;
2) trainer="nnUNetTrainer_250epochs_NoMirroring" ;;
3) trainer="nnUNetTrainer_500epochs_NoMirroring" ;;
4) trainer="nnUNetTrainer_750epochs_NoMirroring" ;;
5) trainer="nnUNetTrainer_2000epochs_NoMirroring" ;;
6) trainer="nnUNetTrainer_4000epochs_NoMirroring" ;;
7) trainer="nnUNetTrainer_8000epochs_NoMirroring" ;;
*) echo "Error: Invalid trainer number."; exit 1 ;;
esac

# Проверяем, указаны ли fold и trainer, и выводим ошибку в противном случае
if [[ -z "$fold" ]]; then
echo "Error: Train fold is required."
exit 1
fi

if [[ -z "$trainer" ]]; then
echo "Error: Trainer is required."
exit 1
fi

echo "Launching the training process..."
nnUNetv2_train "$id" "$conf" "$fold" -p "$p" -tr "$trainer"
nnUNetv2_train "$id" "$conf" "$fold" -p "$plans" -tr "$trainer"

0 comments on commit 9b8642d

Please sign in to comment.