PyTorch Hub `_verbose=False` fix2 (#7550)
Browse files* PyTorch Hub `_verbose=False` fix2
* Update downloads.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update hubconf.py
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Update
* Update
* Update
* Update
* Update
* Update
* Update
* Update
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- hubconf.py +24 -23
- train.py +1 -3
- utils/downloads.py +11 -7
- utils/general.py +1 -0
- utils/plots.py +0 -3
hubconf.py
CHANGED
@@ -36,6 +36,7 @@ def _create(name, pretrained=True, channels=3, classes=80, autoshape=True, verbo
|
|
36 |
|
37 |
if not verbose:
|
38 |
LOGGER.setLevel(logging.WARNING)
|
|
|
39 |
check_requirements(exclude=('tensorboard', 'thop', 'opencv-python'))
|
40 |
name = Path(name)
|
41 |
path = name.with_suffix('.pt') if name.suffix == '' else name # checkpoint path
|
@@ -65,63 +66,63 @@ def _create(name, pretrained=True, channels=3, classes=80, autoshape=True, verbo
|
|
65 |
raise Exception(s) from e
|
66 |
|
67 |
|
68 |
-
def custom(path='path/to/model.pt', autoshape=True,
|
69 |
# YOLOv5 custom or local model
|
70 |
-
return _create(path, autoshape=autoshape, verbose=
|
71 |
|
72 |
|
73 |
-
def yolov5n(pretrained=True, channels=3, classes=80, autoshape=True,
|
74 |
# YOLOv5-nano model https://github.com/ultralytics/yolov5
|
75 |
-
return _create('yolov5n', pretrained, channels, classes, autoshape,
|
76 |
|
77 |
|
78 |
-
def yolov5s(pretrained=True, channels=3, classes=80, autoshape=True,
|
79 |
# YOLOv5-small model https://github.com/ultralytics/yolov5
|
80 |
-
return _create('yolov5s', pretrained, channels, classes, autoshape,
|
81 |
|
82 |
|
83 |
-
def yolov5m(pretrained=True, channels=3, classes=80, autoshape=True,
|
84 |
# YOLOv5-medium model https://github.com/ultralytics/yolov5
|
85 |
-
return _create('yolov5m', pretrained, channels, classes, autoshape,
|
86 |
|
87 |
|
88 |
-
def yolov5l(pretrained=True, channels=3, classes=80, autoshape=True,
|
89 |
# YOLOv5-large model https://github.com/ultralytics/yolov5
|
90 |
-
return _create('yolov5l', pretrained, channels, classes, autoshape,
|
91 |
|
92 |
|
93 |
-
def yolov5x(pretrained=True, channels=3, classes=80, autoshape=True,
|
94 |
# YOLOv5-xlarge model https://github.com/ultralytics/yolov5
|
95 |
-
return _create('yolov5x', pretrained, channels, classes, autoshape,
|
96 |
|
97 |
|
98 |
-
def yolov5n6(pretrained=True, channels=3, classes=80, autoshape=True,
|
99 |
# YOLOv5-nano-P6 model https://github.com/ultralytics/yolov5
|
100 |
-
return _create('yolov5n6', pretrained, channels, classes, autoshape,
|
101 |
|
102 |
|
103 |
-
def yolov5s6(pretrained=True, channels=3, classes=80, autoshape=True,
|
104 |
# YOLOv5-small-P6 model https://github.com/ultralytics/yolov5
|
105 |
-
return _create('yolov5s6', pretrained, channels, classes, autoshape,
|
106 |
|
107 |
|
108 |
-
def yolov5m6(pretrained=True, channels=3, classes=80, autoshape=True,
|
109 |
# YOLOv5-medium-P6 model https://github.com/ultralytics/yolov5
|
110 |
-
return _create('yolov5m6', pretrained, channels, classes, autoshape,
|
111 |
|
112 |
|
113 |
-
def yolov5l6(pretrained=True, channels=3, classes=80, autoshape=True,
|
114 |
# YOLOv5-large-P6 model https://github.com/ultralytics/yolov5
|
115 |
-
return _create('yolov5l6', pretrained, channels, classes, autoshape,
|
116 |
|
117 |
|
118 |
-
def yolov5x6(pretrained=True, channels=3, classes=80, autoshape=True,
|
119 |
# YOLOv5-xlarge-P6 model https://github.com/ultralytics/yolov5
|
120 |
-
return _create('yolov5x6', pretrained, channels, classes, autoshape,
|
121 |
|
122 |
|
123 |
if __name__ == '__main__':
|
124 |
-
model = _create(name='yolov5s', pretrained=True, channels=3, classes=80, autoshape=True, verbose=True)
|
125 |
# model = custom(path='path/to/model.pt') # custom
|
126 |
|
127 |
# Verify inference
|
|
|
36 |
|
37 |
if not verbose:
|
38 |
LOGGER.setLevel(logging.WARNING)
|
39 |
+
|
40 |
check_requirements(exclude=('tensorboard', 'thop', 'opencv-python'))
|
41 |
name = Path(name)
|
42 |
path = name.with_suffix('.pt') if name.suffix == '' else name # checkpoint path
|
|
|
66 |
raise Exception(s) from e
|
67 |
|
68 |
|
69 |
+
def custom(path='path/to/model.pt', autoshape=True, _verbose=True, device=None):
|
70 |
# YOLOv5 custom or local model
|
71 |
+
return _create(path, autoshape=autoshape, verbose=_verbose, device=device)
|
72 |
|
73 |
|
74 |
+
def yolov5n(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
75 |
# YOLOv5-nano model https://github.com/ultralytics/yolov5
|
76 |
+
return _create('yolov5n', pretrained, channels, classes, autoshape, _verbose, device)
|
77 |
|
78 |
|
79 |
+
def yolov5s(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
80 |
# YOLOv5-small model https://github.com/ultralytics/yolov5
|
81 |
+
return _create('yolov5s', pretrained, channels, classes, autoshape, _verbose, device)
|
82 |
|
83 |
|
84 |
+
def yolov5m(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
85 |
# YOLOv5-medium model https://github.com/ultralytics/yolov5
|
86 |
+
return _create('yolov5m', pretrained, channels, classes, autoshape, _verbose, device)
|
87 |
|
88 |
|
89 |
+
def yolov5l(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
90 |
# YOLOv5-large model https://github.com/ultralytics/yolov5
|
91 |
+
return _create('yolov5l', pretrained, channels, classes, autoshape, _verbose, device)
|
92 |
|
93 |
|
94 |
+
def yolov5x(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
95 |
# YOLOv5-xlarge model https://github.com/ultralytics/yolov5
|
96 |
+
return _create('yolov5x', pretrained, channels, classes, autoshape, _verbose, device)
|
97 |
|
98 |
|
99 |
+
def yolov5n6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
100 |
# YOLOv5-nano-P6 model https://github.com/ultralytics/yolov5
|
101 |
+
return _create('yolov5n6', pretrained, channels, classes, autoshape, _verbose, device)
|
102 |
|
103 |
|
104 |
+
def yolov5s6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
105 |
# YOLOv5-small-P6 model https://github.com/ultralytics/yolov5
|
106 |
+
return _create('yolov5s6', pretrained, channels, classes, autoshape, _verbose, device)
|
107 |
|
108 |
|
109 |
+
def yolov5m6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
110 |
# YOLOv5-medium-P6 model https://github.com/ultralytics/yolov5
|
111 |
+
return _create('yolov5m6', pretrained, channels, classes, autoshape, _verbose, device)
|
112 |
|
113 |
|
114 |
+
def yolov5l6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
115 |
# YOLOv5-large-P6 model https://github.com/ultralytics/yolov5
|
116 |
+
return _create('yolov5l6', pretrained, channels, classes, autoshape, _verbose, device)
|
117 |
|
118 |
|
119 |
+
def yolov5x6(pretrained=True, channels=3, classes=80, autoshape=True, _verbose=True, device=None):
|
120 |
# YOLOv5-xlarge-P6 model https://github.com/ultralytics/yolov5
|
121 |
+
return _create('yolov5x6', pretrained, channels, classes, autoshape, _verbose, device)
|
122 |
|
123 |
|
124 |
if __name__ == '__main__':
|
125 |
+
model = _create(name='yolov5s', pretrained=True, channels=3, classes=80, autoshape=True, verbose=True)
|
126 |
# model = custom(path='path/to/model.pt') # custom
|
127 |
|
128 |
# Verify inference
|
train.py
CHANGED
@@ -54,7 +54,7 @@ from utils.loggers import Loggers
|
|
54 |
from utils.loggers.wandb.wandb_utils import check_wandb_resume
|
55 |
from utils.loss import ComputeLoss
|
56 |
from utils.metrics import fitness
|
57 |
-
from utils.plots import
|
58 |
from utils.torch_utils import EarlyStopping, ModelEMA, de_parallel, select_device, torch_distributed_zero_first
|
59 |
|
60 |
LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html
|
@@ -105,8 +105,6 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
|
|
105 |
init_seeds(1 + RANK)
|
106 |
with torch_distributed_zero_first(LOCAL_RANK):
|
107 |
data_dict = data_dict or check_dataset(data) # check if None
|
108 |
-
if not is_ascii(data_dict['names']): # non-latin labels, i.e. asian, arabic, cyrillic
|
109 |
-
check_font('Arial.Unicode.ttf', progress=True)
|
110 |
train_path, val_path = data_dict['train'], data_dict['val']
|
111 |
nc = 1 if single_cls else int(data_dict['nc']) # number of classes
|
112 |
names = ['item'] if single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names
|
|
|
54 |
from utils.loggers.wandb.wandb_utils import check_wandb_resume
|
55 |
from utils.loss import ComputeLoss
|
56 |
from utils.metrics import fitness
|
57 |
+
from utils.plots import plot_evolve, plot_labels
|
58 |
from utils.torch_utils import EarlyStopping, ModelEMA, de_parallel, select_device, torch_distributed_zero_first
|
59 |
|
60 |
LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html
|
|
|
105 |
init_seeds(1 + RANK)
|
106 |
with torch_distributed_zero_first(LOCAL_RANK):
|
107 |
data_dict = data_dict or check_dataset(data) # check if None
|
|
|
|
|
108 |
train_path, val_path = data_dict['train'], data_dict['val']
|
109 |
nc = 1 if single_cls else int(data_dict['nc']) # number of classes
|
110 |
names = ['item'] if single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names
|
utils/downloads.py
CHANGED
@@ -3,6 +3,7 @@
|
|
3 |
Download utils
|
4 |
"""
|
5 |
|
|
|
6 |
import os
|
7 |
import platform
|
8 |
import subprocess
|
@@ -23,27 +24,30 @@ def gsutil_getsize(url=''):
|
|
23 |
|
24 |
def safe_download(file, url, url2=None, min_bytes=1E0, error_msg=''):
|
25 |
# Attempts to download file from url or url2, checks and removes incomplete downloads < min_bytes
|
|
|
|
|
26 |
file = Path(file)
|
27 |
assert_msg = f"Downloaded file '{file}' does not exist or size is < min_bytes={min_bytes}"
|
28 |
try: # url1
|
29 |
-
|
30 |
-
torch.hub.download_url_to_file(url, str(file))
|
31 |
assert file.exists() and file.stat().st_size > min_bytes, assert_msg # check
|
32 |
except Exception as e: # url2
|
33 |
file.unlink(missing_ok=True) # remove partial downloads
|
34 |
-
|
35 |
os.system(f"curl -L '{url2 or url}' -o '{file}' --retry 3 -C -") # curl download, retry and resume on fail
|
36 |
finally:
|
37 |
if not file.exists() or file.stat().st_size < min_bytes: # check
|
38 |
file.unlink(missing_ok=True) # remove partial downloads
|
39 |
-
|
40 |
-
|
41 |
|
42 |
|
43 |
def attempt_download(file, repo='ultralytics/yolov5'): # from utils.downloads import *; attempt_download()
|
44 |
# Attempt file download if does not exist
|
45 |
-
|
46 |
|
|
|
47 |
if not file.exists():
|
48 |
# URL specified
|
49 |
name = Path(urllib.parse.unquote(str(file))).name # decode '%2F' to '/' etc.
|
@@ -51,7 +55,7 @@ def attempt_download(file, repo='ultralytics/yolov5'): # from utils.downloads i
|
|
51 |
url = str(file).replace(':/', '://') # Pathlib turns :// -> :/
|
52 |
file = name.split('?')[0] # parse authentication https://url.com/file.txt?auth...
|
53 |
if Path(file).is_file():
|
54 |
-
|
55 |
else:
|
56 |
safe_download(file=file, url=url, min_bytes=1E5)
|
57 |
return file
|
|
|
3 |
Download utils
|
4 |
"""
|
5 |
|
6 |
+
import logging
|
7 |
import os
|
8 |
import platform
|
9 |
import subprocess
|
|
|
24 |
|
25 |
def safe_download(file, url, url2=None, min_bytes=1E0, error_msg=''):
|
26 |
# Attempts to download file from url or url2, checks and removes incomplete downloads < min_bytes
|
27 |
+
from utils.general import LOGGER
|
28 |
+
|
29 |
file = Path(file)
|
30 |
assert_msg = f"Downloaded file '{file}' does not exist or size is < min_bytes={min_bytes}"
|
31 |
try: # url1
|
32 |
+
LOGGER.info(f'Downloading {url} to {file}...')
|
33 |
+
torch.hub.download_url_to_file(url, str(file), progress=LOGGER.level <= logging.INFO)
|
34 |
assert file.exists() and file.stat().st_size > min_bytes, assert_msg # check
|
35 |
except Exception as e: # url2
|
36 |
file.unlink(missing_ok=True) # remove partial downloads
|
37 |
+
LOGGER.info(f'ERROR: {e}\nRe-attempting {url2 or url} to {file}...')
|
38 |
os.system(f"curl -L '{url2 or url}' -o '{file}' --retry 3 -C -") # curl download, retry and resume on fail
|
39 |
finally:
|
40 |
if not file.exists() or file.stat().st_size < min_bytes: # check
|
41 |
file.unlink(missing_ok=True) # remove partial downloads
|
42 |
+
LOGGER.info(f"ERROR: {assert_msg}\n{error_msg}")
|
43 |
+
LOGGER.info('')
|
44 |
|
45 |
|
46 |
def attempt_download(file, repo='ultralytics/yolov5'): # from utils.downloads import *; attempt_download()
|
47 |
# Attempt file download if does not exist
|
48 |
+
from utils.general import LOGGER
|
49 |
|
50 |
+
file = Path(str(file).strip().replace("'", ''))
|
51 |
if not file.exists():
|
52 |
# URL specified
|
53 |
name = Path(urllib.parse.unquote(str(file))).name # decode '%2F' to '/' etc.
|
|
|
55 |
url = str(file).replace(':/', '://') # Pathlib turns :// -> :/
|
56 |
file = name.split('?')[0] # parse authentication https://url.com/file.txt?auth...
|
57 |
if Path(file).is_file():
|
58 |
+
LOGGER.info(f'Found {url} locally at {file}') # file already exists
|
59 |
else:
|
60 |
safe_download(file=file, url=url, min_bytes=1E5)
|
61 |
return file
|
utils/general.py
CHANGED
@@ -490,6 +490,7 @@ def check_dataset(data, autodownload=True):
|
|
490 |
else:
|
491 |
raise Exception(emojis('Dataset not found ❌'))
|
492 |
|
|
|
493 |
return data # dictionary
|
494 |
|
495 |
|
|
|
490 |
else:
|
491 |
raise Exception(emojis('Dataset not found ❌'))
|
492 |
|
493 |
+
check_font('Arial.ttf' if is_ascii(data['names']) else 'Arial.Unicode.ttf', progress=True) # download fonts
|
494 |
return data # dictionary
|
495 |
|
496 |
|
utils/plots.py
CHANGED
@@ -66,9 +66,6 @@ def check_pil_font(font=FONT, size=10):
|
|
66 |
|
67 |
|
68 |
class Annotator:
|
69 |
-
if RANK in (-1, 0):
|
70 |
-
check_pil_font() # download TTF if necessary
|
71 |
-
|
72 |
# YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
|
73 |
def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc'):
|
74 |
assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.'
|
|
|
66 |
|
67 |
|
68 |
class Annotator:
|
|
|
|
|
|
|
69 |
# YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
|
70 |
def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc'):
|
71 |
assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.'
|