Skip to content
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

frameworks: Use log_artifact. #500

Merged
merged 3 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/dvclive/fastai.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def after_epoch(self):
# fast.ai calls after_epoch but we don't want to increase the step.
if logged_metrics:
if self.model_file:
self.learn.save(self.model_file, with_opt=self.with_opt)
file = self.learn.save(self.model_file, with_opt=self.with_opt)
self.live.log_artifact(str(file))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dberenbaum Opened this one to discuss before implementing all frameworks.

I think we should go ahead and deprecate model_file (#499) but for now I just mad both approaches work (model_file and external callback).

self.live.next_step()

def after_fit(self):
Expand All @@ -76,4 +77,7 @@ def after_fit(self):
if _inside_fine_tune() and not self.freeze_stage_ended:
self.freeze_stage_ended = True
else:
if hasattr(self, "save_model"):
if self.save_model.last_saved_path:
self.live.log_artifact(str(self.save_model.last_saved_path))
daavoo marked this conversation as resolved.
Show resolved Hide resolved
self.live.end()
19 changes: 18 additions & 1 deletion tests/test_frameworks/test_fastai.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import pytest
from fastai.callback.tracker import SaveModelCallback
from fastai.tabular.all import (
Categorify,
Normalize,
Expand Down Expand Up @@ -70,9 +71,12 @@ def test_fastai_model_file(tmp_dir, data_loader, mocker):
learn.remove_cb(ProgressCallback)
learn.model_dir = os.path.abspath("./")
save = mocker.spy(learn, "save")
learn.fit_one_cycle(2, cbs=[DVCLiveCallback("model", with_opt=True)])
live_callback = DVCLiveCallback("model", with_opt=True)
log_artifact = mocker.patch.object(live_callback.live, "log_artifact")
learn.fit_one_cycle(2, cbs=[live_callback])
assert (tmp_dir / "model.pth").is_file()
save.assert_called_with("model", with_opt=True)
log_artifact.assert_called_with(str(tmp_dir / "model.pth"))


def test_fastai_pass_logger():
Expand Down Expand Up @@ -116,3 +120,16 @@ def test_fast_ai_avoid_unnecessary_end_calls(tmp_dir, data_loader, mocker):
learn.fine_tune(2, cbs=[callback])
assert end.call_count == 1
assert after_fit.call_count == 2


def test_fastai_save_model_callback(tmp_dir, data_loader, mocker):
learn = tabular_learner(data_loader, metrics=accuracy)
learn.remove_cb(ProgressCallback)
learn.model_dir = os.path.abspath("./")

save_callback = SaveModelCallback()
live_callback = DVCLiveCallback()
log_artifact = mocker.patch.object(live_callback.live, "log_artifact")
learn.fit_one_cycle(2, cbs=[save_callback, live_callback])
assert (tmp_dir / "model.pth").is_file()
log_artifact.assert_called_with(str(save_callback.last_saved_path))