forked from CVLAB-Unibo/Real-time-self-adaptive-deep-stereo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
callbacks.py
206 lines (190 loc) · 9.41 KB
/
callbacks.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
import wandb
import tensorflow as tf
from madnet import colorize_img
class WandBImagesCallback(tf.keras.callbacks.Callback):
"""
Logs image input data and disparity predictions
to Weights and Biases dashboard. Can use the training data,
validation data or both.
Args:
training_data: tf.data.Dataset object used for the .fit
training.
validation_data: tf.data.Dataset object used for .fit
validation.
val_epochs: Epoch frequency to log images to W and B dashboard.
"""
def __init__(
self,
training_data=None,
validation_data=None,
val_epochs=1
):
self.val_epochs = val_epochs
self.training_data = None
self.validation_data = None
if training_data is not None:
self.training_data = iter(training_data)
if validation_data is not None:
self.validation_data = iter(validation_data)
def on_epoch_end(self, epoch, logs={}):
if epoch % self.val_epochs == 0:
if self.training_data is not None:
data = next(self.training_data)
x, y = data
shape = tf.shape(y)
if shape[0] > 1:
raise ValueError(f"Received batch_size {shape[0]} for training_data dataset. "
"Please make sure batch size is 1")
y_pred = self.model(x)
if y is not None:
# Updates stateful loss metrics.
self.model.compute_loss(x, y, y_pred)
train_logs = self.model.compute_metrics(x, y, y_pred, None)
wandb.log({"Train": train_logs}, commit=True)
train_images_dict = {
"Predicted Disparity": wandb.Image(colorize_img(y_pred, cmap='jet')[0].numpy()),
"Left Image": wandb.Image(x["left_input"].numpy()),
"Right Image": wandb.Image(x["right_input"].numpy())
}
if y is not None:
train_images_dict.update(
{"GroundTruth Disparity": wandb.Image(colorize_img(y, cmap="jet")[0].numpy())}
)
wandb.log({"Train": train_images_dict}, commit=True)
if self.validation_data is not None:
data = next(self.validation_data)
val_x, val_y = data
shape = tf.shape(val_y)
if shape[0] > 1:
raise ValueError(f"Received batch_size {shape[0]} for validation_data dataset. "
"Please make sure batch size is 1")
val_y_pred = self.model(val_x)
if val_y is not None:
# Updates stateful loss metrics.
self.model.compute_loss(val_x, val_y, val_y_pred)
val_logs = self.model.compute_metrics(val_x, val_y, val_y_pred, None)
wandb.log({"Val": val_logs}, commit=True)
val_images_dict = {
"Predicted Disparity": wandb.Image(colorize_img(val_y_pred, cmap='jet')[0].numpy()),
"Left Image": wandb.Image(val_x["left_input"].numpy()),
"Right Image": wandb.Image(val_x["right_input"].numpy())
}
if val_y is not None:
val_images_dict.update(
{"GroundTruth Disparity": wandb.Image(colorize_img(val_y, cmap="jet")[0].numpy())}
)
wandb.log({"Val": val_images_dict}, commit=True)
class TensorboardImagesCallback(tf.keras.callbacks.Callback):
"""
Logs image input data and disparity predictions
to Tensorboard dashboard. Can use the training data,
validation data or both.
Args:
training_data: tf.data.Dataset object used for the .fit
training.
validation_data: tf.data.Dataset object used for .fit
validation.
val_epochs: Epoch frequency to log images to Tensorboard.
"""
def __init__(
self,
training_data=None,
validation_data=None,
val_epochs=1
):
self.val_epochs = val_epochs
self.training_data = None
self.validation_data = None
if training_data is not None:
self.training_data = iter(training_data)
if validation_data is not None:
self.validation_data = iter(validation_data)
def on_epoch_end(self, epoch, logs={}):
if epoch % self.val_epochs == 0:
if self.training_data is not None:
data = next(self.training_data)
x, y = data
shape = tf.shape(x["left_input"])
if shape[0] > 1:
raise ValueError(f"Received batch_size {shape[0]} for training_data dataset. "
"Please make sure batch size is 1")
y_pred = self.model(x)
if y is not None:
# Updates stateful loss metrics.
self.model.compute_loss(x, y, y_pred)
train_logs = self.model.compute_metrics(x, y, y_pred, None)
train_logs = {"train_" + name: val for name, val in train_logs.items()}
for key, value in train_logs.items():
tf.summary.scalar(name=key, data=value, step=epoch)
tf.summary.image('train_01_predicted_disparity', colorize_img(y_pred, cmap='jet'),
step=epoch, max_outputs=1)
if y is not None:
tf.summary.image('train_02_groundtruth_disparity', colorize_img(y, cmap='jet'),
step=epoch, max_outputs=1)
tf.summary.image('train_03_left_image', x["left_input"], step=epoch, max_outputs=1)
tf.summary.image('train_04_right_image', x["right_input"], step=epoch, max_outputs=1)
if self.validation_data is not None:
data = next(self.validation_data)
val_x, val_y = data
shape = tf.shape(val_x["left_input"])
if shape[0] > 1:
raise ValueError(f"Received batch_size {shape[0]} for validation_data dataset. "
"Please make sure batch size is 1")
val_y_pred = self.model(val_x)
if val_y is not None:
# Updates stateful loss metrics.
self.model.compute_loss(val_x, val_y, val_y_pred)
val_logs = self.model.compute_metrics(val_x, val_y, val_y_pred, None)
val_logs = {"val_" + name: val for name, val in val_logs.items()}
for key, value in val_logs.items():
tf.summary.scalar(name=key, data=value, step=epoch)
tf.summary.image('val_01_predicted_disparity', colorize_img(val_y_pred, cmap='jet'),
step=epoch, max_outputs=1)
if val_y is not None:
tf.summary.image('val_02_groundtruth_disparity', colorize_img(val_y, cmap='jet'),
step=epoch, max_outputs=1)
tf.summary.image('val_03_left_image', val_x["left_input"], step=epoch, max_outputs=1)
tf.summary.image('val_04_right_image', val_x["right_input"], step=epoch, max_outputs=1)
class TensorboardTestImagesCallback(tf.keras.callbacks.Callback):
"""
Logs image input data and disparity predictions
to Tensorboard dashboard.
Args:
testing_data: tf.data.Dataset object used for the .fit
testing.
test_steps: Step frequency to log images to Tensorboard.
"""
def __init__(
self,
testing_data=None,
test_steps=1,
pred_dir=None
):
self.test_steps = test_steps
self.testing_data = iter(testing_data)
self.pred_dir = pred_dir
def on_test_batch_end(self, batch, logs={}):
if batch % self.test_steps == 0:
data = next(self.testing_data)
x, y = data
shape = tf.shape(x["left_input"])
if shape[0] > 1:
raise ValueError(f"Received batch_size {shape[0]} for testing_data dataset. "
"Please make sure batch size is 1")
y_pred = self.model(x)
# Updates stateful loss metrics.
self.model.compute_loss(x, y, y_pred)
test_logs = self.model.compute_metrics(x, y, y_pred, None)
test_logs = {"test_" + name: val for name, val in test_logs.items()}
for key, value in test_logs.items():
tf.summary.scalar(name=key, data=value, step=batch)
pred_colorized = colorize_img(y_pred, cmap='jet')
tf.summary.image('test_01_predicted_disparity', pred_colorized,
step=batch, max_outputs=20)
tf.summary.image('test_02_groundtruth_disparity', colorize_img(y, cmap='jet'),
step=batch, max_outputs=20)
tf.summary.image('test_03_left_image', x["left_input"], step=batch, max_outputs=20)
tf.summary.image('test_04_right_image', x["right_input"], step=batch, max_outputs=20)
if self.pred_dir is not None:
image_path = self.pred_dir + f"/step{batch}.png"
tf.keras.utils.save_img(image_path, pred_colorized[0])