files.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/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. """
  12. A context manager and decorator for temporarily changing the working directory.
  13. This class allows for the temporary change of the working directory using a context manager or decorator.
  14. It ensures that the original working directory is restored after the context or decorated function completes.
  15. Attributes:
  16. dir (Path): The new directory to switch to.
  17. cwd (Path): The original current working directory before the switch.
  18. Methods:
  19. __enter__: Changes the current directory to the specified directory.
  20. __exit__: Restores the original working directory on context exit.
  21. Examples:
  22. Using as a context manager:
  23. >>> with WorkingDirectory('/path/to/new/dir'):
  24. >>> # Perform operations in the new directory
  25. >>> pass
  26. Using as a decorator:
  27. >>> @WorkingDirectory('/path/to/new/dir')
  28. >>> def some_function():
  29. >>> # Perform operations in the new directory
  30. >>> pass
  31. """
  32. def __init__(self, new_dir):
  33. """Sets the working directory to 'new_dir' upon instantiation for use with context managers or decorators."""
  34. self.dir = new_dir # new dir
  35. self.cwd = Path.cwd().resolve() # current dir
  36. def __enter__(self):
  37. """Changes the current working directory to the specified directory upon entering the context."""
  38. os.chdir(self.dir)
  39. def __exit__(self, exc_type, exc_val, exc_tb): # noqa
  40. """Restores the original working directory when exiting the context."""
  41. os.chdir(self.cwd)
  42. @contextmanager
  43. def spaces_in_path(path):
  44. """
  45. Context manager to handle paths with spaces in their names. If a path contains spaces, it replaces them with
  46. underscores, copies the file/directory to the new path, executes the context code block, then copies the
  47. file/directory back to its original location.
  48. Args:
  49. path (str | Path): The original path that may contain spaces.
  50. Yields:
  51. (Path): Temporary path with spaces replaced by underscores if spaces were present, otherwise the original path.
  52. Examples:
  53. Use the context manager to handle paths with spaces:
  54. >>> from ultralytics.utils.files import spaces_in_path
  55. >>> with spaces_in_path('/path/with spaces') as new_path:
  56. >>> # Your code here
  57. """
  58. # If path has spaces, replace them with underscores
  59. if " " in str(path):
  60. string = isinstance(path, str) # input type
  61. path = Path(path)
  62. # Create a temporary directory and construct the new path
  63. with tempfile.TemporaryDirectory() as tmp_dir:
  64. tmp_path = Path(tmp_dir) / path.name.replace(" ", "_")
  65. # Copy file/directory
  66. if path.is_dir():
  67. # tmp_path.mkdir(parents=True, exist_ok=True)
  68. shutil.copytree(path, tmp_path)
  69. elif path.is_file():
  70. tmp_path.parent.mkdir(parents=True, exist_ok=True)
  71. shutil.copy2(path, tmp_path)
  72. try:
  73. # Yield the temporary path
  74. yield str(tmp_path) if string else tmp_path
  75. finally:
  76. # Copy file/directory back
  77. if tmp_path.is_dir():
  78. shutil.copytree(tmp_path, path, dirs_exist_ok=True)
  79. elif tmp_path.is_file():
  80. shutil.copy2(tmp_path, path) # Copy back the file
  81. else:
  82. # If there are no spaces, just yield the original path
  83. yield path
  84. def increment_path(path, exist_ok=False, sep="", mkdir=False):
  85. """
  86. Increments a file or directory path, i.e., runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc.
  87. If the path exists and `exist_ok` is not True, the path will be incremented by appending a number and `sep` to
  88. the end of the path. If the path is a file, the file extension will be preserved. If the path is a directory, the
  89. number will be appended directly to the end of the path. If `mkdir` is set to True, the path will be created as a
  90. directory if it does not already exist.
  91. Args:
  92. path (str | pathlib.Path): Path to increment.
  93. exist_ok (bool): If True, the path will not be incremented and returned as-is.
  94. sep (str): Separator to use between the path and the incrementation number.
  95. mkdir (bool): Create a directory if it does not exist.
  96. Returns:
  97. (pathlib.Path): Incremented path.
  98. Examples:
  99. Increment a directory path:
  100. >>> from pathlib import Path
  101. >>> path = Path("runs/exp")
  102. >>> new_path = increment_path(path)
  103. >>> print(new_path)
  104. runs/exp2
  105. Increment a file path:
  106. >>> path = Path("runs/exp/results.txt")
  107. >>> new_path = increment_path(path)
  108. >>> print(new_path)
  109. runs/exp/results2.txt
  110. """
  111. path = Path(path) # os-agnostic
  112. if path.exists() and not exist_ok:
  113. path, suffix = (path.with_suffix(""), path.suffix) if path.is_file() else (path, "")
  114. # Method 1
  115. for n in range(2, 9999):
  116. p = f"{path}{sep}{n}{suffix}" # increment path
  117. if not os.path.exists(p):
  118. break
  119. path = Path(p)
  120. if mkdir:
  121. path.mkdir(parents=True, exist_ok=True) # make directory
  122. return path
  123. def file_age(path=__file__):
  124. """Return days since the last modification of the specified file."""
  125. dt = datetime.now() - datetime.fromtimestamp(Path(path).stat().st_mtime) # delta
  126. return dt.days # + dt.seconds / 86400 # fractional days
  127. def file_date(path=__file__):
  128. """Returns the file modification date in 'YYYY-M-D' format."""
  129. t = datetime.fromtimestamp(Path(path).stat().st_mtime)
  130. return f"{t.year}-{t.month}-{t.day}"
  131. def file_size(path):
  132. """Returns the size of a file or directory in megabytes (MB)."""
  133. if isinstance(path, (str, Path)):
  134. mb = 1 << 20 # bytes to MiB (1024 ** 2)
  135. path = Path(path)
  136. if path.is_file():
  137. return path.stat().st_size / mb
  138. elif path.is_dir():
  139. return sum(f.stat().st_size for f in path.glob("**/*") if f.is_file()) / mb
  140. return 0.0
  141. def get_latest_run(search_dir="."):
  142. """Returns the path to the most recent 'last.pt' file in the specified directory for resuming training."""
  143. last_list = glob.glob(f"{search_dir}/**/last*.pt", recursive=True)
  144. return max(last_list, key=os.path.getctime) if last_list else ""
  145. def update_models(model_names=("yolo11n.pt",), source_dir=Path("."), update_names=False):
  146. """
  147. Updates and re-saves specified YOLO models in an 'updated_models' subdirectory.
  148. Args:
  149. model_names (Tuple[str, ...]): Model filenames to update.
  150. source_dir (Path): Directory containing models and target subdirectory.
  151. update_names (bool): Update model names from a data YAML.
  152. Examples:
  153. Update specified YOLO models and save them in 'updated_models' subdirectory:
  154. >>> from ultralytics.utils.files import update_models
  155. >>> model_names = ("yolo11n.pt", "yolov8s.pt")
  156. >>> update_models(model_names, source_dir=Path("/models"), update_names=True)
  157. """
  158. from ultralytics import YOLO
  159. from ultralytics.nn.autobackend import default_class_names
  160. target_dir = source_dir / "updated_models"
  161. target_dir.mkdir(parents=True, exist_ok=True) # Ensure target directory exists
  162. for model_name in model_names:
  163. model_path = source_dir / model_name
  164. print(f"Loading model from {model_path}")
  165. # Load model
  166. model = YOLO(model_path)
  167. model.half()
  168. if update_names: # update model names from a dataset YAML
  169. model.model.names = default_class_names("coco8.yaml")
  170. # Define new save path
  171. save_path = target_dir / model_name
  172. # Save model using model.save()
  173. print(f"Re-saving {model_name} model to {save_path}")
  174. model.save(save_path)