files.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import contextlib
  3. import glob
  4. import os
  5. import shutil
  6. import tempfile
  7. from contextlib import contextmanager
  8. from datetime import datetime
  9. from pathlib import Path
  10. class WorkingDirectory(contextlib.ContextDecorator):
  11. """Usage: @WorkingDirectory(dir) decorator or 'with WorkingDirectory(dir):' context manager."""
  12. def __init__(self, new_dir):
  13. """Sets the working directory to 'new_dir' upon instantiation."""
  14. self.dir = new_dir # new dir
  15. self.cwd = Path.cwd().resolve() # current dir
  16. def __enter__(self):
  17. """Changes the current directory to the specified directory."""
  18. os.chdir(self.dir)
  19. def __exit__(self, exc_type, exc_val, exc_tb): # noqa
  20. """Restore the current working directory on context exit."""
  21. os.chdir(self.cwd)
  22. @contextmanager
  23. def spaces_in_path(path):
  24. """
  25. Context manager to handle paths with spaces in their names. If a path contains spaces, it replaces them with
  26. underscores, copies the file/directory to the new path, executes the context code block, then copies the
  27. file/directory back to its original location.
  28. Args:
  29. path (str | Path): The original path.
  30. Yields:
  31. (Path): Temporary path with spaces replaced by underscores if spaces were present, otherwise the original path.
  32. Example:
  33. ```python
  34. with ultralytics.utils.files import spaces_in_path
  35. with spaces_in_path('/path/with spaces') as new_path:
  36. # Your code here
  37. ```
  38. """
  39. # If path has spaces, replace them with underscores
  40. if " " in str(path):
  41. string = isinstance(path, str) # input type
  42. path = Path(path)
  43. # Create a temporary directory and construct the new path
  44. with tempfile.TemporaryDirectory() as tmp_dir:
  45. tmp_path = Path(tmp_dir) / path.name.replace(" ", "_")
  46. # Copy file/directory
  47. if path.is_dir():
  48. # tmp_path.mkdir(parents=True, exist_ok=True)
  49. shutil.copytree(path, tmp_path)
  50. elif path.is_file():
  51. tmp_path.parent.mkdir(parents=True, exist_ok=True)
  52. shutil.copy2(path, tmp_path)
  53. try:
  54. # Yield the temporary path
  55. yield str(tmp_path) if string else tmp_path
  56. finally:
  57. # Copy file/directory back
  58. if tmp_path.is_dir():
  59. shutil.copytree(tmp_path, path, dirs_exist_ok=True)
  60. elif tmp_path.is_file():
  61. shutil.copy2(tmp_path, path) # Copy back the file
  62. else:
  63. # If there are no spaces, just yield the original path
  64. yield path
  65. def increment_path(path, exist_ok=False, sep="", mkdir=False):
  66. """
  67. Increments a file or directory path, i.e. runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc.
  68. If the path exists and exist_ok is not set to True, the path will be incremented by appending a number and sep to
  69. the end of the path. If the path is a file, the file extension will be preserved. If the path is a directory, the
  70. number will be appended directly to the end of the path. If mkdir is set to True, the path will be created as a
  71. directory if it does not already exist.
  72. Args:
  73. path (str, pathlib.Path): Path to increment.
  74. exist_ok (bool, optional): If True, the path will not be incremented and returned as-is. Defaults to False.
  75. sep (str, optional): Separator to use between the path and the incrementation number. Defaults to ''.
  76. mkdir (bool, optional): Create a directory if it does not exist. Defaults to False.
  77. Returns:
  78. (pathlib.Path): Incremented path.
  79. """
  80. path = Path(path) # os-agnostic
  81. if path.exists() and not exist_ok:
  82. path, suffix = (path.with_suffix(""), path.suffix) if path.is_file() else (path, "")
  83. # Method 1
  84. for n in range(2, 9999):
  85. p = f"{path}{sep}{n}{suffix}" # increment path
  86. if not os.path.exists(p):
  87. break
  88. path = Path(p)
  89. if mkdir:
  90. path.mkdir(parents=True, exist_ok=True) # make directory
  91. return path
  92. def file_age(path=__file__):
  93. """Return days since last file update."""
  94. dt = datetime.now() - datetime.fromtimestamp(Path(path).stat().st_mtime) # delta
  95. return dt.days # + dt.seconds / 86400 # fractional days
  96. def file_date(path=__file__):
  97. """Return human-readable file modification date, i.e. '2021-3-26'."""
  98. t = datetime.fromtimestamp(Path(path).stat().st_mtime)
  99. return f"{t.year}-{t.month}-{t.day}"
  100. def file_size(path):
  101. """Return file/dir size (MB)."""
  102. if isinstance(path, (str, Path)):
  103. mb = 1 << 20 # bytes to MiB (1024 ** 2)
  104. path = Path(path)
  105. if path.is_file():
  106. return path.stat().st_size / mb
  107. elif path.is_dir():
  108. return sum(f.stat().st_size for f in path.glob("**/*") if f.is_file()) / mb
  109. return 0.0
  110. def get_latest_run(search_dir="."):
  111. """Return path to most recent 'last.pt' in /runs (i.e. to --resume from)."""
  112. last_list = glob.glob(f"{search_dir}/**/last*.pt", recursive=True)
  113. return max(last_list, key=os.path.getctime) if last_list else ""
  114. def update_models(model_names=("yolov8n.pt",), source_dir=Path("."), update_names=False):
  115. """
  116. Updates and re-saves specified YOLO models in an 'updated_models' subdirectory.
  117. Args:
  118. model_names (tuple, optional): Model filenames to update, defaults to ("yolov8n.pt").
  119. source_dir (Path, optional): Directory containing models and target subdirectory, defaults to current directory.
  120. update_names (bool, optional): Update model names from a data YAML.
  121. Example:
  122. ```python
  123. from ultralytics.utils.files import update_models
  124. model_names = (f"rtdetr-{size}.pt" for size in "lx")
  125. update_models(model_names)
  126. ```
  127. """
  128. from ultralytics import YOLO
  129. from ultralytics.nn.autobackend import default_class_names
  130. target_dir = source_dir / "updated_models"
  131. target_dir.mkdir(parents=True, exist_ok=True) # Ensure target directory exists
  132. for model_name in model_names:
  133. model_path = source_dir / model_name
  134. print(f"Loading model from {model_path}")
  135. # Load model
  136. model = YOLO(model_path)
  137. model.half()
  138. if update_names: # update model names from a dataset YAML
  139. model.model.names = default_class_names("coco8.yaml")
  140. # Define new save path
  141. save_path = target_dir / model_name
  142. # Save model using model.save()
  143. print(f"Re-saving {model_name} model to {save_path}")
  144. model.save(save_path, use_dill=False)