forked from halipster/ultrasound_image_segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_B3.py
68 lines (52 loc) · 1.72 KB
/
main_B3.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
'''
This script train a U-NET on the data where nerves are present using scale_3
and cleaned data.
'''
import numpy as np
from keras.optimizers import Adam
from utils import crossentropy, scale_3, Generator
from unet import get_model
input_shape = (128, 160)
batch_size = 4
nb_epoch = 20
seed = 95136
train_dir = '/media/hal/7ECE0648CE05F8E3/train'
clean_dir = 'clean'
model = get_model(input_shape=input_shape,
dropout_seed = seed,
weight_seed = seed)
#%%
model.compile(optimizer=Adam(),
loss=crossentropy)
generator = Generator(input_shape=input_shape,
shuffle_seed=seed,
scale=scale_3,
train_dir=train_dir,
clean_dir=clean_dir,
grid_shape=(4,4),
sigma=10,
elastic_seed=seed,
only_present=True)
loss = np.Inf
for epoch in range(nb_epoch):
print('### EPOCH %d/%d ###' % (epoch + 1, nb_epoch))
x_train, y_train = generator.get_training_set()
hist = model.fit(x_train, y_train,
batch_size=batch_size, nb_epoch=1,
shuffle=False)
if hist.history['loss'][0] < loss:
model.save_weights('weights_B3.hdf5', overwrite=True)
loss = hist.history['loss'][0]
#%%
import cv2 as cv
model.load_weights('weights_B3.hdf5')
x = np.load('x_test.npz')['x']
x = x[:,1:,1:]
N = len(x)
x_test = np.empty((N,*input_shape))
for i in range(N):
x_test[i] = cv.resize(x[i].T, input_shape, cv.INTER_AREA).T
x_test = scale_3(x_test).astype(np.float32)
x_test = np.expand_dims(x_test, axis=1)
y_pred = model.predict(x_test, verbose=1)
np.save('y_pred_test_B3', y_pred)