-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_training_trans.py
339 lines (263 loc) · 12.9 KB
/
final_training_trans.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import os
import glob
import pickle
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import binary_dilation, binary_erosion
from skimage.transform import resize
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras_unet_collection import models
from tensorflow import keras
from keras_unet_collection.utils import dummy_loader
import cv2
import gc
import os
import glob
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import binary_dilation, binary_erosion
from skimage.transform import resize
import tensorflow as tf
import os
from PIL import Image
# Path to data
SRC = 'cloth3d++_subset/'
print('SRC:', SRC)
n_epochs = 20
batch_size = 2
# %%
class TFRecordDataHandler:
def __init__(self, tfrecord_file, batch_size=32, shuffle=True, augment=False):
self.tfrecord_file = tfrecord_file
self.batch_size = batch_size
self.shuffle = shuffle
self.augment = augment
def _parse_function(self, proto):
feature_description = {
'image': tf.io.FixedLenFeature([], tf.string),
'depth': tf.io.FixedLenFeature([], tf.string),
'height': tf.io.FixedLenFeature([], tf.int64),
'width': tf.io.FixedLenFeature([], tf.int64),
'depth_height': tf.io.FixedLenFeature([], tf.int64),
'depth_width': tf.io.FixedLenFeature([], tf.int64)
}
parsed_features = tf.io.parse_single_example(proto, feature_description)
image = tf.io.decode_raw(parsed_features['image'], tf.uint8)
depth = tf.io.decode_raw(parsed_features['depth'], tf.float32)
height = parsed_features['height']
width = parsed_features['width']
image = tf.reshape(image, [height, width, 3])
depth = tf.reshape(depth, [parsed_features['depth_height'], parsed_features['depth_width']])
return image, depth
def _normalize(self, image, depth):
# Convert image to float for processing and normalize to range [0, 1]
image = tf.cast(image, tf.float32) / 255.0
# Create a mask where depth values are greater than zero
depth_mask = depth > 0
# Normalize depth based on masked regions
# Calculate the mean of the depth where it is greater than zero
depth_values = tf.boolean_mask(depth, depth_mask)
depth_mean = tf.reduce_mean(depth_values)
# Subtract the mean from the depth values where mask is true
depth = tf.where(depth_mask, depth - depth_mean, depth)
# Set depth values less than 0 to 0 after subtraction
depth = tf.maximum(depth, 0)
# Prepare the mask for RGB image normalization
mask = tf.tile(tf.expand_dims(depth_mask, axis=-1), [1, 1, 3])
# Masked image for mean and std deviation calculation
masked_image = tf.boolean_mask(image, mask)
mean, variance = tf.nn.moments(masked_image, axes=[0])
std_dev = tf.sqrt(variance + 1e-6) # Adding epsilon to avoid division by zero
# Apply the mask to image normalization
normalized_image = tf.where(
mask,
(image - mean) / std_dev,
image # Preserve original pixels where mask is False
)
return normalized_image, depth
def _augment(self, image, depth):
# Horizontal flip with 50% probability
if tf.random.uniform(()) > 0.5:
image = tf.image.flip_left_right(image)
depth = tf.image.flip_left_right(tf.expand_dims(depth, axis=-1))
depth = tf.squeeze(depth, axis=-1)
# Vertical flip with 50% probability
if tf.random.uniform(()) > 0.5:
image = tf.image.flip_up_down(image)
depth = tf.image.flip_up_down(tf.expand_dims(depth, axis=-1))
depth = tf.squeeze(depth, axis=-1)
# Random rotation
angle = tf.random.uniform([], -20, 20) * (3.14159265 / 180) # convert degrees to radians
image = tfa.image.rotate(image, angle)
depth = tfa.image.rotate(tf.expand_dims(depth, axis=-1), angle)
depth = tf.squeeze(depth, axis=-1)
# Random width shift
width_shift = tf.random.uniform([], -0.1, 0.1) * tf.shape(image)[1]
image = tf.keras.preprocessing.image.random_shift(image, wrg=width_shift, hrg=0, row_axis=0, col_axis=1, channel_axis=2)
depth = tf.keras.preprocessing.image.random_shift(tf.expand_dims(depth, axis=-1), wrg=width_shift, hrg=0, row_axis=0, col_axis=1, channel_axis=2)
depth = tf.squeeze(depth, axis=-1)
# Random height shift
height_shift = tf.random.uniform([], -0.1, 0.1) * tf.shape(image)[0]
image = tf.keras.preprocessing.image.random_shift(image, wrg=0, hrg=height_shift, row_axis=0, col_axis=1, channel_axis=2)
depth = tf.keras.preprocessing.image.random_shift(tf.expand_dims(depth, axis=-1), wrg=0, hrg=height_shift, row_axis=0, col_axis=1, channel_axis=2)
depth = tf.squeeze(depth, axis=-1)
# Random zoom
zoom_factor = tf.random.uniform([], 1 - 0.2, 1 + 0.2)
image = tf.image.resize(image, [tf.shape(image)[0] * zoom_factor, tf.shape(image)[1] * zoom_factor])
depth = tf.image.resize(tf.expand_dims(depth, axis=-1), [tf.shape(depth)[0] * zoom_factor, tf.shape(depth)[1] * zoom_factor])
depth = tf.squeeze(depth, axis=-1)
# Random brightness adjustment
image = tf.image.random_brightness(image, max_delta=0.1)
return image, depth
def load_dataset(self):
dataset = tf.data.TFRecordDataset(self.tfrecord_file)
dataset = dataset.map(self._parse_function, num_parallel_calls=tf.data.AUTOTUNE)
dataset = dataset.map(self._normalize, num_parallel_calls=tf.data.AUTOTUNE)
if self.augment:
dataset = dataset.map(self._augment, num_parallel_calls=tf.data.AUTOTUNE)
if self.shuffle:
dataset = dataset.shuffle(buffer_size=1000)
dataset = dataset.batch(self.batch_size)
dataset = dataset.prefetch(tf.data.AUTOTUNE)
return dataset
def get_num_samples(self):
dataset = tf.data.TFRecordDataset(self.tfrecord_file)
count = 0
for _ in dataset:
count += 1
return count
# %%
class LearningRateLogger(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
lr = self.model.optimizer.lr
if isinstance(lr, tf.keras.optimizers.schedules.LearningRateSchedule):
lr = lr(self.model.optimizer.iterations)
print(f"Epoch {epoch + 1}: Learning rate is {tf.keras.backend.get_value(lr):.6f}")
class TensorBoardLearningRateLogger(tf.keras.callbacks.Callback):
def __init__(self, log_dir='./logs'):
super(TensorBoardLearningRateLogger, self).__init__()
self.file_writer = tf.summary.create_file_writer(log_dir)
def on_epoch_end(self, epoch, logs=None):
lr = self.model.optimizer.lr
if isinstance(lr, tf.keras.optimizers.schedules.LearningRateSchedule):
lr = lr(self.model.optimizer.iterations)
with self.file_writer.as_default():
tf.summary.scalar('learning_rate', tf.keras.backend.get_value(lr), step=epoch)
# %%
def visualize_hist(history, show=True, filename=None, title='Training history'):
train_hist = history.history
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 4))
fig.suptitle(title, fontsize=14, fontweight='bold')
ax1.plot(train_hist['loss'])
ax1.plot(train_hist['val_loss'])
ax1.set(xlabel='epoch', ylabel='Loss')
ax1.legend(['train', 'valid'], loc='upper right')
ax2.plot(train_hist['mae'])
ax2.plot(train_hist['val_mae'])
ax2.set(xlabel='epoch', ylabel='MAE')
ax2.legend(['train', 'valid'], loc='upper right')
if show:
plt.show()
if filename is not None:
fig.savefig(filename)
# %%
class WarmUpCosineDecayScheduler(tf.keras.optimizers.schedules.LearningRateSchedule):
def __init__(self, initial_learning_rate, target_learning_rate, total_steps, warmup_steps):
super(WarmUpCosineDecayScheduler, self).__init__()
self.initial_learning_rate = initial_learning_rate
self.target_learning_rate = target_learning_rate
self.total_steps = total_steps
self.warmup_steps = warmup_steps
self.decay_steps = total_steps - warmup_steps
def __call__(self, step):
# Convert to float32 to ensure the operations are compatible
step = tf.cast(step, tf.float32)
warmup_steps = tf.cast(self.warmup_steps, tf.float32)
decay_steps = tf.cast(self.decay_steps, tf.float32)
# Compute the warmup learning rate
warmup_lr = self.initial_learning_rate + (self.target_learning_rate - self.initial_learning_rate) * (step / warmup_steps)
# Compute the cosine decay learning rate
cosine_decay = 0.5 * (1 + tf.cos(np.pi * (step - warmup_steps) / decay_steps))
decayed_lr = (self.target_learning_rate - self.initial_learning_rate) * cosine_decay + self.initial_learning_rate
# Choose the learning rate based on the step
learning_rate = tf.where(step < warmup_steps, warmup_lr, decayed_lr)
return learning_rate
def get_config(self):
return {
'initial_learning_rate': self.initial_learning_rate,
'target_learning_rate': self.target_learning_rate,
'total_steps': self.total_steps,
'warmup_steps': self.warmup_steps
}
# %%
# Parameters
initial_learning_rate = 1e-5
target_learning_rate = 1e-2
verbose = 1
shuffle = True
checkpoint = './best_model.h5'
trainloader = TFRecordDataHandler('train.tfrecords', batch_size=1, shuffle=shuffle, augment=True)
valloader = TFRecordDataHandler('validation.tfrecords', batch_size=1, shuffle=shuffle, augment=False)
train_dg = trainloader.load_dataset()
validation_dg = valloader.load_dataset()
num_samples_train = trainloader.get_num_samples()
print('Number of data used to train:')
print(num_samples_train * batch_size)
# print('')
for x, y in train_dg.take(1):
print("Input shape:", x.shape) # Should be (batch_size, height, width, channels)
print("Label shape:", y.shape) # Should be (batch_size, ...) depending on your task
total_steps = num_samples_train // num_samples_train * n_epochs
import datetime
for wu_ratio in [0.1]:
warmup_steps = int(wu_ratio * total_steps)
folder_name = f'transunet'
#get the time of the experiment
time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
experiment_name = folder_name + '_' + time
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=os.path.join("logs", experiment_name))
tensorboard_lr_logger = TensorBoardLearningRateLogger(os.path.join("logs", experiment_name))
# Create the learning rate schedule
lr_schedule = WarmUpCosineDecayScheduler(
initial_learning_rate=initial_learning_rate,
target_learning_rate=target_learning_rate,
total_steps=total_steps,
warmup_steps=warmup_steps
)
# Define the optimizer with the custom learning rate schedule
optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
model = models.transunet_2d((256, 256, 3), [64, 128, 256, 512, 1024], n_labels=1,
stack_num_down=2, stack_num_up=1,
activation='GELU', output_activation='Sigmoid',
batch_norm=True, pool='max', unpool=False, name='transunet_2d', embed_dim=100, num_heads=4)
print('Learning rate:', initial_learning_rate)
# defining the optimizer
model.compile(optimizer, loss=tf.keras.losses.MeanSquaredError(), metrics=['mae'])
# es = tf.keras.callbacks.EarlyStopping(monitor='val_mae', patience=3)
# saving the best model based on val_loss
mc = tf.keras.callbacks.ModelCheckpoint(os.path.join("models", experiment_name, checkpoint), monitor='val_mae', mode='min', save_best_only=True)
# training the model and saving the history (.take(10))
history = model.fit(train_dg, validation_data=validation_dg, epochs=n_epochs, verbose=verbose, workers=-1, use_multiprocessing=True,
callbacks=[tensorboard_callback, tensorboard_lr_logger, mc])
# Plot and write the history
if not os.path.exists(experiment_name):
os.makedirs(experiment_name)
filename = os.path.join(experiment_name, 'train_history.jpg')
visualize_hist(history, show=True, filename=filename)
#with open('train_history_lr_'+str(lr)+'.pkl', 'wb') as handle:
# pickle.dump(history.history, handle, protocol=pickle.HIGHEST_PROTOCOL)
# Get the best validation loss and the epoch
best_val_loss = min(history.history['val_loss'])
best_epoch = history.history['val_loss'].index(best_val_loss)
# d_lr[lr] = (best_val_loss, best_epoch)
print('Best validation loss:', best_val_loss, 'at epoch', best_epoch)
# Clear gpu memory
del model
gc.collect()
tf.keras.backend.clear_session()
# %%