glenn-jocher commited on
Commit
6dd82c0
β€’
1 Parent(s): e6e36aa

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

Browse files

* Move `git_describe()` to general.py

* Move `git_describe()` to general.py

Files changed (2) hide show
  1. utils/general.py +21 -0
  2. utils/torch_utils.py +2 -19
utils/general.py CHANGED
@@ -15,6 +15,7 @@ import shutil
15
  import signal
16
  import time
17
  import urllib
 
18
  from itertools import repeat
19
  from multiprocessing.pool import ThreadPool
20
  from pathlib import Path
@@ -221,6 +222,18 @@ def emojis(str=''):
221
  return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str
222
 
223
 
 
 
 
 
 
 
 
 
 
 
 
 
224
  def file_size(path):
225
  # Return file/dir size (MB)
226
  mb = 1 << 20 # bytes to MiB (1024 ** 2)
@@ -243,6 +256,14 @@ def check_online():
243
  return False
244
 
245
 
 
 
 
 
 
 
 
 
246
  @try_except
247
  @WorkingDirectory(ROOT)
248
  def check_git_status():
 
15
  import signal
16
  import time
17
  import urllib
18
+ from datetime import datetime
19
  from itertools import repeat
20
  from multiprocessing.pool import ThreadPool
21
  from pathlib import Path
 
222
  return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str
223
 
224
 
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
+
237
  def file_size(path):
238
  # Return file/dir size (MB)
239
  mb = 1 << 20 # bytes to MiB (1024 ** 2)
 
256
  return False
257
 
258
 
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
+
267
  @try_except
268
  @WorkingDirectory(ROOT)
269
  def check_git_status():
utils/torch_utils.py CHANGED
@@ -3,7 +3,6 @@
3
  PyTorch utils
4
  """
5
 
6
- import datetime
7
  import math
8
  import os
9
  import platform
@@ -12,14 +11,13 @@ import time
12
  import warnings
13
  from contextlib import contextmanager
14
  from copy import deepcopy
15
- from pathlib import Path
16
 
17
  import torch
18
  import torch.distributed as dist
19
  import torch.nn as nn
20
  import torch.nn.functional as F
21
 
22
- from utils.general import LOGGER
23
 
24
  try:
25
  import thop # for FLOPs computation
@@ -40,21 +38,6 @@ def torch_distributed_zero_first(local_rank: int):
40
  dist.barrier(device_ids=[0])
41
 
42
 
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
-
58
  def device_count():
59
  # Returns number of CUDA devices available. Safe version of torch.cuda.device_count(). Only works on Linux.
60
  assert platform.system() == 'Linux', 'device_count() function only works on Linux'
@@ -67,7 +50,7 @@ def device_count():
67
 
68
  def select_device(device='', batch_size=0, newline=True):
69
  # device = 'cpu' or '0' or '0,1,2,3'
70
- s = f'YOLOv5 πŸš€ {git_describe() or date_modified()} torch {torch.__version__} ' # string
71
  device = str(device).strip().lower().replace('cuda:', '') # to string, 'cuda:0' to '0'
72
  cpu = device == 'cpu'
73
  if cpu:
 
3
  PyTorch utils
4
  """
5
 
 
6
  import math
7
  import os
8
  import platform
 
11
  import warnings
12
  from contextlib import contextmanager
13
  from copy import deepcopy
 
14
 
15
  import torch
16
  import torch.distributed as dist
17
  import torch.nn as nn
18
  import torch.nn.functional as F
19
 
20
+ from utils.general import LOGGER, file_update_date, git_describe
21
 
22
  try:
23
  import thop # for FLOPs computation
 
38
  dist.barrier(device_ids=[0])
39
 
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  def device_count():
42
  # Returns number of CUDA devices available. Safe version of torch.cuda.device_count(). Only works on Linux.
43
  assert platform.system() == 'Linux', 'device_count() function only works on Linux'
 
50
 
51
  def select_device(device='', batch_size=0, newline=True):
52
  # device = 'cpu' or '0' or '0,1,2,3'
53
+ s = f'YOLOv5 πŸš€ {git_describe() or file_update_date()} torch {torch.__version__} ' # string
54
  device = str(device).strip().lower().replace('cuda:', '') # to string, 'cuda:0' to '0'
55
  cpu = device == 'cpu'
56
  if cpu: