Move git_describe() to general.py (#6918)

* Move `git_describe()` to general.py

* Move `git_describe()` to general.py
This commit is contained in:
Glenn Jocher 2022-03-09 18:22:53 +01:00 committed by GitHub
parent e6e36aac10
commit 6dd82c0252
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 19 deletions

View File

@ -15,6 +15,7 @@ import shutil
import signal import signal
import time import time
import urllib import urllib
from datetime import datetime
from itertools import repeat from itertools import repeat
from multiprocessing.pool import ThreadPool from multiprocessing.pool import ThreadPool
from pathlib import Path from pathlib import Path
@ -221,6 +222,18 @@ def emojis(str=''):
return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else 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): def file_size(path):
# Return file/dir size (MB) # Return file/dir size (MB)
mb = 1 << 20 # bytes to MiB (1024 ** 2) mb = 1 << 20 # bytes to MiB (1024 ** 2)
@ -243,6 +256,14 @@ def check_online():
return False 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 @try_except
@WorkingDirectory(ROOT) @WorkingDirectory(ROOT)
def check_git_status(): def check_git_status():

View File

@ -3,7 +3,6 @@
PyTorch utils PyTorch utils
""" """
import datetime
import math import math
import os import os
import platform import platform
@ -12,14 +11,13 @@ import time
import warnings import warnings
from contextlib import contextmanager from contextlib import contextmanager
from copy import deepcopy from copy import deepcopy
from pathlib import Path
import torch import torch
import torch.distributed as dist import torch.distributed as dist
import torch.nn as nn import torch.nn as nn
import torch.nn.functional as F import torch.nn.functional as F
from utils.general import LOGGER from utils.general import LOGGER, file_update_date, git_describe
try: try:
import thop # for FLOPs computation import thop # for FLOPs computation
@ -40,21 +38,6 @@ def torch_distributed_zero_first(local_rank: int):
dist.barrier(device_ids=[0]) 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(): def device_count():
# Returns number of CUDA devices available. Safe version of torch.cuda.device_count(). Only works on Linux. # 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' 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): def select_device(device='', batch_size=0, newline=True):
# device = 'cpu' or '0' or '0,1,2,3' # 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' device = str(device).strip().lower().replace('cuda:', '') # to string, 'cuda:0' to '0'
cpu = device == 'cpu' cpu = device == 'cpu'
if cpu: if cpu: