將影像新增至資料集 - Amazon Lookout for Vision

支援終止通知:在 2025 年 10 月 31 日, AWS 將停止對 Amazon Lookout for Vision 的支援。2025 年 10 月 31 日後,您將無法再存取 Lookout for Vision 主控台或 Lookout for Vision 資源。如需詳細資訊,請造訪此部落格文章

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

將影像新增至資料集

建立資料集之後,您可能會想要在資料集中新增更多影像。例如,如果模型評估指出模型不良,您可以透過新增更多影像來增強模型的品質。如果您已建立測試資料集,則新增更多影像可以提高模型效能指標的準確性。

更新資料集後重新訓練模型。

添加更多圖像

您可以從本機電腦上傳影像,將更多影像新增至資料集。若要使用 SDK 新增更多已標記的影像,請使用此UpdateDatasetEntries作業。

若要將更多影像新增至資料集 (主控台)
  1. 選擇 [動作],然後選取要新增影像的資料集。

  2. 選擇您要上傳至資料集的影像。您可以拖動圖像或選擇要從本地計算機上傳的圖像。您一次最多可以上傳 30 張圖片。

  3. 選擇 [上傳圖片]。

  4. 選擇 Save Changes (儲存變更)。

完成新增更多影像後,您需要為其加上標籤,以便使用它們來訓練模型。如需詳細資訊,請參閱分類影像 (主控台)

新增更多影像 (SDK)

若要使用 SDK 新增更多已標記的影像,請使用此UpdateDatasetEntries作業。您提供一個資訊清單檔案,其中包含您要新增的映像檔。您也可以透過在資訊清單檔案中 JSON 行的source-ref欄位中指定影像來更新現有的映像檔。如需詳細資訊,請參閱建立清單檔案

若要將更多影像新增至資料集 (SDK)
  1. 如果您尚未這樣做,請安裝並設定AWS CLI和 AWS SDK。如需詳細資訊,請參閱步驟 4:設定 AWS CLI 以及 AWS SDKs

  2. 使用下列範例程式碼,將更多影像新增至資料集。

    CLI

    變更下列值:

    • project-name到包含您要更新的數據集的項目的名稱。

    • dataset-type到您要更新(traintest)的數據集類型。

    • changes到包含數據集更新的清單文件的位置。

    aws lookoutvision update-dataset-entries\ --project-name project\ --dataset-type train or test\ --changes fileb://manifest file \ --profile lookoutvision-access
    Python

    此代碼取自AWS文檔 SDK 示例 GitHub 存儲庫。請參閱此處的完整範例。

    @staticmethod def update_dataset_entries(lookoutvision_client, project_name, dataset_type, updates_file): """ Adds dataset entries to an Amazon Lookout for Vision dataset. :param lookoutvision_client: The Amazon Rekognition Custom Labels Boto3 client. :param project_name: The project that contains the dataset that you want to update. :param dataset_type: The type of the dataset that you want to update (train or test). :param updates_file: The manifest file of JSON Lines that contains the updates. """ try: status = "" status_message = "" manifest_file = "" # Update dataset entries logger.info(f"""Updating {dataset_type} dataset for project {project_name} with entries from {updates_file}.""") with open(updates_file) as f: manifest_file = f.read() lookoutvision_client.update_dataset_entries( ProjectName=project_name, DatasetType=dataset_type, Changes=manifest_file, ) finished = False while finished == False: dataset = lookoutvision_client.describe_dataset(ProjectName=project_name, DatasetType=dataset_type) status = dataset['DatasetDescription']['Status'] status_message = dataset['DatasetDescription']['StatusMessage'] if status == "UPDATE_IN_PROGRESS": logger.info( (f"Updating {dataset_type} dataset for project {project_name}.")) time.sleep(5) continue if status == "UPDATE_FAILED_ROLLBACK_IN_PROGRESS": logger.info( (f"Update failed, rolling back {dataset_type} dataset for project {project_name}.")) time.sleep(5) continue if status == "UPDATE_COMPLETE": logger.info( f"Dataset updated: {status} : {status_message} : {dataset_type} dataset for project {project_name}.") finished = True continue if status == "UPDATE_FAILED_ROLLBACK_COMPLETE": logger.info( f"Rollback complated after update failure: {status} : {status_message} : {dataset_type} dataset for project {project_name}.") finished = True continue logger.exception( f"Failed. Unexpected state for dataset update: {status} : {status_message} : {dataset_type} dataset for project {project_name}.") raise Exception( f"Failed. Unexpected state for dataset update: {status} : {status_message} :{dataset_type} dataset for project {project_name}.") logger.info(f"Added entries to dataset.") return status, status_message except ClientError as err: logger.exception( f"Couldn't update dataset: {err.response['Error']['Message']}") raise
    Java V2

    此代碼取自AWS文檔 SDK 示例 GitHub 存儲庫。請參閱此處的完整範例。

    /** * Updates an Amazon Lookout for Vision dataset from a manifest file. * Returns after Lookout for Vision updates the dataset. * * @param lfvClient An Amazon Lookout for Vision client. * @param projectName The name of the project in which you want to update a * dataset. * @param datasetType The type of the dataset that you want to update (train or * test). * @param manifestFile The name and location of a local manifest file that you want to * use to update the dataset. * @return DatasetStatus The status of the updated dataset. */ public static DatasetStatus updateDatasetEntries(LookoutVisionClient lfvClient, String projectName, String datasetType, String updateFile) throws FileNotFoundException, LookoutVisionException, InterruptedException { logger.log(Level.INFO, "Updating {0} dataset for project {1}", new Object[] { datasetType, projectName }); InputStream sourceStream = new FileInputStream(updateFile); SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream); UpdateDatasetEntriesRequest updateDatasetEntriesRequest = UpdateDatasetEntriesRequest.builder() .projectName(projectName) .datasetType(datasetType) .changes(sourceBytes) .build(); lfvClient.updateDatasetEntries(updateDatasetEntriesRequest); boolean finished = false; DatasetStatus status = null; // Wait until update completes. do { DescribeDatasetRequest describeDatasetRequest = DescribeDatasetRequest.builder() .projectName(projectName) .datasetType(datasetType) .build(); DescribeDatasetResponse describeDatasetResponse = lfvClient .describeDataset(describeDatasetRequest); DatasetDescription datasetDescription = describeDatasetResponse.datasetDescription(); status = datasetDescription.status(); switch (status) { case UPDATE_COMPLETE: logger.log(Level.INFO, "{0} Dataset updated for project {1}.", new Object[] { datasetType, projectName }); finished = true; break; case UPDATE_IN_PROGRESS: logger.log(Level.INFO, "{0} Dataset update for project {1} in progress.", new Object[] { datasetType, projectName }); TimeUnit.SECONDS.sleep(5); break; case UPDATE_FAILED_ROLLBACK_IN_PROGRESS: logger.log(Level.SEVERE, "{0} Dataset update failed for project {1}. Rolling back", new Object[] { datasetType, projectName }); TimeUnit.SECONDS.sleep(5); break; case UPDATE_FAILED_ROLLBACK_COMPLETE: logger.log(Level.SEVERE, "{0} Dataset update failed for project {1}. Rollback completed.", new Object[] { datasetType, projectName }); finished = true; break; default: logger.log(Level.SEVERE, "{0} Dataset update failed for project {1}. Unexpected error returned.", new Object[] { datasetType, projectName }); finished = true; } } while (!finished); return status; }
  3. 重複上一個步驟,並提供其他資料集類型的值。