Skip to content

Commit

Permalink
Created demo notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
svenbbsp committed Jun 11, 2024
1 parent c19aebf commit 970a999
Show file tree
Hide file tree
Showing 13 changed files with 1,401 additions and 10,274 deletions.
395 changes: 395 additions & 0 deletions Demo-notebooks/DRR.ipynb

Large diffs are not rendered by default.

431 changes: 431 additions & 0 deletions Demo-notebooks/Evaluation.ipynb

Large diffs are not rendered by default.

499 changes: 499 additions & 0 deletions Demo-notebooks/Training.ipynb

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
Binary file not shown.
10,235 changes: 0 additions & 10,235 deletions notebooks/training.ipynb

This file was deleted.

2 changes: 1 addition & 1 deletion src/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def get_datasets(

# Create the corresponding validation dataset
val_dataset = TciaDataset(
root_dir="../data",
root_dir="../data081",
collection=collection,
section="validation",
transform=transform,
Expand Down
113 changes: 75 additions & 38 deletions src/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import matplotlib.pyplot as plt
from timeit import default_timer as timer
from diffdrr.data import read


def loadData():
Expand Down Expand Up @@ -59,20 +60,26 @@ def trainSingleEpoch(model, criterion, optimizer, Volume, Target, device, length

model.train()
for i in range(length):
rotation = random.uniform(-40,40)
rotation = random.uniform(-5,5)
# Create raw DRR
drr_raw = create_drr(
Volume[0],
Target[0],
subject = read(
tensor=Volume[0],
label_tensor=Target[0],
orientation="AP",
bone_attenuation_multiplier=5.0,
)


drr_raw = create_drr(
subject,
sdd=1020,
height=256,
width=256,
rotations=torch.tensor([[rotation, 0.0, 0.0]]),
translations=torch.tensor([[0.0, 850.0, 0.0]]),
mask_to_channels=True,
device="cpu",
)
)


enhancement_factor = random.uniform(0.4,0.6)
Expand All @@ -92,19 +99,25 @@ def testModel(model, criterion, Volume, Target, device):
loss = []

for i in range(5):
rotation = (i*20-40)
drr_raw = create_drr(
Volume[0],
Target[0],
rotation = random.uniform(-5,5)
subject = read(
tensor=Volume[0],
label_tensor=Target[0],
orientation="AP",
bone_attenuation_multiplier=5.0,
)


drr_raw = create_drr(
subject,
sdd=1020,
height=256,
width=256,
rotations=torch.tensor([[rotation, 0.0, 0.0]]),
translations=torch.tensor([[0.0, 850.0, 0.0]]),
mask_to_channels=True,
device="cpu",
)
)
drr_body = drr_raw[:,0,:,:]
drr_vessels = drr_raw[:,1,:,:]

Expand Down Expand Up @@ -159,18 +172,24 @@ def testInferenceSpeed(path):

model.load_state_dict(torch.load(path))

drr_raw = create_drr(
volume[0],
target[0],
subject = read(
tensor=volume[0],
label_tensor=target[0],
orientation="AP",
bone_attenuation_multiplier=5.0,
)


drr_raw = create_drr(
subject,
sdd=1020,
height=256,
width=256,
rotations=torch.tensor([[0.0, 0.0, 0.0]]),
rotations=torch.tensor([[0, 0.0, 0.0]]),
translations=torch.tensor([[0.0, 850.0, 0.0]]),
mask_to_channels=True,
device="cpu",
)
)

drr_input, drr_vessels = drr_to_input(drr_raw, enhancement_factor=0.6, device=device)

Expand All @@ -187,19 +206,25 @@ def testInferenceSpeed(path):


def visualizeModel(model, Volume, Target, device):
rotation = random.uniform(-40,40)
drr_raw = create_drr(
Volume[0],
Target[0],
rotation = random.uniform(-5,5)
subject = read(
tensor=Volume[0],
label_tensor=Target[0],
orientation="AP",
bone_attenuation_multiplier=5.0,
)


drr_raw = create_drr(
subject,
sdd=1020,
height=256,
width=256,
rotations=torch.tensor([[rotation, 0.0, 0.0]]),
translations=torch.tensor([[0.0, 850.0, 0.0]]),
mask_to_channels=True,
device="cpu",
)
)

drr_input, drr_vessels = drr_to_input(drr_raw, enhancement_factor=0.5, device=device)

Expand Down Expand Up @@ -244,19 +269,25 @@ def drr_to_input(drr_raw, enhancement_factor, device):
def visualizationResult(model, Volume, Target, device):
model.eval()

rotation = random.uniform(-40,40)
drr_raw = create_drr(
Volume[0],
Target[0],
rotation = random.uniform(-5,5)
subject = read(
tensor=Volume[0],
label_tensor=Target[0],
orientation="AP",
bone_attenuation_multiplier=5.0,
)


drr_raw = create_drr(
subject,
sdd=1020,
height=256,
width=256,
rotations=torch.tensor([[rotation, 0.0, 0.0]]),
translations=torch.tensor([[0.0, 850.0, 0.0]]),
mask_to_channels=True,
device="cpu",
)
)
drr_body = drr_raw[:,0,:,:].to(device)
fig, axs = plt.subplots(2, 4, figsize=(16, 8))
axs = axs.flatten()
Expand Down Expand Up @@ -306,25 +337,31 @@ def rotationSweep(model, Volume, Target, device, criterion):
loss = []
#Rotation sweep
for i in range(80):
subject = read(
tensor=Volume[0],
label_tensor=Target[0],
orientation="AP",
bone_attenuation_multiplier=5.0,
)


drr_raw = create_drr(
Volume[0],
Target[0],
bone_attenuation_multiplier=5.0,
sdd=1020,
height=256,
width=256,
rotations=torch.tensor([[i-40, 0.0, 0.0]]),
translations=torch.tensor([[0.0, 850.0, 0.0]]),
mask_to_channels=True,
device="cpu",
)
subject,
sdd=1020,
height=256,
width=256,
rotations=torch.tensor([[i-40, 0.0, 0.0]]),
translations=torch.tensor([[0.0, 850.0, 0.0]]),
mask_to_channels=True,
device="cpu",
)

drr_input, drr_vessels = drr_to_input(drr_raw, enhancement_factor=ef, device=device)
prediction, latent_representation = model(Target.to(device), drr_input)
loss.append(criterion(prediction, drr_vessels).item())
print(f"loop: {j}, angle: {i}")
#print(f"loop: {j}, angle: {i}")

print(f"loss of loop {j}: {np.mean(loss)}")
#print(f"loss of loop {j}: {np.mean(loss)}")
total_loss.append(np.mean(loss))


Expand Down

0 comments on commit 970a999

Please sign in to comment.