forked from srihari-humbarwadi/DeepLabV3_Plus-Tensorflow2.0
-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
139 lines (115 loc) · 5.46 KB
/
train.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
from glob import glob
import tensorflow as tf
from tensorflow.keras.callbacks import TensorBoard, ModelCheckpoint
from deeplab import DeepLabV3Plus
print('TensorFlow', tf.__version__)
batch_size = 24
H, W = 512, 512
num_classes = 34
image_list = sorted(glob(
'cityscapes/dataset/train_images/*'))
mask_list = sorted(glob(
'cityscapes/dataset/train_masks/*'))
val_image_list = sorted(glob(
'cityscapes/dataset/val_images/*'))
val_mask_list = sorted(glob(
'cityscapes/dataset/val_masks/*'))
print('Found', len(image_list), 'training images')
print('Found', len(val_image_list), 'validation images')
for i in range(len(image_list)):
assert image_list[i].split(
'/')[-1].split('_leftImg8bit')[0] == mask_list[i].split('/')[-1].split('_gtFine_labelIds')[0]
for i in range(len(val_image_list)):
assert val_image_list[i].split('/')[-1].split('_leftImg8bit')[
0] == val_mask_list[i].split('/')[-1].split('_gtFine_labelIds')[0]
def get_image(image_path, img_height=800, img_width=1600, mask=False, flip=0):
img = tf.io.read_file(image_path)
if not mask:
img = tf.cast(tf.image.decode_png(img, channels=3), dtype=tf.float32)
img = tf.image.resize(images=img, size=[img_height, img_width])
img = tf.image.random_brightness(img, max_delta=50.)
img = tf.image.random_saturation(img, lower=0.5, upper=1.5)
img = tf.image.random_hue(img, max_delta=0.2)
img = tf.image.random_contrast(img, lower=0.5, upper=1.5)
img = tf.clip_by_value(img, 0, 255)
img = tf.case([
(tf.greater(flip, 0), lambda: tf.image.flip_left_right(img))
], default=lambda: img)
img = img[:, :, ::-1] - tf.constant([103.939, 116.779, 123.68])
else:
img = tf.image.decode_png(img, channels=1)
img = tf.cast(tf.image.resize(images=img, size=[
img_height, img_width]), dtype=tf.uint8)
img = tf.case([
(tf.greater(flip, 0), lambda: tf.image.flip_left_right(img))
], default=lambda: img)
return img
def random_crop(image, mask, H=512, W=512):
image_dims = image.shape
offset_h = tf.random.uniform(
shape=(1,), maxval=image_dims[0] - H, dtype=tf.int32)[0]
offset_w = tf.random.uniform(
shape=(1,), maxval=image_dims[1] - W, dtype=tf.int32)[0]
image = tf.image.crop_to_bounding_box(image,
offset_height=offset_h,
offset_width=offset_w,
target_height=H,
target_width=W)
mask = tf.image.crop_to_bounding_box(mask,
offset_height=offset_h,
offset_width=offset_w,
target_height=H,
target_width=W)
return image, mask
def load_data(image_path, mask_path, H=512, W=512):
flip = tf.random.uniform(
shape=[1, ], minval=0, maxval=2, dtype=tf.int32)[0]
image, mask = get_image(image_path, flip=flip), get_image(
mask_path, mask=True, flip=flip)
image, mask = random_crop(image, mask, H=H, W=W)
return image, mask
train_dataset = tf.data.Dataset.from_tensor_slices((image_list,
mask_list))
train_dataset = train_dataset.shuffle(buffer_size=128)
train_dataset = train_dataset.apply(
tf.data.experimental.map_and_batch(map_func=load_data,
batch_size=batch_size,
num_parallel_calls=tf.data.experimental.AUTOTUNE,
drop_remainder=True))
train_dataset = train_dataset.repeat()
train_dataset = train_dataset.prefetch(tf.data.experimental.AUTOTUNE)
print(train_dataset)
val_dataset = tf.data.Dataset.from_tensor_slices((val_image_list,
val_mask_list))
val_dataset = val_dataset.apply(
tf.data.experimental.map_and_batch(map_func=load_data,
batch_size=batch_size,
num_parallel_calls=tf.data.experimental.AUTOTUNE,
drop_remainder=True))
val_dataset = val_dataset.repeat()
val_dataset = val_dataset.prefetch(tf.data.experimental.AUTOTUNE)
loss = tf.losses.SparseCategoricalCrossentropy(from_logits=True)
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
model = DeepLabV3Plus(H, W, num_classes)
for layer in model.layers:
if isinstance(layer, tf.keras.layers.BatchNormalization):
layer.momentum = 0.9997
layer.epsilon = 1e-5
elif isinstance(layer, tf.keras.layers.Conv2D):
layer.kernel_regularizer = tf.keras.regularizers.l2(1e-4)
model.compile(loss=loss,
optimizer=tf.optimizers.Adam(learning_rate=1e-4),
metrics=['accuracy'])
tb = TensorBoard(log_dir='logs', write_graph=True, update_freq='batch')
mc = ModelCheckpoint(mode='min', filepath='top_weights.h5',
monitor='val_loss',
save_best_only='True',
save_weights_only='True', verbose=1)
callbacks = [mc, tb]
model.fit(train_dataset,
steps_per_epoch=len(image_list) // batch_size,
epochs=300,
validation_data=val_dataset,
validation_steps=len(val_image_list) // batch_size,
callbacks=callbacks)