2023-04-14 20:36:16 +08:00
|
|
|
# YOLOv5 🚀 by Ultralytics, AGPL-3.0 license
|
2024-01-08 08:29:14 +08:00
|
|
|
"""utils/initialization."""
|
2021-11-10 06:03:19 +08:00
|
|
|
|
2022-08-25 20:34:26 +08:00
|
|
|
import contextlib
|
2022-09-18 22:15:25 +08:00
|
|
|
import platform
|
2022-08-25 20:34:26 +08:00
|
|
|
import threading
|
|
|
|
|
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
def emojis(str=""):
|
2022-09-18 22:15:25 +08:00
|
|
|
# Return platform-dependent emoji-safe version of string
|
2024-01-08 08:29:14 +08:00
|
|
|
return str.encode().decode("ascii", "ignore") if platform.system() == "Windows" else str
|
2022-09-18 22:15:25 +08:00
|
|
|
|
|
|
|
|
2022-08-25 20:34:26 +08:00
|
|
|
class TryExcept(contextlib.ContextDecorator):
|
|
|
|
# YOLOv5 TryExcept class. Usage: @TryExcept() decorator or 'with TryExcept():' context manager
|
2024-01-08 08:29:14 +08:00
|
|
|
def __init__(self, msg=""):
|
2022-08-25 20:34:26 +08:00
|
|
|
self.msg = msg
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, value, traceback):
|
|
|
|
if value:
|
2022-10-17 20:34:33 +08:00
|
|
|
print(emojis(f"{self.msg}{': ' if self.msg else ''}{value}"))
|
2022-08-25 20:34:26 +08:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def threaded(func):
|
|
|
|
# Multi-threads a target function and returns thread. Usage: @threaded decorator
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
thread = threading.Thread(target=func, args=args, kwargs=kwargs, daemon=True)
|
|
|
|
thread.start()
|
|
|
|
return thread
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
2021-11-10 06:03:19 +08:00
|
|
|
|
2022-11-15 23:27:07 +08:00
|
|
|
def join_threads(verbose=False):
|
|
|
|
# Join all daemon threads, i.e. atexit.register(lambda: join_threads())
|
|
|
|
main_thread = threading.current_thread()
|
|
|
|
for t in threading.enumerate():
|
|
|
|
if t is not main_thread:
|
|
|
|
if verbose:
|
2024-01-08 08:29:14 +08:00
|
|
|
print(f"Joining thread {t.name}")
|
2022-11-15 23:27:07 +08:00
|
|
|
t.join()
|
|
|
|
|
|
|
|
|
2021-12-08 21:57:03 +08:00
|
|
|
def notebook_init(verbose=True):
|
|
|
|
# Check system software and hardware
|
2024-01-08 08:29:14 +08:00
|
|
|
print("Checking setup...")
|
2021-12-08 21:57:03 +08:00
|
|
|
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
|
2023-07-30 19:38:55 +08:00
|
|
|
from ultralytics.utils.checks import check_requirements
|
2023-06-18 22:09:41 +08:00
|
|
|
|
2023-06-15 19:49:19 +08:00
|
|
|
from utils.general import check_font, is_colab
|
2021-12-08 21:57:03 +08:00
|
|
|
from utils.torch_utils import select_device # imports
|
|
|
|
|
2022-08-29 21:12:15 +08:00
|
|
|
check_font()
|
|
|
|
|
2021-12-08 21:57:03 +08:00
|
|
|
import psutil
|
2021-11-10 06:03:19 +08:00
|
|
|
|
2024-01-08 08:29:14 +08:00
|
|
|
if check_requirements("wandb", install=False):
|
|
|
|
os.system("pip uninstall -y wandb") # eliminate unexpected account creation prompt with infinite hang
|
2021-12-08 21:57:03 +08:00
|
|
|
if is_colab():
|
2024-01-08 08:29:14 +08:00
|
|
|
shutil.rmtree("/content/sample_data", ignore_errors=True) # remove colab /sample_data directory
|
2021-12-08 21:57:03 +08:00
|
|
|
|
2022-03-08 02:26:37 +08:00
|
|
|
# System info
|
2023-02-26 08:15:07 +08:00
|
|
|
display = None
|
2021-12-08 21:57:03 +08:00
|
|
|
if verbose:
|
2022-03-08 02:26:37 +08:00
|
|
|
gb = 1 << 30 # bytes to GiB (1024 ** 3)
|
2021-12-08 21:57:03 +08:00
|
|
|
ram = psutil.virtual_memory().total
|
2024-01-08 08:29:14 +08:00
|
|
|
total, used, free = shutil.disk_usage("/")
|
2023-02-26 08:15:07 +08:00
|
|
|
with contextlib.suppress(Exception): # clear display if ipython is installed
|
|
|
|
from IPython import display
|
2024-01-08 08:29:14 +08:00
|
|
|
|
2023-02-26 08:15:07 +08:00
|
|
|
display.clear_output()
|
2024-01-08 08:29:14 +08:00
|
|
|
s = f"({os.cpu_count()} CPUs, {ram / gb:.1f} GB RAM, {(total - free) / gb:.1f}/{total / gb:.1f} GB disk)"
|
2021-12-08 21:57:03 +08:00
|
|
|
else:
|
2024-01-08 08:29:14 +08:00
|
|
|
s = ""
|
2021-11-10 06:03:19 +08:00
|
|
|
|
|
|
|
select_device(newline=False)
|
2024-01-08 08:29:14 +08:00
|
|
|
print(emojis(f"Setup complete ✅ {s}"))
|
2021-11-10 06:03:19 +08:00
|
|
|
return display
|