Visitar URL original
Fix CSVLogger: read existing learning_stats.csv file when resuming previous training by deruyter92 · Pull Request #3177 · DeepLabCut/DeepLabCut · GitHub
Skip to content
Merged
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
Prev Previous commit
Next Next commit
CSVLogger: change _load_existing_data handling of partly valid CSVs
In case an exception occurrs during reading (e.g. corrupted CSV, or CSV that misses 'step' column), the reader now starts with an empty CSV instead of keeping the succeeded rows from the existent CSV.
  • Loading branch information
deruyter92 committed Jan 19, 2026
commit dfa3c3f0f4c0623a2f98c6247a75dc1e48ffbd74
13 changes: 10 additions & 3 deletions deeplabcut/pose_estimation_pytorch/runners/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,22 +456,26 @@ def log_config(self, config: dict = None) -> None:

def _load_existing_data(self) -> None:
"""Loads existing CSV data if the log file exists"""
logging.info(f"Loading existing CSV data from {self.log_file}")
try:
with open(self.log_file, "r", newline="") as f:
reader = csv.DictReader(f)

# Update logged metrics from header
if "step" not in reader.fieldnames:
return # Invalid format
raise ValueError("Invalid CSV format: missing 'step' column")

metric_names = [m for m in reader.fieldnames if m != "step"]
self._logged_metrics.update(metric_names)

# Load data rows
steps = []
metric_store = []
for row in reader:
try:
step = int(row["step"])
except (ValueError, KeyError):
logging.warning(f"Invalid step value in row: {row}")
continue

# Convert metric values: empty strings -> None, numeric strings -> float
Expand All @@ -486,14 +490,17 @@ def _load_existing_data(self) -> None:
except ValueError:
step_metrics[metric] = value

self._steps.append(step)
self._metric_store.append(step_metrics)
steps.append(step)
metric_store.append(step_metrics)

except Exception as e:
logging.warning(
f"Failed to load existing CSV data from {self.log_file}: {e}. "
"Starting with empty log."
)
return
self._steps.extend(steps)
self._metric_store.extend(metric_store)

def _prepare_logs(self) -> list[list]:
"""Prepares the data to log as a list of strings"""
Expand Down