-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwod_runner.py
146 lines (95 loc) · 5 KB
/
twod_runner.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
import numpy as np
from twodunet import TwoDUnet
from imageparser import ImageParser
from imageaugmentator import ImageAugmentator
from sklearn.model_selection import train_test_split
import cv2
import matplotlib.pyplot as plt
parser = ImageParser()
utrech_dataset, singapore_dataset, amsterdam_dataset = parser.get_all_images_and_labels()
t1_utrecht = [row[1] for row in utrech_dataset]
flair_utrecht = [row[2] for row in utrech_dataset]
labels_utrecht = [row[0] for row in utrech_dataset]
t1_singapore = [row[1] for row in singapore_dataset]
flair_singapore = [row[2] for row in singapore_dataset]
labels_singapore = [row[0] for row in singapore_dataset]
t1_amsterdam = [row[1] for row in amsterdam_dataset]
flair_amsterdam = [row[2] for row in amsterdam_dataset]
labels_amsterdam = [row[0] for row in amsterdam_dataset]
slice_shape = (240, 240)
print('Utrecht: ', len(t1_utrecht), len(flair_utrecht), len(labels_utrecht))
print('Singapore: ', len(t1_singapore), len(flair_singapore), len(labels_singapore))
print('Amsterdam: ', len(t1_amsterdam), len(flair_amsterdam), len(labels_amsterdam))
'''
T1 DATA
'''
utrecht_data_t1 = parser.get_all_images_np_twod(t1_utrecht)
utrecht_resized_t1 = parser.resize_slices(utrecht_data_t1, slice_shape)
utrecht_normalized_t1 = parser.normalize_images(utrecht_resized_t1)
singapore_data_t1 = parser.get_all_images_np_twod(t1_singapore)
singapore_resized_t1 = parser.resize_slices(singapore_data_t1, slice_shape)
singapore_normalized_t1 = parser.normalize_images(singapore_resized_t1)
amsterdam_data_t1 = parser.get_all_images_np_twod(t1_amsterdam)
amsterdam_resized_t1 = parser.resize_slices(amsterdam_data_t1, slice_shape)
amsterdam_normalized_t1 = parser.normalize_images(amsterdam_resized_t1)
#print('T1: ', np.max(np.asanyarray(utrecht_data_t1).ravel()), np.max(np.asanyarray(singapore_resized_t1).ravel()), np.max(np.asanyarray(amsterdam_data_t1).ravel()))
'''
FLAIR DATA
'''
utrecht_data_flair = parser.get_all_images_np_twod(flair_utrecht)
utrecht_resized_flairs = parser.resize_slices(utrecht_data_flair, slice_shape)
utrecht_normalized_flairs = parser.normalize_images(utrecht_resized_flairs)
utrecht_data_tophat = parser.generate_tophat(utrecht_normalized_flairs)
singapore_data_flair = parser.get_all_images_np_twod(flair_singapore)
singapore_resized_flairs = parser.resize_slices(singapore_data_flair, slice_shape)
singapore_normalized_flairs = parser.normalize_images(singapore_resized_flairs)
singapore_data_tophat = parser.generate_tophat(singapore_normalized_flairs)
amsterdam_data_flair = parser.get_all_images_np_twod(flair_amsterdam)
amsterdam_resized_flairs = parser.resize_slices(amsterdam_data_flair, slice_shape)
amsterdam_normalized_flairs = parser.normalize_images(amsterdam_resized_flairs)
amsterdam_data_tophat = parser.generate_tophat(amsterdam_normalized_flairs)
#print('Flairs: ', np.max(np.asanyarray(utrecht_data_flair)), np.max(np.asanyarray(singapore_resized_flairs)), np.max(np.asanyarray(amsterdam_data_flair)))
'''
DATA CONCAT
'''
normalized_t1 = utrecht_normalized_t1 + singapore_normalized_t1 + amsterdam_normalized_t1
normalized_flairs = utrecht_normalized_flairs + singapore_normalized_flairs + amsterdam_normalized_flairs
data_tophat = utrecht_data_tophat + singapore_data_tophat + amsterdam_data_tophat
data_t1 = np.expand_dims(np.asanyarray(normalized_t1), axis=3)
data_flair = np.expand_dims(np.asanyarray(normalized_flairs), axis=3)
data_tophat = np.asanyarray(data_tophat)
all_data = np.concatenate([data_t1, data_flair, data_tophat], axis=3)
# All labels as np
all_labels_paths = labels_utrecht + labels_singapore + labels_amsterdam
all_labels_imgs = parser.get_all_images_np_twod(all_labels_paths)
# Extra gets for separated analysis
labels_utrecht_imgs = parser.get_all_images_np_twod(labels_utrecht)
labels_singapore_imgs = parser.get_all_images_np_twod(labels_singapore)
labels_amsterdam_imgs = parser.get_all_images_np_twod(labels_amsterdam)
resized_labels = parser.resize_slices(all_labels_imgs, slice_shape)
final_label_imgs = parser.remove_third_label(resized_labels)
final_label_imgs = np.expand_dims(np.asanyarray(final_label_imgs), axis=3)
'''
AUGMENTATION
'''
augmentator = ImageAugmentator()
data_augmented, labels_agumented = augmentator.perform_all_augmentations(all_data, final_label_imgs)
data_train, validation_data, labels_train, validation_labels = train_test_split(data_augmented, labels_agumented, test_size=0.05)
data_train = np.asanyarray(data_train)
labels_train = np.asanyarray(labels_train)
'''
TRAINING
'''
training_name = 'longer_training'
base_path = '/home/ubuntu/pablo/WhiteMatterHyperintensities'
test_size = 0.3
print(data_train.shape, labels_train.shape)
unet = TwoDUnet(model_path=None, img_shape=data_train.shape[1:])
unet.train(data_train, labels_train, test_size, training_name, base_path, epochs=70, batch_size=30)
'''
VALIDATING
'''
output_path = base_path + '/output/'
validation_data = np.asanyarray(validation_data)
validation_labels = np.asanyarray(validation_labels)
unet.predict_and_save(validation_data, validation_labels, output_path)