您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:使用“自定义视觉”网站生成对象检测器Quickstart: Build an object detector with the Custom Vision website

本快速入门介绍如何使用自定义视觉网站创建对象检测器模型。In this quickstart, you'll learn how to use the Custom Vision website to create an object detector model. 生成模型后,可以使用新图像测试该模型,并最终将该模型集成到你自己的图像识别应用中。Once you build a model, you can test it with new images and eventually integrate it into your own image recognition app.

如果没有 Azure 订阅,请在开始之前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

先决条件Prerequisites

  • 一组用于训练检测器模型的图像。A set of images with which to train your detector model. 可以使用 GitHub 上的一组示例图像You can use the set of sample images on GitHub. 或者,可以根据下面的提示选择你自己的图像。Or, you can choose your own images using the tips below.

创建自定义视觉资源Create Custom Vision resources

若要使用自定义视觉服务,需要在 Azure 中创建“自定义视觉训练和预测”资源。To use the Custom Vision Service you will need to create Custom Vision Training and Prediction resources in Azure. 为此,在 Azure 门户中填写创建自定义视觉页上的对话框窗口,以创建“训练和预测”资源。To do so in the Azure portal, fill out the dialog window on the Create Custom Vision page to create both a Training and Prediction resource.

创建新项目Create a new project

在 Web 浏览器中,导航到自定义影像服务网页,然后选择“登录” 。In your web browser, navigate to the Custom Vision web page and select Sign in. 使用用于登录 Azure 门户的帐户登录。Sign in with the same account you used to sign into the Azure portal.

“登录”页的图像

  1. 若要创建首个项目,请选择“新建项目” 。To create your first project, select New Project. 将出现“创建新项目”对话框 。The Create new project dialog box will appear.

    “新建项目”对话框中包含名称、描述和域字段。

  2. 输入项目名称和描述。Enter a name and a description for the project. 然后选择一个资源组。Then select a Resource Group. 如果登录帐户与 Azure 帐户相关联,则“资源组”下拉列表将显示包含自定义影像服务资源的所有 Azure 资源组。If your signed-in account is associated with an Azure account, the Resource Group dropdown will display all of your Azure Resource Groups that include a Custom Vision Service Resource.

    备注

    如果没有可用的资源组,请确认已使用登录 Azure 门户时所用的同一帐户登录 customvision.aiIf no resource group is available, please confirm that you have logged into customvision.ai with the same account as you used to log into the Azure portal. 此外,请确认在自定义视觉网站中选择的“目录”与自定义视觉资源所在 Azure 门户中的目录相同。Also, please confirm you have selected the same "Directory" in the Custom Vision website as the directory in the Azure portal where your Custom Vision resources are located. 在这两个站点中,可从屏幕右上角的下拉帐户菜单中选择目录。In both sites, you may select your directory from the drop down account menu at the top right corner of the screen.

  3. 选择“项目类型”下的“对象检测”。 Select Object Detection under Project Types.

  4. 接下来,选择一个可用域。Next, select one of the available domains. 每个域都会针对特定类型的图像优化检测器,如下表所述。Each domain optimizes the detector for specific types of images, as described in the following table. 将能够根据需要稍后更改域。You will be able to change the domain later if you wish.

    Domain 目的Purpose
    常规General 已针对各种对象检测任务进行优化。Optimized for a broad range of object detection tasks. 如果其他域都不合适,或者不确定要选择哪个域,请选择“通用”域。If none of the other domains are appropriate, or you are unsure of which domain to choose, select the Generic domain.
    徽标Logo 已针对在图像中查找品牌徽标进行优化。Optimized for finding brand logos in images.
    货架上的产品Products on shelves 已针对检测和分类货架上的产品进行优化。Optimized for detecting and classifying products on shelves.
    压缩域Compact domains 已针对移动设备上实时对象检测的约束进行优化。Optimized for the constraints of real-time object detection on mobile devices. 可导出压缩域生成的模型在本地运行。The models generated by compact domains can be exported to run locally.
  5. 最后,选择“创建项目”。Finally, select Create project.

选择训练图像Choose training images

