forked from silverlight6/Ultrasound_Modeling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTBIFFNeuralNetP1.py
More file actions
160 lines (129 loc) · 4.88 KB
/
TBIFFNeuralNetP1.py
File metadata and controls
160 lines (129 loc) · 4.88 KB
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
import tensorflow as tf
from tensorflow import keras
import numpy as np
from datetime import datetime
training_data_path = '/home/silver/Documents/TBI_NNs/Datasets/NPFiles/Polar3Class/TrainingData.npy'
training_label_path = '/home/silver/Documents/TBI_NNs/Datasets/NPFiles/Polar3Class/TrainingLabels.npy'
testing_data_path = '/home/silver/Documents/TBI_NNs/Datasets/NPFiles/Polar3Class/ValidationData.npy'
testing_label_path = '/home/silver/Documents/TBI_NNs/Datasets/NPFiles/Polar3Class/ValidationLabels.npy'
validation_data_path = '/home/silver/Documents/TBI_NNs/Datasets/NPFiles/Polar3Class/ValidationData.npy'
validation_label_path = '/home/silver/Documents/TBI_NNs/Datasets/NPFiles/Polar3Class/ValidationLabels.npy'
# channel 0: outside the brain
# channel 1: no-bleed
# channel 2: bleed
OUTPUT_CHANNELS = 3
BATCH_SIZE = 50
BUFFER_SIZE = 100
BLEED_THRESHOLD = .015
xdim = 256
ydim = 64
bleedCount = 0
nonBleedCount = 0
x_train = np.load(training_data_path)
x_train = x_train[:, :, :, :, range(1, 18)]
y_train = np.load(training_label_path)
x_test = np.load(testing_data_path)
x_test = x_test[:, :, :, :, range(1, 18)]
y_test = np.load(testing_label_path)
x_val = np.load(validation_data_path)
x_val = x_val[:, :, :, :, range(1, 18)]
y_val = np.load(validation_label_path)
y_train = y_train.astype(int)
y_test = y_test.astype(int)
y_val = y_val.astype(int)
x_shape = x_train.shape
y_shape = x_test.shape
z_shape = x_val.shape
x_train = np.reshape(x_train, (x_shape[0], x_shape[2], x_shape[3], x_shape[4]))
x_test = np.reshape(x_test, (y_shape[0], y_shape[2], y_shape[3], y_shape[4]))
x_val = np.reshape(x_val, (z_shape[0], z_shape[2], z_shape[3], z_shape[4]))
# print(y_train)
# print(y_test)
# print(x_train.shape)
# print(x_test.shape)
def downsample(filters, size, conv_id, stride=2, batch_norm=True):
initializer = tf.random_normal_initializer(0., 0.2)
result = keras.Sequential() # construct Sequential model
result.add(
keras.layers.Conv2D(filters, size, strides=stride, padding='same',
kernel_initializer=initializer, use_bias=False,
name='conv_{}'.format(conv_id)))
if batch_norm:
result.add(keras.layers.BatchNormalization())
result.add(keras.layers.LeakyReLU())
return result
def SegNet():
inputs = keras.layers.Input(shape=[xdim, ydim, 17])
fScaleFactor = 8
# encoder layers
down_stack = [
downsample(4 * fScaleFactor, 4, conv_id=0, batch_norm=False), # (bs, 128, 32, 64)
downsample(16 * fScaleFactor, 4, conv_id=1), # (bs, 64, 16, 256)
downsample(32 * fScaleFactor, 4, conv_id=2), # (bs, 32, 8, 512)
downsample(32 * fScaleFactor, 4, conv_id=3), # (bs, 16, 4, 512)
downsample(32 * fScaleFactor, 4, conv_id=4), # (bs, 8, 2, 512)
downsample(32 * fScaleFactor, 4, conv_id=5), # (bs, 4, 1, 512)
]
up_stack = [
tf.keras.layers.Flatten(),
tf.keras.layers.Dropout(.5),
tf.keras.layers.Dense(100, activation='relu'),
tf.keras.layers.Dropout(.5),
tf.keras.layers.Dense(1, activation='sigmoid'),
]
x = inputs
# Downsampling through the model
# iterate over the downsample layers and connect them together
for down in down_stack:
x = down(x)
# Upsampling and establishing the skip connections
for up in up_stack:
x = up(x)
return keras.Model(inputs=inputs, outputs=x)
stop_callbacks = [
keras.callbacks.EarlyStopping(
# Stop training when `val_loss` is no longer improving
monitor="val_loss",
# "no longer improving" being defined as "no better than 1e-3 less"
min_delta=1e-4,
# "no longer improving" being further defined as "for at least 7 epochs"
patience=5,
verbose=1,
),
keras.callbacks.ReduceLROnPlateau(
monitor='val_loss',
factor=0.3,
patience=2,
min_lr=0.00001,
verbose=1
)
]
SegNetF = SegNet()
# print(SegNetF.summary())
tf.keras.utils.plot_model(SegNetF, to_file='NNTBI.png', show_shapes=True)
epochs = 30
log_dir = "logs/fit/" + datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
segnet_optimizer = keras.optimizers.Adam(1e-5, beta_1=0.5)
SegNetF.compile(optimizer=segnet_optimizer,
loss=keras.losses.BinaryCrossentropy(),
metrics=['mse'])
SegNetF.fit(x_train, y_train,
batch_size=BATCH_SIZE,
shuffle=True,
validation_data=(x_test, y_test),
epochs=epochs,
callbacks=[tensorboard_callback]
)
for image, label in zip(x_test, y_test):
image = np.reshape(image, (1, 256, 64, 17))
pred = SegNetF(image)
# pred = tf.math.round(pred)
tf.print(pred, label)
print("End of testing set \n")
for image, label in zip(x_val, y_val):
image = np.reshape(image, (1, 256, 64, 17))
pred = SegNetF(image)
# pred = tf.math.round(pred)
tf.print(pred, label)
SegNetF.save('nn_polar_1')