-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackbone_engine.py
More file actions
171 lines (112 loc) · 6.88 KB
/
backbone_engine.py
File metadata and controls
171 lines (112 loc) · 6.88 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
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 the feature embedding backbone
def train_step(model,
dataloader,
optimizer,
loss_function):
dataloader = dataloader.shuffle(1000)
# model.compile(optimizer=optimizer , experimental_run_tf_function=False)
# model.compile(optimizer=optimizer)
epochs_losses_summary= defaultdict(list)
print(f"[INFO] training on {len(dataloader)} pairs")
for i, batch in tqdm(enumerate(dataloader)):
input_images, template_images, labels,_instances = batch
# make sure the labels have the right shape
assert labels.shape == (input_images.shape[0], 8), "labels shape is not (batch_size, 8)"
# tf.config.run_functions_eagerly(True)
gt_matrix = DatasetTools.get_ground_truth_homographies(labels)
# tf.config.run_functions_eagerly(False)
warped_inputs, _ = DatasetTools._get_warped_sampled(images = input_images, homography_matrices = gt_matrix)
with tf.GradientTape() as tape:
#persistent=True
rgb_fmaps , ir_fmaps = model.call((input_images, template_images), training=True)
# total_loss = tf.constant(0.0)
tape.watch(rgb_fmaps)
tape.watch(ir_fmaps)
warped_fmaps,_ = DatasetTools._get_warped_sampled( images = rgb_fmaps,
homography_matrices = gt_matrix)
tape.watch(warped_fmaps)
total_loss , detailed_batch_losses = loss_functions.get_losses_febackbone( warped_inputs,
template_images,
warped_fmaps,
ir_fmaps,
loss_function)
# get gradients and backpropagate
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)
print(f"[VALUES] labels shape: {labels.shape}")
print(f"[VALUES] input_images shape: {input_images.shape}")
print(f"[VALUES] instances: {_instances}")
print("*"*100)
continue
grads_are_safe = np.array([ tf.math.is_finite(g).numpy().all() for g in grads ]).all()
assert grads_are_safe, F"Gradients are not safe at iteration: {i}"
#backpropagate
grads = [tf.clip_by_value(i,-0.1,0.1) for i in grads]
optimizer.apply_gradients(zip(grads, all_parameters))
detailed_batch_losses = {str(i): k.numpy() for i, k in detailed_batch_losses.items()}
# loss_message = " | ".join([str(str(i)+ " : " + str(k)) for i,k in detailed_batch_losses.items()])
# add losses to epoch losses
for key, value in detailed_batch_losses.items():
epochs_losses_summary[key].append(value)
# compute mean of losses
for key, value in epochs_losses_summary.items():
epochs_losses_summary[key] = np.mean(value)
# display losses
# display losses
log = " | ".join([str(str(i)+ " : " + str(k)) for i,k in epochs_losses_summary.items()])
# print(f"[train_loss] : {log}")
return model, epochs_losses_summary ,log
# test step for the feature embedding backbone
def test_step(model,
dataloader,
loss_function
):
epochs_losses_summary= defaultdict(list)
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)
# assert gt_matrix.shape == (input_images.shape[0], 3, 3), "gt_matrix shape is not (batch_size, 3, 3)"
warped_inputs, _ = DatasetTools._get_warped_sampled(images = input_images,
homography_matrices = gt_matrix)
rgb_fmaps , ir_fmaps = model.call((input_images, template_images), training=False)
warped_fmaps, _ = DatasetTools._get_warped_sampled(images = rgb_fmaps,
homography_matrices = gt_matrix)
total_loss , detailed_batch_losses = loss_functions.get_losses_febackbone(warped_inputs,
template_images,
warped_fmaps,
ir_fmaps,
loss_function)
# loss shouldn't be nan
# assert tf.reduce_all(tf.math.is_finite(tf.cast(total_loss, dtype="float"))), "Loss is NaN"
# add losses to epoch losses
detailed_batch_losses = {str(i): k.numpy() for i, k in detailed_batch_losses.items()}
for key, value in detailed_batch_losses.items():
epochs_losses_summary[key].append(value)
# log = " ".join([str(i + " :" + k + "\n") for i,k in batch_losses.items()])
# print(log)
# compute mean of losses
for key, value in epochs_losses_summary.items():
epochs_losses_summary[key] = np.mean(value)
# display losses
log = " | ".join([str(str(i)+ " :" + str(k)) for i,k in epochs_losses_summary.items()])
# print(f"[test_loss] : {log}")
return epochs_losses_summary ,log