|
|
@ -5,7 +5,7 @@ from ultralytics import yolo # noqa required for python usage
|
|
|
|
from ultralytics.nn.tasks import ClassificationModel, DetectionModel, SegmentationModel, attempt_load_weights
|
|
|
|
from ultralytics.nn.tasks import ClassificationModel, DetectionModel, SegmentationModel, attempt_load_weights
|
|
|
|
# from ultralytics.yolo.data.utils import check_dataset, check_dataset_yaml
|
|
|
|
# from ultralytics.yolo.data.utils import check_dataset, check_dataset_yaml
|
|
|
|
from ultralytics.yolo.engine.trainer import DEFAULT_CONFIG
|
|
|
|
from ultralytics.yolo.engine.trainer import DEFAULT_CONFIG
|
|
|
|
from ultralytics.yolo.utils import LOGGER
|
|
|
|
from ultralytics.yolo.utils import HELP_MSG, LOGGER
|
|
|
|
from ultralytics.yolo.utils.checks import check_yaml
|
|
|
|
from ultralytics.yolo.utils.checks import check_yaml
|
|
|
|
from ultralytics.yolo.utils.configs import get_config
|
|
|
|
from ultralytics.yolo.utils.configs import get_config
|
|
|
|
from ultralytics.yolo.utils.files import yaml_load
|
|
|
|
from ultralytics.yolo.utils.files import yaml_load
|
|
|
@ -28,12 +28,16 @@ class YOLO:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Python interface which emulates a model-like behaviour by wrapping trainers.
|
|
|
|
Python interface which emulates a model-like behaviour by wrapping trainers.
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
__init_key = object()
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, type="v8") -> None:
|
|
|
|
def __init__(self, init_key=None, type="v8") -> None:
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
Args:
|
|
|
|
type (str): Type/version of models to use
|
|
|
|
type (str): Type/version of models to use
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
if init_key != YOLO.__init_key:
|
|
|
|
|
|
|
|
raise Exception(HELP_MSG)
|
|
|
|
|
|
|
|
|
|
|
|
self.type = type
|
|
|
|
self.type = type
|
|
|
|
self.ModelClass = None
|
|
|
|
self.ModelClass = None
|
|
|
|
self.TrainerClass = None
|
|
|
|
self.TrainerClass = None
|
|
|
@ -44,8 +48,10 @@ class YOLO:
|
|
|
|
self.task = None
|
|
|
|
self.task = None
|
|
|
|
self.ckpt = None
|
|
|
|
self.ckpt = None
|
|
|
|
self.overrides = {}
|
|
|
|
self.overrides = {}
|
|
|
|
|
|
|
|
self.init_disabled = False
|
|
|
|
|
|
|
|
|
|
|
|
def new(self, cfg: str):
|
|
|
|
@classmethod
|
|
|
|
|
|
|
|
def new(cls, cfg: str):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Initializes a new model and infers the task type from the model definitions
|
|
|
|
Initializes a new model and infers the task type from the model definitions
|
|
|
|
|
|
|
|
|
|
|
@ -55,12 +61,15 @@ class YOLO:
|
|
|
|
cfg = check_yaml(cfg) # check YAML
|
|
|
|
cfg = check_yaml(cfg) # check YAML
|
|
|
|
with open(cfg, encoding='ascii', errors='ignore') as f:
|
|
|
|
with open(cfg, encoding='ascii', errors='ignore') as f:
|
|
|
|
cfg = yaml.safe_load(f) # model dict
|
|
|
|
cfg = yaml.safe_load(f) # model dict
|
|
|
|
self.task = self._guess_task_from_head(cfg["head"][-1][-2])
|
|
|
|
obj = cls(init_key=cls.__init_key)
|
|
|
|
self.ModelClass, self.TrainerClass, self.ValidatorClass, self.PredictorClass = self._guess_ops_from_task(
|
|
|
|
obj.task = obj._guess_task_from_head(cfg["head"][-1][-2])
|
|
|
|
self.task)
|
|
|
|
obj.ModelClass, obj.TrainerClass, obj.ValidatorClass, obj.PredictorClass = obj._guess_ops_from_task(obj.task)
|
|
|
|
self.model = self.ModelClass(cfg) # initialize
|
|
|
|
obj.model = obj.ModelClass(cfg) # initialize
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
def load(self, weights: str):
|
|
|
|
@classmethod
|
|
|
|
|
|
|
|
def load(cls, weights: str):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Initializes a new model and infers the task type from the model head
|
|
|
|
Initializes a new model and infers the task type from the model head
|
|
|
|
|
|
|
|
|
|
|
@ -68,15 +77,18 @@ class YOLO:
|
|
|
|
weights (str): model checkpoint to be loaded
|
|
|
|
weights (str): model checkpoint to be loaded
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
self.ckpt = torch.load(weights, map_location="cpu")
|
|
|
|
obj = cls(init_key=cls.__init_key)
|
|
|
|
self.task = self.ckpt["train_args"]["task"]
|
|
|
|
obj.ckpt = torch.load(weights, map_location="cpu")
|
|
|
|
self.overrides = dict(self.ckpt["train_args"])
|
|
|
|
obj.task = obj.ckpt["train_args"]["task"]
|
|
|
|
self.overrides["device"] = '' # reset device
|
|
|
|
obj.overrides = dict(obj.ckpt["train_args"])
|
|
|
|
|
|
|
|
obj.overrides["device"] = '' # reset device
|
|
|
|
LOGGER.info("Device has been reset to ''")
|
|
|
|
LOGGER.info("Device has been reset to ''")
|
|
|
|
|
|
|
|
|
|
|
|
self.ModelClass, self.TrainerClass, self.ValidatorClass, self.PredictorClass = self._guess_ops_from_task(
|
|
|
|
obj.ModelClass, obj.TrainerClass, obj.ValidatorClass, obj.PredictorClass = obj._guess_ops_from_task(
|
|
|
|
task=self.task)
|
|
|
|
task=obj.task)
|
|
|
|
self.model = attempt_load_weights(weights)
|
|
|
|
obj.model = attempt_load_weights(weights)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
def reset(self):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|