Skip to content

Commit ce8e8b8

Browse files
committed
Made datasets/modules importable and installable
1 parent 5a9fb6b commit ce8e8b8

File tree

9 files changed

+74
-2
lines changed

9 files changed

+74
-2
lines changed
File renamed without changes.

Radiology_and_AI/__init__.py

Whitespace-only changes.

Radiology_and_AI/datasets/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Custom torch.utils.data.Dataset objects go in this folder

Radiology_and_AI/datasets/__init__.py

Whitespace-only changes.
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import torch
2+
import nibabel as nb
3+
import os
4+
import numpy as np
5+
6+
#Dataset
7+
class brats_dataset(torch.utils.data.Dataset):
8+
def __init__(self,data_folders):
9+
self.data_list = []
10+
11+
#Perform necessary input data preparation in this function
12+
#add each input example into the data_last function
13+
#takes in a list of folders and processes the data contained
14+
15+
# U net requires all dimensions be divisible by 8 (by default)
16+
# or we'd have to manually do the padding in the U-net model
17+
# no padding="valid" exists in Pytorch for... reasons?
18+
for i, folder in enumerate(data_folders):
19+
i_str = folder[-3:]
20+
21+
f_flair = nb.load(os.path.join(folder,'BraTS20_Training_%s_flair.nii' % i_str),mmap=False).get_fdata()
22+
f_seg = nb.load(os.path.join(folder,'BraTS20_Training_%s_seg.nii'% i_str),mmap=False).get_fdata()
23+
f_t1ce = nb.load(os.path.join(folder,'BraTS20_Training_%s_t1ce.nii'% i_str),mmap=False).get_fdata()
24+
f_t1 = nb.load(os.path.join(folder,'BraTS20_Training_%s_t1.nii'% i_str),mmap=False).get_fdata()
25+
f_t2 = nb.load(os.path.join(folder,'BraTS20_Training_%s_t2.nii'% i_str),mmap=False).get_fdata()
26+
27+
f_flair = torch.as_tensor(np.expand_dims(np.pad(f_flair, [(0, 0), (0, 0), (2, 3)]), axis=0)).half()
28+
f_t1 = torch.as_tensor(np.expand_dims(np.pad(f_t1, [(0, 0), (0, 0), (2, 3)]), axis=0)).half()
29+
f_t2 = torch.as_tensor(np.expand_dims(np.pad(f_t2, [(0, 0), (0, 0), (2, 3)]), axis=0)).half()
30+
f_seg = torch.as_tensor(np.expand_dims(np.pad(f_seg, [(0, 0), (0, 0), (2, 3)]), axis=0)).half()
31+
f_t1ce = torch.as_tensor(np.expand_dims(np.pad(f_t1ce, [(0, 0), (0, 0), (2, 3)]), axis=0)).half()
32+
33+
34+
concat = torch.cat([f_t1, f_t1ce, f_t2, f_flair], axis=0)
35+
36+
self.data_list.append([concat, f_seg])
37+
def __len__(self):
38+
return len(self.data_list)
39+
def __getitem__(self, index):
40+
return self.data_list[index]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Custom lightning modules go in this folder

Radiology_and_AI/lightning_modules/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytorch_lightning as pl
2+
import torch
3+
4+
#Pytorch-lightning setup
5+
class TumourSegmentation(pl.LightningModule):
6+
def __init__(self,model):
7+
super().__init__()
8+
self.model = model
9+
def forward(self,x):
10+
# x=x.half()
11+
f = self.model.forward(x)
12+
13+
# print('Done forward step!')
14+
return f
15+
16+
def training_step(self, batch, batch_idx):
17+
18+
x, y = batch
19+
20+
y_hat = self.model(x)
21+
22+
# I'm not really sure why the shape is weird here, but this seems to run
23+
y_hat = torch.squeeze(y_hat,axis=1)
24+
25+
loss = torch.mean(torch.abs(y_hat - y))
26+
# this CE results in a CUDA error because the U-net implementation is strange
27+
#F.binary_cross_entropy(y_hat, y)
28+
return loss
29+
def configure_optimizers(self):
30+
return torch.optim.Adam(self.parameters(), lr=0.02)

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from setuptools import setup
22

33
setup(
4-
name='Radiology-and-AI',
4+
name='Radiology_and_AI',
55
version='1.0',
6-
packages=['Radiology-and-AI'],
6+
packages=['Radiology_and_AI'],
77
url='https://github.com/McMasterAI/Radiology-and-AI.git',
88
license='',
99
author='',

0 commit comments

Comments
 (0)