<jupyter_start><jupyter_text>Training Example  Training a model is very simple, follow this example to train your own model<jupyter_code>#First import the training tool and the torchio library
import sys
sys.path.append('../Radiology_and_AI')
from training.run_training import run_training
import torchio as tio
#Next define what transforms you want applied to the training data
#Both the training and validation data must have the same normalization and data preparation steps
#Only the training samples should have the augmentations applied
#Any transforms found at https://torchio.readthedocs.io/transforms/transforms.html can be applied
#Keep track of the  normalization and data preparation steps steps performed, you will need to apply the to all data passed into the model into the future

#These transforms are applied to data before it is used for training the model
training_transform = tio.Compose([
    #Normalization
    tio.ZNormalization(masking_method=tio.ZNormalization.mean), 
    
    #Augmentation
    #Play around with different augmentations as you desire, refer to the torchio docs to see how they work
    tio.RandomNoise(p=0.5),
    tio.RandomGamma(log_gamma=(-0.3, 0.3)),
    tio.RandomElasticDeformation(),
    
    #Preparation
    tio.CropOrPad((240, 240, 160)), #Crop/pad the images to a dimension your model can handle, our default unnet model requires the dimensions be multiples of 8
    tio.OneHot(num_classes=5), #Set num_classes to the max segmentation label + 1
    
])

#These transforms are applied to data before it is used to determined the performance of the model on the validation set
validation_transform = tio.Compose([
    #Normalization
    tio.ZNormalization(masking_method=tio.ZNormalization.mean),
    
    #Preparation
    tio.CropOrPad((240, 240, 160)),        
    tio.OneHot(num_classes=5)    
    
])
#The run training method applies the transforms you set and trains a model based on the parameters set here
run_training(
    #input_data_path must be set to the path to the folder containing the subfolders for each training example.
    #Each subfolder should contain one nii.gz file for each of the imaging series and the segmentation for that example
    #The name of each nii.gz file should be the name of the parent folder followed by the name of the imaging series type or seg if it is the segmentation
    #For example,MICCAI_BraTS2020_TrainingData contains ~300 folders, each corresponding to an input example,
    # one folder BraTS20_Training_001, contains five files: BraTS20_Training_001_flair.nii.gz, BraTS20_Training_001_seg.nii.gz, BraTS20_Training_001_t1.nii.gz , BraTS20_Training_001_t2.nii.gz,and BraTS20_Training_001_t1ce.nii.gz
    input_data_path = '../../brats_new/BraTS2020_TrainingData/MICCAI_BraTS2020_TrainingData',
    
    #Where you want your trained model to be saved after training is completed
    output_model_path = '../Models/test_train_many_1e-3.pt',
    
    #The transforms you created previously
    training_transform = training_transform,    
    validation_transform = validation_transform,
    
    #The names of the modalities every example in your input data has
    input_channels_list = ['flair','t1','t2','t1ce'],
    
    #Which of the labels in your segmentation you want to train your model to predict
    seg_channels = [1,2,4],
    
    #The name of the type of model you want to train, currently UNet3D is the only available model
    model_type = 'UNet3D',
    
    #The amount of examples per training batch, reduce/increase this based on memory availability
    batch_size = 1,
    
    #The amount of cpus you want to be avaiable for loading the input data into the model
    num_loading_cpus = 1,
    
    #The learning rate of the AdamW optimizer
    learning_rate = 1e-3,
    
    #Whether or not you want to run wandb logging of your run, install wandb to use these parameters
    wandb_logging = False,
    wandb_project_name = None,
    wandb_run_name = None,
    
    #The seed determines how your training and validation data will be randomly split
    #training_split_ratio is the share of your input data you want to use for training the model, the remainder is used for the validation data
    #Keep track of both the seed and ratio used if you want to be able to split your input data the same way in the future
    seed=42,    
    training_split_ratio = 0.9,
    
    #Any parameters which can be applied to a pytorch lightning trainer can also be applied, below is a selection of parameters you can apply
    #Refer to https://pytorch-lightning.readthedocs.io/en/latest/common/trainer.html#trainer-class-api to see the other parameters you could apply
    max_epochs=10,
    amp_backend = 'apex',
    amp_level = 'O1',
    precision=16,
    check_val_every_n_epoch = 1,
    log_every_n_steps=10,      
    val_check_interval= 50,
    progress_bar_refresh_rate=1,      
)<jupyter_output><empty_output><jupyter_text>Evaluation Example  If you want to evaluate your model in the future on a certain test dataset follow the below<jupyter_code>#First import the training tool and the torchio library
import sys
sys.path.append('.../Radiology_and_AI')
from training.run_training import run_eval
import torchio as tio
#Whatever normalization and data preperation steps you performed must also be applied here
#Refer to the above for more info
#These transforms are applied to data before it is used to determined the performance of the model on the validation set
test_transform = tio.Compose([
    #Normalization
    tio.ZNormalization(masking_method=tio.ZNormalization.mean),
    
    #Preparation
    tio.CropOrPad((240, 240, 160)),        
    tio.OneHot(num_classes=5)    
    
])
#The run_eval method evaluates and prints your models performance on a test dataset by averaging the Dice loss per batch
run_eval(
    #The path to the folder containing the data, refer to the training example for more info
    input_data_path= '../../brats_new/BraTS2020_TrainingData/MICCAI_BraTS2020_TrainingData',
    
    #The path to the saved model weights
    model_path="../../randgamma.pt",
    
    #The transforms you specified above
    validation_transform=validation_transform,   
    
    #The names of the modalities every example in your input data has
    input_channels_list = ['flair','t1','t2','t1ce'],
    #Which of the labels in your segmentation you want to train your model to predict
    seg_channels = [1,2,4],
    #The name of the type of model you want to train, currently UNet3D is the only available model
    model_type = 'UNet3D'
    
    #If set to true, we only return the performance of the model on the example which were not used for training, based on the train_val_split_ration and seed
    #If false we evaluate on all data and ignore seed and training_split_ratio,
    #set to false if input_data_path is set to a dataset you did not use during training
    is_validation_data = True,
    training_split_ratio=0.9,
    seed=42,
    
    #The amount of examples per training batch, reduce/increase this based on memory availability
    batch_size=1,
    #The amount of cpus you want to be avaiable for loading the input data into the model
    num_loading_cpus = 1,   
)<jupyter_output><empty_output><jupyter_text>Visualization Example  Tools for generating gifs, slices, and nifti files from input data and model predictions<jupyter_code>#First import the training tool and the torchio library
import sys
sys.path.append('../Radiology_and_AI')
sys.path.append('../../MedicalZooPytorch')
from visuals.run_visualization import gen_visuals
import torchio as tio
#Whatever normalization and data preperation steps you performed must also be applied here
#Refer to the above for more info
#These transforms are applied to data before it is used to determined the performance of the model on the validation set
validation_transform = tio.Compose([
    #Normalization
    tio.ZNormalization(masking_method=tio.ZNormalization.mean),
    
    #Preparation
    tio.CropOrPad((240, 240, 160)),        
    tio.OneHot(num_classes=5)        
])
#The gen_visuals method can be used for generating gifs of the inpu
gen_visuals(
    #The path to the folder containing the nifti files for an example
    image_path="../../brats_new/BraTS2020_TrainingData/MICCAI_BraTS2020_TrainingData/BraTS20_Training_010",
    
    #The transforms applied to the input should the same applied to the validation data during model training
    transforms = validation_transform,
    
    #The path to the model to use for predictions 
    model_path =  "../Models/test_train_many_1e-3.pt",
    
    #Generate visuals using segmentations generated by the model
    gen_pred = True,
    #Generate visuals using annotated segmentations
    gen_true = True,
    
    #The modalities your input example has
    input_channels_list = ['flair','t1','t2','t1ce'],
    #The labels your segmentation has
    seg_channels = [1,2,4],

    #Save a gif of the brain in 3D spinning on its vertical axis
    gen_gif = False,
    #Where to output the gif of the brain with segmentations either from the annotated labels or the predicted labels
    true_gif_output_path = "../../output/true",
    pred_gif_output_path = "../../output/pred",    
    #Which segmentation labels to display in the gif
    seg_channels_to_display_gif = [1,2,4],
    #The angle from the horizontal axis you are looking down on the brain at as it is spinning
    gif_view_angle = 30,
    #How much the brain rotates between images of the gif
    gif_angle_rotation = 20,
    #fig size of the gif images
    fig_size_gif = (50,25),

    #Save an image of slices of the brain at different views and with segmentations
    gen_slice = True,
    #where to save the generated slice image
    slice_output_path = "../../output/slices",
    #Fig size of the slice images
    fig_size_slice = (25,50),
    #Which seg labels to display in the slice, they will be layered in this order on the image
    seg_channels_to_display_slice = [2,4,1],
    #Which slice to display for different views of the brain
    sag_slice = None, #Sagittal
    cor_slice = None, #Coronal
    axi_slice = None, #Axial
    disp_slice_base = True, #WHether or not to display the input image in the background
    slice_title = None, #THe title of the slice images figure

    gen_nifti = True, #Whether or not to generate nifti files for the input image and the segmentations
    nifti_output_path = "../../output/nifti", #WHere to ssave the nifti files
)<jupyter_output>/home/cameron/storage/miniconda3/envs/cameronenv/lib/python3.8/site-packages/matplotlib/image.py:446: UserWarning: Warning: converting a masked element to nan.
  dv = np.float64(self.norm.vmax) - np.float64(self.norm.vmin)
/home/cameron/storage/miniconda3/envs/cameronenv/lib/python3.8/site-packages/matplotlib/image.py:453: UserWarning: Warning: converting a masked element to nan.
  a_min = np.float64(newmin)
/home/cameron/storage/miniconda3/envs/cameronenv/lib/python3.8/site-packages/matplotlib/image.py:458: UserWarning: Warning: converting a masked element to nan.
  a_max = np.float64(newmax)
{'fraction_non_alphanumeric': 0.06388339031851718, 'fraction_numerical': 0.019165017095555157, 'mean_word_length': 3.8054474708171204, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '40284', 'license': 'MIT', 'n_tokens_mistral': 3404, 'n_tokens_neox': 3193, 'n_words': 1274, 'path': 'Notebooks/usage.ipynb', 'repo_name': 'McMasterAI/Radiology-and-AI', 'size': '11114'}