Skip to content

Commit

Permalink
Move git_describe() to general.py (ultralytics#6918)
Browse files Browse the repository at this point in the history
* Move `git_describe()` to general.py

* Move `git_describe()` to general.py
  • Loading branch information
glenn-jocher committed Mar 9, 2022
1 parent 095cada commit c776574
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
21 changes: 21 additions & 0 deletions utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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():
Expand Down
21 changes: 2 additions & 19 deletions utils/torch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
PyTorch utils
"""

import datetime
import math
import os
import platform
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -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:
Expand Down

0 comments on commit c776574

Please sign in to comment.