Upload inference.py
Browse files- inference.py +23 -0
inference.py
CHANGED
|
@@ -37,6 +37,17 @@ plt = platform.system()
|
|
| 37 |
if plt != 'Windows':
|
| 38 |
pathlib.WindowsPath = pathlib.PosixPath
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
class ModelInference:
|
| 42 |
"""YOLOv8 inference implementation for New Zealand invasive species classifier."""
|
|
@@ -89,6 +100,18 @@ class ModelInference:
|
|
| 89 |
|
| 90 |
try:
|
| 91 |
self.model = YOLO(str(self.model_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
raise RuntimeError(f"Failed to load YOLOv8 model from {self.model_path}: {e}") from e
|
| 94 |
|
|
|
|
| 37 |
if plt != 'Windows':
|
| 38 |
pathlib.WindowsPath = pathlib.PosixPath
|
| 39 |
|
| 40 |
+
# This checkpoint was exported by an older ultralytics that pickled
|
| 41 |
+
# references to ultralytics.utils.yaml_load / yaml_save. Ultralytics 8.3
|
| 42 |
+
# renamed those to the YAML class, so restore the old names when they are
|
| 43 |
+
# gone. Guarded by the YAML check, so it's a no-op on the old ultralytics.
|
| 44 |
+
import ultralytics.utils as _ul_utils # noqa: E402
|
| 45 |
+
if hasattr(_ul_utils, 'YAML'):
|
| 46 |
+
if not hasattr(_ul_utils, 'yaml_load'):
|
| 47 |
+
_ul_utils.yaml_load = _ul_utils.YAML.load
|
| 48 |
+
if not hasattr(_ul_utils, 'yaml_save'):
|
| 49 |
+
_ul_utils.yaml_save = _ul_utils.YAML.save
|
| 50 |
+
|
| 51 |
|
| 52 |
class ModelInference:
|
| 53 |
"""YOLOv8 inference implementation for New Zealand invasive species classifier."""
|
|
|
|
| 100 |
|
| 101 |
try:
|
| 102 |
self.model = YOLO(str(self.model_path))
|
| 103 |
+
# This checkpoint's preprocessing transforms were pickled by an
|
| 104 |
+
# older ultralytics. On that same old ultralytics they work; on
|
| 105 |
+
# 8.3 (the same YAML refactor shimmed above) the crop transform no
|
| 106 |
+
# longer unpickles into a working class and raises at predict time.
|
| 107 |
+
# On the new ultralytics only, rebuild the standard classify
|
| 108 |
+
# transform. get_crop returns a square crop, so this is the same
|
| 109 |
+
# preprocessing (verified: reproduces the legacy baseline on both
|
| 110 |
+
# the old and new env).
|
| 111 |
+
if hasattr(_ul_utils, 'YAML'):
|
| 112 |
+
from ultralytics.data.augment import classify_transforms
|
| 113 |
+
imgsz = self.model.overrides.get("imgsz", 224)
|
| 114 |
+
self.model.model.transforms = classify_transforms(imgsz)
|
| 115 |
except Exception as e:
|
| 116 |
raise RuntimeError(f"Failed to load YOLOv8 model from {self.model_path}: {e}") from e
|
| 117 |
|