-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathtest_loggers.py
336 lines (287 loc) · 12.2 KB
/
test_loggers.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import argparse
import os
import os.path
import pathlib
import tempfile
from time import sleep
import pytest
import torch
from torchrl.record.loggers.csv import CSVLogger
from torchrl.record.loggers.mlflow import _has_mlflow, _has_tv, MLFlowLogger
from torchrl.record.loggers.tensorboard import _has_tb, TensorboardLogger
from torchrl.record.loggers.wandb import _has_wandb, WandbLogger
if _has_tv:
import torchvision
if _has_tb:
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
if _has_mlflow:
import mlflow
@pytest.fixture
def tb_logger(tmp_path_factory):
tmpdir1 = tmp_path_factory.mktemp("tmpdir1")
exp_name = "ramala"
logger = TensorboardLogger(log_dir=tmpdir1, exp_name=exp_name)
yield logger
del logger
@pytest.mark.skipif(not _has_tb, reason="TensorBoard not installed")
class TestTensorboard:
@pytest.mark.parametrize("steps", [None, [1, 10, 11]])
def test_log_scalar(self, steps, tb_logger):
torch.manual_seed(0)
values = torch.rand(3)
for i in range(3):
scalar_name = "foo"
scalar_value = values[i].item()
tb_logger.log_scalar(
value=scalar_value,
name=scalar_name,
step=steps[i] if steps else None,
)
sleep(0.01) # wait until events are registered
event_acc = EventAccumulator(tb_logger.experiment.get_logdir())
event_acc.Reload()
assert len(event_acc.Scalars("foo")) == 3, str(event_acc.Scalars("foo"))
for i in range(3):
assert event_acc.Scalars("foo")[i].value == values[i]
if steps:
assert event_acc.Scalars("foo")[i].step == steps[i]
@pytest.mark.parametrize("steps", [None, [1, 10, 11]])
def test_log_video(self, steps, tb_logger):
torch.manual_seed(0)
# creating a sample video (T, C, H, W), where T - number of frames,
# C - number of image channels (e.g. 3 for RGB), H, W - image dimensions.
# the first 64 frames are black and the next 64 are white
video = torch.cat(
(torch.zeros(64, 1, 32, 32), torch.full((64, 1, 32, 32), 255))
)
video = video[None, :]
for i in range(3):
tb_logger.log_video(
name="foo",
video=video,
step=steps[i] if steps else None,
fps=6, # we can't test for the difference between fps, because the result is an encoded_string
)
sleep(0.01) # wait until events are registered
event_acc = EventAccumulator(tb_logger.experiment.get_logdir())
event_acc.Reload()
assert len(event_acc.Images("foo")) == 3, str(event_acc.Images("foo"))
# check that we catch the error in case the format of the tensor is wrong
# here the number of color channels is set to 2, which is not correct
video_wrong_format = torch.zeros(64, 2, 32, 32)
video_wrong_format = video_wrong_format[None, :]
with pytest.raises(Exception):
tb_logger.log_video(
name="foo",
video=video_wrong_format,
step=steps[i] if steps else None,
)
def test_log_histogram(self, tb_logger):
torch.manual_seed(0)
# test with torch
data = torch.randn(10)
tb_logger.log_histogram("hist", data, step=0, bins=2)
# test with np
data = torch.randn(10).numpy()
tb_logger.log_histogram("hist", data, step=1, bins=2)
class TestCSVLogger:
@pytest.mark.parametrize("steps", [None, [1, 10, 11]])
def test_log_scalar(self, steps):
torch.manual_seed(0)
with tempfile.TemporaryDirectory() as log_dir:
exp_name = "ramala"
logger = CSVLogger(log_dir=log_dir, exp_name=exp_name)
values = torch.rand(3)
for i in range(3):
scalar_name = "foo"
scalar_value = values[i].item()
logger.log_scalar(
value=scalar_value,
name=scalar_name,
step=steps[i] if steps else None,
)
with open(
os.path.join(log_dir, exp_name, "scalars", "foo.csv"), "r"
) as file:
for i, row in enumerate(file.readlines()):
step = steps[i] if steps else i
assert row == f"{step},{values[i].item()}\n"
@pytest.mark.parametrize("steps", [None, [1, 10, 11]])
def test_log_video(self, steps):
torch.manual_seed(0)
with tempfile.TemporaryDirectory() as log_dir:
exp_name = "ramala"
logger = CSVLogger(log_dir=log_dir, exp_name=exp_name)
# creating a sample video (T, C, H, W), where T - number of frames,
# C - number of image channels (e.g. 3 for RGB), H, W - image dimensions.
# the first 64 frames are black and the next 64 are white
video = torch.cat(
(torch.zeros(64, 1, 32, 32), torch.full((64, 1, 32, 32), 255))
)
video = video[None, :]
for i in range(3):
logger.log_video(
name="foo",
video=video,
step=steps[i] if steps else None,
)
sleep(0.01) # wait until events are registered
# check that the logged videos are the same as the initial video
video_file_name = "foo_" + ("0" if not steps else str(steps[0])) + ".pt"
logged_video = torch.load(
os.path.join(log_dir, exp_name, "videos", video_file_name)
)
assert torch.equal(video, logged_video), logged_video
# check that we catch the error in case the format of the tensor is wrong
video_wrong_format = torch.zeros(64, 2, 32, 32)
video_wrong_format = video_wrong_format[None, :]
with pytest.raises(Exception):
logger.log_video(
name="foo",
video=video_wrong_format,
step=steps[i] if steps else None,
)
def test_log_histogram(self):
torch.manual_seed(0)
with tempfile.TemporaryDirectory() as log_dir:
exp_name = "ramala"
logger = CSVLogger(log_dir=log_dir, exp_name=exp_name)
with pytest.raises(NotImplementedError):
data = torch.randn(10)
logger.log_histogram("hist", data, step=0, bins=2)
@pytest.fixture(scope="class")
def wandb_logger(tmp_path_factory):
tmpdir1 = tmp_path_factory.mktemp("tmpdir1")
exp_name = "ramala"
logger = WandbLogger(log_dir=tmpdir1, exp_name=exp_name, offline=True)
yield logger
logger.experiment.finish()
del logger
@pytest.mark.skipif(not _has_wandb, reason="Wandb not installed")
class TestWandbLogger:
@pytest.mark.parametrize("steps", [None, [1, 10, 11]])
def test_log_scalar(self, steps, wandb_logger):
torch.manual_seed(0)
values = torch.rand(3)
for i in range(3):
scalar_name = "foo"
scalar_value = values[i].item()
wandb_logger.log_scalar(
value=scalar_value,
name=scalar_name,
step=steps[i] if steps else None,
)
assert wandb_logger.experiment.summary["foo"] == values[-1].item()
assert wandb_logger.experiment.summary["_step"] == i if not steps else steps[i]
def test_log_video(self, wandb_logger):
torch.manual_seed(0)
# creating a sample video (T, C, H, W), where T - number of frames,
# C - number of image channels (e.g. 3 for RGB), H, W - image dimensions.
# the first 64 frames are black and the next 64 are white
video = torch.cat(
(torch.zeros(64, 1, 32, 32), torch.full((64, 1, 32, 32), 255))
)
video = video[None, :]
wandb_logger.log_video(
name="foo",
video=video,
fps=6,
)
wandb_logger.log_video(
name="foo_12fps",
video=video,
fps=24,
)
sleep(0.01) # wait until events are registered
# check that fps can be passed and that it has impact on the length of the video
video_6fps_size = wandb_logger.experiment.summary["foo"]["size"]
video_24fps_size = wandb_logger.experiment.summary["foo_12fps"]["size"]
assert video_6fps_size > video_24fps_size, video_6fps_size
# check that we catch the error in case the format of the tensor is wrong
video_wrong_format = torch.zeros(64, 2, 32, 32)
video_wrong_format = video_wrong_format[None, :]
with pytest.raises(Exception):
wandb_logger.log_video(
name="foo",
video=video_wrong_format,
)
def test_log_histogram(self, wandb_logger):
torch.manual_seed(0)
# test with torch
data = torch.randn(10)
wandb_logger.log_histogram("hist", data, step=0, bins=2)
# test with np
data = torch.randn(10).numpy()
wandb_logger.log_histogram("hist", data, step=1, bins=2)
@pytest.fixture
def mlflow_fixture():
torch.manual_seed(0)
with tempfile.TemporaryDirectory() as log_dir:
exp_name = "ramala"
log_dir_uri = pathlib.Path(log_dir).as_uri()
logger = MLFlowLogger(exp_name=exp_name, tracking_uri=log_dir_uri)
client = mlflow.MlflowClient()
yield logger, client
mlflow.end_run()
@pytest.mark.skipif(not _has_mlflow, reason="MLFlow not installed")
class TestMLFlowLogger:
@pytest.mark.parametrize("steps", [None, [1, 10, 11]])
def test_log_scalar(self, steps, mlflow_fixture):
logger, client = mlflow_fixture
values = torch.rand(3)
for i in range(3):
scalar_name = "foo"
scalar_value = values[i].item()
logger.log_scalar(
value=scalar_value,
name=scalar_name,
step=steps[i] if steps else None,
)
run_id = mlflow.active_run().info.run_id
for i, metric in enumerate(client.get_metric_history(run_id, "foo")):
assert metric.key == "foo"
assert metric.step == (steps[i] if steps else 0)
assert metric.value == values[i].item()
@pytest.mark.parametrize("steps", [None, [1, 10, 11]])
@pytest.mark.skipif(not _has_tv, reason="torchvision not installed")
def test_log_video(self, steps, mlflow_fixture):
logger, client = mlflow_fixture
videos = torch.cat(
(torch.full((3, 64, 3, 32, 32), 255), torch.zeros(3, 64, 3, 32, 32)),
dim=1,
)
fps = 6
for i in range(3):
logger.log_video(
name="test_video",
video=videos[i],
fps=fps,
step=steps[i] if steps else None,
)
run_id = mlflow.active_run().info.run_id
with tempfile.TemporaryDirectory() as artifacts_dir:
videos_dir = client.download_artifacts(run_id, "videos", artifacts_dir)
for i, video_name in enumerate(os.listdir(videos_dir)):
video_path = os.path.join(videos_dir, video_name)
loaded_video, _, _ = torchvision.io.read_video(
video_path, pts_unit="sec", output_format="TCHW"
)
if steps:
assert torch.allclose(loaded_video.int(), videos[i].int(), rtol=0.1)
else:
assert torch.allclose(
loaded_video.int(), videos[-1].int(), rtol=0.1
)
def test_log_histogram(self, mlflow_fixture):
logger, client = mlflow_fixture
torch.manual_seed(0)
with pytest.raises(NotImplementedError):
data = torch.randn(10)
logger.log_histogram("hist", data, step=0, bins=2)
if __name__ == "__main__":
args, unknown = argparse.ArgumentParser().parse_known_args()
pytest.main([__file__, "--capture", "no", "--exitfirst"] + unknown)