forked from halipster/ultrasound_image_segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_C3.py
73 lines (51 loc) · 1.86 KB
/
main_C3.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
'''
This script train a convolutional network on the data outputed from A3 in order
to decide if a nerve is present or not.
'''
import numpy as np
from utils import SeedDropout, HeInit
from keras.models import Sequential
from keras.layers.core import Dense, Flatten
from keras.layers.convolutional import MaxPooling2D
from keras.optimizers import Adam
seed = 16545
rng = np.random.RandomState(seed)
weights_rng = HeInit(seed)
#%% Load Data
def scale(x):
return x - 0.025
x_train = scale(np.load('y_pred_A3.npy'))
y_train = np.max(np.load('y_true_A.npy'), axis=(1,2,3))
img_rows, img_cols = x_train.shape[2:]
#%% Define Model
model = Sequential()
model.add(MaxPooling2D(pool_size=(8, 8),input_shape=(1, img_rows, img_cols)))
model.add(Flatten())
model.add(Dense(256, activation='relu',W_regularizer='l2',b_regularizer='l2',init=weights_rng))
model.add(SeedDropout(0.5, rng.randint(-32768, 32767)))
model.add(Dense(1, activation='sigmoid',W_regularizer='l2',b_regularizer='l2',init=weights_rng))
#%%
model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['acc'])
loss = np.Inf
nb_epoch = 20
for epoch in range(nb_epoch):
print('### EPOCH %d/%d ###' % (epoch + 1, nb_epoch))
p = rng.permutation(len(x_train))
hist = model.fit(x_train[p], y_train[p],
batch_size=128, nb_epoch=1,
shuffle=False)
if hist.history['loss'][0] < loss:
model.save_weights('weights_C3.hdf5', overwrite=True)
loss = hist.history['loss'][0]
model.load_weights('weights_C3.hdf5')
y_pred = model.predict(x_train, verbose=1)
np.save('y_pred_C3', y_pred)
np.save('y_true_C3', y_train)
#%%
model.load_weights('weights_C3.hdf5')
def scale(x):
return x - 0.025
x_test = scale(np.load('y_pred_test_A3.npy'))
img_rows, img_cols = x_test.shape[2:]
is_present = model.predict(x_test, verbose=1)
np.save('y_pred_test_C3', is_present)