diff --git a/utils/general.py b/utils/general.py index 36c180fe4cf2..a7891cbccbab 100755 --- a/utils/general.py +++ b/utils/general.py @@ -15,6 +15,7 @@ import signal import time import urllib +from datetime import datetime from itertools import repeat from multiprocessing.pool import ThreadPool from pathlib import Path @@ -221,6 +222,18 @@ def emojis(str=''): return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str +def file_age(path=__file__): + # Return days since last file update + dt = (datetime.now() - datetime.fromtimestamp(Path(path).stat().st_mtime)) # delta + return dt.days # + dt.seconds / 86400 # fractional days + + +def file_update_date(path=__file__): + # Return human-readable file modification date, i.e. '2021-3-26' + t = datetime.fromtimestamp(Path(path).stat().st_mtime) + return f'{t.year}-{t.month}-{t.day}' + + def file_size(path): # Return file/dir size (MB) mb = 1 << 20 # bytes to MiB (1024 ** 2) @@ -243,6 +256,14 @@ def check_online(): return False +def git_describe(path=ROOT): # path must be a directory + # Return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe + try: + return check_output(f'git -C {path} describe --tags --long --always', shell=True).decode()[:-1] + except Exception: + return '' + + @try_except @WorkingDirectory(ROOT) def check_git_status(): diff --git a/utils/torch_utils.py b/utils/torch_utils.py index 2e6fba06626a..efcacc9ca735 100644 --- a/utils/torch_utils.py +++ b/utils/torch_utils.py @@ -3,7 +3,6 @@ PyTorch utils """ -import datetime import math import os import platform @@ -12,14 +11,13 @@ import warnings from contextlib import contextmanager from copy import deepcopy -from pathlib import Path import torch import torch.distributed as dist import torch.nn as nn import torch.nn.functional as F -from utils.general import LOGGER +from utils.general import LOGGER, file_update_date, git_describe try: import thop # for FLOPs computation @@ -40,21 +38,6 @@ def torch_distributed_zero_first(local_rank: int): dist.barrier(device_ids=[0]) -def date_modified(path=__file__): - # Return human-readable file modification date, i.e. '2021-3-26' - t = datetime.datetime.fromtimestamp(Path(path).stat().st_mtime) - return f'{t.year}-{t.month}-{t.day}' - - -def git_describe(path=Path(__file__).parent): # path must be a directory - # Return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe - s = f'git -C {path} describe --tags --long --always' - try: - return subprocess.check_output(s, shell=True, stderr=subprocess.STDOUT).decode()[:-1] - except subprocess.CalledProcessError: - return '' # not a git repository - - def device_count(): # Returns number of CUDA devices available. Safe version of torch.cuda.device_count(). Only works on Linux. assert platform.system() == 'Linux', 'device_count() function only works on Linux' @@ -67,7 +50,7 @@ def device_count(): def select_device(device='', batch_size=0, newline=True): # device = 'cpu' or '0' or '0,1,2,3' - s = f'YOLOv5 🚀 {git_describe() or date_modified()} torch {torch.__version__} ' # string + s = f'YOLOv5 🚀 {git_describe() or file_update_date()} torch {torch.__version__} ' # string device = str(device).strip().lower().replace('cuda:', '') # to string, 'cuda:0' to '0' cpu = device == 'cpu' if cpu: