Improve training startup visibility with preparing stage and dataset prep progress logs (update 22)
Browse files- CHANGELOG.md +6 -0
- app.py +39 -6
CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
| 1 |
# Changelog
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
## Update ke-21 - 2026-04-15
|
| 4 |
- Perbaikan antrian event: polling monitor training (`load/timer/refresh`) diset `queue=False` agar tidak menghambat tombol `Mulai Training`.
|
| 5 |
- Tambah log awal saat request training dipicu, sehingga indikator log muncul lebih cepat.
|
|
|
|
| 1 |
# Changelog
|
| 2 |
|
| 3 |
+
## Update ke-22 - 2026-04-15
|
| 4 |
+
- Tambah status tahap training lebih jelas: `preparing_dataset` lalu `training`.
|
| 5 |
+
- Saat tombol training ditekan, log langsung menulis `Preparing dataset...` agar terlihat proses benar-benar dimulai.
|
| 6 |
+
- Progress persiapan dataset kini menulis log per 10 gambar (train/val) untuk feedback yang lebih real-time.
|
| 7 |
+
- Minor update dinaikkan ke-22.
|
| 8 |
+
|
| 9 |
## Update ke-21 - 2026-04-15
|
| 10 |
- Perbaikan antrian event: polling monitor training (`load/timer/refresh`) diset `queue=False` agar tidak menghambat tombol `Mulai Training`.
|
| 11 |
- Tambah log awal saat request training dipicu, sehingga indikator log muncul lebih cepat.
|
app.py
CHANGED
|
@@ -25,7 +25,7 @@ from ultralytics import YOLO
|
|
| 25 |
CM_INIT = {"tp": 0, "fp": 0, "fn": 0, "tn": 0}
|
| 26 |
EVAL_CLASSES = ["Sesuai", "Tidak Sesuai"]
|
| 27 |
APP_VERSION = os.getenv("APP_VERSION", "v0.9.0")
|
| 28 |
-
APP_MINOR_UPDATE = os.getenv("APP_MINOR_UPDATE", "
|
| 29 |
|
| 30 |
|
| 31 |
# Gradio/Spaces can trigger noisy asyncio cleanup traces ("Invalid file descriptor: -1")
|
|
@@ -467,7 +467,12 @@ def _write_pseudo_label(img_rgb: np.ndarray, label_path: Path, conf_threshold: f
|
|
| 467 |
return len(lines)
|
| 468 |
|
| 469 |
|
| 470 |
-
def _prepare_training_dataset(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
train_src = dataset_root / "train"
|
| 472 |
test_src = dataset_root / "test"
|
| 473 |
if not train_src.exists() or not test_src.exists():
|
|
@@ -482,10 +487,17 @@ def _prepare_training_dataset(dataset_root: Path, use_pseudo_if_missing: bool, p
|
|
| 482 |
for p in (img_train, img_val, lbl_train, lbl_val):
|
| 483 |
p.mkdir(parents=True, exist_ok=True)
|
| 484 |
|
| 485 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
images = _iter_images(src)
|
| 487 |
if not images:
|
| 488 |
raise ValueError(f"Tidak ada gambar di {src}")
|
|
|
|
| 489 |
copied = 0
|
| 490 |
labeled = 0
|
| 491 |
for img in images:
|
|
@@ -508,10 +520,12 @@ def _prepare_training_dataset(dataset_root: Path, use_pseudo_if_missing: bool, p
|
|
| 508 |
rgb = cv2.cvtColor(cv2.imread(str(target_img)), cv2.COLOR_BGR2RGB)
|
| 509 |
labeled += 1 if _write_pseudo_label(rgb, label_target, pseudo_conf) >= 0 else 0
|
| 510 |
copied += 1
|
|
|
|
|
|
|
| 511 |
return copied, labeled
|
| 512 |
|
| 513 |
-
c_train, l_train = process_split(train_src, img_train, lbl_train)
|
| 514 |
-
c_val, l_val = process_split(test_src, img_val, lbl_val)
|
| 515 |
if l_train == 0 or l_val == 0:
|
| 516 |
raise ValueError(
|
| 517 |
"Label tidak ditemukan. Aktifkan pseudo-label atau sediakan file .txt YOLO untuk train/test."
|
|
@@ -955,7 +969,11 @@ def _training_status_md(state: Dict[str, Any], rows_count: int) -> str:
|
|
| 955 |
status = state.get("status", "unknown")
|
| 956 |
run_name = state.get("run_name", "-")
|
| 957 |
if status == "running":
|
| 958 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 959 |
if status == "done":
|
| 960 |
return f"Status training: **Selesai** \nRun: `{run_name}`"
|
| 961 |
if status == "failed":
|
|
@@ -1015,11 +1033,24 @@ def run_training(
|
|
| 1015 |
return poll_training_status()
|
| 1016 |
|
| 1017 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1018 |
dataset_root = base_dir / "dataset"
|
| 1019 |
data_yaml, prep_info = _prepare_training_dataset(
|
| 1020 |
dataset_root=dataset_root,
|
| 1021 |
use_pseudo_if_missing=use_pseudo_if_missing,
|
| 1022 |
pseudo_conf=float(pseudo_conf),
|
|
|
|
| 1023 |
)
|
| 1024 |
|
| 1025 |
model_path = str(base_dir / "yolov8n.pt") if (base_dir / "yolov8n.pt").exists() else resolve_model_path("YOLOv8n")
|
|
@@ -1140,6 +1171,7 @@ except Exception as e:
|
|
| 1140 |
paths["status"],
|
| 1141 |
{
|
| 1142 |
"status": "running",
|
|
|
|
| 1143 |
"run_name": run_name,
|
| 1144 |
"pid": proc.pid,
|
| 1145 |
"save_dir": str(save_dir),
|
|
@@ -1155,6 +1187,7 @@ except Exception as e:
|
|
| 1155 |
paths["status"],
|
| 1156 |
{
|
| 1157 |
"status": "failed",
|
|
|
|
| 1158 |
"run_name": "-",
|
| 1159 |
"error": str(err),
|
| 1160 |
"ended_at": datetime.now().isoformat(),
|
|
|
|
| 25 |
CM_INIT = {"tp": 0, "fp": 0, "fn": 0, "tn": 0}
|
| 26 |
EVAL_CLASSES = ["Sesuai", "Tidak Sesuai"]
|
| 27 |
APP_VERSION = os.getenv("APP_VERSION", "v0.9.0")
|
| 28 |
+
APP_MINOR_UPDATE = os.getenv("APP_MINOR_UPDATE", "22")
|
| 29 |
|
| 30 |
|
| 31 |
# Gradio/Spaces can trigger noisy asyncio cleanup traces ("Invalid file descriptor: -1")
|
|
|
|
| 467 |
return len(lines)
|
| 468 |
|
| 469 |
|
| 470 |
+
def _prepare_training_dataset(
|
| 471 |
+
dataset_root: Path,
|
| 472 |
+
use_pseudo_if_missing: bool,
|
| 473 |
+
pseudo_conf: float,
|
| 474 |
+
log_file: Path | None = None,
|
| 475 |
+
) -> Tuple[Path, str]:
|
| 476 |
train_src = dataset_root / "train"
|
| 477 |
test_src = dataset_root / "test"
|
| 478 |
if not train_src.exists() or not test_src.exists():
|
|
|
|
| 487 |
for p in (img_train, img_val, lbl_train, lbl_val):
|
| 488 |
p.mkdir(parents=True, exist_ok=True)
|
| 489 |
|
| 490 |
+
def _log(msg: str):
|
| 491 |
+
if log_file is None:
|
| 492 |
+
return
|
| 493 |
+
with log_file.open("a", encoding="utf-8") as lf:
|
| 494 |
+
lf.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {msg}\n")
|
| 495 |
+
|
| 496 |
+
def process_split(src: Path, out_img: Path, out_lbl: Path, split_name: str) -> Tuple[int, int]:
|
| 497 |
images = _iter_images(src)
|
| 498 |
if not images:
|
| 499 |
raise ValueError(f"Tidak ada gambar di {src}")
|
| 500 |
+
_log(f"Prepare split `{split_name}`: total gambar={len(images)}")
|
| 501 |
copied = 0
|
| 502 |
labeled = 0
|
| 503 |
for img in images:
|
|
|
|
| 520 |
rgb = cv2.cvtColor(cv2.imread(str(target_img)), cv2.COLOR_BGR2RGB)
|
| 521 |
labeled += 1 if _write_pseudo_label(rgb, label_target, pseudo_conf) >= 0 else 0
|
| 522 |
copied += 1
|
| 523 |
+
if copied % 10 == 0 or copied == len(images):
|
| 524 |
+
_log(f"Prepare `{split_name}` progress: {copied}/{len(images)} (labeled={labeled})")
|
| 525 |
return copied, labeled
|
| 526 |
|
| 527 |
+
c_train, l_train = process_split(train_src, img_train, lbl_train, "train")
|
| 528 |
+
c_val, l_val = process_split(test_src, img_val, lbl_val, "val")
|
| 529 |
if l_train == 0 or l_val == 0:
|
| 530 |
raise ValueError(
|
| 531 |
"Label tidak ditemukan. Aktifkan pseudo-label atau sediakan file .txt YOLO untuk train/test."
|
|
|
|
| 969 |
status = state.get("status", "unknown")
|
| 970 |
run_name = state.get("run_name", "-")
|
| 971 |
if status == "running":
|
| 972 |
+
stage = state.get("stage", "running")
|
| 973 |
+
return (
|
| 974 |
+
f"Status training: **Running** \nRun: `{run_name}` \n"
|
| 975 |
+
f"Tahap: **{stage}** \nEpoch tercatat: **{rows_count}**"
|
| 976 |
+
)
|
| 977 |
if status == "done":
|
| 978 |
return f"Status training: **Selesai** \nRun: `{run_name}`"
|
| 979 |
if status == "failed":
|
|
|
|
| 1033 |
return poll_training_status()
|
| 1034 |
|
| 1035 |
try:
|
| 1036 |
+
_write_status(
|
| 1037 |
+
paths["status"],
|
| 1038 |
+
{
|
| 1039 |
+
"status": "running",
|
| 1040 |
+
"stage": "preparing_dataset",
|
| 1041 |
+
"run_name": current.get("run_name", "-"),
|
| 1042 |
+
"started_at": datetime.now().isoformat(),
|
| 1043 |
+
},
|
| 1044 |
+
)
|
| 1045 |
+
with paths["log"].open("a", encoding="utf-8") as lf:
|
| 1046 |
+
lf.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Preparing dataset...\n")
|
| 1047 |
+
|
| 1048 |
dataset_root = base_dir / "dataset"
|
| 1049 |
data_yaml, prep_info = _prepare_training_dataset(
|
| 1050 |
dataset_root=dataset_root,
|
| 1051 |
use_pseudo_if_missing=use_pseudo_if_missing,
|
| 1052 |
pseudo_conf=float(pseudo_conf),
|
| 1053 |
+
log_file=paths["log"],
|
| 1054 |
)
|
| 1055 |
|
| 1056 |
model_path = str(base_dir / "yolov8n.pt") if (base_dir / "yolov8n.pt").exists() else resolve_model_path("YOLOv8n")
|
|
|
|
| 1171 |
paths["status"],
|
| 1172 |
{
|
| 1173 |
"status": "running",
|
| 1174 |
+
"stage": "training",
|
| 1175 |
"run_name": run_name,
|
| 1176 |
"pid": proc.pid,
|
| 1177 |
"save_dir": str(save_dir),
|
|
|
|
| 1187 |
paths["status"],
|
| 1188 |
{
|
| 1189 |
"status": "failed",
|
| 1190 |
+
"stage": "failed_before_spawn",
|
| 1191 |
"run_name": "-",
|
| 1192 |
"error": str(err),
|
| 1193 |
"ended_at": datetime.now().isoformat(),
|