model.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import inspect
  3. import sys
  4. from pathlib import Path
  5. from typing import Union
  6. from ultralytics.cfg import get_cfg
  7. from ultralytics.engine.exporter import Exporter
  8. from ultralytics.hub.utils import HUB_WEB_ROOT
  9. from ultralytics.nn.tasks import attempt_load_one_weight, guess_model_task, nn, yaml_model_load
  10. from ultralytics.utils import (ASSETS, DEFAULT_CFG, DEFAULT_CFG_DICT, DEFAULT_CFG_KEYS, LOGGER, RANK, callbacks, emojis,
  11. yaml_load)
  12. from ultralytics.utils.checks import check_file, check_imgsz, check_pip_update_available, check_yaml
  13. from ultralytics.utils.downloads import GITHUB_ASSETS_STEMS
  14. from ultralytics.utils.torch_utils import smart_inference_mode
  15. class Model:
  16. """
  17. A base model class to unify apis for all the models.
  18. Args:
  19. model (str, Path): Path to the model file to load or create.
  20. task (Any, optional): Task type for the YOLO model. Defaults to None.
  21. Attributes:
  22. predictor (Any): The predictor object.
  23. model (Any): The model object.
  24. trainer (Any): The trainer object.
  25. task (str): The type of model task.
  26. ckpt (Any): The checkpoint object if the model loaded from *.pt file.
  27. cfg (str): The model configuration if loaded from *.yaml file.
  28. ckpt_path (str): The checkpoint file path.
  29. overrides (dict): Overrides for the trainer object.
  30. metrics (Any): The data for metrics.
  31. Methods:
  32. __call__(source=None, stream=False, **kwargs):
  33. Alias for the predict method.
  34. _new(cfg:str, verbose:bool=True) -> None:
  35. Initializes a new model and infers the task type from the model definitions.
  36. _load(weights:str, task:str='') -> None:
  37. Initializes a new model and infers the task type from the model head.
  38. _check_is_pytorch_model() -> None:
  39. Raises TypeError if the model is not a PyTorch model.
  40. reset() -> None:
  41. Resets the model modules.
  42. info(verbose:bool=False) -> None:
  43. Logs the model info.
  44. fuse() -> None:
  45. Fuses the model for faster inference.
  46. predict(source=None, stream=False, **kwargs) -> List[ultralytics.engine.results.Results]:
  47. Performs prediction using the YOLO model.
  48. Returns:
  49. list(ultralytics.engine.results.Results): The prediction results.
  50. """
  51. def __init__(self, model: Union[str, Path] = 'yolov8n.pt', task=None) -> None:
  52. """
  53. Initializes the YOLO model.
  54. Args:
  55. model (Union[str, Path], optional): Path or name of the model to load or create. Defaults to 'yolov8n.pt'.
  56. task (Any, optional): Task type for the YOLO model. Defaults to None.
  57. """
  58. self.callbacks = callbacks.get_default_callbacks()
  59. self.predictor = None # reuse predictor
  60. self.model = None # model object
  61. self.trainer = None # trainer object
  62. self.ckpt = None # if loaded from *.pt
  63. self.cfg = None # if loaded from *.yaml
  64. self.ckpt_path = None
  65. self.overrides = {} # overrides for trainer object
  66. self.metrics = None # validation/training metrics
  67. self.session = None # HUB session
  68. self.task = task # task type
  69. model = str(model).strip() # strip spaces
  70. # Check if Ultralytics HUB model from https://hub.ultralytics.com
  71. if self.is_hub_model(model):
  72. from ultralytics.hub.session import HUBTrainingSession
  73. self.session = HUBTrainingSession(model)
  74. model = self.session.model_file
  75. # Load or create new YOLO model
  76. suffix = Path(model).suffix
  77. if not suffix and Path(model).stem in GITHUB_ASSETS_STEMS:
  78. model, suffix = Path(model).with_suffix('.pt'), '.pt' # add suffix, i.e. yolov8n -> yolov8n.pt
  79. if suffix in ('.yaml', '.yml'):
  80. self._new(model, task)
  81. else:
  82. self._load(model, task)
  83. def __call__(self, source=None, stream=False, **kwargs):
  84. """Calls the 'predict' function with given arguments to perform object detection."""
  85. return self.predict(source, stream, **kwargs)
  86. @staticmethod
  87. def is_hub_model(model):
  88. """Check if the provided model is a HUB model."""
  89. return any((
  90. model.startswith(f'{HUB_WEB_ROOT}/models/'), # i.e. https://hub.ultralytics.com/models/MODEL_ID
  91. [len(x) for x in model.split('_')] == [42, 20], # APIKEY_MODELID
  92. len(model) == 20 and not Path(model).exists() and all(x not in model for x in './\\'))) # MODELID
  93. def _new(self, cfg: str, task=None, model=None, verbose=True):
  94. """
  95. Initializes a new model and infers the task type from the model definitions.
  96. Args:
  97. cfg (str): model configuration file
  98. task (str | None): model task
  99. model (BaseModel): Customized model.
  100. verbose (bool): display model info on load
  101. """
  102. cfg_dict = yaml_model_load(cfg)
  103. self.cfg = cfg
  104. self.task = task or guess_model_task(cfg_dict)
  105. model = model or self.smart_load('model')
  106. self.model = model(cfg_dict, verbose=verbose and RANK == -1) # build model
  107. self.overrides['model'] = self.cfg
  108. # Below added to allow export from YAMLs
  109. args = {**DEFAULT_CFG_DICT, **self.overrides} # combine model and default args, preferring model args
  110. self.model.args = {k: v for k, v in args.items() if k in DEFAULT_CFG_KEYS} # attach args to model
  111. self.model.task = self.task
  112. def _load(self, weights: str, task=None):
  113. """
  114. Initializes a new model and infers the task type from the model head.
  115. Args:
  116. weights (str): model checkpoint to be loaded
  117. task (str | None): model task
  118. """
  119. suffix = Path(weights).suffix
  120. if suffix == '.pt':
  121. self.model, self.ckpt = attempt_load_one_weight(weights)
  122. self.task = self.model.args['task']
  123. self.overrides = self.model.args = self._reset_ckpt_args(self.model.args)
  124. self.ckpt_path = self.model.pt_path
  125. else:
  126. weights = check_file(weights)
  127. self.model, self.ckpt = weights, None
  128. self.task = task or guess_model_task(weights)
  129. self.ckpt_path = weights
  130. self.overrides['model'] = weights
  131. self.overrides['task'] = self.task
  132. def _check_is_pytorch_model(self):
  133. """
  134. Raises TypeError is model is not a PyTorch model
  135. """
  136. pt_str = isinstance(self.model, (str, Path)) and Path(self.model).suffix == '.pt'
  137. pt_module = isinstance(self.model, nn.Module)
  138. if not (pt_module or pt_str):
  139. raise TypeError(f"model='{self.model}' must be a *.pt PyTorch model, but is a different type. "
  140. f'PyTorch models can be used to train, val, predict and export, i.e. '
  141. f"'yolo export model=yolov8n.pt', but exported formats like ONNX, TensorRT etc. only "
  142. f"support 'predict' and 'val' modes, i.e. 'yolo predict model=yolov8n.onnx'.")
  143. @smart_inference_mode()
  144. def reset_weights(self):
  145. """
  146. Resets the model modules parameters to randomly initialized values, losing all training information.
  147. """
  148. self._check_is_pytorch_model()
  149. for m in self.model.modules():
  150. if hasattr(m, 'reset_parameters'):
  151. m.reset_parameters()
  152. for p in self.model.parameters():
  153. p.requires_grad = True
  154. return self
  155. @smart_inference_mode()
  156. def load(self, weights='yolov8n.pt'):
  157. """
  158. Transfers parameters with matching names and shapes from 'weights' to model.
  159. """
  160. self._check_is_pytorch_model()
  161. if isinstance(weights, (str, Path)):
  162. weights, self.ckpt = attempt_load_one_weight(weights)
  163. self.model.load(weights)
  164. return self
  165. def info(self, detailed=False, verbose=True):
  166. """
  167. Logs model info.
  168. Args:
  169. detailed (bool): Show detailed information about model.
  170. verbose (bool): Controls verbosity.
  171. """
  172. self._check_is_pytorch_model()
  173. return self.model.info(detailed=detailed, verbose=verbose)
  174. def fuse(self):
  175. """Fuse PyTorch Conv2d and BatchNorm2d layers."""
  176. self._check_is_pytorch_model()
  177. self.model.fuse()
  178. @smart_inference_mode()
  179. def predict(self, source=None, stream=False, predictor=None, **kwargs):
  180. """
  181. Perform prediction using the YOLO model.
  182. Args:
  183. source (str | int | PIL | np.ndarray): The source of the image to make predictions on.
  184. Accepts all source types accepted by the YOLO model.
  185. stream (bool): Whether to stream the predictions or not. Defaults to False.
  186. predictor (BasePredictor): Customized predictor.
  187. **kwargs : Additional keyword arguments passed to the predictor.
  188. Check the 'configuration' section in the documentation for all available options.
  189. Returns:
  190. (List[ultralytics.engine.results.Results]): The prediction results.
  191. """
  192. if source is None:
  193. source = ASSETS
  194. LOGGER.warning(f"WARNING ⚠️ 'source' is missing. Using 'source={source}'.")
  195. is_cli = (sys.argv[0].endswith('yolo') or sys.argv[0].endswith('ultralytics')) and any(
  196. x in sys.argv for x in ('predict', 'track', 'mode=predict', 'mode=track'))
  197. # Check prompts for SAM/FastSAM
  198. prompts = kwargs.pop('prompts', None)
  199. overrides = self.overrides.copy()
  200. overrides['conf'] = 0.25
  201. overrides.update(kwargs) # prefer kwargs
  202. overrides['mode'] = kwargs.get('mode', 'predict')
  203. assert overrides['mode'] in ['track', 'predict']
  204. if not is_cli:
  205. overrides['save'] = kwargs.get('save', False) # do not save by default if called in Python
  206. if not self.predictor:
  207. self.task = overrides.get('task') or self.task
  208. predictor = predictor or self.smart_load('predictor')
  209. self.predictor = predictor(overrides=overrides, _callbacks=self.callbacks)
  210. self.predictor.setup_model(model=self.model, verbose=is_cli)
  211. else: # only update args if predictor is already setup
  212. self.predictor.args = get_cfg(self.predictor.args, overrides)
  213. if 'project' in overrides or 'name' in overrides:
  214. self.predictor.save_dir = self.predictor.get_save_dir()
  215. # Set prompts for SAM/FastSAM
  216. if len and hasattr(self.predictor, 'set_prompts'):
  217. self.predictor.set_prompts(prompts)
  218. return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)
  219. def track(self, source=None, stream=False, persist=False, **kwargs):
  220. """
  221. Perform object tracking on the input source using the registered trackers.
  222. Args:
  223. source (str, optional): The input source for object tracking. Can be a file path or a video stream.
  224. stream (bool, optional): Whether the input source is a video stream. Defaults to False.
  225. persist (bool, optional): Whether to persist the trackers if they already exist. Defaults to False.
  226. **kwargs (optional): Additional keyword arguments for the tracking process.
  227. Returns:
  228. (List[ultralytics.engine.results.Results]): The tracking results.
  229. """
  230. if not hasattr(self.predictor, 'trackers'):
  231. from ultralytics.trackers import register_tracker
  232. register_tracker(self, persist)
  233. # ByteTrack-based method needs low confidence predictions as input
  234. conf = kwargs.get('conf') or 0.1
  235. kwargs['conf'] = conf
  236. kwargs['mode'] = 'track'
  237. return self.predict(source=source, stream=stream, **kwargs)
  238. @smart_inference_mode()
  239. def val(self, data=None, validator=None, **kwargs):
  240. """
  241. Validate a model on a given dataset.
  242. Args:
  243. data (str): The dataset to validate on. Accepts all formats accepted by yolo
  244. validator (BaseValidator): Customized validator.
  245. **kwargs : Any other args accepted by the validators. To see all args check 'configuration' section in docs
  246. """
  247. overrides = self.overrides.copy()
  248. overrides['rect'] = True # rect batches as default
  249. overrides.update(kwargs)
  250. overrides['mode'] = 'val'
  251. if overrides.get('imgsz') is None:
  252. overrides['imgsz'] = self.model.args['imgsz'] # use trained imgsz unless custom value is passed
  253. args = get_cfg(cfg=DEFAULT_CFG, overrides=overrides)
  254. args.data = data or args.data
  255. if 'task' in overrides:
  256. self.task = args.task
  257. else:
  258. args.task = self.task
  259. validator = validator or self.smart_load('validator')
  260. args.imgsz = check_imgsz(args.imgsz, max_dim=1)
  261. validator = validator(args=args, _callbacks=self.callbacks)
  262. validator(model=self.model)
  263. self.metrics = validator.metrics
  264. return validator.metrics
  265. @smart_inference_mode()
  266. def benchmark(self, **kwargs):
  267. """
  268. Benchmark a model on all export formats.
  269. Args:
  270. **kwargs : Any other args accepted by the validators. To see all args check 'configuration' section in docs
  271. """
  272. self._check_is_pytorch_model()
  273. from ultralytics.utils.benchmarks import benchmark
  274. overrides = self.model.args.copy()
  275. overrides.update(kwargs)
  276. overrides['mode'] = 'benchmark'
  277. overrides = {**DEFAULT_CFG_DICT, **overrides} # fill in missing overrides keys with defaults
  278. return benchmark(
  279. model=self,
  280. data=kwargs.get('data'), # if no 'data' argument passed set data=None for default datasets
  281. imgsz=overrides['imgsz'],
  282. half=overrides['half'],
  283. int8=overrides['int8'],
  284. device=overrides['device'],
  285. verbose=kwargs.get('verbose'))
  286. def export(self, **kwargs):
  287. """
  288. Export model.
  289. Args:
  290. **kwargs : Any other args accepted by the predictors. To see all args check 'configuration' section in docs
  291. """
  292. self._check_is_pytorch_model()
  293. overrides = self.overrides.copy()
  294. overrides.update(kwargs)
  295. overrides['mode'] = 'export'
  296. if overrides.get('imgsz') is None:
  297. overrides['imgsz'] = self.model.args['imgsz'] # use trained imgsz unless custom value is passed
  298. if 'batch' not in kwargs:
  299. overrides['batch'] = 1 # default to 1 if not modified
  300. if 'data' not in kwargs:
  301. overrides['data'] = None # default to None if not modified (avoid int8 calibration with coco.yaml)
  302. args = get_cfg(cfg=DEFAULT_CFG, overrides=overrides)
  303. args.task = self.task
  304. return Exporter(overrides=args, _callbacks=self.callbacks)(model=self.model)
  305. def train(self, trainer=None, **kwargs):
  306. """
  307. Trains the model on a given dataset.
  308. Args:
  309. trainer (BaseTrainer, optional): Customized trainer.
  310. **kwargs (Any): Any number of arguments representing the training configuration.
  311. """
  312. self._check_is_pytorch_model()
  313. if self.session: # Ultralytics HUB session
  314. if any(kwargs):
  315. LOGGER.warning('WARNING ⚠️ using HUB training arguments, ignoring local training arguments.')
  316. kwargs = self.session.train_args
  317. check_pip_update_available()
  318. overrides = self.overrides.copy()
  319. if kwargs.get('cfg'):
  320. LOGGER.info(f"cfg file passed. Overriding default params with {kwargs['cfg']}.")
  321. overrides = yaml_load(check_yaml(kwargs['cfg']))
  322. overrides.update(kwargs)
  323. overrides['mode'] = 'train'
  324. if not overrides.get('data'):
  325. raise AttributeError("Dataset required but missing, i.e. pass 'data=coco128.yaml'")
  326. if overrides.get('resume'):
  327. overrides['resume'] = self.ckpt_path
  328. self.task = overrides.get('task') or self.task
  329. trainer = trainer or self.smart_load('trainer')
  330. self.trainer = trainer(overrides=overrides, _callbacks=self.callbacks)
  331. if not overrides.get('resume'): # manually set model only if not resuming
  332. self.trainer.model = self.trainer.get_model(weights=self.model if self.ckpt else None, cfg=self.model.yaml)
  333. self.model = self.trainer.model
  334. self.trainer.hub_session = self.session # attach optional HUB session
  335. self.trainer.train()
  336. # Update model and cfg after training
  337. if RANK in (-1, 0):
  338. self.model, _ = attempt_load_one_weight(str(self.trainer.best))
  339. self.overrides = self.model.args
  340. self.metrics = getattr(self.trainer.validator, 'metrics', None) # TODO: no metrics returned by DDP
  341. def to(self, device):
  342. """
  343. Sends the model to the given device.
  344. Args:
  345. device (str): device
  346. """
  347. self._check_is_pytorch_model()
  348. self.model.to(device)
  349. return self
  350. def tune(self, *args, **kwargs):
  351. """
  352. Runs hyperparameter tuning using Ray Tune. See ultralytics.utils.tuner.run_ray_tune for Args.
  353. Returns:
  354. (dict): A dictionary containing the results of the hyperparameter search.
  355. Raises:
  356. ModuleNotFoundError: If Ray Tune is not installed.
  357. """
  358. self._check_is_pytorch_model()
  359. from ultralytics.utils.tuner import run_ray_tune
  360. return run_ray_tune(self, *args, **kwargs)
  361. @property
  362. def names(self):
  363. """Returns class names of the loaded model."""
  364. return self.model.names if hasattr(self.model, 'names') else None
  365. @property
  366. def device(self):
  367. """Returns device if PyTorch model."""
  368. return next(self.model.parameters()).device if isinstance(self.model, nn.Module) else None
  369. @property
  370. def transforms(self):
  371. """Returns transform of the loaded model."""
  372. return self.model.transforms if hasattr(self.model, 'transforms') else None
  373. def add_callback(self, event: str, func):
  374. """Add a callback."""
  375. self.callbacks[event].append(func)
  376. def clear_callback(self, event: str):
  377. """Clear all event callbacks."""
  378. self.callbacks[event] = []
  379. @staticmethod
  380. def _reset_ckpt_args(args):
  381. """Reset arguments when loading a PyTorch model."""
  382. include = {'imgsz', 'data', 'task', 'single_cls'} # only remember these arguments when loading a PyTorch model
  383. return {k: v for k, v in args.items() if k in include}
  384. def _reset_callbacks(self):
  385. """Reset all registered callbacks."""
  386. for event in callbacks.default_callbacks.keys():
  387. self.callbacks[event] = [callbacks.default_callbacks[event][0]]
  388. def __getattr__(self, attr):
  389. """Raises error if object has no requested attribute."""
  390. name = self.__class__.__name__
  391. raise AttributeError(f"'{name}' object has no attribute '{attr}'. See valid attributes below.\n{self.__doc__}")
  392. def smart_load(self, key):
  393. """Load model/trainer/validator/predictor."""
  394. try:
  395. return self.task_map[self.task][key]
  396. except Exception as e:
  397. name = self.__class__.__name__
  398. mode = inspect.stack()[1][3] # get the function name.
  399. raise NotImplementedError(
  400. emojis(f'WARNING ⚠️ `{name}` model does not support `{mode}` mode for `{self.task}` task yet.')) from e
  401. @property
  402. def task_map(self):
  403. """
  404. Map head to model, trainer, validator, and predictor classes.
  405. Returns:
  406. task_map (dict): The map of model task to mode classes.
  407. """
  408. raise NotImplementedError('Please provide task map for your model!')