Zhen Ye Claude Opus 4.6 commited on
Commit
a4e3c2b
·
1 Parent(s): e745021

fix: download yolo11m.pt via huggingface_hub to writable /tmp path

Browse files

ultralytics YOLO() tries to download to CWD which is read-only in
the container. Use hf_hub_download to fetch weights to /tmp/yolo_weights
then load from that path.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. models/detectors/yolov11.py +16 -6
models/detectors/yolov11.py CHANGED
@@ -1,19 +1,21 @@
1
  import logging
2
  import os
 
3
  from typing import List, Sequence
4
 
5
  import numpy as np
6
  import torch
7
-
8
- # Ensure ultralytics downloads weights to a writable directory (container /app is read-only)
9
- if "YOLO_CONFIG_DIR" not in os.environ:
10
- os.environ["YOLO_CONFIG_DIR"] = "/tmp/ultralytics"
11
-
12
  from ultralytics import YOLO
13
 
14
  from models.detectors.base import DetectionResult, ObjectDetector
15
  from utils.tiling import get_slice_bboxes, slice_image, shift_bboxes, batched_nms
16
 
 
 
 
 
 
17
 
18
  class Yolo11Detector(ObjectDetector):
19
  """YOLO11m detector with COCO-pretrained weights from Ultralytics."""
@@ -37,7 +39,15 @@ class Yolo11Detector(ObjectDetector):
37
  "Loading YOLO11m COCO-pretrained weights onto %s",
38
  self.device,
39
  )
40
- self.model = YOLO("yolo11m.pt")
 
 
 
 
 
 
 
 
41
  self.model.to(self.device)
42
  self.class_names = self.model.names
43
 
 
1
  import logging
2
  import os
3
+ from pathlib import Path
4
  from typing import List, Sequence
5
 
6
  import numpy as np
7
  import torch
8
+ from huggingface_hub import hf_hub_download
 
 
 
 
9
  from ultralytics import YOLO
10
 
11
  from models.detectors.base import DetectionResult, ObjectDetector
12
  from utils.tiling import get_slice_bboxes, slice_image, shift_bboxes, batched_nms
13
 
14
+ # Download YOLO11m weights to a writable cache (container /app is read-only).
15
+ _WEIGHTS_CACHE = Path(os.environ.get("YOLO_CACHE", "/tmp/yolo_weights"))
16
+ _WEIGHTS_CACHE.mkdir(parents=True, exist_ok=True)
17
+ _YOLO11M_PATH = _WEIGHTS_CACHE / "yolo11m.pt"
18
+
19
 
20
  class Yolo11Detector(ObjectDetector):
21
  """YOLO11m detector with COCO-pretrained weights from Ultralytics."""
 
39
  "Loading YOLO11m COCO-pretrained weights onto %s",
40
  self.device,
41
  )
42
+ # Download weights via huggingface_hub to a writable path, then load.
43
+ if not _YOLO11M_PATH.exists():
44
+ logging.info("Downloading yolo11m.pt to %s ...", _YOLO11M_PATH)
45
+ hf_hub_download(
46
+ repo_id="Ultralytics/YOLO11",
47
+ filename="yolo11m.pt",
48
+ local_dir=str(_WEIGHTS_CACHE),
49
+ )
50
+ self.model = YOLO(str(_YOLO11M_PATH))
51
  self.model.to(self.device)
52
  self.class_names = self.model.names
53