作为最低要求,我们建议在初始训练集中每个标记使用至少 30 张图像。As a minimum, we recommend you use at least 30 images per tag in the initial training set. 此外还需要收集一些额外的图像,以便在训练模型后测试模型。You'll also want to collect a few extra images to test your model once it's trained.

为了有效地训练模型,请使用具有视觉多样性的图像。In order to train your model effectively, use images with visual variety. 选择在以下方面有所不同的图像:Select images that vary by:

  • 照相机角度camera angle
  • 照明lighting
  • backgroundbackground
  • 视觉样式visual style
  • 个人/分组主题individual/grouped subject(s)
  • 大小size
  • typetype

此外,请确保所有训练图像满足以下条件:Additionally, make sure all of your training images meet the following criteria:

  • .jpg、.png、.bmp 或 .gif 格式.jpg, .png, .bmp, or .gif format
  • 大小不超过 6 MB (预测图像不超过 4 MB)no greater than 6MB in size (4MB for prediction images)
  • 最短的边不小于 256 像素;任何小于此像素的图像将通过自定义影像服务自动纵向扩展no less than 256 pixels on the shortest edge; any images shorter than this will be automatically scaled up by the Custom Vision Service

备注

是否需要一组范围更广的图像来完成训练?Do you need a broader set of images to complete your training? Trove 是一个 Microsoft Garage 项目,可用于收集和购买图像集以便进行训练。Trove, a Microsoft Garage project, allows you to collect and purchase sets of images for training purposes. 收集图像后,可以通过一般方式下载映像,然后将其导入到自定义视觉项目。Once you've collected your images, you can download them and then import them into your Custom Vision project in the usual way. 若要了解详细信息,请访问 Trove 页面Visit the Trove page to learn more.

上传和标记图像Upload and tag images

在本部分,你将上传图像并手动标记图像来帮助训练检测器。In this section, you will upload and manually tag images to help train the detector.

  1. 若要添加图像,请选择“添加图像”,然后选择“浏览本地文件” 。To add images, select Add images and then select Browse local files. 选择“打开”以上传图像。Select Open to upload the images.

    添加图像控件位于左上角,以按钮形式显示在底部中央。

  2. UI 的“未标记”部分会显示已上传的图像。You'll see your uploaded images in the Untagged section of the UI. 下一步是手动标记你希望检测器通过学习识别的对象。The next step is to manually tag the objects that you want the detector to learn to recognize. 单击第一个图像打开“标记”对话框窗口。Click the first image to open the tagging dialog window.

    “未标记”部分中已上传的图像

  3. 单击并拖动图像中对象周围的矩形。Click and drag a rectangle around the object in your image. 然后,使用 + 按钮输入新的标记名称,或者从下拉列表中选择现有的标记。Then, enter a new tag name with the + button, or select an existing tag from the drop-down list. 请务必标记要检测的对象的每个实例,因为检测器在训练中使用未标记的背景区域作为负面示例。It's important to tag every instance of the object(s) you want to detect, because the detector uses the untagged background area as a negative example in training. 完成标记后,单击右侧的箭头保存标记并转到下一个图像。When you're done tagging, click the arrow on the right to save your tags and move on to the next image.

    使用矩形选择控件来标记对象

若要上传另一组图像,请返回到本部分顶部并重复上述步骤。To upload another set of images, return to the top of this section and repeat the steps.

训练检测器Train the detector

若要训练检测器模式,请选择“训练”按钮。To train the detector model, select the Train button. 检测器将使用所有当前图像及其标记来创建用于识别每个标记对象的模型。The detector uses all of the current images and their tags to create a model that identifies each tagged object.

网页标头工具栏右上角的训练按钮

此训练过程应该只需要几分钟的时间。The training process should only take a few minutes. 在此期间,会在“性能”选项卡显示有关训练过程的信息。During this time, information about the training process is displayed in the Performance tab.

主要部分中带有训练对话的浏览器窗口

评估检测器Evaluate the detector

