跳至内容

Ultralytics HUB-SDK 模型管理操作指南

欢迎访问Ultralytics HUB-SDK 模型管理文档!无论你是刚刚开始管理机器学习模型,还是经验丰富的数据科学家在寻找具体的操作指南,你都找对地方了。让我们一起来畅游 HUB-SDK 的各项功能,确保你掌握高效管理模型的诀窍。

通过唯一标识符检索模型

在机器学习工作流中,您经常需要访问特定的模型。有了Ultralytics HUB-SDK,通过 ID 获取模型变得轻而易举。这一基本功能根据提供的唯一标识符设置模型对象,让您可以完全访问模型的详细信息和操作。

from hub_sdk import HUBClient

credentials = {"api_key": "<YOUR-API-KEY>"}
client = HUBClient(credentials)

model = client.model("<Model ID>")
print(model.data)  # Outputs the model's metadata and configuration

Access 项目和数据集的先决条件

在创建或训练模型之前,确保项目和数据集的存在至关重要。这个简单明了的代码片段可以帮助您通过初始化对象来验证这些组件是否可用。虽然利用项目和数据集组织模型训练是有益的,但需要注意的是,这并不是强制性的。如果缺少其中任何一个 ID,对象数据 (project.data, dataset.data) 将为空。

from hub_sdk import HUBClient

credentials = {"api_key": "<YOUR-API-KEY>"}
client = HUBClient(credentials)

project = client.project("<Project ID>")
dataset = client.dataset("<Dataset ID>")

创建具有自定义配置的新模型

利用这一便捷的功能,创建符合项目要求的新模型变得非常简单。指定模型名称并将其与项目和数据集关联。您还可以根据需要自定义配置,如设置批量大小或设备等。请注意 projectIddatasetId 参数是可选的,如果您还没有准备好将模型与项目或数据集绑定。

from hub_sdk import HUBClient

credentials = {"api_key": "<YOUR-API-KEY>"}
client = HUBClient(credentials)

data = {
    "meta": {"name": "sdk model"},  # Give your model a recognizable name
    "projectId": project.id,        # Associate with an existing project
    "datasetId": dataset.id,        # Associate with an existing dataset
    "config": {                     # Define hyperparameters and settings
        "batchSize": "-1",          
        "cache": "ram",
        "device": "name",
        "epochs": "5",
        "imageSize": "640",
        "patience": "5"             # Stop training if validation doesn't improve
    }
}
model = client.model()
model.create_model(data)  # Creates the model with your specified details

更新现有模型的元数据或配置

在项目开发过程中,您可能需要更新模型的元数据,例如重新命名以提高清晰度。SDK 提供了毫不费力地刷新这些细节的方法,最大限度地减少了手动错误,为您节省了宝贵的时间。

from hub_sdk import HUBClient

credentials = {"api_key": "<YOUR-API-KEY>"}
client = HUBClient(credentials)

model = client.model("<Model ID>")
model.update({"meta": {"name": "Updated Model Name"}})  # Renames the specified model

安全删除模型

删除模型是不可逆的,因此应谨慎使用此功能。如果确定要从系统中删除模型,下面的命令将永久删除指定模型及其所有相关数据。

from hub_sdk import HUBClient

credentials = {"api_key": "<YOUR-API-KEY>"}
client = HUBClient(credentials)

model = client.model("<Model ID>")
model.delete(hard=True)  # Permanently deletes the specified model
默认情况下,delete 方法执行软删除,将模型标记为非活动,但不会永久删除。如果想执行硬删除,永久删除模型及其相关数据,可以通过参数 hard=True 如上例所示。使用 "硬删除 "选项时要小心,因为它是不可逆的,会导致指定型号从系统中完全删除。

通过分页列出所有模型

Ultralytics HUB-SDK 简化了获取模型列表的任务,同时还实现了分页功能,以有效浏览潜在的大量模型集合。通过自定义参数,如 page_size此外,您还可以根据自己的需要调整输出结果,包括查看私人和公共项目。

from hub_sdk import HUBClient

credentials = {"api_key": "<YOUR-API-KEY>"}
client = HUBClient(credentials)

model_list = client.model_list(page_size=10)  # Fetches the first page with 10 models
print("Current result:", model_list.results)  # Displays the current page's models

model_list.next()  # Move to the next page of results
print("Next page result:", model_list.results)  # Displays the next page's models

model_list.previous()  # Return to the previous page of results
print("Previous page result:", model_list.results)  # Displays the previous page's models

上传和可视化培训指标

要在整个训练过程中跟踪和可视化模型的性能指标,可使用此功能上传损失和准确性等指标。这样就能持续监控训练进度,简化分析阶段。

from hub_sdk import HUBClient

credentials = {"api_key": "<YOUR-API-KEY>"}
client = HUBClient(credentials)

model = client.model("<Model ID>")

# Define your metrics structure. Keys are steps, and values are JSON strings of metrics.
data = {
    1: '{"loss/1": 0.5, "accuracy/1": 0.85}',
    2: '{"loss/2": 0.4, "accuracy/2": 0.88}',
    3: '{"loss/3": 0.3, "accuracy/3": 0.90}',
}

model.upload_metrics(data)  # Uploads the specified metrics to the model

导出模型以进行部署或分析

为部署或深入分析等各种目的导出模型从未如此简单。指定您需要的格式,该功能就会相应地准备模型。无论您需要Tensorflow 还是PyTorch 格式,SDK 都能无缝处理。

from hub_sdk import HUBClient

credentials = {"api_key": "<YOUR-API-KEY>"}
client = HUBClient(credentials)

model = client.model("<Model ID>")
model.export(format="pyTorch")  # Exports the model as a PyTorch file

检索直接权重 URL

有时,您可能需要直接访问模型的远程存储工件。这个方便的功能提供了一个 URL,用于访问特定文件,如表现最好的模型权重。

from hub_sdk import HUBClient

credentials = {"api_key": "<YOUR-API-KEY>"}
client = HUBClient(credentials)

model = client.model("<Model ID>")
weight_url = model.get_weights_url("best")  # Retrieves the URL for the model's optimal checkpoint weights. By default, it returns the URL for the best weights. To obtain the most recent weights, specify 'last.
print("Weight URL link:", weight_url)  # Prints out the weight url link

上传模型检查点

使用 upload_model 功能。只需用 is_best 标志和训练纪元,以提高清晰度。

from hub_sdk import HUBClient

credentials = {"api_key": "<YOUR-API-KEY>"}
client = HUBClient(credentials)

model = client.model("<Model ID>")
model.upload_model(is_best=True, epoch=5, weights="<Weight File>")  # Uploads the specified model checkpoint

总之,Ultralytics HUB-SDK 为有效的模型管理提供了一套全面的操作,使您能够专注于在机器学习方面取得最佳成果。如果您有任何进一步的问题或需要帮助,请联系我们热情的社区或支持团队。建模愉快🚀


评论