ultralytics 8.0.48
Edge TPU fix and Metrics updates (#1171)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: majid nasiri <majnasai@gmail.com>
This commit is contained in:
@ -3,11 +3,11 @@
|
||||
import requests
|
||||
|
||||
from ultralytics.hub.auth import Auth
|
||||
from ultralytics.hub.session import HubTrainingSession
|
||||
from ultralytics.hub.utils import split_key
|
||||
from ultralytics.hub.session import HUBTrainingSession
|
||||
from ultralytics.hub.utils import PREFIX, split_key
|
||||
from ultralytics.yolo.engine.exporter import EXPORT_FORMATS_LIST
|
||||
from ultralytics.yolo.engine.model import YOLO
|
||||
from ultralytics.yolo.utils import LOGGER, PREFIX, emojis
|
||||
from ultralytics.yolo.utils import LOGGER, emojis
|
||||
|
||||
# Define all export formats
|
||||
EXPORT_FORMATS_HUB = EXPORT_FORMATS_LIST + ['ultralytics_tflite', 'ultralytics_coreml']
|
||||
@ -18,23 +18,19 @@ def start(key=''):
|
||||
Start training models with Ultralytics HUB. Usage: from ultralytics.hub import start; start('API_KEY')
|
||||
"""
|
||||
auth = Auth(key)
|
||||
try:
|
||||
if not auth.get_state():
|
||||
model_id = request_api_key(auth)
|
||||
else:
|
||||
_, model_id = split_key(key)
|
||||
if not auth.get_state():
|
||||
model_id = request_api_key(auth)
|
||||
else:
|
||||
_, model_id = split_key(key)
|
||||
|
||||
if not model_id:
|
||||
raise ConnectionError(emojis('Connecting with global API key is not currently supported. ❌'))
|
||||
if not model_id:
|
||||
raise ConnectionError(emojis('Connecting with global API key is not currently supported. ❌'))
|
||||
|
||||
session = HubTrainingSession(model_id=model_id, auth=auth)
|
||||
session.check_disk_space()
|
||||
session = HUBTrainingSession(model_id=model_id, auth=auth)
|
||||
session.check_disk_space()
|
||||
|
||||
model = YOLO(session.input_file)
|
||||
session.register_callbacks(model)
|
||||
model.train(**session.train_args)
|
||||
except Exception as e:
|
||||
LOGGER.warning(f'{PREFIX}{e}')
|
||||
model = YOLO(model=session.model_file, session=session)
|
||||
model.train(**session.train_args)
|
||||
|
||||
|
||||
def request_api_key(auth, max_attempts=3):
|
||||
@ -62,9 +58,9 @@ def reset_model(key=''):
|
||||
r = requests.post('https://api.ultralytics.com/model-reset', json={'apiKey': api_key, 'modelId': model_id})
|
||||
|
||||
if r.status_code == 200:
|
||||
LOGGER.info(f'{PREFIX}model reset successfully')
|
||||
LOGGER.info(f'{PREFIX}Model reset successfully')
|
||||
return
|
||||
LOGGER.warning(f'{PREFIX}model reset failure {r.status_code} {r.reason}')
|
||||
LOGGER.warning(f'{PREFIX}Model reset failure {r.status_code} {r.reason}')
|
||||
|
||||
|
||||
def export_model(key='', format='torchscript'):
|
||||
@ -76,7 +72,7 @@ def export_model(key='', format='torchscript'):
|
||||
'apiKey': api_key,
|
||||
'modelId': model_id,
|
||||
'format': format})
|
||||
assert (r.status_code == 200), f'{PREFIX}{format} export failure {r.status_code} {r.reason}'
|
||||
assert r.status_code == 200, f'{PREFIX}{format} export failure {r.status_code} {r.reason}'
|
||||
LOGGER.info(f'{PREFIX}{format} export started ✅')
|
||||
|
||||
|
||||
@ -89,7 +85,7 @@ def get_export(key='', format='torchscript'):
|
||||
'apiKey': api_key,
|
||||
'modelId': model_id,
|
||||
'format': format})
|
||||
assert (r.status_code == 200), f'{PREFIX}{format} get_export failure {r.status_code} {r.reason}'
|
||||
assert r.status_code == 200, f'{PREFIX}{format} get_export failure {r.status_code} {r.reason}'
|
||||
return r.json()
|
||||
|
||||
|
||||
|
@ -1,30 +1,27 @@
|
||||
# Ultralytics YOLO 🚀, GPL-3.0 license
|
||||
import json
|
||||
import signal
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from time import sleep, time
|
||||
from time import sleep
|
||||
|
||||
import requests
|
||||
|
||||
from ultralytics.hub.utils import HUB_API_ROOT, check_dataset_disk_space, smart_request
|
||||
from ultralytics.yolo.utils import LOGGER, PREFIX, __version__, emojis, is_colab, threaded
|
||||
from ultralytics.yolo.utils.torch_utils import get_flops, get_num_params
|
||||
from ultralytics.yolo.utils import LOGGER, PREFIX, __version__, checks, emojis, is_colab, threaded
|
||||
|
||||
AGENT_NAME = f'python-{__version__}-colab' if is_colab() else f'python-{__version__}-local'
|
||||
session = None
|
||||
|
||||
|
||||
class HubTrainingSession:
|
||||
class HUBTrainingSession:
|
||||
|
||||
def __init__(self, model_id, auth):
|
||||
self.agent_id = None # identifies which instance is communicating with server
|
||||
self.model_id = model_id
|
||||
self.api_url = f'{HUB_API_ROOT}/v1/models/{model_id}'
|
||||
self.auth_header = auth.get_auth_header()
|
||||
self._rate_limits = {'metrics': 3.0, 'ckpt': 900.0, 'heartbeat': 300.0} # rate limits (seconds)
|
||||
self._timers = {} # rate limit timers (seconds)
|
||||
self._metrics_queue = {} # metrics queue
|
||||
self.rate_limits = {'metrics': 3.0, 'ckpt': 900.0, 'heartbeat': 300.0} # rate limits (seconds)
|
||||
self.timers = {} # rate limit timers (seconds)
|
||||
self.metrics_queue = {} # metrics queue
|
||||
self.model = self._get_model()
|
||||
self.alive = True
|
||||
self._start_heartbeat() # start heartbeats
|
||||
@ -50,16 +47,15 @@ class HubTrainingSession:
|
||||
self.alive = False
|
||||
|
||||
def upload_metrics(self):
|
||||
payload = {'metrics': self._metrics_queue.copy(), 'type': 'metrics'}
|
||||
smart_request(f'{self.api_url}', json=payload, headers=self.auth_header, code=2)
|
||||
payload = {'metrics': self.metrics_queue.copy(), 'type': 'metrics'}
|
||||
smart_request('post', self.api_url, json=payload, headers=self.auth_header, code=2)
|
||||
|
||||
def _get_model(self):
|
||||
# Returns model from database by id
|
||||
api_url = f'{HUB_API_ROOT}/v1/models/{self.model_id}'
|
||||
headers = self.auth_header
|
||||
|
||||
try:
|
||||
response = smart_request(api_url, method='get', headers=headers, thread=False, code=0)
|
||||
response = smart_request('get', api_url, headers=self.auth_header, thread=False, code=0)
|
||||
data = response.json().get('data', None)
|
||||
|
||||
if data.get('status', None) == 'trained':
|
||||
@ -82,11 +78,8 @@ class HubTrainingSession:
|
||||
'cache': data['cache'],
|
||||
'data': data['data']}
|
||||
|
||||
self.input_file = data.get('cfg', data['weights'])
|
||||
|
||||
# hack for yolov5 cfg adds u
|
||||
if 'cfg' in data and 'yolov5' in data['cfg']:
|
||||
self.input_file = data['cfg'].replace('.yaml', 'u.yaml')
|
||||
self.model_file = data.get('cfg', data['weights'])
|
||||
self.model_file = checks.check_yolov5u_filename(self.model_file, verbose=False) # YOLOv5->YOLOv5u
|
||||
|
||||
return data
|
||||
except requests.exceptions.ConnectionError as e:
|
||||
@ -98,86 +91,44 @@ class HubTrainingSession:
|
||||
if not check_dataset_disk_space(self.model['data']):
|
||||
raise MemoryError('Not enough disk space')
|
||||
|
||||
def register_callbacks(self, trainer):
|
||||
trainer.add_callback('on_pretrain_routine_end', self.on_pretrain_routine_end)
|
||||
trainer.add_callback('on_fit_epoch_end', self.on_fit_epoch_end)
|
||||
trainer.add_callback('on_model_save', self.on_model_save)
|
||||
trainer.add_callback('on_train_end', self.on_train_end)
|
||||
|
||||
def on_pretrain_routine_end(self, trainer):
|
||||
"""
|
||||
Start timer for upload rate limit.
|
||||
This method does not use trainer. It is passed to all callbacks by default.
|
||||
"""
|
||||
# Start timer for upload rate limit
|
||||
LOGGER.info(f'{PREFIX}View model at https://hub.ultralytics.com/models/{self.model_id} 🚀')
|
||||
self._timers = {'metrics': time(), 'ckpt': time()} # start timer on self.rate_limit
|
||||
|
||||
def on_fit_epoch_end(self, trainer):
|
||||
# Upload metrics after val end
|
||||
all_plots = {**trainer.label_loss_items(trainer.tloss, prefix='train'), **trainer.metrics}
|
||||
|
||||
if trainer.epoch == 0:
|
||||
model_info = {
|
||||
'model/parameters': get_num_params(trainer.model),
|
||||
'model/GFLOPs': round(get_flops(trainer.model), 3),
|
||||
'model/speed(ms)': round(trainer.validator.speed['inference'], 3)}
|
||||
all_plots = {**all_plots, **model_info}
|
||||
self._metrics_queue[trainer.epoch] = json.dumps(all_plots)
|
||||
if time() - self._timers['metrics'] > self._rate_limits['metrics']:
|
||||
self.upload_metrics()
|
||||
self._timers['metrics'] = time() # reset timer
|
||||
self._metrics_queue = {} # reset queue
|
||||
|
||||
def on_model_save(self, trainer):
|
||||
# Upload checkpoints with rate limiting
|
||||
is_best = trainer.best_fitness == trainer.fitness
|
||||
if time() - self._timers['ckpt'] > self._rate_limits['ckpt']:
|
||||
LOGGER.info(f'{PREFIX}Uploading checkpoint {self.model_id}')
|
||||
self._upload_model(trainer.epoch, trainer.last, is_best)
|
||||
self._timers['ckpt'] = time() # reset timer
|
||||
|
||||
def on_train_end(self, trainer):
|
||||
# Upload final model and metrics with exponential standoff
|
||||
LOGGER.info(f'{PREFIX}Training completed successfully ✅\n'
|
||||
f'{PREFIX}Uploading final {self.model_id}')
|
||||
|
||||
self._upload_model(trainer.epoch, trainer.best, map=trainer.metrics.get('metrics/mAP50-95(B)', 0), final=True)
|
||||
self.alive = False # stop heartbeats
|
||||
LOGGER.info(f'{PREFIX}View model at https://hub.ultralytics.com/models/{self.model_id} 🚀')
|
||||
|
||||
def _upload_model(self, epoch, weights, is_best=False, map=0.0, final=False):
|
||||
def upload_model(self, epoch, weights, is_best=False, map=0.0, final=False):
|
||||
# Upload a model to HUB
|
||||
if Path(weights).is_file():
|
||||
with open(weights, 'rb') as f:
|
||||
file = f.read()
|
||||
else:
|
||||
LOGGER.warning(f'{PREFIX}WARNING ⚠️ Model upload failed. Missing model {weights}.')
|
||||
LOGGER.warning(f'{PREFIX}WARNING ⚠️ Model upload issue. Missing model {weights}.')
|
||||
file = None
|
||||
url = f'{self.api_url}/upload'
|
||||
# url = 'http://httpbin.org/post' # for debug
|
||||
data = {'epoch': epoch}
|
||||
if final:
|
||||
data.update({'type': 'final', 'map': map})
|
||||
smart_request('post',
|
||||
url,
|
||||
data=data,
|
||||
files={'best.pt': file},
|
||||
headers=self.auth_header,
|
||||
retry=10,
|
||||
timeout=3600,
|
||||
thread=False,
|
||||
progress=True,
|
||||
code=4)
|
||||
else:
|
||||
data.update({'type': 'epoch', 'isBest': bool(is_best)})
|
||||
|
||||
smart_request(f'{self.api_url}/upload',
|
||||
data=data,
|
||||
files={'best.pt' if final else 'last.pt': file},
|
||||
headers=self.auth_header,
|
||||
retry=10 if final else None,
|
||||
timeout=3600 if final else None,
|
||||
code=4 if final else 3)
|
||||
smart_request('post', url, data=data, files={'last.pt': file}, headers=self.auth_header, code=3)
|
||||
|
||||
@threaded
|
||||
def _start_heartbeat(self):
|
||||
while self.alive:
|
||||
r = smart_request(f'{HUB_API_ROOT}/v1/agent/heartbeat/models/{self.model_id}',
|
||||
r = smart_request('post',
|
||||
f'{HUB_API_ROOT}/v1/agent/heartbeat/models/{self.model_id}',
|
||||
json={
|
||||
'agent': AGENT_NAME,
|
||||
'agentId': self.agent_id},
|
||||
headers=self.auth_header,
|
||||
retry=0,
|
||||
code=5,
|
||||
thread=False)
|
||||
thread=False) # already in a thread
|
||||
self.agent_id = r.json().get('data', {}).get('agentId', None)
|
||||
sleep(self._rate_limits['heartbeat'])
|
||||
sleep(self.rate_limits['heartbeat'])
|
||||
|
@ -10,13 +10,13 @@ from pathlib import Path
|
||||
from random import random
|
||||
|
||||
import requests
|
||||
from tqdm import tqdm
|
||||
|
||||
from ultralytics.yolo.utils import (DEFAULT_CFG_DICT, ENVIRONMENT, LOGGER, RANK, SETTINGS, TESTS_RUNNING, TryExcept,
|
||||
__version__, colorstr, emojis, get_git_origin_url, is_colab, is_git_dir,
|
||||
is_pip_package)
|
||||
from ultralytics.yolo.utils.checks import check_online
|
||||
from ultralytics.yolo.utils import (DEFAULT_CFG_DICT, ENVIRONMENT, LOGGER, ONLINE, RANK, SETTINGS, TESTS_RUNNING,
|
||||
TQDM_BAR_FORMAT, TryExcept, __version__, colorstr, emojis, get_git_origin_url,
|
||||
is_colab, is_git_dir, is_pip_package)
|
||||
|
||||
PREFIX = colorstr('Ultralytics: ')
|
||||
PREFIX = colorstr('Ultralytics HUB: ')
|
||||
HELP_MSG = 'If this issue persists please visit https://github.com/ultralytics/hub/issues for assistance.'
|
||||
HUB_API_ROOT = os.environ.get('ULTRALYTICS_HUB_API', 'https://api.ultralytics.com')
|
||||
|
||||
@ -60,7 +60,6 @@ def request_with_credentials(url: str) -> any:
|
||||
return output.eval_js('_hub_tmp')
|
||||
|
||||
|
||||
# Deprecated TODO: eliminate this function?
|
||||
def split_key(key=''):
|
||||
"""
|
||||
Verify and split a 'api_key[sep]model_id' string, sep is one of '.' or '_'
|
||||
@ -84,36 +83,61 @@ def split_key(key=''):
|
||||
return api_key, model_id
|
||||
|
||||
|
||||
def smart_request(*args, retry=3, timeout=30, thread=True, code=-1, method='post', verbose=True, **kwargs):
|
||||
def requests_with_progress(method, url, **kwargs):
|
||||
"""
|
||||
Make an HTTP request using the specified method and URL, with an optional progress bar.
|
||||
|
||||
Args:
|
||||
method (str): The HTTP method to use (e.g. 'GET', 'POST').
|
||||
url (str): The URL to send the request to.
|
||||
progress (bool, optional): Whether to display a progress bar. Defaults to False.
|
||||
**kwargs: Additional keyword arguments to pass to the underlying `requests.request` function.
|
||||
|
||||
Returns:
|
||||
requests.Response: The response from the HTTP request.
|
||||
|
||||
"""
|
||||
progress = kwargs.pop('progress', False)
|
||||
if not progress:
|
||||
return requests.request(method, url, **kwargs)
|
||||
response = requests.request(method, url, stream=True, **kwargs)
|
||||
total = int(response.headers.get('content-length', 0)) # total size
|
||||
pbar = tqdm(total=total, unit='B', unit_scale=True, unit_divisor=1024, bar_format=TQDM_BAR_FORMAT)
|
||||
for data in response.iter_content(chunk_size=1024):
|
||||
pbar.update(len(data))
|
||||
pbar.close()
|
||||
return response
|
||||
|
||||
|
||||
def smart_request(method, url, retry=3, timeout=30, thread=True, code=-1, verbose=True, progress=False, **kwargs):
|
||||
"""
|
||||
Makes an HTTP request using the 'requests' library, with exponential backoff retries up to a specified timeout.
|
||||
|
||||
Args:
|
||||
*args: Positional arguments to be passed to the requests function specified in method.
|
||||
method (str): The HTTP method to use for the request. Choices are 'post' and 'get'.
|
||||
url (str): The URL to make the request to.
|
||||
retry (int, optional): Number of retries to attempt before giving up. Default is 3.
|
||||
timeout (int, optional): Timeout in seconds after which the function will give up retrying. Default is 30.
|
||||
thread (bool, optional): Whether to execute the request in a separate daemon thread. Default is True.
|
||||
code (int, optional): An identifier for the request, used for logging purposes. Default is -1.
|
||||
method (str, optional): The HTTP method to use for the request. Choices are 'post' and 'get'. Default is 'post'.
|
||||
verbose (bool, optional): A flag to determine whether to print out to console or not. Default is True.
|
||||
progress (bool, optional): Whether to show a progress bar during the request. Default is False.
|
||||
**kwargs: Keyword arguments to be passed to the requests function specified in method.
|
||||
|
||||
Returns:
|
||||
requests.Response: The HTTP response object. If the request is executed in a separate thread, returns None.
|
||||
|
||||
"""
|
||||
retry_codes = (408, 500) # retry only these codes
|
||||
|
||||
@TryExcept(verbose=verbose)
|
||||
def func(*func_args, **func_kwargs):
|
||||
def func(func_method, func_url, **func_kwargs):
|
||||
r = None # response
|
||||
t0 = time.time() # initial time for timer
|
||||
for i in range(retry + 1):
|
||||
if (time.time() - t0) > timeout:
|
||||
break
|
||||
if method == 'post':
|
||||
r = requests.post(*func_args, **func_kwargs) # i.e. post(url, data, json, files)
|
||||
elif method == 'get':
|
||||
r = requests.get(*func_args, **func_kwargs) # i.e. get(url, data, json, files)
|
||||
r = requests_with_progress(func_method, func_url, **func_kwargs) # i.e. get(url, data, json, files)
|
||||
if r.status_code == 200:
|
||||
break
|
||||
try:
|
||||
@ -134,6 +158,8 @@ def smart_request(*args, retry=3, timeout=30, thread=True, code=-1, method='post
|
||||
time.sleep(2 ** i) # exponential standoff
|
||||
return r
|
||||
|
||||
args = method, url
|
||||
kwargs['progress'] = progress
|
||||
if thread:
|
||||
threading.Thread(target=func, args=args, kwargs=kwargs, daemon=True).start()
|
||||
else:
|
||||
@ -157,8 +183,8 @@ class Traces:
|
||||
self.enabled = \
|
||||
SETTINGS['sync'] and \
|
||||
RANK in {-1, 0} and \
|
||||
check_online() and \
|
||||
not TESTS_RUNNING and \
|
||||
ONLINE and \
|
||||
(is_pip_package() or get_git_origin_url() == 'https://github.com/ultralytics/ultralytics.git')
|
||||
|
||||
def __call__(self, cfg, all_keys=False, traces_sample_rate=1.0):
|
||||
@ -182,13 +208,7 @@ class Traces:
|
||||
trace = {'uuid': SETTINGS['uuid'], 'cfg': cfg, 'metadata': self.metadata}
|
||||
|
||||
# Send a request to the HUB API to sync analytics
|
||||
smart_request(f'{HUB_API_ROOT}/v1/usage/anonymous',
|
||||
json=trace,
|
||||
headers=None,
|
||||
code=3,
|
||||
retry=0,
|
||||
timeout=1.0,
|
||||
verbose=False)
|
||||
smart_request('post', f'{HUB_API_ROOT}/v1/usage/anonymous', json=trace, code=3, retry=0, verbose=False)
|
||||
|
||||
|
||||
# Run below code on hub/utils init -------------------------------------------------------------------------------------
|
||||
|
Reference in New Issue
Block a user