Mengubah file manifes SageMaker AI Ground Truth multi-label - Rekognition

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Mengubah file manifes SageMaker AI Ground Truth multi-label

Topik ini menunjukkan kepada Anda cara mengubah file manifes Amazon SageMaker AI Ground Truth multi-label menjadi file manifes format Label Kustom Rekognition Amazon.

SageMaker File manifes AI Ground Truth untuk pekerjaan multi-label diformat secara berbeda dari file manifes format Amazon Rekognition Custom Labels. Klasifikasi multi-label adalah ketika gambar diklasifikasikan ke dalam satu set kelas, tetapi mungkin milik beberapa kelas sekaligus. Dalam hal ini, gambar berpotensi memiliki beberapa label (multi-label), seperti sepak bola dan bola.

Untuk informasi tentang pekerjaan SageMaker AI Ground Truth multi-label, lihat Klasifikasi Gambar (Multi-label). Untuk informasi tentang file manifes Label Kustom Amazon Rekognition format multi-label, lihat. Menambahkan beberapa label tingkat gambar ke gambar

Mendapatkan file manifes untuk pekerjaan SageMaker AI Ground Truth

Prosedur berikut menunjukkan cara mendapatkan file manifes keluaran (output.manifest) untuk pekerjaan Amazon SageMaker AI Ground Truth. Anda menggunakan output.manifest sebagai masukan untuk prosedur berikutnya.

Untuk mengunduh file manifes pekerjaan SageMaker AI Ground Truth
  1. Buka https://console.aws.amazon.com/sagemaker/.

  2. Di panel navigasi, pilih Ground Truth lalu pilih Labeling Jobs.

  3. Pilih pekerjaan pelabelan yang berisi file manifes yang ingin Anda gunakan.

  4. Pada halaman detail, pilih tautan di bawah Lokasi set data keluaran. Konsol Amazon S3 dibuka di lokasi dataset.

  5. PilihManifests, output dan kemudianoutput.manifest.

  6. Pilih Tindakan Objek dan kemudian pilih Unduh untuk mengunduh file manifes.

Mengubah file manifes SageMaker AI multi-label

Prosedur berikut membuat file manifes Amazon Rekognition Custom Labels format multi-label dari file manifes AI format SageMaker GroundTruth multi-label yang ada.

catatan

Untuk menjalankan kode, Anda memerlukan Python versi 3, atau lebih tinggi.

Untuk mengubah file manifes SageMaker AI multi-label
  1. Jalankan kode python berikut. Berikan nama file manifes yang Anda buat Mendapatkan file manifes untuk pekerjaan SageMaker AI Ground Truth sebagai argumen baris perintah.

    # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Purpose Shows how to create and Amazon Rekognition Custom Labels format manifest file from an Amazon SageMaker Ground Truth Image Classification (Multi-label) format manifest file. """ import json import logging import argparse import os.path logger = logging.getLogger(__name__) def create_manifest_file(ground_truth_manifest_file): """ Creates an Amazon Rekognition Custom Labels format manifest file from an Amazon SageMaker Ground Truth Image Classification (Multi-label) format manifest file. :param: ground_truth_manifest_file: The name of the Ground Truth manifest file, including the relative path. :return: The name of the new Custom Labels manifest file. """ logger.info('Creating manifest file from %s', ground_truth_manifest_file) new_manifest_file = f'custom_labels_{os.path.basename(ground_truth_manifest_file)}' # Read the SageMaker Ground Truth manifest file into memory. with open(ground_truth_manifest_file) as gt_file: lines = gt_file.readlines() #Iterate through the lines one at a time to generate the #new lines for the Custom Labels manifest file. with open(new_manifest_file, 'w') as the_new_file: for line in lines: #job_name - The of the Amazon Sagemaker Ground Truth job. job_name = '' # Load in the old json item from the Ground Truth manifest file old_json = json.loads(line) # Get the job name keys = old_json.keys() for key in keys: if 'source-ref' not in key and '-metadata' not in key: job_name = key new_json = {} # Set the location of the image new_json['source-ref'] = old_json['source-ref'] # Temporarily store the list of labels labels = old_json[job_name] # Iterate through the labels and reformat to Custom Labels format for index, label in enumerate(labels): new_json[f'{job_name}{index}'] = index metadata = {} metadata['class-name'] = old_json[f'{job_name}-metadata']['class-map'][str(label)] metadata['confidence'] = old_json[f'{job_name}-metadata']['confidence-map'][str(label)] metadata['type'] = 'groundtruth/image-classification' metadata['job-name'] = old_json[f'{job_name}-metadata']['job-name'] metadata['human-annotated'] = old_json[f'{job_name}-metadata']['human-annotated'] metadata['creation-date'] = old_json[f'{job_name}-metadata']['creation-date'] # Add the metadata to new json line new_json[f'{job_name}{index}-metadata'] = metadata # Write the current line to the json file the_new_file.write(json.dumps(new_json)) the_new_file.write('\n') logger.info('Created %s', new_manifest_file) return new_manifest_file def add_arguments(parser): """ Adds command line arguments to the parser. :param parser: The command line parser. """ parser.add_argument( "manifest_file", help="The Amazon SageMaker Ground Truth manifest file" "that you want to use." ) def main(): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") try: # get command line arguments parser = argparse.ArgumentParser(usage=argparse.SUPPRESS) add_arguments(parser) args = parser.parse_args() # Create the manifest file manifest_file = create_manifest_file(args.manifest_file) print(f'Manifest file created: {manifest_file}') except FileNotFoundError as err: logger.exception('File not found: %s', err) print(f'File not found: {err}. Check your manifest file.') if __name__ == "__main__": main()
  2. Perhatikan nama file manifes baru yang ditampilkan skrip. Anda menggunakannya di langkah berikutnya.

  3. Unggah file manifes Anda ke bucket Amazon S3 yang ingin Anda gunakan untuk menyimpan file manifes.

    catatan

    Pastikan Label Kustom Amazon Rekognition memiliki akses ke bucket Amazon S3 yang direferensikan di bidang baris file manifessource-ref. JSON Untuk informasi selengkapnya, lihat Mengakses Bucket Amazon S3 eksternal. Jika lowongan Ground Truth menyimpan gambar di Bucket Konsol Label Kustom Amazon Rekognition, Anda tidak perlu menambahkan izin.

  4. Ikuti petunjuk di Membuat kumpulan data dengan file manifes SageMaker AI Ground Truth (Console) untuk membuat kumpulan data dengan file manifes yang diunggah. Untuk langkah 8, di lokasi file.manifest, masukkan Amazon URL S3 untuk lokasi file manifes. Jika Anda menggunakan AWS SDK, lakukanMembuat kumpulan data dengan file manifes SageMaker AI Ground Truth () SDK.