Add HUB URL environment variables (#3988)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
single_channel
Kalen Michael 1 year ago committed by GitHub
parent 1a0eb3f099
commit b5ea8ce872
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -27,7 +27,7 @@ on:
jobs: jobs:
HUB: HUB:
if: github.repository == 'ultralytics/ultralytics' && (github.event_name == 'schedule' || github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.tests == 'true')) if: github.repository == 'ultralytics/ultralytics' && (github.event_name == 'schedule' || github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.hub == 'true'))
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
strategy: strategy:
fail-fast: false fail-fast: false

@ -7,6 +7,7 @@ from typing import Union
from ultralytics.cfg import get_cfg from ultralytics.cfg import get_cfg
from ultralytics.engine.exporter import Exporter from ultralytics.engine.exporter import Exporter
from ultralytics.hub.utils import HUB_WEB_ROOT
from ultralytics.nn.tasks import attempt_load_one_weight, guess_model_task, nn, yaml_model_load from ultralytics.nn.tasks import attempt_load_one_weight, guess_model_task, nn, yaml_model_load
from ultralytics.utils import (DEFAULT_CFG, DEFAULT_CFG_DICT, DEFAULT_CFG_KEYS, LOGGER, RANK, ROOT, callbacks, from ultralytics.utils import (DEFAULT_CFG, DEFAULT_CFG_DICT, DEFAULT_CFG_KEYS, LOGGER, RANK, ROOT, callbacks,
is_git_dir, yaml_load) is_git_dir, yaml_load)
@ -100,7 +101,7 @@ class Model:
def is_hub_model(model): def is_hub_model(model):
"""Check if the provided model is a HUB model.""" """Check if the provided model is a HUB model."""
return any(( return any((
model.startswith('https://hub.ultralytics.com/models/'), # i.e. https://hub.ultralytics.com/models/MODEL_ID model.startswith(f'{HUB_WEB_ROOT}/models/'), # i.e. https://hub.ultralytics.com/models/MODEL_ID
[len(x) for x in model.split('_')] == [42, 20], # APIKEY_MODELID [len(x) for x in model.split('_')] == [42, 20], # APIKEY_MODELID
len(model) == 20 and not Path(model).exists() and all(x not in model for x in './\\'))) # MODELID len(model) == 20 and not Path(model).exists() and all(x not in model for x in './\\'))) # MODELID

@ -4,7 +4,7 @@ import requests
from ultralytics.data.utils import HUBDatasetStats from ultralytics.data.utils import HUBDatasetStats
from ultralytics.hub.auth import Auth from ultralytics.hub.auth import Auth
from ultralytics.hub.utils import PREFIX from ultralytics.hub.utils import HUB_API_ROOT, HUB_WEB_ROOT, PREFIX
from ultralytics.utils import LOGGER, SETTINGS, USER_CONFIG_DIR, yaml_save from ultralytics.utils import LOGGER, SETTINGS, USER_CONFIG_DIR, yaml_save
@ -50,13 +50,13 @@ WARNING ⚠️ ultralytics.start() is deprecated after 8.0.60. Updated usage to
from ultralytics import YOLO, hub from ultralytics import YOLO, hub
hub.login('{api_key}') hub.login('{api_key}')
model = YOLO('https://hub.ultralytics.com/models/{model_id}') model = YOLO('{HUB_WEB_ROOT}/models/{model_id}')
model.train()""") model.train()""")
def reset_model(model_id=''): def reset_model(model_id=''):
"""Reset a trained model to an untrained state.""" """Reset a trained model to an untrained state."""
r = requests.post('https://api.ultralytics.com/model-reset', json={'apiKey': Auth().api_key, 'modelId': model_id}) r = requests.post(f'{HUB_API_ROOT}/model-reset', json={'apiKey': Auth().api_key, 'modelId': model_id})
if r.status_code == 200: if r.status_code == 200:
LOGGER.info(f'{PREFIX}Model reset successfully') LOGGER.info(f'{PREFIX}Model reset successfully')
return return
@ -72,7 +72,7 @@ def export_fmts_hub():
def export_model(model_id='', format='torchscript'): def export_model(model_id='', format='torchscript'):
"""Export a model to all formats.""" """Export a model to all formats."""
assert format in export_fmts_hub(), f"Unsupported export format '{format}', valid formats are {export_fmts_hub()}" assert format in export_fmts_hub(), f"Unsupported export format '{format}', valid formats are {export_fmts_hub()}"
r = requests.post(f'https://api.ultralytics.com/v1/models/{model_id}/export', r = requests.post(f'{HUB_API_ROOT}/v1/models/{model_id}/export',
json={'format': format}, json={'format': format},
headers={'x-api-key': Auth().api_key}) headers={'x-api-key': Auth().api_key})
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}'
@ -82,7 +82,7 @@ def export_model(model_id='', format='torchscript'):
def get_export(model_id='', format='torchscript'): def get_export(model_id='', format='torchscript'):
"""Get an exported model dictionary with download URL.""" """Get an exported model dictionary with download URL."""
assert format in export_fmts_hub(), f"Unsupported export format '{format}', valid formats are {export_fmts_hub()}" assert format in export_fmts_hub(), f"Unsupported export format '{format}', valid formats are {export_fmts_hub()}"
r = requests.post('https://api.ultralytics.com/get-export', r = requests.post(f'{HUB_API_ROOT}/get-export',
json={ json={
'apiKey': Auth().api_key, 'apiKey': Auth().api_key,
'modelId': model_id, 'modelId': model_id,
@ -110,7 +110,7 @@ def check_dataset(path='', task='detect'):
``` ```
""" """
HUBDatasetStats(path=path, task=task).get_json() HUBDatasetStats(path=path, task=task).get_json()
LOGGER.info('Checks completed correctly ✅. Upload this dataset to https://hub.ultralytics.com/datasets/.') LOGGER.info(f'Checks completed correctly ✅. Upload this dataset to {HUB_WEB_ROOT}/datasets/.')
if __name__ == '__main__': if __name__ == '__main__':

@ -2,10 +2,10 @@
import requests import requests
from ultralytics.hub.utils import HUB_API_ROOT, PREFIX, request_with_credentials from ultralytics.hub.utils import HUB_API_ROOT, HUB_WEB_ROOT, PREFIX, request_with_credentials
from ultralytics.utils import LOGGER, SETTINGS, emojis, is_colab from ultralytics.utils import LOGGER, SETTINGS, emojis, is_colab
API_KEY_URL = 'https://hub.ultralytics.com/settings?tab=api+keys' API_KEY_URL = f'{HUB_WEB_ROOT}/settings?tab=api+keys'
class Auth: class Auth:

@ -6,7 +6,7 @@ from time import sleep
import requests import requests
from ultralytics.hub.utils import HUB_API_ROOT, PREFIX, smart_request from ultralytics.hub.utils import HUB_API_ROOT, HUB_WEB_ROOT, PREFIX, smart_request
from ultralytics.utils import LOGGER, __version__, checks, emojis, is_colab, threaded from ultralytics.utils import LOGGER, __version__, checks, emojis, is_colab, threaded
from ultralytics.utils.errors import HUBModelError from ultralytics.utils.errors import HUBModelError
@ -49,21 +49,21 @@ class HUBTrainingSession:
from ultralytics.hub.auth import Auth from ultralytics.hub.auth import Auth
# Parse input # Parse input
if url.startswith('https://hub.ultralytics.com/models/'): if url.startswith(f'{HUB_WEB_ROOT}/models/'):
url = url.split('https://hub.ultralytics.com/models/')[-1] url = url.split(f'{HUB_WEB_ROOT}/models/')[-1]
if [len(x) for x in url.split('_')] == [42, 20]: if [len(x) for x in url.split('_')] == [42, 20]:
key, model_id = url.split('_') key, model_id = url.split('_')
elif len(url) == 20: elif len(url) == 20:
key, model_id = '', url key, model_id = '', url
else: else:
raise HUBModelError(f"model='{url}' not found. Check format is correct, i.e. " raise HUBModelError(f"model='{url}' not found. Check format is correct, i.e. "
f"model='https://hub.ultralytics.com/models/MODEL_ID' and try again.") f"model='{HUB_WEB_ROOT}/models/MODEL_ID' and try again.")
# Authorize # Authorize
auth = Auth(key) auth = Auth(key)
self.agent_id = None # identifies which instance is communicating with server self.agent_id = None # identifies which instance is communicating with server
self.model_id = model_id self.model_id = model_id
self.model_url = f'https://hub.ultralytics.com/models/{model_id}' self.model_url = f'{HUB_WEB_ROOT}/models/{model_id}'
self.api_url = f'{HUB_API_ROOT}/v1/models/{model_id}' self.api_url = f'{HUB_API_ROOT}/v1/models/{model_id}'
self.auth_header = auth.get_auth_header() self.auth_header = auth.get_auth_header()
self.rate_limits = {'metrics': 3.0, 'ckpt': 900.0, 'heartbeat': 300.0} # rate limits (seconds) self.rate_limits = {'metrics': 3.0, 'ckpt': 900.0, 'heartbeat': 300.0} # rate limits (seconds)

@ -18,6 +18,7 @@ from ultralytics.utils.downloads import GITHUB_ASSET_NAMES
PREFIX = colorstr('Ultralytics HUB: ') PREFIX = colorstr('Ultralytics HUB: ')
HELP_MSG = 'If this issue persists please visit https://github.com/ultralytics/hub/issues for assistance.' 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') HUB_API_ROOT = os.environ.get('ULTRALYTICS_HUB_API', 'https://api.ultralytics.com')
HUB_WEB_ROOT = os.environ.get('ULTRALYTICS_HUB_WEB', 'https://hub.ultralytics.com')
def request_with_credentials(url: str) -> any: def request_with_credentials(url: str) -> any:

@ -3,7 +3,7 @@
import json import json
from time import time from time import time
from ultralytics.hub.utils import PREFIX, events from ultralytics.hub.utils import HUB_WEB_ROOT, PREFIX, events
from ultralytics.utils import LOGGER, SETTINGS from ultralytics.utils import LOGGER, SETTINGS
from ultralytics.utils.torch_utils import model_info_for_loggers from ultralytics.utils.torch_utils import model_info_for_loggers
@ -13,7 +13,7 @@ def on_pretrain_routine_end(trainer):
session = getattr(trainer, 'hub_session', None) session = getattr(trainer, 'hub_session', None)
if session: if session:
# Start timer for upload rate limit # Start timer for upload rate limit
LOGGER.info(f'{PREFIX}View model at https://hub.ultralytics.com/models/{session.model_id} 🚀') LOGGER.info(f'{PREFIX}View model at {HUB_WEB_ROOT}/models/{session.model_id} 🚀')
session.timers = {'metrics': time(), 'ckpt': time()} # start timer on session.rate_limit session.timers = {'metrics': time(), 'ckpt': time()} # start timer on session.rate_limit
@ -39,7 +39,7 @@ def on_model_save(trainer):
# Upload checkpoints with rate limiting # Upload checkpoints with rate limiting
is_best = trainer.best_fitness == trainer.fitness is_best = trainer.best_fitness == trainer.fitness
if time() - session.timers['ckpt'] > session.rate_limits['ckpt']: if time() - session.timers['ckpt'] > session.rate_limits['ckpt']:
LOGGER.info(f'{PREFIX}Uploading checkpoint https://hub.ultralytics.com/models/{session.model_id}') LOGGER.info(f'{PREFIX}Uploading checkpoint {HUB_WEB_ROOT}/models/{session.model_id}')
session.upload_model(trainer.epoch, trainer.last, is_best) session.upload_model(trainer.epoch, trainer.last, is_best)
session.timers['ckpt'] = time() # reset timer session.timers['ckpt'] = time() # reset timer
@ -53,7 +53,7 @@ def on_train_end(trainer):
session.upload_model(trainer.epoch, trainer.best, map=trainer.metrics.get('metrics/mAP50-95(B)', 0), final=True) session.upload_model(trainer.epoch, trainer.best, map=trainer.metrics.get('metrics/mAP50-95(B)', 0), final=True)
session.alive = False # stop heartbeats session.alive = False # stop heartbeats
LOGGER.info(f'{PREFIX}Done ✅\n' LOGGER.info(f'{PREFIX}Done ✅\n'
f'{PREFIX}View model at https://hub.ultralytics.com/models/{session.model_id} 🚀') f'{PREFIX}View model at {HUB_WEB_ROOT}/models/{session.model_id} 🚀')
def on_train_start(trainer): def on_train_start(trainer):

Loading…
Cancel
Save