3
3
PyTorch utils
4
4
"""
5
5
6
- import datetime
7
6
import math
8
7
import os
9
8
import platform
12
11
import warnings
13
12
from contextlib import contextmanager
14
13
from copy import deepcopy
15
- from pathlib import Path
16
14
17
15
import torch
18
16
import torch .distributed as dist
19
17
import torch .nn as nn
20
18
import torch .nn .functional as F
21
19
22
- from utils .general import LOGGER
20
+ from utils .general import LOGGER , file_update_date , git_describe
23
21
24
22
try :
25
23
import thop # for FLOPs computation
@@ -40,21 +38,6 @@ def torch_distributed_zero_first(local_rank: int):
40
38
dist .barrier (device_ids = [0 ])
41
39
42
40
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
41
def device_count ():
59
42
# Returns number of CUDA devices available. Safe version of torch.cuda.device_count(). Only works on Linux.
60
43
assert platform .system () == 'Linux' , 'device_count() function only works on Linux'
@@ -67,7 +50,7 @@ def device_count():
67
50
68
51
def select_device (device = '' , batch_size = 0 , newline = True ):
69
52
# 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
71
54
device = str (device ).strip ().lower ().replace ('cuda:' , '' ) # to string, 'cuda:0' to '0'
72
55
cpu = device == 'cpu'
73
56
if cpu :
0 commit comments