Skip to content

Commit

Permalink
Merge pull request #64 from mprires/main
Browse files Browse the repository at this point in the history
Changed vertebrae model. Made custom trainers available.
  • Loading branch information
LalithShiyam authored Oct 3, 2023
2 parents b784025 + 73dd226 commit 8547bca
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions moosez/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
from . import predict
from . import resources
from .moosez import moose
from .nnUNet_custom_trainer import utility
5 changes: 5 additions & 0 deletions moosez/moosez.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from moosez import resources
from moosez.image_processing import ImageResampler
from moosez.resources import MODELS, AVAILABLE_MODELS
from moosez.nnUNet_custom_trainer.utility import add_custom_trainers_to_local_nnunetv2

logging.basicConfig(format='%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s', level=logging.INFO,
filename=datetime.now().strftime('moosez-v.2.0.0.%H-%M-%d-%m-%Y.log'),
Expand Down Expand Up @@ -108,6 +109,8 @@ def main():
print(f'{constants.ANSI_VIOLET} {emoji.emojize(":memo:")} NOTE:{constants.ANSI_RESET}')
print(' ')
modalities = display.expectations(model_name)
custom_trainer_status = add_custom_trainers_to_local_nnunetv2()
logging.info('- Custom trainer: ' + custom_trainer_status)
accelerator = resources.check_cuda()

# ----------------------------------
Expand Down Expand Up @@ -279,6 +282,8 @@ def moose(model_name: str, input_dir: str, output_dir: str, accelerator: str) ->
model_path = constants.NNUNET_RESULTS_FOLDER
file_utilities.create_directory(model_path)
download.model(model_name, model_path)
custom_trainer_status = add_custom_trainers_to_local_nnunetv2()
logging.info('- Custom trainer: ' + custom_trainer_status)
input_validation.make_nnunet_compatible(input_dir)
predict.predict(model_name, input_dir, output_dir, accelerator)

Expand Down
23 changes: 23 additions & 0 deletions moosez/nnUNet_custom_trainer/MOOSE_custom_trainers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from nnunetv2.training.nnUNetTrainer.variants.data_augmentation.nnUNetTrainerDA5 import nnUNetTrainerDA5
import torch


class nnUNetTrainer_2000_epochs_DA5NoMirroring(nnUNetTrainerDA5):
def __init__(self, plans: dict, configuration: str, fold: int, dataset_json: dict, unpack_dataset: bool = True,
device: torch.device = torch.device('cuda')):
super().__init__(plans, configuration, fold, dataset_json, unpack_dataset, device)
self.num_epochs = 2000

print(f"Epochs: {self.num_epochs}")


def configure_rotation_dummyDA_mirroring_and_inital_patch_size(self):
rotation_for_DA, do_dummy_2d_data_aug, initial_patch_size, mirror_axes = \
super().configure_rotation_dummyDA_mirroring_and_inital_patch_size()
print(f"Mirroring: {mirror_axes} | {self.inference_allowed_mirroring_axes}")

mirror_axes = None
self.inference_allowed_mirroring_axes = None
print(f"Mirroring: {mirror_axes} | {self.inference_allowed_mirroring_axes}")

return rotation_for_DA, do_dummy_2d_data_aug, initial_patch_size, mirror_axes
Empty file.
28 changes: 28 additions & 0 deletions moosez/nnUNet_custom_trainer/utility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import site
import os
import platform
from moosez import file_utilities


def add_custom_trainers_to_local_nnunetv2():
# Locate the site-packages directory
site_packages = site.getsitepackages()[0]

if platform.system() == 'Windows':
site_packages = os.path.join(site_packages, 'Lib', 'site-packages')

source_file_path = os.path.join(site_packages, 'moosez', 'nnUNet_custom_trainer', 'MOOSE_custom_trainers.py')
target_file_path = os.path.join(site_packages, 'nnunetv2', 'training', 'nnUNetTrainer', 'variants',
'MOOSE_custom_trainers.py')

# Check if the file exists
if not os.path.exists(source_file_path):
return f'Could not find custom trainer file: {source_file_path}'

# Check if the file exists
if os.path.exists(target_file_path):
return f'Custom trainer already installed: {target_file_path}.'

file_utilities.copy_file(source_file_path, target_file_path)

return f"Custom trainer added to {target_file_path}"
4 changes: 2 additions & 2 deletions moosez/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@
"multilabel_prefix": "CT_Fat_"
},
"clin_ct_vertebrae":{
"url": "https://moose-files.s3.eu-de.cloud-object-storage.appdomain.cloud/clin_ct_vertebrae_04092023.zip",
"url": "https://moose-files.s3.eu-de.cloud-object-storage.appdomain.cloud/clin_ct_vertebrae_da5_03102023.zip",
"filename": "Dataset111_Vertebrae.zip",
"directory": "Dataset111_Vertebrae",
"trainer": "nnUNetTrainer_2000epochs_NoMirroring",
"trainer": "nnUNetTrainer_2000_epochs_DA5NoMirroring",
"voxel_spacing": [1.5, 1.5, 1.5],
"multilabel_prefix": "CT_Vertebrae_"
},
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='moosez',
version='2.2.32',
version='2.2.33',
author='Lalith Kumar Shiyam Sundar | Sebastian Gutschmayer',
author_email='[email protected]',
description='An AI-inference engine for 3D clinical and preclinical whole-body segmentation tasks',
Expand Down

0 comments on commit 8547bca

Please sign in to comment.