05_create_dataset/05_label_images.ipynb (376 lines of code) (raw):
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 72
},
"id": "0uUeDqA32K9o",
"outputId": "27b66765-ee49-4504-f32e-f34776c4f3b4"
},
"outputs": [
{
"data": {
"text/markdown": [
"\n",
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://console.cloud.google.com/ai-platform/notebooks/deploy-notebook?name=Labeling+images+efficiently&url=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fpractical-ml-vision-book%2Fblob%2Fmaster%2F05_create_dataset%2F05_label_images.ipynb&download_url=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fpractical-ml-vision-book%2Fraw%2Fmaster%2F05_create_dataset%2F05_label_images.ipynb\">\n",
" <img src=\"https://raw.githubusercontent.com/GoogleCloudPlatform/practical-ml-vision-book/master/logo-cloud.png\"/> Run in AI Platform Notebook</a>\n",
" </td>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/GoogleCloudPlatform/practical-ml-vision-book/blob/master/05_create_dataset/05_label_images.ipynb\">\n",
" <img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/GoogleCloudPlatform/practical-ml-vision-book/blob/master/05_create_dataset/05_label_images.ipynb\">\n",
" <img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://raw.githubusercontent.com/GoogleCloudPlatform/practical-ml-vision-book/master/05_create_dataset/05_label_images.ipynb\">\n",
" <img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
" </td>\n",
"</table>\n"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from IPython.display import Markdown as md\n",
"\n",
"### change to reflect your notebook\n",
"_nb_loc = \"05_create_dataset/05_label_images.ipynb\"\n",
"_nb_title = \"Labeling images efficiently\"\n",
"\n",
"### no need to change any of this\n",
"_nb_safeloc = _nb_loc.replace('/', '%2F')\n",
"_nb_safetitle = _nb_title.replace(' ', '+')\n",
"md(\"\"\"\n",
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://console.cloud.google.com/ai-platform/notebooks/deploy-notebook?name={1}&url=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fpractical-ml-vision-book%2Fblob%2Fmaster%2F{2}&download_url=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fpractical-ml-vision-book%2Fraw%2Fmaster%2F{2}\">\n",
" <img src=\"https://raw.githubusercontent.com/GoogleCloudPlatform/practical-ml-vision-book/master/logo-cloud.png\"/> Run in AI Platform Notebook</a>\n",
" </td>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/GoogleCloudPlatform/practical-ml-vision-book/blob/master/{0}\">\n",
" <img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/GoogleCloudPlatform/practical-ml-vision-book/blob/master/{0}\">\n",
" <img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://raw.githubusercontent.com/GoogleCloudPlatform/practical-ml-vision-book/master/{0}\">\n",
" <img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
" </td>\n",
"</table>\n",
"\"\"\".format(_nb_loc, _nb_safetitle, _nb_safeloc))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Gksy_Cqe2PND"
},
"source": [
"# Labeling images efficiently\n",
"\n",
"This notebook shows you how to efficiently label images for multiple tasks.\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install --quiet multi-label-pigeon"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "obrcfJe0LgQe",
"outputId": "5ca8eaf2-2599-46c4-e906-318e1542adb2"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Copying gs://practical-ml-vision-book-data/flowers_5_jpeg/flower_photos/daisy/100080576_f52e8ee070_n.jpg...\n",
"/ [1 files][ 26.2 KiB/ 26.2 KiB] \n",
"Operation completed over 1 objects/26.2 KiB. \n",
"Copying gs://practical-ml-vision-book-data/flowers_5_jpeg/flower_photos/daisy/10140303196_b88d3d6cec.jpg...\n",
"/ [1 files][114.5 KiB/114.5 KiB] \n",
"Operation completed over 1 objects/114.5 KiB. \n",
"Copying gs://practical-ml-vision-book-data/flowers_5_jpeg/flower_photos/daisy/10172379554_b296050f82_n.jpg...\n",
"/ [1 files][ 35.6 KiB/ 35.6 KiB] \n",
"Operation completed over 1 objects/35.6 KiB. \n"
]
}
],
"source": [
"%%bash\n",
"mkdir flower_images\n",
"for filename in 100080576_f52e8ee070_n.jpg 10140303196_b88d3d6cec.jpg 10172379554_b296050f82_n.jpg; do\n",
" gsutil cp gs://practical-ml-vision-book-data/flowers_5_jpeg/flower_photos/daisy/$filename flower_images\n",
"done"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['flower_images/10172379554_b296050f82_n.jpg', 'flower_images/100080576_f52e8ee070_n.jpg', 'flower_images/10140303196_b88d3d6cec.jpg']\n"
]
}
],
"source": [
"import glob\n",
"filenames = glob.glob('flower_images/*.jpg')\n",
"print(filenames)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "dcd8397d14e245559eb7b8768c252d44",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HTML(value='0 examples annotated, 4 examples left')"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"flower\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "eec4ba1fed4a4f60ba0ae22c55798625",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(Button(description='daisy', style=ButtonStyle()), Button(description='tulip', style=ButtonStyle…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"color\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7d78646ea2b84a3b942ae4f5613a1e52",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(Button(description='yellow', style=ButtonStyle()), Button(description='red', style=ButtonStyle(…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"location\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "14b23e6e1c384bfa82848fcdd5478f21",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(Button(description='indoors', style=ButtonStyle()), Button(description='outdoors', style=Button…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c1dcc2f7ba814cd38ad1f5cec531ff2b",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(Button(description='done', style=ButtonStyle()), Button(description='back', style=ButtonStyle()…"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e7f06ca5a78d461b84caf8dc4a7fc18d",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from multi_label_pigeon import multi_label_annotate\n",
"from IPython.display import display, Image\n",
"\n",
"annotations = multi_label_annotate(\n",
" filenames,\n",
" options={'flower':['daisy','tulip', 'rose'], 'color':['yellow','red', 'other'],'location':['indoors','outdoors']},\n",
" display_fn=lambda filename: display(Image(filename))\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"with open('label.json', 'w') as ofp:\n",
" json.dump(annotations, ofp, indent=2)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"flower_images/10172379554_b296050f82_n.jpg\": {\n",
" \"flower\": [\n",
" \"daisy\"\n",
" ],\n",
" \"color\": [\n",
" \"red\"\n",
" ],\n",
" \"location\": [\n",
" \"outdoors\"\n",
" ]\n",
" },\n",
" \"flower_images/100080576_f52e8ee070_n.jpg\": {\n",
" \"flower\": [\n",
" \"daisy\"\n"
]
}
],
"source": [
"!head -15 label.json"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "l_fNzWuY2UoB"
},
"source": [
"Copyright 2020 Google Inc. Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License."
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "04_audio.ipynb",
"provenance": [],
"toc_visible": true
},
"environment": {
"name": "tf2-gpu.2-1.m59",
"type": "gcloud",
"uri": "gcr.io/deeplearning-platform-release/tf2-gpu.2-1:m59"
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.7.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}