-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add patch_tensorboard
#204
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import gorilla | ||
import tensorflow as tf | ||
|
||
from dvclive import Live | ||
|
||
# pylint: disable=unused-argument, no-member | ||
|
||
|
||
def patch_tensorboard(override: bool = True, **kwargs): | ||
live = Live(**kwargs) | ||
settings = gorilla.Settings(allow_hit=True, store_hit=True) | ||
|
||
original_scalar = gorilla.Patch( | ||
tf.summary, "original_scalar", tf.summary.scalar, settings=settings | ||
) | ||
gorilla.apply(original_scalar) | ||
|
||
def log_scalar(name, data, step=None, description=None): | ||
if step is not None: | ||
if step != live.get_step(): | ||
live.set_step(step) | ||
live.log(name, data) | ||
if not override: | ||
tf.summary.original_scalar(name, data, step=None, description=None) | ||
|
||
original_image = gorilla.Patch( | ||
tf.summary, "original_image", tf.summary.image, settings=settings | ||
) | ||
gorilla.apply(original_image) | ||
|
||
def log_image(name, data, step=None, max_outputs=3, description=None): | ||
name += ".png" | ||
if step is not None: | ||
if step != live.get_step(): | ||
live.set_step(step) | ||
if len(data) > 1: | ||
for n, image in enumerate(data): | ||
if n > max_outputs: | ||
break | ||
live.log_image(f"sample-{n}-{name}", image) | ||
else: | ||
live.log_image(name, data[0]) | ||
|
||
if not override: | ||
tf.summary.original_image(name, data, step=None, description=None) | ||
|
||
scalar_patch = gorilla.Patch(tf.summary, "scalar", log_scalar, settings) | ||
gorilla.apply(scalar_patch) | ||
|
||
image_patch = gorilla.Patch(tf.summary, "image", log_image, settings) | ||
gorilla.apply(image_patch) | ||
|
||
return original_scalar, original_image, scalar_patch, image_patch |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import tensorflow as tf | ||
|
||
from dvclive import Live | ||
from tests.test_main import _parse_json | ||
|
||
# pylint: disable=unused-argument | ||
|
||
|
||
def test_tensorflow(tmp_dir): | ||
dvclive = Live() | ||
dvclive.log("int", tf.constant(1)) | ||
dvclive.log("float", tf.constant(1.5)) | ||
|
||
summary = _parse_json("dvclive.json") | ||
|
||
assert isinstance(summary["int"], int) | ||
assert summary["int"] == 1 | ||
assert isinstance(summary["float"], float) | ||
assert summary["float"] == 1.5 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,3 +179,37 @@ def test_keras_None_model_file_skip_load( | |
) | ||
|
||
assert load_weights.call_count == 0 | ||
|
||
|
||
def test_patch_tensorboard_keras_callback(tmp_dir, xor_model, mocker): | ||
import gorilla | ||
import tensorflow as tf | ||
|
||
from dvclive.tensorboard import patch_tensorboard | ||
|
||
scalar = mocker.spy(tf.summary, "scalar") | ||
image = mocker.spy(tf.summary, "image") | ||
|
||
patches = patch_tensorboard(path="logs") | ||
|
||
model, x, y = xor_model() | ||
|
||
model.fit( | ||
x, | ||
y, | ||
epochs=2, | ||
batch_size=1, | ||
callbacks=[tf.keras.callbacks.TensorBoard()], | ||
) | ||
|
||
assert not scalar.call_count | ||
assert not image.call_count | ||
|
||
assert os.path.exists("logs") | ||
logs, _ = read_logs("logs/scalars") | ||
|
||
assert "epoch_accuracy" in logs | ||
assert len(logs["epoch_accuracy"]) == 2 | ||
|
||
for patch in patches: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So in case of using Shouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to test it but I think that the patch doesn't persist across python executions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about the same process? I think safest way would be to let the user control when the |
||
gorilla.revert(patch) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import os | ||
|
||
import gorilla | ||
import tensorflow as tf | ||
|
||
from dvclive.tensorboard import patch_tensorboard | ||
from tests.test_main import _parse_json | ||
|
||
# pylint: disable=unused-argument, no-value-for-parameter | ||
|
||
|
||
def test_patch_tensorboard(tmp_dir, mocker): | ||
scalar = mocker.spy(tf.summary, "scalar") | ||
image = mocker.spy(tf.summary, "image") | ||
|
||
patches = patch_tensorboard() | ||
|
||
tf.summary.scalar("m", 0.5) | ||
tf.summary.image("image", [tf.zeros(shape=[8, 8, 1], dtype=tf.uint8)]) | ||
|
||
assert not scalar.call_count | ||
assert not image.call_count | ||
|
||
summary = _parse_json("dvclive.json") | ||
image_path = os.path.join("dvclive", "images", "image.png") | ||
assert summary["m"] == 0.5 | ||
assert os.path.exists(image_path) | ||
|
||
for patch in patches: | ||
gorilla.revert(patch) | ||
|
||
|
||
def test_patch_tensorboard_no_override(tmp_dir, mocker): | ||
scalar = mocker.spy(tf.summary, "scalar") | ||
image = mocker.spy(tf.summary, "image") | ||
|
||
patches = patch_tensorboard(override=False) | ||
|
||
tf.summary.scalar("m", 0.5) | ||
tf.summary.image("image", [tf.zeros(shape=[8, 8, 1], dtype=tf.uint8)]) | ||
|
||
assert scalar.call_count | ||
assert image.call_count | ||
|
||
summary = _parse_json("dvclive.json") | ||
image_path = os.path.join("dvclive", "images", "image.png") | ||
assert summary["m"] == 0.5 | ||
assert os.path.exists(image_path) | ||
|
||
for patch in patches: | ||
gorilla.revert(patch) | ||
|
||
|
||
def test_patch_tensorboard_live_args(tmp_dir, mocker): | ||
scalar = mocker.spy(tf.summary, "scalar") | ||
image = mocker.spy(tf.summary, "image") | ||
|
||
patches = patch_tensorboard(path="logs") | ||
|
||
tf.summary.scalar("m", 0.5) | ||
tf.summary.image("image", [tf.zeros(shape=[8, 8, 1], dtype=tf.uint8)]) | ||
|
||
assert not scalar.call_count | ||
assert not image.call_count | ||
|
||
summary = _parse_json("logs.json") | ||
image_path = os.path.join("logs", "images", "image.png") | ||
assert summary["m"] == 0.5 | ||
assert os.path.exists(image_path) | ||
|
||
for patch in patches: | ||
gorilla.revert(patch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if
patch_tensorboard
should instead optionally accept aLive
instance