-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregression_head_engine.py
More file actions
285 lines (224 loc) · 14.8 KB
/
regression_head_engine.py
File metadata and controls
285 lines (224 loc) · 14.8 KB
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
import tensorflow as tf
import keras
from tensorflow.keras import layers
import numpy as np
from pathlib import Path
from tqdm.auto import tqdm
import Tools.backboneUtils as backboneUtils
import Tools.loss_functions as loss_functions
import Tools.datasetTools as DatasetTools
import Tools.utilities as common_utils
from collections import defaultdict
tf.keras.mixed_precision.set_global_policy('mixed_float16')
# train step for regression head
def train_step(model,
backBone,
dataloader,
optimizer,
predicting_homography,
backbone_loss_function,
loss_function_to_use):
"""
A train step for the regression head
"""
dataloader = dataloader.shuffle(1000)
# model.compile(optimizer=optimizer,experimental_run_tf_function=False)
epochs_losses_summary= {"backbone": defaultdict(list),
"regression_head": defaultdict(list)
}
assert backBone is not None, "the feature embedding backbone is not defined"
print(f"[INFO] training on {len(dataloader)} pairs")
for i, batch in tqdm(enumerate(dataloader)):
input_images, template_images, labels,_instances = batch
# add batch dim if shape is not (batch_size, height, width, channels)
if len(input_images.shape) != 4:
input_images = tf.expand_dims(input_images, axis=0)
template_images = tf.expand_dims(template_images, axis=0)
labels = tf.expand_dims(labels, axis=0)
# pass the input images through the backbone
gt_matrix=DatasetTools.get_ground_truth_homographies(labels)
warped_inputs, _ = DatasetTools._get_warped_sampled(input_images, gt_matrix)
rgb_fmaps , ir_fmaps = backBone.call((input_images, template_images),training=False)
# weighted sum if needed
summed_rgb_fmaps = tf.constant(1.0) * tf.cast(input_images,"float") + tf.constant(0.2) * tf.cast(rgb_fmaps,"float")
summed_ir_fmaps = tf.constant(0.2) * tf.cast(template_images,"float") + tf.constant(1.0) * tf.cast(ir_fmaps,'float')
# padd the ir_fmaps to match the shape of the rgb_fmaps
ir_fmaps_padded = backboneUtils.get_padded_fmaps(fmaps=summed_ir_fmaps, desired_shape = rgb_fmaps.shape)
# concatenate the rgb_fmaps and ir_fmaps
concatenated_fmaps = tf.concat([summed_rgb_fmaps, ir_fmaps_padded], axis=-1)
with tf.GradientTape() as tape:
predictions = model.call((concatenated_fmaps), training=True)
tape.watch(predictions)
total_loss , detailed_batch_losses = loss_functions.get_losses_regression_head( predictions = predictions,
ground_truth_corners = labels,
gt_matrix = gt_matrix,
predicting_homography=predicting_homography,
loss_function_to_use = loss_function_to_use)
# assert tf.reduce_all(tf.math.is_finite(total_loss)), "Loss is NaN"
all_parameters= model.trainable_variables
try:
grads = tape.gradient(total_loss, all_parameters, unconnected_gradients=tf.UnconnectedGradients.ZERO)
except Exception as e:
# somestupid error
print("*"*100)
print(f"[ERROR] ----------------------- skipping batch {i} --------------------------")
print("*"*100)
continue
grads = [tf.clip_by_value(i,-0.1,0.1) for i in grads]
# assert tf.reduce_all(tf.math.is_finite(grads)), "Gradients in regression head are inf or NaN"
optimizer.apply_gradients(zip(grads, all_parameters))
# add losses to epoch losses
detailed_batch_losses = {key: value.numpy() for key, value in detailed_batch_losses.items()}
for key, value in detailed_batch_losses.items():
epochs_losses_summary["regression_head"][key].append(value)
# backbone losses
warped_fmaps,_ = DatasetTools._get_warped_sampled(rgb_fmaps, gt_matrix)
total_loss_backbone , detailed_batch_losses_backbone = loss_functions.get_losses_febackbone(warped_inputs,
template_images,
warped_fmaps,
ir_fmaps,
backbone_loss_function)
# loss shouldn't be nan
# assert tf.reduce_all(tf.math.is_finite(total_loss_backbone)), "total_loss_backbone Loss is NaN"
# add losses to epoch losses
detailed_batch_losses_backbone = {key: value.numpy() for key, value in detailed_batch_losses_backbone.items()}
for key, value in detailed_batch_losses_backbone.items():
epochs_losses_summary["backbone"][key].append(value)
# log = " ".join([str(i + " :" + k + "\n") for i,k in detailed_batch_losses.items()])
# print(log)
# compute mean of losses
for step, losses in epochs_losses_summary.items():
for key, value in losses.items():
epochs_losses_summary[step][key] = np.mean(value)
# display losses
log = " | ".join([str(str(i)+ " : " + str(k)) for i,k in epochs_losses_summary["regression_head"].items()])
# print(f"[train_loss] : {log}")
return model , epochs_losses_summary , log
# test step for the regression head
def test_step(model,
backBone,
dataloader,
predicting_homography,
backbone_loss_function,
loss_function_to_use):
"""
Test step for the regression head
"""
epochs_losses_summary= {"backbone": defaultdict(list),
"regression_head": defaultdict(list)
}
assert backBone is not None, "the feature embedding backbone is not defined"
print(f"[INFO] testing on {len(dataloader)} pairs")
for i, batch in tqdm(enumerate(dataloader)):
input_images, template_images, labels,_instances = batch
# add batch dim if shape is not (batch_size, height, width, channels)
# if len(input_images.shape) != 4:
# input_images = tf.expand_dims(input_images, axis=0)
# template_images = tf.expand_dims(template_images, axis=0)
# labels = tf.expand_dims(labels, axis=0)
# pass the input images through the backbone
gt_matrix=DatasetTools.get_ground_truth_homographies(labels)
warped_inputs, _ = DatasetTools._get_warped_sampled(input_images, gt_matrix)
rgb_fmaps , ir_fmaps = backBone.call((input_images, template_images),training=False)
# padd the ir_fmaps to match the shape of the rgb_fmaps
# weighted sum if needed
summed_rgb_fmaps = tf.constant(1.0) * tf.cast(input_images,"float") + tf.constant(0.2) * tf.cast(rgb_fmaps,"float")
summed_ir_fmaps = tf.constant(0.2) * tf.cast(template_images,"float") + tf.constant(1.0) * tf.cast(ir_fmaps,'float')
# padd the ir_fmaps to match the shape of the rgb_fmaps
ir_fmaps_padded = backboneUtils.get_padded_fmaps(fmaps=summed_ir_fmaps, desired_shape = rgb_fmaps.shape)
# concatenate the rgb_fmaps and ir_fmaps
concatenated_fmaps = tf.concat([summed_rgb_fmaps, ir_fmaps_padded], axis=-1)
predictions = model.call((concatenated_fmaps),training=False)
total_loss , detailed_batch_losses = loss_functions.get_losses_regression_head( predictions = predictions,
ground_truth_corners = labels,
gt_matrix = gt_matrix,
predicting_homography=predicting_homography,
loss_function_to_use = loss_function_to_use)
# assert tf.reduce_all(tf.math.is_finite(total_loss)), "Loss is NaN or Inf"
# add losses to epoch losses
detailed_batch_losses = {key: value.numpy() for key, value in detailed_batch_losses.items()}
for key, value in detailed_batch_losses.items():
epochs_losses_summary["regression_head"][key].append(value)
# backbone losses
warped_fmaps,_ = DatasetTools._get_warped_sampled(rgb_fmaps, gt_matrix)
total_loss_backbone , detailed_batch_losses_backbone = loss_functions.get_losses_febackbone(warped_inputs,
template_images,
warped_fmaps,
ir_fmaps,
backbone_loss_function)
# loss shouldn't be nan
# assert tf.reduce_all(tf.math.is_finite(total_loss_backbone)), "BackBone Loss is NaN or Inf"
# add losses to epoch losses
detailed_batch_losses_backbone = {key: value.numpy() for key, value in detailed_batch_losses_backbone.items()}
for key, value in detailed_batch_losses_backbone.items():
epochs_losses_summary["backbone"][key].append(value)
# compute mean of losses
for step, losses in epochs_losses_summary.items():
for key, value in losses.items():
epochs_losses_summary[step][key] = np.mean(value)
# display losses
log = " | ".join([str(str(i)+ " : " + str(k)) for i,k in epochs_losses_summary["regression_head"].items()])
# print(f"[test_loss] : {log}")
return epochs_losses_summary , log
# run predictions
def predict(model,
backBone,
dataloader,
predicting_homography,
backbone_loss_function,
loss_function_to_use):
"""
Test step for the regression head
"""
epochs_losses_summary= {"backbone": defaultdict(list),
"regression_head": defaultdict(list)
}
assert backBone is not None, "the feature embedding backbone is not defined"
print(f"[INFO] testing on {len(dataloader)} pairs")
for i, batch in tqdm(enumerate(dataloader)):
input_images, template_images, labels,_instances = batch
gt_matrix=DatasetTools.get_ground_truth_homographies(labels)
warped_inputs, _ = DatasetTools._get_warped_sampled(input_images, gt_matrix)
rgb_fmaps , ir_fmaps = backBone.call((input_images, template_images),training=False)
# padd the ir_fmaps to match the shape of the rgb_fmaps
# weighted sum if needed
summed_rgb_fmaps = tf.constant(1.0) * tf.cast(input_images,"float") + tf.constant(0.2) * tf.cast(rgb_fmaps,"float")
summed_ir_fmaps = tf.constant(0.2) * tf.cast(template_images,"float") + tf.constant(1.0) * tf.cast(ir_fmaps,'float')
# padd the ir_fmaps to match the shape of the rgb_fmaps
ir_fmaps_padded = backboneUtils.get_padded_fmaps(fmaps=summed_ir_fmaps, desired_shape = rgb_fmaps.shape)
# concatenate the rgb_fmaps and ir_fmaps
concatenated_fmaps = tf.concat([summed_rgb_fmaps, ir_fmaps_padded], axis=-1)
predictions = model.call((concatenated_fmaps),training=False)
total_loss , detailed_batch_losses = loss_functions.get_inference_losses( predictions = predictions,
ground_truth_corners = labels,
gt_matrix = gt_matrix,
predicting_homography=predicting_homography,
loss_function_to_use = loss_function_to_use)
# assert tf.reduce_all(tf.math.is_finite(total_loss)), "Loss is NaN or Inf"
# add losses to epoch losses
detailed_batch_losses = {key: value.numpy() for key, value in detailed_batch_losses.items()}
for key, value in detailed_batch_losses.items():
epochs_losses_summary["regression_head"][key].append(value)
# backbone losses
warped_fmaps,_ = DatasetTools._get_warped_sampled(rgb_fmaps, gt_matrix)
total_loss_backbone , detailed_batch_losses_backbone = loss_functions.get_losses_febackbone(warped_inputs,
template_images,
warped_fmaps,
ir_fmaps,
backbone_loss_function)
# loss shouldn't be nan
# assert tf.reduce_all(tf.math.is_finite(total_loss_backbone)), "BackBone Loss is NaN or Inf"
# add losses to epoch losses
detailed_batch_losses_backbone = {key: value.numpy() for key, value in detailed_batch_losses_backbone.items()}
for key, value in detailed_batch_losses_backbone.items():
epochs_losses_summary["backbone"][key].append(value)
losses_summary = {epochs_losses_summary["regression_head"]}
losses_summary.update(epochs_losses_summary["backbone"])
# compute mean of losses
for step, losses in epochs_losses_summary.items():
for key, value in losses.items():
epochs_losses_summary[step][key] = np.mean(value)
# display losses
log = " | ".join([str(str(i)+ " : " + str(k)) for i,k in epochs_losses_summary["regression_head"].items()])
# print(f"[test_loss] : {log}")
return epochs_losses_summary , log , losses_summary