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

快速入门:使用自定义视觉服务网站生成分类器Quickstart: Build a classifier with the Custom Vision website

在本快速入门中,你将了解如何使用自定义视觉服务网站创建图像分类模型。In this quickstart, you'll learn how to use the Custom Vision website to create an image classification 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 classifier. 有关选择图像的提示,请参阅下文。See below for tips on choosing images.

创建自定义视觉资源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 Classification under Project Types. 然后,在“分类类型”下,根据用例选择“多标签”或“多类”。Then, under Classification Types, choose either Multilabel or Multiclass, depending on your use case. 多标签分类将任意数量的标记应用于图像(零个或多个),而多类分类将图像分类为单个类别(提交的每个图像将被分类为最有可能的标记)。Multilabel classification applies any number of your tags to an image (zero or more), while multiclass classification sorts images into single categories (every image you submit will be sorted into the most likely tag). 以后可以更改分类类型(如果需要)。You'll be able to change the classification type later if you want to.

  4. 接下来,选择一个可用域。Next, select one of the available domains. 每个域都会针对特定类型的图像优化分类器,如下表所述。Each domain optimizes the classifier 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
    常规Generic 针对各种图像分类任务进行优化。Optimized for a broad range of image classification tasks. 如果其他域都不合适,或者不确定要选择哪个域,请选择“通用”域。If none of the other domains are appropriate, or you're unsure of which domain to choose, select the Generic domain.
    食物Food 针对餐厅菜肴的照片进行优化。Optimized for photographs of dishes as you would see them on a restaurant menu. 如果要对各种水果或蔬菜的照片进行分类,请使用“食品”域。If you want to classify photographs of individual fruits or vegetables, use the Food domain.
    特征点Landmarks 针对可识别的自然和人造地标进行优化。Optimized for recognizable landmarks, both natural and artificial. 在照片中的地标清晰可见的情况下,该域效果最佳。This domain works best when the landmark is clearly visible in the photograph. 即使照片中的人物稍微遮挡了地标,该域仍然有效。This domain works even if the landmark is slightly obstructed by people in front of it.
    零售Retail 针对购物目录或购物网站中的图像进行优化。Optimized for images that are found in a shopping catalog or shopping website. 若想对连衣裙、裤子和衬衫进行精准分类,请使用此域。If you want high precision classifying between dresses, pants, and shirts, use this domain.
    压缩域Compact domains 针对移动设备上实时分类的约束进行优化。Optimized for the constraints of real-time classification 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'll upload and manually tag images to help train the classifier.

  1. 若要添加图像,请选择“添加图像”,然后选择“浏览本地文件” 。To add images, select Add images and then select Browse local files. 选择“打开”以移至标记。Select Open to move to tagging. 标记选择将应用于已选择要上传的整组图像,因此根据其应用的标记将图像分成单独的组更容易上传。Your tag selection will be applied to the entire group of images you've selected to upload, so it's easier to upload images in separate groups according to their applied tags. 还可在上传图像后更改单个图像的标记。You can also change the tags for individual images after they've been uploaded.

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

  2. 若要创建标记,请在“我的标记”字段中输入文本,然后按 Enter 键。To create a tag, enter text in the My Tags field and press Enter. 如果标记已存在,它会在下拉列表菜单中显示。If the tag already exists, it will appear in a dropdown menu. 在多标签项目中,可以将多个标记添加到图像,但多类项目中只能添加一个标记。In a multilabel project, you can add more than one tag to your images, but in a multiclass project you can add only one. 若要完成上传图像,请使用“上传 [编号] 文件”按钮。To finish uploading the images, use the Upload [number] files button.

    “标记和上传”页面的图像

  3. 上传图像后,选择“完成”。Select Done once the images have been uploaded.

    进度条显示已完成的所有任务。

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

训练分类器Train the classifier

若要训练分类器,请选择“训练”按钮。To train the classifier, select the Train button. 分类器使用所有当前图像来创建模型,该模型可标识每个标记的视觉质量。The classifier uses all of the current images to create a model that identifies the visual qualities of each tag.

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

此训练过程应该只需要几分钟的时间。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 classifier

完成训练后,评估并显示该模型的性能。After training has completed, the model's performance is estimated and displayed. 自定义影像服务使用所提交的训练图像来计算精确度和召回率,采用的是名为 k 倍交叉验证的过程。The Custom Vision Service uses the images that you submitted for training to calculate precision and recall, using a process called k-fold cross validation. 精确度和召回率是分类器有效性的两个不同的度量:Precision and recall are two different measurements of the effectiveness of a classifier:

  • 精确度表示已识别的正确分类的分数。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%.

训练结果显示分类器中的整体精确度和召回率,以及每个标签的精确度和召回率。

概率阈值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.

管理训练迭代Manage training iterations

每次训练分类器时,都会创建一个新的迭代,其中包含自己更新的性能指标。Each time you train your classifier, 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. 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 image classification model using the Custom Vision website. 接下来,获取有关改进模型的迭代过程的详细信息。Next, get more information on the iterative process of improving your model.