33PyTorch utils
44"""
55
6- import datetime
76import math
87import os
98import platform
1211import warnings
1312from contextlib import contextmanager
1413from copy import deepcopy
15- from pathlib import Path
1614
1715import torch
1816import torch .distributed as dist
1917import torch .nn as nn
2018import torch .nn .functional as F
2119
22- from utils .general import LOGGER
20+ from utils .general import LOGGER , file_update_date , git_describe
2321
2422try :
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-
5841def 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
6851def 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