Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MOD | FIX] -> object_detection.ipynb #5

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 63 additions & 61 deletions object_detection.ipynb
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Copy of Copy of object-detection.ipynb",
"provenance": [],
"collapsed_sections": [],
"toc_visible": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
Expand All @@ -26,25 +11,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "GmloKE6Qx8Lu"
},
"outputs": [],
"source": [
"credentials = {\n",
" \"bucket\": \"BUCKET\",\n",
" \"access_key_id\": \"ACCESS_KEY_ID\",\n",
" \"secret_access_key\": \"SECRET_ACCESS_KEY\",\n",
" \"endpoint_url\": \"ENDPOINT_URL\"\n",
"}"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "hVPzEKoLuEHy"
},
"outputs": [],
"source": [
"NUM_TRAIN_STEPS = 500\n",
"MODEL_TYPE = 'ssd_mobilenet_v1_quantized_300x300_coco14_sync_2018_07_18'\n",
Expand All @@ -62,9 +49,7 @@
"LABEL_MAP_PATH = os.path.join(DATA_PATH, 'label_map.pbtxt')\n",
"TRAIN_RECORD_PATH = os.path.join(DATA_PATH, 'train.record')\n",
"VAL_RECORD_PATH = os.path.join(DATA_PATH, 'val.record')"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -89,9 +74,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "o33_jgwGm3NV"
},
"outputs": [],
"source": [
"%tensorflow_version 1.x\n",
"import os\n",
Expand All @@ -114,9 +101,7 @@
"\n",
"pwd = os.getcwd()\n",
"os.environ['PYTHONPATH'] += f':{pwd}:{pwd}/slim'"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -130,14 +115,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "iM8sOHwL64Rp"
},
"outputs": [],
"source": [
"!python object_detection/builders/model_builder_tf1_test.py"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -151,15 +136,15 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "jydTH9gYQ3y7"
},
"outputs": [],
"source": [
"import cloud_annotations as ca\n",
"ca.mount(CLOUD_ANNOTATIONS_MOUNT, credentials)"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -191,9 +176,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "nJsKCG3UdDsn"
},
"outputs": [],
"source": [
"import os\n",
"import json\n",
Expand All @@ -213,9 +200,7 @@
" f.write(\"\\tname: '{}'\\n\".format(label))\n",
" f.write('\\tid: {}\\n'.format(idx + 1)) # indexes must start at 1\n",
" f.write('}\\n')"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -247,17 +232,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "cAkOvP-gZR1x"
},
"outputs": [],
"source": [
"import os\n",
"import io\n",
"import json\n",
"import random\n",
"\n",
"import PIL.Image\n",
"import tensorflow as tf\n",
"import PIL.Image\n",
"from PIL import JpegImagePlugin\n",
"JpegImagePlugin._getmp = lambda x: None\n",
"\n",
"from object_detection.utils import dataset_util\n",
"from object_detection.utils import label_map_util\n",
Expand Down Expand Up @@ -328,8 +317,8 @@
" if tf_example:\n",
" # Write the TFExample to the TFRecord.\n",
" writer.write(tf_example.SerializeToString())\n",
" except ValueError:\n",
" print('Invalid example, ignoring.')\n",
" except ValueError as e:\n",
" print(e)\n",
" pass\n",
" except IOError:\n",
" print(\"Can't read example, ignoring.\")\n",
Expand All @@ -349,9 +338,7 @@
"\n",
" create_tf_record(train_examples, annotations, label_map, CLOUD_ANNOTATIONS_MOUNT, TRAIN_RECORD_PATH)\n",
" create_tf_record(val_examples, annotations, label_map, CLOUD_ANNOTATIONS_MOUNT, VAL_RECORD_PATH)"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -375,9 +362,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "oHD1Jm0v7jfz"
},
"outputs": [],
"source": [
"import os\n",
"import tarfile\n",
Expand All @@ -401,9 +390,7 @@
" tar.extract(member, path=CHECKPOINT_PATH)\n",
"\n",
" os.remove(tmp)"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -417,9 +404,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "C8CVExv6HsJS"
},
"outputs": [],
"source": [
"import re\n",
"\n",
Expand Down Expand Up @@ -447,9 +436,7 @@
"configs = config_util.merge_external_params_with_configs(configs, kwargs_dict=override_dict)\n",
"pipeline_config = config_util.create_pipeline_proto_from_configs(configs)\n",
"config_util.save_pipeline_config(pipeline_config, DATA_PATH)"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -467,19 +454,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Wv5h2bwBVO0V"
},
"outputs": [],
"source": [
"!rm -rf $OUTPUT_PATH\n",
"!python -m object_detection.model_main \\\n",
" --pipeline_config_path=$DATA_PATH/pipeline.config \\\n",
" --model_dir=$OUTPUT_PATH \\\n",
" --num_train_steps=$NUM_TRAIN_STEPS \\\n",
" --num_eval_steps=100"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -495,9 +482,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "BZgP_FZUoE0d"
},
"outputs": [],
"source": [
"import os\n",
"import re\n",
Expand All @@ -513,9 +502,7 @@
" --pipeline_config_path=$DATA_PATH/pipeline.config \\\n",
" --trained_checkpoint_prefix=$TRAINED_CHECKPOINT_PREFIX \\\n",
" --output_directory=$EXPORTED_PATH"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -531,9 +518,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "HWHvlnyjmCIN"
},
"outputs": [],
"source": [
"from IPython.display import display, Javascript, Image\n",
"from google.colab.output import eval_js\n",
Expand Down Expand Up @@ -642,9 +631,7 @@
" line_thickness=8)\n",
" plt.figure(figsize=(12, 8))\n",
" plt.imshow(image_np)"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -660,9 +647,11 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "A-f5lfcnp01e"
},
"outputs": [],
"source": [
"!tensorflowjs_converter \\\n",
" --input_format=tf_frozen_model \\\n",
Expand All @@ -684,9 +673,7 @@
" json.dump(label_array, f)\n",
"\n",
"!cd /content/model_web && zip -r /content/model_web.zip *"
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -702,15 +689,15 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FL_miSj2r1yt"
},
"outputs": [],
"source": [
"from google.colab import files\n",
"files.download('/content/model_web.zip') "
],
"execution_count": null,
"outputs": []
]
},
{
"cell_type": "markdown",
Expand All @@ -722,5 +709,20 @@
"Use the `model_web` folder generated here, with this [project](https://github.com/cloud-annotations/object-detection-react) to create a real-time webcam object detection app."
]
}
]
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "Copy of Copy of object-detection.ipynb",
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}