完成训练后,将计算并显示模型的性能。After training has completed, the model's performance is calculated and displayed. 自定义视觉服务使用提交用于训练的图像来计算精度、召回率和平均精度。The Custom Vision service uses the images that you submitted for training to calculate precision, recall, and mean average precision. 精度和召回率是检测器有效性的两个不同度量值:Precision and recall are two different measurements of the effectiveness of a detector:

  • 精确度表示已识别的正确分类的分数。Precision indicates the fraction of identified classifications that were correct. 例如,如果模型将 100 张图像识别为狗,实际上其中 99 张是狗,那么精确度为 99%。For example, if the model identified 100 images as dogs, and 99 of them were actually of dogs, then the precision would be 99%.
  • 召回率表示正确识别的实际分类的分数。Recall indicates the fraction of actual classifications that were correctly identified. 例如,如果实际上有 100 张苹果的图像,并且该模型将 80 张标识为苹果,则召回率为 80%。For example, if there were actually 100 images of apples, and the model identified 80 as apples, the recall would be 80%.
  • 平均精度是平均精准率 (AP) 的平均值。Mean average precision is the average value of the average precision (AP). AP 是精准率/召回率曲线下的区域(精准率根据每次进行的预测的召回率绘制)。AP is the area under the precision/recall curve (precision plotted against recall for each prediction made).

训练结果将显示总体精度和召回率,以及平均精度。

概率阈值Probability threshold

请注意“性能”选项卡左窗格上的“概率阈值”滑块 。这是预测被视为正确时所需具有的置信度(用于计算精度和召回率)。Note the Probability Threshold slider on the left pane of the Performance tab. This is the level of confidence that a prediction needs to have in order to be considered correct (for the purposes of calculating precision and recall).

当使用较高的概率阈值来解释预测调用时,它们往往会以高精度返回结果,但以召回率为代价 — 检测到的分类是正确的,但许多仍未被检测到。When you interpret prediction calls with a high probability threshold, they tend to return results with high precision at the expense of recall—the detected classifications are correct, but many remain undetected. 使用较低的概率阈值则恰恰相反 — 实际分类会被检测到,但该集合内有更多误报。A low probability threshold does the opposite—most of the actual classifications are detected, but there are more false positives within that set. 考虑到这一点,应该根据项目的特定需求设置概率阈值。With this in mind, you should set the probability threshold according to the specific needs of your project. 稍后,在客户端接收预测结果时,应使用与此处所用概率阈值相同的概率阈值。Later, when you're receiving prediction results on the client side, you should use the same probability threshold value as you used here.

重叠阈值Overlap threshold

“重叠阈值”滑块处理在训练中必须将对象预测视为“正确”的方式。The Overlap Threshold slider deals with how correct an object prediction must be to be considered "correct" in training. 此滑块设置预测的对象边界框和实际用户输入的边界框之间允许的最小重叠。It sets the minimum allowed overlap between the predicted object bounding box and the actual user-entered bounding box. 如果边界框未达到这种重叠度,则不会将预测视为正确。If the bounding boxes don't overlap to this degree, the prediction won't be considered correct.

管理训练迭代Manage training iterations

每次训练检测器时,都会创建一个新的迭代,其中包含其自身的已更新性能指标。Each time you train your detector, you create a new iteration with its own updated performance metrics. 可以在“性能”选项卡的左窗格中查看所有迭代。在左侧窗格中,还可以找到“删除”按钮,如果迭代已过时,可以使用该按钮删除迭代。You can view all of your iterations in the left pane of the Performance tab. In the left pane you'll also find the Delete button, which you can use to delete an iteration if it's obsolete. 删除迭代时,会删除唯一与其关联的所有图像。When you delete an iteration, you delete any images that are uniquely associated with it.

请参阅将模型与预测 API 配合使用,以了解如何以编程方式访问已训练模型。See Use your model with the prediction API to learn how to access your trained models programmatically.

后续步骤Next steps

本快速入门已介绍如何使用自定义视觉网站创建和训练对象检测器模型。In this quickstart, you learned how to create and train an object detector model using the Custom Vision website. 接下来,获取有关改进模型的迭代过程的详细信息。Next, get more information on the iterative process of improving your model.