{ "cells": [ { "cell_type": "markdown", "id": "aa1e877a", "metadata": {}, "source": [ "# Ground Truth getting started\n", "\n", "- reference : https://aws.amazon.com/getting-started/hands-on/build-training-datasets-amazon-sagemaker-ground-truth/\n", "\n", "## 1. 데이터셋 준비" ] }, { "cell_type": "code", "execution_count": 1, "id": "ccb10e2e", "metadata": {}, "outputs": [], "source": [ "import sagemaker\n", "sess = sagemaker.session.Session()\n", "\n", "# Prepare data and upload to your S3 bucket\n", "BUCKET = sess.default_bucket()\n", "EXP_NAME = 'ground-truth-demo' # Any valid S3 prefix." ] }, { "cell_type": "markdown", "id": "076dc051", "metadata": {}, "source": [ "다음 코드는 s3로부터 테스트용 이미지를 복사하여 SageMaker 디폴트 버킷에 저장합니다. (실행에 수 분 정도 소요됩니다.)" ] }, { "cell_type": "code", "execution_count": 2, "id": "2bbaf560", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--2021-07-17 13:21:27-- https://storage.googleapis.com/openimages/2018_04/test/test-annotations-human-imagelabels-boxable.csv\n", "Resolving storage.googleapis.com (storage.googleapis.com)... 172.217.9.208, 172.217.12.240, 172.217.13.240, ...\n", "Connecting to storage.googleapis.com (storage.googleapis.com)|172.217.9.208|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 26460276 (25M) [text/csv]\n", "Saving to: ‘openimgs-annotations.csv’\n", "\n", "openimgs-annotation 100%[===================>] 25.23M 89.9MB/s in 0.3s \n", "\n", "2021-07-17 13:21:27 (89.9 MB/s) - ‘openimgs-annotations.csv’ saved [26460276/26460276]\n", "\n" ] } ], "source": [ "import itertools\n", "import boto3\n", "\n", "# Download and process the Open Images annotations\n", "!wget https://storage.googleapis.com/openimages/2018_04/test/test-annotations-human-imagelabels-boxable.csv -O openimgs-annotations.csv\n", "with open('openimgs-annotations.csv', 'r') as f:\n", " all_labels = [line.strip().split(',') for line in f.readlines()]\n", " \n", " # Extract image ids in each of our desired classes\n", "ims = {}\n", "ims['Truck'] = [label[0] for label in all_labels if (label[2] == '/m/07r04' and label[3] == '1')][:300]\n", "ims['Limousine'] = [label[0] for label in all_labels if (label[2] == '/m/01lcw4' and label[3] == '1')][:300]\n", "ims['Van'] = [label[0] for label in all_labels if (label[2] == '/m/0h2r6' and label[3] == '1')][:300]\n", "ims['Car'] = [label[0] for label in all_labels if (label[2] == '/m/0pg52' and label[3] == '1')][:300]\n", "ims['Motorcycle'] = [label[0] for label in all_labels if (label[2] == '/m/04_sv' and label[3] == '1')][:300]\n", "num_classes = len(ims)\n", "\n", "for key in ims.keys():\n", " ims[key] = set(ims[key])\n", "\n", "# Create a new bucket for images to be labeled\n", "s3 = boto3.client('s3')\n", "sess = boto3.session.Session()\n", "region = sess.region_name\n", "if (region == 'us-east-1'):\n", " s3.create_bucket(Bucket=BUCKET)\n", "else:\n", " s3.create_bucket(Bucket=BUCKET, \n", " CreateBucketConfiguration={'LocationConstraint': region})\n", "\n", "# Copy the images to your local bucket\n", "for img_id, img in enumerate(itertools.chain.from_iterable(ims.values())):\n", " copy_source = {\n", " 'Bucket': 'open-images-dataset',\n", " 'Key': 'test/{}.jpg'.format(img)\n", " }\n", " s3.copy(copy_source, BUCKET, '{}/images/{}.jpg'.format(EXP_NAME, img))" ] }, { "cell_type": "markdown", "id": "0c346965", "metadata": {}, "source": [ "S3에 저장된 이미지는 다음과 같습니다. (1014장의 이미지가 복사되었습니다.)" ] }, { "cell_type": "code", "execution_count": 11, "id": "2f21c7db", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2021-07-17 13:22:25 581486 000aa0b1c8fd5ddf.jpg\n", "2021-07-17 13:21:55 132505 000b14e4ee4a2b2b.jpg\n", "2021-07-17 13:23:38 166471 0013a0927e6bbefc.jpg\n", "2021-07-17 13:22:30 195847 004b17294aff80dc.jpg\n", "2021-07-17 13:21:31 305583 008168cfd6dcf2aa.jpg\n", "\n", "[Errno 32] Broken pipe\n", "Exception ignored in: <_io.TextIOWrapper name='' mode='w' encoding='UTF-8'>\n", "BrokenPipeError: [Errno 32] Broken pipe\n" ] } ], "source": [ "!aws s3 ls s3://{BUCKET}/{EXP_NAME}/images/ |head -5" ] }, { "cell_type": "code", "execution_count": 12, "id": "3faca4f7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1014 4056 52728\n" ] } ], "source": [ "!aws s3 ls s3://{BUCKET}/{EXP_NAME}/images/ | wc " ] }, { "cell_type": "markdown", "id": "c38fe50e", "metadata": {}, "source": [ "## 2. Private 팀 생성\n", "\n", "### 콘솔에서 팀 생성 및 초대\n", "Sagemaker 콘솔로 이동하여 private team을 만듭니다. `Groundt Truth`메뉴에서 `Private`탭을 클릭한 후 `Create private team`버튼을 클릭합니다. \n", "\n", "\n", "\n", "아래 그림을 참고하여 이메일로 팀원을 초대하고 팀을 구성합니다. \n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "88b74473", "metadata": {}, "source": [ "### 이메일을 확인하고 패스워드를 설정합니다.\n", "\n", "초대메일로 one-time 패스워드가 발송되었을 것입니다. https:// 로 시작하는 링크를 클릭한 후 전달받은 패스워드로 접속하고 새로운 패스워드를 입력합니다.\n", "\n", "![](images/signin.png)\n", "\n" ] }, { "cell_type": "markdown", "id": "7c163c7c", "metadata": {}, "source": [ "## 3. 레이블 작업 생성\n", "\n", "아래 링크의 Step3부터 가이드를 참조하여 레이블작업을 설정하고 실행해봅니다. \n", "\n", " NOTE : Step3의 g.단계에서 Mechanical Turk 가 아닌 private team을 선택합니다. 나머지 설정은 동일합니다. \n", "\n", "https://aws.amazon.com/getting-started/hands-on/build-training-datasets-amazon-sagemaker-ground-truth/\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "f4bc2e4e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "conda_tensorflow_p36", "language": "python", "name": "conda_tensorflow_p36" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.13" } }, "nbformat": 4, "nbformat_minor": 5 }