baskarmother commited on
Commit
ab48c24
·
verified ·
1 Parent(s): d670cbb

Upload train_ppe_fixed.py

Browse files
Files changed (1) hide show
  1. train_ppe_fixed.py +13 -4
train_ppe_fixed.py CHANGED
@@ -1,13 +1,24 @@
1
  #!/usr/bin/env python3
2
  """
3
  PPE Compliance Detection Training - FIXED VERSION
 
4
  - Downloads keremberke dataset as ZIP files (script-based datasets no longer supported)
5
  - Uses model.train() return value correctly (no results.best)
6
  - Pushes best.pt to HuggingFace Hub after training
7
  """
8
 
9
- import os
10
  import sys
 
 
 
 
 
 
 
 
 
 
11
  import zipfile
12
  import shutil
13
  import json
@@ -179,7 +190,7 @@ def merge_datasets(ppe_extract_dir: Path, keremberke_dir: Path, output_dir: Path
179
 
180
  if ppe_dir is None:
181
  print(" ERROR: Could not find PPE dataset structure")
182
- sys.exit(1)
183
 
184
  print(f" Found PPE dataset at: {ppe_dir}")
185
 
@@ -244,8 +255,6 @@ def train_model(data_yaml_path: Path):
244
 
245
  model = YOLO("yolov8s.pt")
246
 
247
- # model.train() returns metrics, NOT the model object
248
- # The trained weights are auto-saved to /app/runs/ppe_improved/weights/best.pt
249
  model.train(
250
  data=str(data_yaml_path),
251
  epochs=EPOCHS,
 
1
  #!/usr/bin/env python3
2
  """
3
  PPE Compliance Detection Training - FIXED VERSION
4
+ - Swaps opencv-python for opencv-python-headless before importing ultralytics
5
  - Downloads keremberke dataset as ZIP files (script-based datasets no longer supported)
6
  - Uses model.train() return value correctly (no results.best)
7
  - Pushes best.pt to HuggingFace Hub after training
8
  """
9
 
10
+ import subprocess
11
  import sys
12
+ import os
13
+
14
+ # FIX: opencv-python needs libGL which is missing in container; use headless instead
15
+ print("[0/5] Swapping opencv-python for opencv-python-headless...")
16
+ subprocess.run([sys.executable, "-m", "pip", "uninstall", "-y", "opencv-python"],
17
+ capture_output=True)
18
+ subprocess.run([sys.executable, "-m", "pip", "install", "--quiet", "opencv-python-headless"],
19
+ capture_output=True)
20
+ print(" Done")
21
+
22
  import zipfile
23
  import shutil
24
  import json
 
190
 
191
  if ppe_dir is None:
192
  print(" ERROR: Could not find PPE dataset structure")
193
+ os._exit(1)
194
 
195
  print(f" Found PPE dataset at: {ppe_dir}")
196
 
 
255
 
256
  model = YOLO("yolov8s.pt")
257
 
 
 
258
  model.train(
259
  data=str(data_yaml_path),
260
  epochs=EPOCHS,