COCO データセットの変換 - Rekognition

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

COCO データセットの変換

次の Python の例を使用して、境界ボックス情報を、COCO 形式のデータセットから Amazon Rekognition Custom Labels マニフェストファイルに変換します。このコードは、作成したマニフェストファイルを Amazon S3 バケットにアップロードします。このコードには、イメージのアップロードに使用できる AWS CLI コマンドも用意されています。

COCO データセットを変換するには (SDK)
  1. まだ実行していない場合:

    1. AmazonS3FullAccess の権限があることを確認します。詳細については、「SDK アクセス許可の設定」を参照してください。

    2. と AWS SDKsをインストール AWS CLI して設定します。詳細については、「ステップ 4: AWS CLI と AWS SDKsを設定する」を参照してください。

  2. 次の Python コードを使用して COCO データセットを変換します。次の値を設定します。

    • s3_bucket - イメージと Amazon Rekognition Custom Labels マニフェストファイルを保存する S3 バケットの名前。

    • s3_key_path_images - S3 バケット (s3_bucket) 内のイメージを保存する場所へのパス。

    • s3_key_path_manifest_file - S3 バケット (s3_bucket) 内の Custom Labels マニフェストファイルを保存する場所へのパス。

    • local_path - サンプルが入力 COCO データセットを開き、新しいカスタムラベルマニフェストファイルを保存するローカルパス。

    • local_images_path - トレーニングに使用するイメージへのローカルパス。

    • coco_manifest - 入力 COCO データセットのファイル名。

    • cl_manifest_file - このサンプルで作成されたマニフェストファイルの名前。ファイルは local_path で指定された場所に保存されます。慣例により、ファイルには拡張子 .manifest が付いていますが、必須ではありません。

    • job_name - カスタムラベルジョブの名前。

    import json import os import random import shutil import datetime import botocore import boto3 import PIL.Image as Image import io #S3 location for images s3_bucket = 'bucket' s3_key_path_manifest_file = 'path to custom labels manifest file/' s3_key_path_images = 'path to images/' s3_path='s3://' + s3_bucket + '/' + s3_key_path_images s3 = boto3.resource('s3') #Local file information local_path='path to input COCO dataset and output Custom Labels manifest/' local_images_path='path to COCO images/' coco_manifest = 'COCO dataset JSON file name' coco_json_file = local_path + coco_manifest job_name='Custom Labels job name' cl_manifest_file = 'custom_labels.manifest' label_attribute ='bounding-box' open(local_path + cl_manifest_file, 'w').close() # class representing a Custom Label JSON line for an image class cl_json_line: def __init__(self,job, img): #Get image info. Annotations are dealt with seperately sizes=[] image_size={} image_size["width"] = img["width"] image_size["depth"] = 3 image_size["height"] = img["height"] sizes.append(image_size) bounding_box={} bounding_box["annotations"] = [] bounding_box["image_size"] = sizes self.__dict__["source-ref"] = s3_path + img['file_name'] self.__dict__[job] = bounding_box #get metadata metadata = {} metadata['job-name'] = job_name metadata['class-map'] = {} metadata['human-annotated']='yes' metadata['objects'] = [] date_time_obj = datetime.datetime.strptime(img['date_captured'], '%Y-%m-%d %H:%M:%S') metadata['creation-date']= date_time_obj.strftime('%Y-%m-%dT%H:%M:%S') metadata['type']='groundtruth/object-detection' self.__dict__[job + '-metadata'] = metadata print("Getting image, annotations, and categories from COCO file...") with open(coco_json_file) as f: #Get custom label compatible info js = json.load(f) images = js['images'] categories = js['categories'] annotations = js['annotations'] print('Images: ' + str(len(images))) print('annotations: ' + str(len(annotations))) print('categories: ' + str(len (categories))) print("Creating CL JSON lines...") images_dict = {image['id']: cl_json_line(label_attribute, image) for image in images} print('Parsing annotations...') for annotation in annotations: image=images_dict[annotation['image_id']] cl_annotation = {} cl_class_map={} # get bounding box information cl_bounding_box={} cl_bounding_box['left'] = annotation['bbox'][0] cl_bounding_box['top'] = annotation['bbox'][1] cl_bounding_box['width'] = annotation['bbox'][2] cl_bounding_box['height'] = annotation['bbox'][3] cl_bounding_box['class_id'] = annotation['category_id'] getattr(image, label_attribute)['annotations'].append(cl_bounding_box) for category in categories: if annotation['category_id'] == category['id']: getattr(image, label_attribute + '-metadata')['class-map'][category['id']]=category['name'] cl_object={} cl_object['confidence'] = int(1) #not currently used by Custom Labels getattr(image, label_attribute + '-metadata')['objects'].append(cl_object) print('Done parsing annotations') # Create manifest file. print('Writing Custom Labels manifest...') for im in images_dict.values(): with open(local_path+cl_manifest_file, 'a+') as outfile: json.dump(im.__dict__,outfile) outfile.write('\n') outfile.close() # Upload manifest file to S3 bucket. print ('Uploading Custom Labels manifest file to S3 bucket') print('Uploading' + local_path + cl_manifest_file + ' to ' + s3_key_path_manifest_file) print(s3_bucket) s3 = boto3.resource('s3') s3.Bucket(s3_bucket).upload_file(local_path + cl_manifest_file, s3_key_path_manifest_file + cl_manifest_file) # Print S3 URL to manifest file, print ('S3 URL Path to manifest file. ') print('\033[1m s3://' + s3_bucket + '/' + s3_key_path_manifest_file + cl_manifest_file + '\033[0m') # Display aws s3 sync command. print ('\nAWS CLI s3 sync command to upload your images to S3 bucket. ') print ('\033[1m aws s3 sync ' + local_images_path + ' ' + s3_path + '\033[0m')
  3. コードを実行します。

  4. プログラムの出力で、s3 sync コマンドを記録しておきます。それは次の手順で必要となります。

  5. コマンドラインプロンプトで、s3 sync コマンドを実行します。イメージが S3 バケットにアップロードされます。アップロード中にコマンドが失敗した場合は、ローカルイメージ S3 バケットと同期されるまでコマンドを再度実行してください。

  6. プログラムの出力で、マニフェストファイルへの S3 URL パスを記録しておきます。それは次の手順で必要となります。

  7. SageMaker Ground Truth マニフェストファイルを使用したデータセットの作成 (コンソール)」の手順に従って、アップロードしたマニフェストファイルを使用してデータセットを作成します。ステップ 8 では、[マニフェストファイルの場所] に、前のステップで記録しておいた Amazon S3 URL を入力します。 AWS SDK を使用している場合、 SageMaker Ground Truth マニフェストファイル (SDK) を使用したデータセットの作成 を実行してください。