-
Notifications
You must be signed in to change notification settings - Fork 72
/
train_model.py
executable file
·58 lines (47 loc) · 2.3 KB
/
train_model.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
from image_gen import ImageDataGenerator
from load_data import loadDataMontgomery, loadDataJSRT
from build_model import build_UNet2D_4L
import pandas as pd
from keras.utils.vis_utils import plot_model
from keras.callbacks import ModelCheckpoint
if __name__ == '__main__':
# Path to csv-file. File should contain X-ray filenames as first column,
# mask filenames as second column.
csv_path = '/path/to/JSRT/idx.csv'
# Path to the folder with images. Images will be read from path + path_from_csv
path = csv_path[:csv_path.rfind('/')] + '/'
df = pd.read_csv(csv_path)
# Shuffle rows in dataframe. Random state is set for reproducibility.
df = df.sample(frac=1, random_state=23)
n_train = int(len(df))
df_train = df[:n_train]
df_val = df[n_train:]
# Load training and validation data
im_shape = (256, 256)
X_train, y_train = loadDataJSRT(df_train, path, im_shape)
X_val, y_val = loadDataJSRT(df_val, path, im_shape)
# Build model
inp_shape = X_train[0].shape
UNet = build_UNet2D_4L(inp_shape)
UNet.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Visualize model
plot_model(UNet, 'model.png', show_shapes=True)
##########################################################################################
model_file_format = 'model.{epoch:03d}.hdf5'
print model_file_format
checkpointer = ModelCheckpoint(model_file_format, period=10)
train_gen = ImageDataGenerator(rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
rescale=1.,
zoom_range=0.2,
fill_mode='nearest',
cval=0)
test_gen = ImageDataGenerator(rescale=1.)
batch_size = 8
UNet.fit_generator(train_gen.flow(X_train, y_train, batch_size),
steps_per_epoch=(X_train.shape[0] + batch_size - 1) // batch_size,
epochs=100,
callbacks=[checkpointer],
validation_data=test_gen.flow(X_val, y_val),
validation_steps=(X_val.shape[0] + batch_size - 1) // batch_size)