-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainUNet.py
147 lines (110 loc) · 4.05 KB
/
trainUNet.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
146
147
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import numpy as np
import cv2
from glob import glob
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tensorflow.keras.callbacks import ModelCheckpoint, CSVLogger, ReduceLROnPlateau, EarlyStopping, TensorBoard
from tensorflow.keras.metrics import Recall, Precision
from UNet import build_unet
from metrics import dice_coef, iou
# Habana specific libraries
import habana_frameworks.tensorflow as htf
htf.load_habana_module()
H = 256
W = 256
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
def shuffling(x, y):
x, y = shuffle(x, y, random_state=42)
return x, y
def load_data(dataset_path, split=0.2):
images = sorted(glob(os.path.join(dataset_path, "images/images", "*.tif")))
masks = sorted(glob(os.path.join(dataset_path, "masks/masks", "*.tif")))
test_size = int(len(images) * split)
train_x, valid_x = train_test_split(images, test_size=test_size, random_state=42)
train_y, valid_y = train_test_split(masks, test_size=test_size, random_state=42)
train_x, test_x = train_test_split(train_x, test_size=test_size, random_state=42)
train_y, test_y = train_test_split(train_y, test_size=test_size, random_state=42)
return (train_x, train_y), (valid_x, valid_y), (test_x, test_y)
def read_image(path):
path = path.decode()
x = cv2.imread(path, cv2.IMREAD_COLOR) # (H, W, 3)
x = cv2.resize(x, (W, H))
x = x / 255.0
x = x.astype(np.float32)
return x # (256, 256, 3)
def read_mask(path):
path = path.decode()
x = cv2.imread(path, cv2.IMREAD_GRAYSCALE) # (H, W)
x = cv2.resize(x, (W, H))
x = x / 255.0
x = x.astype(np.float32) # (256, 256)
x = np.expand_dims(x, axis=-1) # (256, 256, 1)
return x
def tf_parse(x, y):
def _parse(x, y):
x = read_image(x)
y = read_mask(y)
return x, y
x, y = tf.numpy_function(_parse, [x, y], [tf.float32, tf.float32])
x.set_shape([H, W, 3])
y.set_shape([H, W, 1])
return x, y
def tf_dataset(X, Y, batch):
dataset = tf.data.Dataset.from_tensor_slices((X, Y))
dataset = dataset.map(tf_parse)
dataset = dataset.batch(batch)
dataset = dataset.prefetch(10)
return dataset
if __name__ == "__main__":
np.random.seed(42)
tf.random.set_seed(42)
# Folder to save weights + model
create_dir("/media/weights")
# Hyperparameters
batch_size = 4
lr = 1e-4
num_epoch = 10
model_path = "weights/unet.h5"
csv_path = "weights/data.csv"
# Create dataset
dataset_path = '...tgz'
(train_x, train_y), (valid_x, valid_y), (test_x, test_y) = load_data(dataset_path)
print(f"Train: {len(train_x)} - {len(train_y)}")
print(f"Valid: {len(valid_x)} - {len(valid_y)}")
print(f"Test: {len(test_x)} - {len(test_y)}")
train_dataset = tf_dataset(train_x, train_y, batch_size)
valid_dataset = tf_dataset(valid_x, valid_y, batch_size)
train_steps = len(train_x) // batch_size
valid_steps = len(valid_x) // batch_size
if len(train_x) % batch_size != 0:
train_steps += 1
if len(valid_x) % batch_size != 0:
valid_steps += 1
""" Model """
model = build_unet((H, W, 3))
metrics = [dice_coef, iou, Recall(), Precision()]
model.compile(loss="binary_crossentropy", optimizer=tf.keras.optimizers.Adam(lr), metrics=metrics)
model.summary()
callbacks = [
ModelCheckpoint(model_path, verbose=1, save_best_only=True),
ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, min_lr=1e-7, verbose=1),
CSVLogger(csv_path),
TensorBoard(),
EarlyStopping(monitor='val_loss', patience=20, restore_best_weights=False)
]
model.fit(
train_dataset,
epochs=num_epoch,
validation_data=valid_dataset,
steps_per_epoch=train_steps,
validation_steps=valid_steps,
callbacks=callbacks
)
# Once training is complete, save the weights
# for the Attention net to use
model.save_weights('weights/<TASK>.h5')