Skip to content

Commit 6dd82c0

Browse files
authored
Move git_describe() to general.py (#6918)
* Move `git_describe()` to general.py * Move `git_describe()` to general.py
1 parent e6e36aa commit 6dd82c0

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

utils/general.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import signal
1616
import time
1717
import urllib
18+
from datetime import datetime
1819
from itertools import repeat
1920
from multiprocessing.pool import ThreadPool
2021
from pathlib import Path
@@ -221,6 +222,18 @@ def emojis(str=''):
221222
return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str
222223

223224

225+
def file_age(path=__file__):
226+
# Return days since last file update
227+
dt = (datetime.now() - datetime.fromtimestamp(Path(path).stat().st_mtime)) # delta
228+
return dt.days # + dt.seconds / 86400 # fractional days
229+
230+
231+
def file_update_date(path=__file__):
232+
# Return human-readable file modification date, i.e. '2021-3-26'
233+
t = datetime.fromtimestamp(Path(path).stat().st_mtime)
234+
return f'{t.year}-{t.month}-{t.day}'
235+
236+
224237
def file_size(path):
225238
# Return file/dir size (MB)
226239
mb = 1 << 20 # bytes to MiB (1024 ** 2)
@@ -243,6 +256,14 @@ def check_online():
243256
return False
244257

245258

259+
def git_describe(path=ROOT): # path must be a directory
260+
# Return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe
261+
try:
262+
return check_output(f'git -C {path} describe --tags --long --always', shell=True).decode()[:-1]
263+
except Exception:
264+
return ''
265+
266+
246267
@try_except
247268
@WorkingDirectory(ROOT)
248269
def check_git_status():

utils/torch_utils.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
PyTorch utils
44
"""
55

6-
import datetime
76
import math
87
import os
98
import platform
@@ -12,14 +11,13 @@
1211
import warnings
1312
from contextlib import contextmanager
1413
from copy import deepcopy
15-
from pathlib import Path
1614

1715
import torch
1816
import torch.distributed as dist
1917
import torch.nn as nn
2018
import torch.nn.functional as F
2119

22-
from utils.general import LOGGER
20+
from utils.general import LOGGER, file_update_date, git_describe
2321

2422
try:
2523
import thop # for FLOPs computation
@@ -40,21 +38,6 @@ def torch_distributed_zero_first(local_rank: int):
4038
dist.barrier(device_ids=[0])
4139

4240

43-
def date_modified(path=__file__):
44-
# Return human-readable file modification date, i.e. '2021-3-26'
45-
t = datetime.datetime.fromtimestamp(Path(path).stat().st_mtime)
46-
return f'{t.year}-{t.month}-{t.day}'
47-
48-
49-
def git_describe(path=Path(__file__).parent): # path must be a directory
50-
# Return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe
51-
s = f'git -C {path} describe --tags --long --always'
52-
try:
53-
return subprocess.check_output(s, shell=True, stderr=subprocess.STDOUT).decode()[:-1]
54-
except subprocess.CalledProcessError:
55-
return '' # not a git repository
56-
57-
5841
def device_count():
5942
# Returns number of CUDA devices available. Safe version of torch.cuda.device_count(). Only works on Linux.
6043
assert platform.system() == 'Linux', 'device_count() function only works on Linux'
@@ -67,7 +50,7 @@ def device_count():
6750

6851
def select_device(device='', batch_size=0, newline=True):
6952
# device = 'cpu' or '0' or '0,1,2,3'
70-
s = f'YOLOv5 🚀 {git_describe() or date_modified()} torch {torch.__version__} ' # string
53+
s = f'YOLOv5 🚀 {git_describe() or file_update_date()} torch {torch.__version__} ' # string
7154
device = str(device).strip().lower().replace('cuda:', '') # to string, 'cuda:0' to '0'
7255
cpu = device == 'cpu'
7356
if cpu:

0 commit comments

Comments
 (0)