first commit
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
"""Train a float32 YOLO detector on the prepared Russian sign dataset.
|
||||
|
||||
Expects tools/prepare_hf_dataset.py to have written data/russian-road-signs/data.yaml.
|
||||
Exports signs.tflite into assets/models and rewrites labels.txt from the
|
||||
trained class order.
|
||||
|
||||
python tools/train_signs_model.py
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument(
|
||||
"--data",
|
||||
type=Path,
|
||||
default=Path("data/russian-road-signs/data.yaml"),
|
||||
)
|
||||
parser.add_argument("--model", default="yolo11n.pt")
|
||||
parser.add_argument("--imgsz", type=int, default=320)
|
||||
parser.add_argument("--epochs", type=int, default=40)
|
||||
parser.add_argument("--batch", type=int, default=32)
|
||||
parser.add_argument("--device", default="0")
|
||||
parser.add_argument(
|
||||
"--weights",
|
||||
type=Path,
|
||||
default=None,
|
||||
help="Skip training and export this checkpoint to float32 TFLite.",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
if args.weights is None and not args.data.is_file():
|
||||
raise SystemExit(f"Missing {args.data}. Run tools/prepare_hf_dataset.py first.")
|
||||
|
||||
from ultralytics import YOLO
|
||||
|
||||
if args.weights is None:
|
||||
model = YOLO(args.model)
|
||||
result = model.train(
|
||||
data=str(args.data),
|
||||
imgsz=args.imgsz,
|
||||
epochs=args.epochs,
|
||||
batch=args.batch,
|
||||
device=args.device,
|
||||
workers=4,
|
||||
patience=8,
|
||||
project="runs/signs",
|
||||
name="yolo11n-russian-signs",
|
||||
exist_ok=True,
|
||||
)
|
||||
weights = Path(result.save_dir) / "weights" / "best.pt"
|
||||
else:
|
||||
weights = args.weights
|
||||
if not weights.is_file():
|
||||
raise SystemExit(f"Missing weights: {weights}")
|
||||
|
||||
trained = YOLO(str(weights))
|
||||
# LiteRT export is Linux/macOS only. On Windows the SavedModel path
|
||||
# still writes a float32 .tflite via onnx2tf.
|
||||
exported = Path(
|
||||
trained.export(format="saved_model", imgsz=args.imgsz, half=False, int8=False)
|
||||
)
|
||||
|
||||
tflite = _find_tflite(exported)
|
||||
destination = Path("assets/models/signs.tflite")
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(tflite, destination)
|
||||
|
||||
names = trained.names
|
||||
ordered = [str(names[index]) for index in range(len(names))]
|
||||
Path("assets/models/labels.txt").write_text(
|
||||
"\n".join(ordered) + "\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
print(f"Copied {tflite} -> {destination}")
|
||||
|
||||
|
||||
def _find_tflite(exported: Path) -> Path:
|
||||
candidates: list[Path] = []
|
||||
if exported.suffix == ".tflite" and exported.is_file():
|
||||
candidates.append(exported)
|
||||
search_root = exported if exported.is_dir() else exported.parent
|
||||
candidates.extend(search_root.rglob("*.tflite"))
|
||||
float32 = [
|
||||
path
|
||||
for path in candidates
|
||||
if "float32" in path.name and "float16" not in path.name and "int8" not in path.name
|
||||
]
|
||||
if float32:
|
||||
return float32[0]
|
||||
plain = [
|
||||
path
|
||||
for path in candidates
|
||||
if "float16" not in path.name and "int8" not in path.name and "quant" not in path.name
|
||||
]
|
||||
if plain:
|
||||
return plain[0]
|
||||
raise SystemExit(f"Export finished but no float32 .tflite file was found near {exported}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user