Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 12 additions & 2 deletions src/deepforest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,19 @@ def calculate_empty_frame_accuracy(self, ground_df, predictions_df):

def on_validation_epoch_end(self):
"""Compute metrics."""
# Check if we are in a standalone validation run
if self.trainer is not None:
is_validate_phase = self.trainer.state.fn == "validate"
else:
is_validate_phase = False

# Evaluate every n epochs or during standalone validation
evaluate_this_epoch = is_validate_phase or (
self.config["validation"]["val_accuracy_interval"]
<= self.config["train"]["epochs"] and
self.current_epoch % self.config["validation"]["val_accuracy_interval"] == 0)

#Evaluate every n epochs
if self.current_epoch % self.config["validation"]["val_accuracy_interval"] == 0:
if evaluate_this_epoch:

if len(self.predictions) == 0:
return None
Expand Down
11 changes: 11 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,3 +995,14 @@ def test_set_labels_invalid_length(m): # Expect a ValueError when setting an inv
invalid_mapping = {"Object": 0, "Extra": 1}
with pytest.raises(ValueError):
m.set_labels(invalid_mapping)

def test_validation_interval_greater_than_epochs(m):
# Set interval higher than max_epochs to disable evaluation
m.config["validation"]["val_accuracy_interval"] = 3
m.config["train"]["epochs"] = 2
m.create_trainer()
m.trainer.fit(m)

assert "box_precision" not in m.trainer.logged_metrics
assert "box_recall" not in m.trainer.logged_metrics
assert "empty_frame_accuracy" not in m.trainer.logged_metrics