Upload folder using huggingface_hub
Browse files
mmdet_code/__pycache__/dala_hf_upload.cpython-313.pyc
ADDED
|
Binary file (8.66 kB). View file
|
|
|
mmdet_code/__pycache__/train_dala.cpython-313.pyc
CHANGED
|
Binary files a/mmdet_code/__pycache__/train_dala.cpython-313.pyc and b/mmdet_code/__pycache__/train_dala.cpython-313.pyc differ
|
|
|
mmdet_code/dala_hf_upload.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Best-effort Hugging Face uploads for DALA ablation artifacts.
|
| 2 |
+
|
| 3 |
+
This module is intentionally small and optional. Training must not fail just
|
| 4 |
+
because the network, token, or huggingface_hub package is unavailable.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
from dataclasses import dataclass
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
from typing import Any, Mapping
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
DEFAULT_REPO_ID = "TLMHoang/mmdet_code"
|
| 16 |
+
DEFAULT_REPO_TYPE = "dataset"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@dataclass(frozen=True)
|
| 20 |
+
class DalaHfUploadConfig:
|
| 21 |
+
enabled: bool
|
| 22 |
+
repo_id: str
|
| 23 |
+
repo_type: str
|
| 24 |
+
token: str
|
| 25 |
+
path_prefix: str
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _as_bool(value: Any, default: bool) -> bool:
|
| 29 |
+
if value is None:
|
| 30 |
+
return default
|
| 31 |
+
if isinstance(value, bool):
|
| 32 |
+
return value
|
| 33 |
+
return str(value).strip().lower() not in {"0", "false", "no", "off"}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def _clean_remote_part(value: str) -> str:
|
| 37 |
+
return str(value).strip().strip("/").replace("\\", "/")
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def build_upload_config(plan: Mapping[str, Any], output_root: Path) -> DalaHfUploadConfig:
|
| 41 |
+
"""Build upload settings from notebook plan and environment variables."""
|
| 42 |
+
enabled = _as_bool(plan.get("hf_upload", True), True)
|
| 43 |
+
repo_id = str(
|
| 44 |
+
plan.get("hf_repo_id")
|
| 45 |
+
or os.environ.get("DALA_HF_REPO_ID")
|
| 46 |
+
or os.environ.get("HF_REPO_ID")
|
| 47 |
+
or DEFAULT_REPO_ID
|
| 48 |
+
)
|
| 49 |
+
repo_type = str(
|
| 50 |
+
plan.get("hf_repo_type")
|
| 51 |
+
or os.environ.get("DALA_HF_REPO_TYPE")
|
| 52 |
+
or DEFAULT_REPO_TYPE
|
| 53 |
+
)
|
| 54 |
+
token = str(plan.get("hf_token") or os.environ.get("HF_TOKEN") or "")
|
| 55 |
+
prefix = str(
|
| 56 |
+
plan.get("hf_path_prefix")
|
| 57 |
+
or os.environ.get("DALA_HF_PATH_PREFIX")
|
| 58 |
+
or f"dala_runs/{output_root.name}"
|
| 59 |
+
)
|
| 60 |
+
return DalaHfUploadConfig(
|
| 61 |
+
enabled=enabled,
|
| 62 |
+
repo_id=repo_id,
|
| 63 |
+
repo_type=repo_type,
|
| 64 |
+
token=token,
|
| 65 |
+
path_prefix=_clean_remote_part(prefix),
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
class DalaHfUploader:
|
| 70 |
+
"""Upload CSV progress and per-case best.pt artifacts on a best-effort basis."""
|
| 71 |
+
|
| 72 |
+
def __init__(self, plan: Mapping[str, Any], output_root: Path, csv_path: Path) -> None:
|
| 73 |
+
self.output_root = output_root.resolve()
|
| 74 |
+
self.csv_path = csv_path.resolve()
|
| 75 |
+
self.config = build_upload_config(plan, self.output_root)
|
| 76 |
+
self._api = None
|
| 77 |
+
self._disabled_reason = ""
|
| 78 |
+
|
| 79 |
+
def _get_api(self):
|
| 80 |
+
if not self.config.enabled:
|
| 81 |
+
self._disabled_reason = "hf_upload disabled"
|
| 82 |
+
return None
|
| 83 |
+
if not self.config.token:
|
| 84 |
+
self._disabled_reason = "HF_TOKEN missing"
|
| 85 |
+
return None
|
| 86 |
+
if self._api is not None:
|
| 87 |
+
return self._api
|
| 88 |
+
try:
|
| 89 |
+
from huggingface_hub import HfApi
|
| 90 |
+
except ImportError:
|
| 91 |
+
self._disabled_reason = "huggingface_hub missing"
|
| 92 |
+
return None
|
| 93 |
+
self._api = HfApi(token=self.config.token)
|
| 94 |
+
return self._api
|
| 95 |
+
|
| 96 |
+
def _remote_path(self, local_path: Path) -> str:
|
| 97 |
+
local_path = local_path.resolve()
|
| 98 |
+
try:
|
| 99 |
+
relative = local_path.relative_to(self.output_root)
|
| 100 |
+
except ValueError:
|
| 101 |
+
relative = Path(local_path.name)
|
| 102 |
+
return _clean_remote_part(f"{self.config.path_prefix}/{relative.as_posix()}")
|
| 103 |
+
|
| 104 |
+
def _upload_file(self, local_path: Path, remote_path: str, message: str) -> None:
|
| 105 |
+
api = self._get_api()
|
| 106 |
+
if api is None:
|
| 107 |
+
print(f"HF UPLOAD SKIP {local_path}: {self._disabled_reason}", flush=True)
|
| 108 |
+
return
|
| 109 |
+
if not local_path.is_file():
|
| 110 |
+
print(f"HF UPLOAD SKIP missing file: {local_path}", flush=True)
|
| 111 |
+
return
|
| 112 |
+
try:
|
| 113 |
+
api.create_repo(
|
| 114 |
+
repo_id=self.config.repo_id,
|
| 115 |
+
repo_type=self.config.repo_type,
|
| 116 |
+
private=False,
|
| 117 |
+
exist_ok=True,
|
| 118 |
+
)
|
| 119 |
+
api.upload_file(
|
| 120 |
+
path_or_fileobj=str(local_path),
|
| 121 |
+
path_in_repo=remote_path,
|
| 122 |
+
repo_id=self.config.repo_id,
|
| 123 |
+
repo_type=self.config.repo_type,
|
| 124 |
+
commit_message=message,
|
| 125 |
+
)
|
| 126 |
+
print(
|
| 127 |
+
f"HF UPLOAD OK {local_path} -> "
|
| 128 |
+
f"hf://{self.config.repo_type}/{self.config.repo_id}/{remote_path}",
|
| 129 |
+
flush=True,
|
| 130 |
+
)
|
| 131 |
+
except Exception as exc:
|
| 132 |
+
print(
|
| 133 |
+
f"HF UPLOAD WARN {local_path}: {type(exc).__name__}: {exc}",
|
| 134 |
+
flush=True,
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
def upload_csv(self) -> None:
|
| 138 |
+
"""Upload the current CSV progress snapshot."""
|
| 139 |
+
self._upload_file(
|
| 140 |
+
self.csv_path,
|
| 141 |
+
self._remote_path(self.csv_path),
|
| 142 |
+
f"Update DALA results CSV: {self.csv_path.name}",
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
def upload_best_pt(self, row: Mapping[str, Any]) -> None:
|
| 146 |
+
"""Upload a completed run's best.pt if the row contains one."""
|
| 147 |
+
best_pt = str(row.get("best_pt") or "").strip()
|
| 148 |
+
if not best_pt:
|
| 149 |
+
return
|
| 150 |
+
local_path = Path(best_pt)
|
| 151 |
+
remote_path = self._remote_path(local_path)
|
| 152 |
+
self._upload_file(
|
| 153 |
+
local_path,
|
| 154 |
+
remote_path,
|
| 155 |
+
f"Upload DALA best.pt for {row.get('ablation_case', 'case')}",
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
def upload_case_update(self, row: Mapping[str, Any]) -> None:
|
| 159 |
+
"""Upload artifacts after one run row reaches ok/failed status."""
|
| 160 |
+
if row.get("status") == "ok":
|
| 161 |
+
self.upload_best_pt(row)
|
| 162 |
+
self.upload_csv()
|
mmdet_code/train_dala.py
CHANGED
|
@@ -27,7 +27,7 @@ from pathlib import Path
|
|
| 27 |
from typing import Any, Mapping
|
| 28 |
|
| 29 |
|
| 30 |
-
RUNNER_VERSION = "1.1.
|
| 31 |
MODEL_FAMILY = "EfficientDet-D0"
|
| 32 |
METRIC_NAME = "coco/bbox_mAP"
|
| 33 |
ABLATION_IDENTIFIER_PATTERN = re.compile(r"^[A-Za-z][A-Za-z0-9_-]*$")
|
|
@@ -54,6 +54,11 @@ RUNTIME_SITECUSTOMIZE_SOURCE = Path(__file__).with_name(
|
|
| 54 |
"dala_runtime_sitecustomize.py"
|
| 55 |
)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def _as_path(value: Any) -> Path:
|
| 59 |
return Path(value).expanduser().resolve()
|
|
@@ -179,6 +184,10 @@ class DalaAblationRunner:
|
|
| 179 |
self.generated_dir = self.output_root / "generated_configs"
|
| 180 |
self.artifact_root = self.output_root / "artifacts"
|
| 181 |
self.scheduler_mode = str(plan.get("lr_scheduler", "multistep")).lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
self.variants = {
|
| 183 |
name: dict(spec) for name, spec in plan["variants"].items()
|
| 184 |
if bool(spec.get("enabled", True))
|
|
@@ -575,6 +584,11 @@ class DalaAblationRunner:
|
|
| 575 |
writer.writerows(all_rows)
|
| 576 |
temporary.replace(self.csv_path)
|
| 577 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 578 |
def _base_row(self, variant: str) -> dict[str, Any]:
|
| 579 |
spec = self.variants[variant]
|
| 580 |
plateau = self.scheduler_mode == "plateau"
|
|
@@ -712,6 +726,7 @@ class DalaAblationRunner:
|
|
| 712 |
existing = run_rows.get((variant, seed))
|
| 713 |
if self.plan.get("skip_completed", True) and existing and existing.get("status") == "ok":
|
| 714 |
print(f"SKIP {variant} seed={seed}: đã có metric hợp lệ", flush=True)
|
|
|
|
| 715 |
continue
|
| 716 |
running = self._base_row(variant)
|
| 717 |
running.update({
|
|
@@ -737,10 +752,13 @@ class DalaAblationRunner:
|
|
| 737 |
row = running
|
| 738 |
run_rows[(variant, seed)] = row
|
| 739 |
self.write_results(run_rows, complexity)
|
|
|
|
| 740 |
if not self.plan.get("continue_on_error", False):
|
| 741 |
raise
|
|
|
|
| 742 |
run_rows[(variant, seed)] = row
|
| 743 |
self.write_results(run_rows, complexity)
|
|
|
|
| 744 |
|
| 745 |
try:
|
| 746 |
import pandas as pd
|
|
|
|
| 27 |
from typing import Any, Mapping
|
| 28 |
|
| 29 |
|
| 30 |
+
RUNNER_VERSION = "1.1.2"
|
| 31 |
MODEL_FAMILY = "EfficientDet-D0"
|
| 32 |
METRIC_NAME = "coco/bbox_mAP"
|
| 33 |
ABLATION_IDENTIFIER_PATTERN = re.compile(r"^[A-Za-z][A-Za-z0-9_-]*$")
|
|
|
|
| 54 |
"dala_runtime_sitecustomize.py"
|
| 55 |
)
|
| 56 |
|
| 57 |
+
try:
|
| 58 |
+
from dala_hf_upload import DalaHfUploader
|
| 59 |
+
except ImportError: # pragma: no cover - keeps older snapshots runnable.
|
| 60 |
+
DalaHfUploader = None
|
| 61 |
+
|
| 62 |
|
| 63 |
def _as_path(value: Any) -> Path:
|
| 64 |
return Path(value).expanduser().resolve()
|
|
|
|
| 184 |
self.generated_dir = self.output_root / "generated_configs"
|
| 185 |
self.artifact_root = self.output_root / "artifacts"
|
| 186 |
self.scheduler_mode = str(plan.get("lr_scheduler", "multistep")).lower()
|
| 187 |
+
self.hf_uploader = (
|
| 188 |
+
DalaHfUploader(self.plan, self.output_root, self.csv_path)
|
| 189 |
+
if DalaHfUploader is not None else None
|
| 190 |
+
)
|
| 191 |
self.variants = {
|
| 192 |
name: dict(spec) for name, spec in plan["variants"].items()
|
| 193 |
if bool(spec.get("enabled", True))
|
|
|
|
| 584 |
writer.writerows(all_rows)
|
| 585 |
temporary.replace(self.csv_path)
|
| 586 |
|
| 587 |
+
def upload_case_update(self, row: Mapping[str, Any]) -> None:
|
| 588 |
+
if self.hf_uploader is None:
|
| 589 |
+
return
|
| 590 |
+
self.hf_uploader.upload_case_update(row)
|
| 591 |
+
|
| 592 |
def _base_row(self, variant: str) -> dict[str, Any]:
|
| 593 |
spec = self.variants[variant]
|
| 594 |
plateau = self.scheduler_mode == "plateau"
|
|
|
|
| 726 |
existing = run_rows.get((variant, seed))
|
| 727 |
if self.plan.get("skip_completed", True) and existing and existing.get("status") == "ok":
|
| 728 |
print(f"SKIP {variant} seed={seed}: đã có metric hợp lệ", flush=True)
|
| 729 |
+
self.upload_case_update(existing)
|
| 730 |
continue
|
| 731 |
running = self._base_row(variant)
|
| 732 |
running.update({
|
|
|
|
| 752 |
row = running
|
| 753 |
run_rows[(variant, seed)] = row
|
| 754 |
self.write_results(run_rows, complexity)
|
| 755 |
+
self.upload_case_update(row)
|
| 756 |
if not self.plan.get("continue_on_error", False):
|
| 757 |
raise
|
| 758 |
+
continue
|
| 759 |
run_rows[(variant, seed)] = row
|
| 760 |
self.write_results(run_rows, complexity)
|
| 761 |
+
self.upload_case_update(row)
|
| 762 |
|
| 763 |
try:
|
| 764 |
import pandas as pd
|