快速入門:使用自訂視覺網站建置分類器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.

PrerequisitesPrerequisites

  • 一組用來定型分類的影像。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

在網頁瀏覽器中,瀏覽到 自訂視覺網頁,並選取 [登入] 。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
  • 背景資訊background
  • 視覺效果樣式visual style
  • 單一/群組對象individual/grouped subject(s)
  • {1}size{2}size
  • typetype

此外,請確定所有的訓練映像符合下列準則:Additionally, make sure all of your training images meet the following criteria:

  • .jpg、.png、.bmp 或 .gif 格式.jpg, .png, .bmp, or .gif format
  • 不大於 6MB (預測影像為 4MB)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

注意

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, click the Add images button 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 desired tags. 上傳個別影像之後,您也可以變更影像的標記。You can also change the tags for individual images after they have 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. 若要完成上傳影像,請使用 [上傳 [number] 個檔案] 按鈕。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.

若要了解如何以程式設計方式存取已定型的模型,請參閱使用您的模型搭配預測 APISee 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.