glenn-jocher commited on
Commit
0ff5aec
Β·
unverified Β·
1 Parent(s): 196bf10

Create date_modified() (#2616)

Browse files

Updated device selection string with fallback for non-git directories.
```python
def select_device(device='', batch_size=None):
# device = 'cpu' or '0' or '0,1,2,3'
s = f'YOLOv5 πŸš€ {git_describe() or date_modified()} torch {torch.__version__} ' # string
...
```

Files changed (1) hide show
  1. utils/torch_utils.py +12 -4
utils/torch_utils.py CHANGED
@@ -1,4 +1,6 @@
1
- # PyTorch utils
 
 
2
  import logging
3
  import math
4
  import os
@@ -43,9 +45,15 @@ def init_torch_seeds(seed=0):
43
  cudnn.benchmark, cudnn.deterministic = True, False
44
 
45
 
46
- def git_describe():
 
 
 
 
 
 
47
  # return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe
48
- s = f'git -C {Path(__file__).resolve().parent} describe --tags --long --always'
49
  try:
50
  return subprocess.check_output(s, shell=True).decode()[:-1]
51
  except subprocess.CalledProcessError as e:
@@ -54,7 +62,7 @@ def git_describe():
54
 
55
  def select_device(device='', batch_size=None):
56
  # device = 'cpu' or '0' or '0,1,2,3'
57
- s = f'YOLOv5 πŸš€ {git_describe()} torch {torch.__version__} ' # string
58
  cpu = device.lower() == 'cpu'
59
  if cpu:
60
  os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False
 
1
+ # YOLOv5 PyTorch utils
2
+
3
+ import datetime
4
  import logging
5
  import math
6
  import os
 
45
  cudnn.benchmark, cudnn.deterministic = True, False
46
 
47
 
48
+ def date_modified(path=__file__):
49
+ # return human-readable file modification date, i.e. '2021-3-26'
50
+ t = datetime.datetime.fromtimestamp(Path(path).stat().st_mtime)
51
+ return f'{t.year}-{t.month}-{t.day}'
52
+
53
+
54
+ def git_describe(path=Path(__file__).parent): # path must be a directory
55
  # return human-readable git description, i.e. v5.0-5-g3e25f1e https://git-scm.com/docs/git-describe
56
+ s = f'git -C {path} describe --tags --long --always'
57
  try:
58
  return subprocess.check_output(s, shell=True).decode()[:-1]
59
  except subprocess.CalledProcessError as e:
 
62
 
63
  def select_device(device='', batch_size=None):
64
  # device = 'cpu' or '0' or '0,1,2,3'
65
+ s = f'YOLOv5 πŸš€ {git_describe() or date_modified()} torch {torch.__version__} ' # string
66
  cpu = device.lower() == 'cpu'
67
  if cpu:
68
  os.environ['CUDA_VISIBLE_DEVICES'] = '-1' # force torch.cuda.is_available() = False