<jupyter_start><jupyter_text>1. Set up training environment<jupyter_code>#Get necessary packages downloaded
#May take a few minutes
#This does not have to be run again if you restart a runtime, only if you terminate, are disconnected, or factory reset it
!pip install pytorch_lightning
!pip install torchsummaryX
!pip install webdataset
!pip install datasets
!pip install wandb
!git clone https://github.com/black0017/MedicalZooPytorch.git
!pip install -r MedicalZooPytorch/installation/requirements.txt
!pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
!git clone https://github.com/McMasterAI/Radiology-and-AI.git<jupyter_output><empty_output><jupyter_text>If you make a change to a branch of the repo and want this script to reflect those changes, restart the runtime and run the code here, then the below imports again<jupyter_code>!rm -r ./Radiology-and-AI
!git clone --branch <branchname> https://github.com/McMasterAI/Radiology-and-AI.git<jupyter_output>/bin/bash: branchname: No such file or directory<jupyter_text>Import libraries<jupyter_code>from google.colab import drive
drive.mount('/content/drive', force_remount=True)
import sys
sys.path.append('./Radiology-and-AI/Radiology_and_AI')
sys.path.append('./MedicalZooPytorch')
import os
import torch
import numpy as np
from torch.utils.data import Dataset, DataLoader, random_split
from pytorch_lightning.loggers import WandbLogger
import pytorch_lightning as pl
import sys
import nibabel as nb
from skimage import transform
import matplotlib.pyplot as plt
import webdataset as wds
from collators.brats_collator import col_img
from lightning_modules.segmentation import TumourSegmentation
cd drive/MyDrive<jupyter_output>/content/drive/MyDrive<jupyter_text>2. Load Data<jupyter_code>#Loading data, must have shortcut to the shared macai_datasets folder in the base level of your google drive
train_dataset = wds.Dataset("macai_datasets/brats/train/brats_train.tar.gz")
eval_dataset = wds.Dataset("macai_datasets/brats/validation/brats_validation.tar.gz")<jupyter_output><empty_output><jupyter_text>3. Find optimal learning rate<jupyter_code>#Finding Learning rate
trainer = pl.Trainer(
    precision=16,
    gpus = 1,
    accumulate_grad_batches=20 #Shammo change this values
)    
model = TumourSegmentation(learning_rate = 1e-4, collator=col_img, batch_size=5, train_dataset=train_dataset, eval_dataset=eval_dataset)
lr_finder = trainer.tuner.lr_find(model,min_lr=1e-7,max_lr=1e-2,mode='linear')
print(lr_finder.suggestion())<jupyter_output><empty_output><jupyter_text>4. Train<jupyter_code>wandb_logger = WandbLogger(project='macai',name='test_run', offline = True)

#Training
trainer = pl.Trainer(
    accumulate_grad_batches = 20,
    gpus = 1,
    max_epochs = 10,
    precision=16,
    check_val_every_n_epoch = 1,
    logger = wandb_logger,
    log_every_n_steps=10,            
)
model = TumourSegmentation(learning_rate = lr_finder.suggestion(), collator=col_img, batch_size=5, train_dataset=train_dataset, eval_dataset=eval_dataset)
trainer.fit(model)
#Run after youre done training and the run will be uploaded to wandb
!wandb sync --sync-all<jupyter_output>Syncing: https://wandb.ai/dufaultc/macai/runs/2jbnz20l ...done.<jupyter_text>5. Evaluate<jupyter_code># Example code for crudely showing some images
# Probably best in another notebook, but w/e

model = TumourSegmentation.load_from_checkpoint('lightning_logs/version_1/checkpoints/epoch=1-step=598.ckpt').cuda().half()
# 1 for the tumor core
# 2 for edema
# 4 for surrounding
model.cuda().half()
for z in train_dataloader:
  print('======================================================')
  prediction = model.forward(torch.unsqueeze(z[0], axis=0).cuda().half())

  sl = z[1][0, :, 100]
  sl[sl==4] = 1

  plt.title('Label')
  plt.imshow(sl, vmin = 0, vmax=2)
  plt.show()

  prediction = prediction[0].cpu().detach().numpy().astype('float32')

  plt.title('Prediction class 0')
  plt.imshow(prediction[0, :, 100], vmin = 0, vmax=1)
  plt.show()

  plt.title('Prediction class 1')
  plt.imshow(prediction[1, :, 100], vmin = 0, vmax=1)
  plt.show()<jupyter_output>======================================================
{'fraction_non_alphanumeric': 0.13051555968416165, 'fraction_numerical': 0.019972131908964234, 'mean_word_length': 5.333823529411765, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '41089', 'license': 'MIT', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1490, 'n_words': 334, 'path': 'Notebooks/old/learning_rate_finding.ipynb', 'repo_name': 'McMasterAI/Radiology-and-AI', 'size': '4306'}