-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathutilities.py
194 lines (150 loc) · 6.99 KB
/
utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from monai.utils import first
import matplotlib.pyplot as plt
import torch
import os
import numpy as np
from monai.losses import DiceLoss
from tqdm import tqdm
def dice_metric(predicted, target):
'''
In this function we take `predicted` and `target` (label) to calculate the dice coeficient then we use it
to calculate a metric value for the training and the validation.
'''
dice_value = DiceLoss(to_onehot_y=True, sigmoid=True, squared_pred=True)
value = 1 - dice_value(predicted, target).item()
return value
def calculate_weights(val1, val2):
'''
In this function we take the number of the background and the forgroud pixels to return the `weights`
for the cross entropy loss values.
'''
count = np.array([val1, val2])
summ = count.sum()
weights = count/summ
weights = 1/weights
summ = weights.sum()
weights = weights/summ
return torch.tensor(weights, dtype=torch.float32)
def train(model, data_in, loss, optim, max_epochs, model_dir, test_interval=1 , device=torch.device("cuda:0")):
best_metric = -1
best_metric_epoch = -1
save_loss_train = []
save_loss_test = []
save_metric_train = []
save_metric_test = []
train_loader, test_loader = data_in
for epoch in range(max_epochs):
print("-" * 10)
print(f"epoch {epoch + 1}/{max_epochs}")
model.train()
train_epoch_loss = 0
train_step = 0
epoch_metric_train = 0
for batch_data in train_loader:
train_step += 1
volume = batch_data["vol"]
label = batch_data["seg"]
label = label != 0
volume, label = (volume.to(device), label.to(device))
optim.zero_grad()
outputs = model(volume)
train_loss = loss(outputs, label)
train_loss.backward()
optim.step()
train_epoch_loss += train_loss.item()
print(
f"{train_step}/{len(train_loader) // train_loader.batch_size}, "
f"Train_loss: {train_loss.item():.4f}")
train_metric = dice_metric(outputs, label)
epoch_metric_train += train_metric
print(f'Train_dice: {train_metric:.4f}')
print('-'*20)
train_epoch_loss /= train_step
print(f'Epoch_loss: {train_epoch_loss:.4f}')
save_loss_train.append(train_epoch_loss)
np.save(os.path.join(model_dir, 'loss_train.npy'), save_loss_train)
epoch_metric_train /= train_step
print(f'Epoch_metric: {epoch_metric_train:.4f}')
save_metric_train.append(epoch_metric_train)
np.save(os.path.join(model_dir, 'metric_train.npy'), save_metric_train)
if (epoch + 1) % test_interval == 0:
model.eval()
with torch.no_grad():
test_epoch_loss = 0
test_metric = 0
epoch_metric_test = 0
test_step = 0
for test_data in test_loader:
test_step += 1
test_volume = test_data["vol"]
test_label = test_data["seg"]
test_label = test_label != 0
test_volume, test_label = (test_volume.to(device), test_label.to(device),)
test_outputs = model(test_volume)
test_loss = loss(test_outputs, test_label)
test_epoch_loss += test_loss.item()
test_metric = dice_metric(test_outputs, test_label)
epoch_metric_test += test_metric
test_epoch_loss /= test_step
print(f'test_loss_epoch: {test_epoch_loss:.4f}')
save_loss_test.append(test_epoch_loss)
np.save(os.path.join(model_dir, 'loss_test.npy'), save_loss_test)
epoch_metric_test /= test_step
print(f'test_dice_epoch: {epoch_metric_test:.4f}')
save_metric_test.append(epoch_metric_test)
np.save(os.path.join(model_dir, 'metric_test.npy'), save_metric_test)
if epoch_metric_test > best_metric:
best_metric = epoch_metric_test
best_metric_epoch = epoch + 1
torch.save(model.state_dict(), os.path.join(
model_dir, "best_metric_model.pth"))
print(
f"current epoch: {epoch + 1} current mean dice: {test_metric:.4f}"
f"\nbest mean dice: {best_metric:.4f} "
f"at epoch: {best_metric_epoch}"
)
print(
f"train completed, best_metric: {best_metric:.4f} "
f"at epoch: {best_metric_epoch}")
def show_patient(data, SLICE_NUMBER=1, train=True, test=False):
"""
This function is to show one patient from your datasets, so that you can si if the it is okay or you need
to change/delete something.
`data`: this parameter should take the patients from the data loader, which means you need to can the function
prepare first and apply the transforms that you want after that pass it to this function so that you visualize
the patient with the transforms that you want.
`SLICE_NUMBER`: this parameter will take the slice number that you want to display/show
`train`: this parameter is to say that you want to display a patient from the training data (by default it is true)
`test`: this parameter is to say that you want to display a patient from the testing patients.
"""
check_patient_train, check_patient_test = data
view_train_patient = first(check_patient_train)
view_test_patient = first(check_patient_test)
if train:
plt.figure("Visualization Train", (12, 6))
plt.subplot(1, 2, 1)
plt.title(f"vol {SLICE_NUMBER}")
plt.imshow(view_train_patient["vol"][0, 0, :, :, SLICE_NUMBER], cmap="gray")
plt.subplot(1, 2, 2)
plt.title(f"seg {SLICE_NUMBER}")
plt.imshow(view_train_patient["seg"][0, 0, :, :, SLICE_NUMBER])
plt.show()
if test:
plt.figure("Visualization Test", (12, 6))
plt.subplot(1, 2, 1)
plt.title(f"vol {SLICE_NUMBER}")
plt.imshow(view_test_patient["vol"][0, 0, :, :, SLICE_NUMBER], cmap="gray")
plt.subplot(1, 2, 2)
plt.title(f"seg {SLICE_NUMBER}")
plt.imshow(view_test_patient["seg"][0, 0, :, :, SLICE_NUMBER])
plt.show()
def calculate_pixels(data):
val = np.zeros((1, 2))
for batch in tqdm(data):
batch_label = batch["seg"] != 0
_, count = np.unique(batch_label, return_counts=True)
if len(count) == 1:
count = np.append(count, 0)
val += count
print('The last values:', val)
return val