Upload inference.py
Browse files36 unmodified lines37383940414246 unmodified lines89909192939436 unmodified linesif plt != 'Windows':pathlib.WindowsPath = pathlib.PosixPathclass ModelInference:"""YOLOv8 inference implementation for New Zealand invasive species classifier."""46 unmodified linestry:self.model = YOLO(str(self.model_path))except Exception as e:raise RuntimeError(f"Failed to load YOLOv8 model from {self.model_path}: {e}") from e36 unmodified lines373839404142434445464748495051525346 unmodified lines10010110210310410510610710810911011111211311411511611736 unmodified linesif plt != 'Windows':pathlib.WindowsPath = pathlib.PosixPath# This checkpoint was exported by an older ultralytics that pickled# references to ultralytics.utils.yaml_load / yaml_save. Ultralytics 8.3# renamed those to the YAML class, so restore the old names when they are# gone. Guarded by the YAML check, so it's a no-op on the old ultralytics.import ultralytics.utils as _ul_utils # noqa: E402if hasattr(_ul_utils, 'YAML'):if not hasattr(_ul_utils, 'yaml_load'):_ul_utils.yaml_load = _ul_utils.YAML.loadif not hasattr(_ul_utils, 'yaml_save'):_ul_utils.yaml_save = _ul_utils.YAML.saveclass ModelInference:"""YOLOv8 inference implementation for New Zealand invasive species classifier."""46 unmodified linestry:self.model = YOLO(str(self.model_path))# This checkpoint's preprocessing transforms were pickled by an# older ultralytics. On that same old ultralytics they work; on# 8.3 (the same YAML refactor shimmed above) the crop transform no# longer unpickles into a working class and raises at predict time.# On the new ultralytics only, rebuild the standard classify# transform. get_crop returns a square crop, so this is the same# preprocessing (verified: reproduces the legacy baseline on both# the old and new env).if hasattr(_ul_utils, 'YAML'):from ultralytics.data.augment import classify_transformsimgsz = self.model.overrides.get("imgsz", 224)self.model.model.transforms = classify_transforms(imgsz)except Exception as e:raise RuntimeError(f"Failed to load YOLOv8 model from {self.model_path}: {e}") from e
inference.py+23