diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..0e40fe8
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,3 @@
+
+# Default ignored files
+/workspace.xml
\ No newline at end of file
diff --git a/.idea/TensorFlowObjectDetectionTutorial.iml b/.idea/TensorFlowObjectDetectionTutorial.iml
new file mode 100644
index 0000000..d68dc8a
--- /dev/null
+++ b/.idea/TensorFlowObjectDetectionTutorial.iml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml
new file mode 100644
index 0000000..a55e7a1
--- /dev/null
+++ b/.idea/codeStyles/codeStyleConfig.xml
@@ -0,0 +1,5 @@
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml
new file mode 100644
index 0000000..105ce2d
--- /dev/null
+++ b/.idea/inspectionProfiles/profiles_settings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..77add3b
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..9570b9b
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/docs/build/.buildinfo b/docs/build/.buildinfo
index b3b3e26..9336572 100644
--- a/docs/build/.buildinfo
+++ b/docs/build/.buildinfo
@@ -1,4 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
-config: 71ee4cae48b6d2f290dbbba66f289314
+config: fe7be69e6c8f8e32eecf964d57e6c7b9
tags: 645f666f9bcd5a90fca523b33c5a78b7
diff --git a/docs/build/.doctrees/auto_examples/index.doctree b/docs/build/.doctrees/auto_examples/index.doctree
new file mode 100644
index 0000000..668a3bc
Binary files /dev/null and b/docs/build/.doctrees/auto_examples/index.doctree differ
diff --git a/docs/build/.doctrees/auto_examples/object_detection_camera.doctree b/docs/build/.doctrees/auto_examples/object_detection_camera.doctree
new file mode 100644
index 0000000..9e8a01e
Binary files /dev/null and b/docs/build/.doctrees/auto_examples/object_detection_camera.doctree differ
diff --git a/docs/build/.doctrees/auto_examples/plot_object_detection_simple.doctree b/docs/build/.doctrees/auto_examples/plot_object_detection_simple.doctree
new file mode 100644
index 0000000..491ece6
Binary files /dev/null and b/docs/build/.doctrees/auto_examples/plot_object_detection_simple.doctree differ
diff --git a/docs/build/.doctrees/auto_examples/sg_execution_times.doctree b/docs/build/.doctrees/auto_examples/sg_execution_times.doctree
new file mode 100644
index 0000000..85b2784
Binary files /dev/null and b/docs/build/.doctrees/auto_examples/sg_execution_times.doctree differ
diff --git a/docs/build/.doctrees/camera.doctree b/docs/build/.doctrees/camera.doctree
deleted file mode 100644
index 9d7ad12..0000000
Binary files a/docs/build/.doctrees/camera.doctree and /dev/null differ
diff --git a/docs/build/.doctrees/environment.pickle b/docs/build/.doctrees/environment.pickle
index a38219f..9fc8152 100644
Binary files a/docs/build/.doctrees/environment.pickle and b/docs/build/.doctrees/environment.pickle differ
diff --git a/docs/build/.doctrees/index.doctree b/docs/build/.doctrees/index.doctree
index 374d166..9cd2072 100644
Binary files a/docs/build/.doctrees/index.doctree and b/docs/build/.doctrees/index.doctree differ
diff --git a/docs/build/.doctrees/install.doctree b/docs/build/.doctrees/install.doctree
index 447ce6d..d5ec6f1 100644
Binary files a/docs/build/.doctrees/install.doctree and b/docs/build/.doctrees/install.doctree differ
diff --git a/docs/build/.doctrees/issues.doctree b/docs/build/.doctrees/issues.doctree
index 8edfd7c..d0c4847 100644
Binary files a/docs/build/.doctrees/issues.doctree and b/docs/build/.doctrees/issues.doctree differ
diff --git a/docs/build/.doctrees/training.doctree b/docs/build/.doctrees/training.doctree
index 3cc8720..79c947a 100644
Binary files a/docs/build/.doctrees/training.doctree and b/docs/build/.doctrees/training.doctree differ
diff --git a/docs/build/_downloads/1a3be7f9b5153f8f6d44c96a51032716/plot_object_detection_simple.ipynb b/docs/build/_downloads/1a3be7f9b5153f8f6d44c96a51032716/plot_object_detection_simple.ipynb
new file mode 100644
index 0000000..bf75f22
--- /dev/null
+++ b/docs/build/_downloads/1a3be7f9b5153f8f6d44c96a51032716/plot_object_detection_simple.ipynb
@@ -0,0 +1,158 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "%matplotlib inline"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "\nObject Detection Test\n=====================\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "This demo will take you through the steps of running an \"out-of-the-box\" detection model on a\ncollection of images.\n\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Create the data directory\n~~~~~~~~~~~~~~~~~~~~~~~~~\nThe snippet shown below will create the ``data`` directory where all our data will be stored. The\ncode will create a directory structure as shown bellow:\n\n.. code-block:: bash\n\n data\n \u251c\u2500\u2500 images\n \u2514\u2500\u2500 models\n\nwhere the ``images`` folder will contain the downlaoded test images, while ``models`` will\ncontain the downloaded models.\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "import os\n\nDATA_DIR = os.path.join(os.getcwd(), 'data')\nIMAGES_DIR = os.path.join(DATA_DIR, 'images')\nMODELS_DIR = os.path.join(DATA_DIR, 'models')\nfor dir in [DATA_DIR, IMAGES_DIR, MODELS_DIR]:\n if not os.path.exists(dir):\n os.mkdir(dir)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Download the test images\n~~~~~~~~~~~~~~~~~~~~~~~~\nFirst we will download the images that we will use throughout this tutorial. The code snippet\nshown bellow will download the test images from the `TensorFlow Model Garden `_\nand save them inside the ``data/images`` folder.\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "import urllib.request\n\nIMAGE_FILENAMES = ['image1.jpg', 'image2.jpg']\nIMAGES_DOWNLOAD_BASE = \\\n 'https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/test_images/'\n\nfor image_filename in IMAGE_FILENAMES:\n\n image_path = os.path.join(IMAGES_DIR, image_filename)\n\n # Download image\n if not os.path.exists(image_path):\n print('Downloading {}... '.format(image_filename), end='')\n urllib.request.urlretrieve(IMAGES_DOWNLOAD_BASE + image_filename, image_path)\n print('Done')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Download the model\n~~~~~~~~~~~~~~~~~~\nThe code snippet shown below is used to download the object detection model checkpoint file,\nas well as the labels file (.pbtxt) which contains a list of strings used to add the correct\nlabel to each detection (e.g. person). Once downloaded the files will be stored under the\n``data/models`` folder.\n\nThe particular detection algorithm we will use is the `CenterNet HourGlass104 1024x1024`. More\nmodels can be found in the `TensorFlow 2 Detection Model Zoo `_.\nTo use a different model you will need the URL name of the specific model. This can be done as\nfollows:\n\n1. Right click on the `Model name` of the model you would like to use;\n2. Click on `Copy link address` to copy the download link of the model;\n3. Paste the link in a text editor of your choice. You should observe a link similar to ``download.tensorflow.org/models/object_detection/tf2/YYYYYYYY/XXXXXXXXX.tar.gz``;\n4. Copy the ``XXXXXXXXX`` part of the link and use it to replace the value of the ``MODEL_NAME`` variable in the code shown below;\n5. Copy the ``YYYYYYYY`` part of the link and use it to replace the value of the ``MODEL_DATE`` variable in the code shown below.\n\nFor example, the download link for the model used below is: ``download.tensorflow.org/models/object_detection/tf2/20200711/centernet_hg104_1024x1024_coco17_tpu-32.tar.gz``\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "import tarfile\n\n# Download and extract model\nMODEL_DATE = '20200711'\nMODEL_NAME = 'centernet_hg104_1024x1024_coco17_tpu-32'\nMODEL_TAR_FILENAME = MODEL_NAME + '.tar.gz'\nMODELS_DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/tf2/'\nMODEL_DOWNLOAD_LINK = MODELS_DOWNLOAD_BASE + MODEL_DATE + '/' + MODEL_TAR_FILENAME\nPATH_TO_MODEL_TAR = os.path.join(MODELS_DIR, MODEL_TAR_FILENAME)\nPATH_TO_CKPT = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, 'checkpoint/'))\nPATH_TO_CFG = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, 'pipeline.config'))\nif not os.path.exists(PATH_TO_CKPT):\n print('Downloading model. This may take a while... ', end='')\n urllib.request.urlretrieve(MODEL_DOWNLOAD_LINK, PATH_TO_MODEL_TAR)\n tar_file = tarfile.open(PATH_TO_MODEL_TAR)\n tar_file.extractall(MODELS_DIR)\n tar_file.close()\n os.remove(PATH_TO_MODEL_TAR)\n print('Done')\n\n# Download labels file\nLABEL_FILENAME = 'mscoco_label_map.pbtxt'\nLABELS_DOWNLOAD_BASE = \\\n 'https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/data/'\nPATH_TO_LABELS = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, LABEL_FILENAME))\nif not os.path.exists(PATH_TO_LABELS):\n print('Downloading label file... ', end='')\n urllib.request.urlretrieve(LABELS_DOWNLOAD_BASE + LABEL_FILENAME, PATH_TO_LABELS)\n print('Done')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Load the model\n~~~~~~~~~~~~~~\nNext we load the downloaded model\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging (1)\nimport tensorflow as tf\nfrom object_detection.utils import label_map_util\nfrom object_detection.utils import config_util\nfrom object_detection.utils import visualization_utils as viz_utils\nfrom object_detection.builders import model_builder\n\ntf.get_logger().setLevel('ERROR') # Suppress TensorFlow logging (2)\n\n# Enable GPU dynamic memory allocation\ngpus = tf.config.experimental.list_physical_devices('GPU')\nfor gpu in gpus:\n tf.config.experimental.set_memory_growth(gpu, True)\n\n# Load pipeline config and build a detection model\nconfigs = config_util.get_configs_from_pipeline_file(PATH_TO_CFG)\nmodel_config = configs['model']\ndetection_model = model_builder.build(model_config=model_config, is_training=False)\n\n# Restore checkpoint\nckpt = tf.compat.v2.train.Checkpoint(\n model=detection_model)\nckpt.restore(os.path.join(PATH_TO_CKPT, 'ckpt-0')).expect_partial()\n\n@tf.function\ndef detect_fn(image):\n \"\"\"Detect objects in image.\"\"\"\n\n image, shapes = detection_model.preprocess(image)\n prediction_dict = detection_model.predict(image, shapes)\n detections = detection_model.postprocess(prediction_dict, shapes)\n\n return detections, prediction_dict, tf.reshape(shapes, [-1])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Load label map data (for plotting)\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nLabel maps correspond index numbers to category names, so that when our convolution network\npredicts `5`, we know that this corresponds to `airplane`. Here we use internal utility\nfunctions, but anything that returns a dictionary mapping integers to appropriate string labels\nwould be fine.\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,\n use_display_name=True)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Putting everything together\n~~~~~~~~~~~~~~~~~~~~~~~~~~~\nThe code shown below loads an image, runs it through the detection model and visualizes the\ndetection results, including the keypoints.\n\nNote that this will take a long time (several minutes) the first time you run this code due to\ntf.function's trace-compilation --- on subsequent runs (e.g. on new images), things will be\nfaster.\n\nHere are some simple things to try out if you are curious:\n\n* Modify some of the input images and see if detection still works. Some simple things to try out here (just uncomment the relevant portions of code) include flipping the image horizontally, or converting to grayscale (note that we still expect the input image to have 3 channels).\n* Print out `detections['detection_boxes']` and try to match the box locations to the boxes in the image. Notice that coordinates are given in normalized form (i.e., in the interval [0, 1]).\n* Set ``min_score_thresh`` to other values (between 0 and 1) to allow more detections in or to filter out more detections.\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "import numpy as np\nfrom six import BytesIO\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings('ignore') # Suppress Matplotlib warnings\n\ndef load_image_into_numpy_array(path):\n \"\"\"Load an image from file into a numpy array.\n\n Puts image into numpy array to feed into tensorflow graph.\n Note that by convention we put it into a numpy array with shape\n (height, width, channels), where channels=3 for RGB.\n\n Args:\n path: the file path to the image\n\n Returns:\n uint8 numpy array with shape (img_height, img_width, 3)\n \"\"\"\n img_data = tf.io.gfile.GFile(path, 'rb').read()\n image = Image.open(BytesIO(img_data))\n (im_width, im_height) = image.size\n return np.array(image.getdata()).reshape(\n (im_height, im_width, 3)).astype(np.uint8)\n\n\nfor image_filename in IMAGE_FILENAMES:\n\n print('Running inference for {}... '.format(image_filename), end='')\n\n image_path = os.path.join(IMAGES_DIR, image_filename)\n image_np = load_image_into_numpy_array(image_path)\n\n # Things to try:\n # Flip horizontally\n # image_np = np.fliplr(image_np).copy()\n\n # Convert image to grayscale\n # image_np = np.tile(\n # np.mean(image_np, 2, keepdims=True), (1, 1, 3)).astype(np.uint8)\n\n input_tensor = tf.convert_to_tensor(\n np.expand_dims(image_np, 0), dtype=tf.float32)\n detections, predictions_dict, shapes = detect_fn(input_tensor)\n\n label_id_offset = 1\n image_np_with_detections = image_np.copy()\n\n viz_utils.visualize_boxes_and_labels_on_image_array(\n image_np_with_detections,\n detections['detection_boxes'][0].numpy(),\n (detections['detection_classes'][0].numpy() + label_id_offset).astype(int),\n detections['detection_scores'][0].numpy(),\n category_index,\n use_normalized_coordinates=True,\n max_boxes_to_draw=200,\n min_score_thresh=.30,\n agnostic_mode=False)\n\n plt.figure()\n plt.imshow(image_np_with_detections)\n print('Done')\nplt.show()\n\n# sphinx_gallery_thumbnail_number = 2"
+ ]
+ }
+ ],
+ "metadata": {
+ "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.8.3"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
\ No newline at end of file
diff --git a/docs/build/_downloads/20476501054a7e18cf729f8808e7672e/auto_examples_python.zip b/docs/build/_downloads/20476501054a7e18cf729f8808e7672e/auto_examples_python.zip
new file mode 100644
index 0000000..0bd34f9
Binary files /dev/null and b/docs/build/_downloads/20476501054a7e18cf729f8808e7672e/auto_examples_python.zip differ
diff --git a/docs/build/_downloads/29dd112360879edd9c41f5e05a1e5bae/object_detection_camera.py b/docs/build/_downloads/29dd112360879edd9c41f5e05a1e5bae/object_detection_camera.py
new file mode 100644
index 0000000..cd4443e
--- /dev/null
+++ b/docs/build/_downloads/29dd112360879edd9c41f5e05a1e5bae/object_detection_camera.py
@@ -0,0 +1,196 @@
+#!/usr/bin/env python
+# coding: utf-8
+"""
+Detect Objects Using Your Webcam
+================================
+"""
+
+# %%
+# This demo will take you through the steps of running an "out-of-the-box" detection model to
+# detect objects in the video stream extracted from your camera.
+
+# %%
+# Create the data directory
+# ~~~~~~~~~~~~~~~~~~~~~~~~~
+# The snippet shown below will create the ``data`` directory where all our data will be stored. The
+# code will create a directory structure as shown bellow:
+#
+# .. code-block:: bash
+#
+# data
+# └── models
+#
+# where the ``models`` folder will will contain the downloaded models.
+import os
+
+DATA_DIR = os.path.join(os.getcwd(), 'data')
+MODELS_DIR = os.path.join(DATA_DIR, 'models')
+for dir in [DATA_DIR, MODELS_DIR]:
+ if not os.path.exists(dir):
+ os.mkdir(dir)
+
+# %%
+# Download the model
+# ~~~~~~~~~~~~~~~~~~
+# The code snippet shown below is used to download the object detection model checkpoint file,
+# as well as the labels file (.pbtxt) which contains a list of strings used to add the correct
+# label to each detection (e.g. person).
+#
+# The particular detection algorithm we will use is the `SSD ResNet101 V1 FPN 640x640`. More
+# models can be found in the `TensorFlow 2 Detection Model Zoo `_.
+# To use a different model you will need the URL name of the specific model. This can be done as
+# follows:
+#
+# 1. Right click on the `Model name` of the model you would like to use;
+# 2. Click on `Copy link address` to copy the download link of the model;
+# 3. Paste the link in a text editor of your choice. You should observe a link similar to ``download.tensorflow.org/models/object_detection/tf2/YYYYYYYY/XXXXXXXXX.tar.gz``;
+# 4. Copy the ``XXXXXXXXX`` part of the link and use it to replace the value of the ``MODEL_NAME`` variable in the code shown below;
+# 5. Copy the ``YYYYYYYY`` part of the link and use it to replace the value of the ``MODEL_DATE`` variable in the code shown below.
+#
+# For example, the download link for the model used below is: ``download.tensorflow.org/models/object_detection/tf2/20200711/ssd_resnet101_v1_fpn_640x640_coco17_tpu-8.tar.gz``
+import tarfile
+import urllib.request
+
+# Download and extract model
+MODEL_DATE = '20200711'
+MODEL_NAME = 'ssd_resnet101_v1_fpn_640x640_coco17_tpu-8'
+MODEL_TAR_FILENAME = MODEL_NAME + '.tar.gz'
+MODELS_DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/tf2/'
+MODEL_DOWNLOAD_LINK = MODELS_DOWNLOAD_BASE + MODEL_DATE + '/' + MODEL_TAR_FILENAME
+PATH_TO_MODEL_TAR = os.path.join(MODELS_DIR, MODEL_TAR_FILENAME)
+PATH_TO_CKPT = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, 'checkpoint/'))
+PATH_TO_CFG = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, 'pipeline.config'))
+if not os.path.exists(PATH_TO_CKPT):
+ print('Downloading model. This may take a while... ', end='')
+ urllib.request.urlretrieve(MODEL_DOWNLOAD_LINK, PATH_TO_MODEL_TAR)
+ tar_file = tarfile.open(PATH_TO_MODEL_TAR)
+ tar_file.extractall(MODELS_DIR)
+ tar_file.close()
+ os.remove(PATH_TO_MODEL_TAR)
+ print('Done')
+
+# Download labels file
+LABEL_FILENAME = 'mscoco_label_map.pbtxt'
+LABELS_DOWNLOAD_BASE = \
+ 'https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/data/'
+PATH_TO_LABELS = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, LABEL_FILENAME))
+if not os.path.exists(PATH_TO_LABELS):
+ print('Downloading label file... ', end='')
+ urllib.request.urlretrieve(LABELS_DOWNLOAD_BASE + LABEL_FILENAME, PATH_TO_LABELS)
+ print('Done')
+
+# %%
+# Load the model
+# ~~~~~~~~~~~~~~
+# Next we load the downloaded model
+
+os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging
+import tensorflow as tf
+from object_detection.utils import label_map_util
+from object_detection.utils import config_util
+from object_detection.utils import visualization_utils as viz_utils
+from object_detection.builders import model_builder
+
+tf.get_logger().setLevel('ERROR') # Suppress TensorFlow logging (2)
+
+# Enable GPU dynamic memory allocation
+gpus = tf.config.experimental.list_physical_devices('GPU')
+for gpu in gpus:
+ tf.config.experimental.set_memory_growth(gpu, True)
+
+# Load pipeline config and build a detection model
+configs = config_util.get_configs_from_pipeline_file(PATH_TO_CFG)
+model_config = configs['model']
+detection_model = model_builder.build(model_config=model_config, is_training=False)
+
+# Restore checkpoint
+ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
+ckpt.restore(os.path.join(PATH_TO_CKPT, 'ckpt-0')).expect_partial()
+
+@tf.function
+def detect_fn(image):
+ """Detect objects in image."""
+
+ image, shapes = detection_model.preprocess(image)
+ prediction_dict = detection_model.predict(image, shapes)
+ detections = detection_model.postprocess(prediction_dict, shapes)
+
+ return detections, prediction_dict, tf.reshape(shapes, [-1])
+
+
+# %%
+# Load label map data (for plotting)
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+# Label maps correspond index numbers to category names, so that when our convolution network
+# predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility
+# functions, but anything that returns a dictionary mapping integers to appropriate string labels
+# would be fine.
+category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,
+ use_display_name=True)
+
+# %%
+# Define the video stream
+# ~~~~~~~~~~~~~~~~~~~~~~~
+# We will use `OpenCV `_ to capture the video stream
+# generated by our webcam. For more information you can refer to the `OpenCV-Python Tutorials `_
+import cv2
+
+cap = cv2.VideoCapture(0)
+
+# %%
+# Putting everything together
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+# The code shown below loads an image, runs it through the detection model and visualizes the
+# detection results, including the keypoints.
+#
+# Note that this will take a long time (several minutes) the first time you run this code due to
+# tf.function's trace-compilation --- on subsequent runs (e.g. on new images), things will be
+# faster.
+#
+# Here are some simple things to try out if you are curious:
+#
+# * Modify some of the input images and see if detection still works. Some simple things to try out here (just uncomment the relevant portions of code) include flipping the image horizontally, or converting to grayscale (note that we still expect the input image to have 3 channels).
+# * Print out `detections['detection_boxes']` and try to match the box locations to the boxes in the image. Notice that coordinates are given in normalized form (i.e., in the interval [0, 1]).
+# * Set ``min_score_thresh`` to other values (between 0 and 1) to allow more detections in or to filter out more detections.
+import numpy as np
+
+while True:
+ # Read frame from camera
+ ret, image_np = cap.read()
+
+ # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
+ image_np_expanded = np.expand_dims(image_np, axis=0)
+
+ # Things to try:
+ # Flip horizontally
+ # image_np = np.fliplr(image_np).copy()
+
+ # Convert image to grayscale
+ # image_np = np.tile(
+ # np.mean(image_np, 2, keepdims=True), (1, 1, 3)).astype(np.uint8)
+
+ input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
+ detections, predictions_dict, shapes = detect_fn(input_tensor)
+
+ label_id_offset = 1
+ image_np_with_detections = image_np.copy()
+
+ viz_utils.visualize_boxes_and_labels_on_image_array(
+ image_np_with_detections,
+ detections['detection_boxes'][0].numpy(),
+ (detections['detection_classes'][0].numpy() + label_id_offset).astype(int),
+ detections['detection_scores'][0].numpy(),
+ category_index,
+ use_normalized_coordinates=True,
+ max_boxes_to_draw=200,
+ min_score_thresh=.30,
+ agnostic_mode=False)
+
+ # Display output
+ cv2.imshow('object detection', cv2.resize(image_np_with_detections, (800, 600)))
+
+ if cv2.waitKey(25) & 0xFF == ord('q'):
+ break
+
+cap.release()
+cv2.destroyAllWindows()
\ No newline at end of file
diff --git a/docs/build/_downloads/a2a6b5af38efde6fafcb44df9762f22d/object_detection_camera.ipynb b/docs/build/_downloads/a2a6b5af38efde6fafcb44df9762f22d/object_detection_camera.ipynb
new file mode 100644
index 0000000..9bbf23c
--- /dev/null
+++ b/docs/build/_downloads/a2a6b5af38efde6fafcb44df9762f22d/object_detection_camera.ipynb
@@ -0,0 +1,158 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "%matplotlib inline"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "\nDetect Objects Using Your Webcam\n================================\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "This demo will take you through the steps of running an \"out-of-the-box\" detection model to\ndetect objects in the video stream extracted from your camera.\n\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Create the data directory\n~~~~~~~~~~~~~~~~~~~~~~~~~\nThe snippet shown below will create the ``data`` directory where all our data will be stored. The\ncode will create a directory structure as shown bellow:\n\n.. code-block:: bash\n\n data\n \u2514\u2500\u2500 models\n\nwhere the ``models`` folder will will contain the downloaded models.\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "import os\n\nDATA_DIR = os.path.join(os.getcwd(), 'data')\nMODELS_DIR = os.path.join(DATA_DIR, 'models')\nfor dir in [DATA_DIR, MODELS_DIR]:\n if not os.path.exists(dir):\n os.mkdir(dir)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Download the model\n~~~~~~~~~~~~~~~~~~\nThe code snippet shown below is used to download the object detection model checkpoint file,\nas well as the labels file (.pbtxt) which contains a list of strings used to add the correct\nlabel to each detection (e.g. person).\n\nThe particular detection algorithm we will use is the `SSD ResNet101 V1 FPN 640x640`. More\nmodels can be found in the `TensorFlow 2 Detection Model Zoo `_.\nTo use a different model you will need the URL name of the specific model. This can be done as\nfollows:\n\n1. Right click on the `Model name` of the model you would like to use;\n2. Click on `Copy link address` to copy the download link of the model;\n3. Paste the link in a text editor of your choice. You should observe a link similar to ``download.tensorflow.org/models/object_detection/tf2/YYYYYYYY/XXXXXXXXX.tar.gz``;\n4. Copy the ``XXXXXXXXX`` part of the link and use it to replace the value of the ``MODEL_NAME`` variable in the code shown below;\n5. Copy the ``YYYYYYYY`` part of the link and use it to replace the value of the ``MODEL_DATE`` variable in the code shown below.\n\nFor example, the download link for the model used below is: ``download.tensorflow.org/models/object_detection/tf2/20200711/ssd_resnet101_v1_fpn_640x640_coco17_tpu-8.tar.gz``\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "import tarfile\nimport urllib.request\n\n# Download and extract model\nMODEL_DATE = '20200711'\nMODEL_NAME = 'ssd_resnet101_v1_fpn_640x640_coco17_tpu-8'\nMODEL_TAR_FILENAME = MODEL_NAME + '.tar.gz'\nMODELS_DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/tf2/'\nMODEL_DOWNLOAD_LINK = MODELS_DOWNLOAD_BASE + MODEL_DATE + '/' + MODEL_TAR_FILENAME\nPATH_TO_MODEL_TAR = os.path.join(MODELS_DIR, MODEL_TAR_FILENAME)\nPATH_TO_CKPT = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, 'checkpoint/'))\nPATH_TO_CFG = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, 'pipeline.config'))\nif not os.path.exists(PATH_TO_CKPT):\n print('Downloading model. This may take a while... ', end='')\n urllib.request.urlretrieve(MODEL_DOWNLOAD_LINK, PATH_TO_MODEL_TAR)\n tar_file = tarfile.open(PATH_TO_MODEL_TAR)\n tar_file.extractall(MODELS_DIR)\n tar_file.close()\n os.remove(PATH_TO_MODEL_TAR)\n print('Done')\n\n# Download labels file\nLABEL_FILENAME = 'mscoco_label_map.pbtxt'\nLABELS_DOWNLOAD_BASE = \\\n 'https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/data/'\nPATH_TO_LABELS = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, LABEL_FILENAME))\nif not os.path.exists(PATH_TO_LABELS):\n print('Downloading label file... ', end='')\n urllib.request.urlretrieve(LABELS_DOWNLOAD_BASE + LABEL_FILENAME, PATH_TO_LABELS)\n print('Done')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Load the model\n~~~~~~~~~~~~~~\nNext we load the downloaded model\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging\nimport tensorflow as tf\nfrom object_detection.utils import label_map_util\nfrom object_detection.utils import config_util\nfrom object_detection.utils import visualization_utils as viz_utils\nfrom object_detection.builders import model_builder\n\ntf.get_logger().setLevel('ERROR') # Suppress TensorFlow logging (2)\n\n# Enable GPU dynamic memory allocation\ngpus = tf.config.experimental.list_physical_devices('GPU')\nfor gpu in gpus:\n tf.config.experimental.set_memory_growth(gpu, True)\n\n# Load pipeline config and build a detection model\nconfigs = config_util.get_configs_from_pipeline_file(PATH_TO_CFG)\nmodel_config = configs['model']\ndetection_model = model_builder.build(model_config=model_config, is_training=False)\n\n# Restore checkpoint\nckpt = tf.compat.v2.train.Checkpoint(model=detection_model)\nckpt.restore(os.path.join(PATH_TO_CKPT, 'ckpt-0')).expect_partial()\n\n@tf.function\ndef detect_fn(image):\n \"\"\"Detect objects in image.\"\"\"\n\n image, shapes = detection_model.preprocess(image)\n prediction_dict = detection_model.predict(image, shapes)\n detections = detection_model.postprocess(prediction_dict, shapes)\n\n return detections, prediction_dict, tf.reshape(shapes, [-1])"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Load label map data (for plotting)\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nLabel maps correspond index numbers to category names, so that when our convolution network\npredicts `5`, we know that this corresponds to `airplane`. Here we use internal utility\nfunctions, but anything that returns a dictionary mapping integers to appropriate string labels\nwould be fine.\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,\n use_display_name=True)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Define the video stream\n~~~~~~~~~~~~~~~~~~~~~~~\nWe will use `OpenCV `_ to capture the video stream\ngenerated by our webcam. For more information you can refer to the `OpenCV-Python Tutorials `_\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "import cv2\n\ncap = cv2.VideoCapture(0)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Putting everything together\n~~~~~~~~~~~~~~~~~~~~~~~~~~~\nThe code shown below loads an image, runs it through the detection model and visualizes the\ndetection results, including the keypoints.\n\nNote that this will take a long time (several minutes) the first time you run this code due to\ntf.function's trace-compilation --- on subsequent runs (e.g. on new images), things will be\nfaster.\n\nHere are some simple things to try out if you are curious:\n\n* Modify some of the input images and see if detection still works. Some simple things to try out here (just uncomment the relevant portions of code) include flipping the image horizontally, or converting to grayscale (note that we still expect the input image to have 3 channels).\n* Print out `detections['detection_boxes']` and try to match the box locations to the boxes in the image. Notice that coordinates are given in normalized form (i.e., in the interval [0, 1]).\n* Set ``min_score_thresh`` to other values (between 0 and 1) to allow more detections in or to filter out more detections.\n\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [],
+ "source": [
+ "import numpy as np\n\nwhile True:\n # Read frame from camera\n ret, image_np = cap.read()\n\n # Expand dimensions since the model expects images to have shape: [1, None, None, 3]\n image_np_expanded = np.expand_dims(image_np, axis=0)\n\n # Things to try:\n # Flip horizontally\n # image_np = np.fliplr(image_np).copy()\n\n # Convert image to grayscale\n # image_np = np.tile(\n # np.mean(image_np, 2, keepdims=True), (1, 1, 3)).astype(np.uint8)\n\n input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)\n detections, predictions_dict, shapes = detect_fn(input_tensor)\n\n label_id_offset = 1\n image_np_with_detections = image_np.copy()\n\n viz_utils.visualize_boxes_and_labels_on_image_array(\n image_np_with_detections,\n detections['detection_boxes'][0].numpy(),\n (detections['detection_classes'][0].numpy() + label_id_offset).astype(int),\n detections['detection_scores'][0].numpy(),\n category_index,\n use_normalized_coordinates=True,\n max_boxes_to_draw=200,\n min_score_thresh=.30,\n agnostic_mode=False)\n\n # Display output\n cv2.imshow('object detection', cv2.resize(image_np_with_detections, (800, 600)))\n\n if cv2.waitKey(25) & 0xFF == ord('q'):\n break\n\ncap.release()\ncv2.destroyAllWindows()"
+ ]
+ }
+ ],
+ "metadata": {
+ "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.8.3"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
\ No newline at end of file
diff --git a/docs/build/_downloads/c257975512db4c0870a09ff2e222a88d/auto_examples_jupyter.zip b/docs/build/_downloads/c257975512db4c0870a09ff2e222a88d/auto_examples_jupyter.zip
new file mode 100644
index 0000000..e897c64
Binary files /dev/null and b/docs/build/_downloads/c257975512db4c0870a09ff2e222a88d/auto_examples_jupyter.zip differ
diff --git a/docs/build/_downloads/cbc63cc7463d82d360a24cb612cedafe/plot_object_detection_simple.py b/docs/build/_downloads/cbc63cc7463d82d360a24cb612cedafe/plot_object_detection_simple.py
new file mode 100644
index 0000000..a262fc5
--- /dev/null
+++ b/docs/build/_downloads/cbc63cc7463d82d360a24cb612cedafe/plot_object_detection_simple.py
@@ -0,0 +1,239 @@
+#!/usr/bin/env python
+# coding: utf-8
+"""
+Object Detection Test
+=====================
+"""
+
+# %%
+# This demo will take you through the steps of running an "out-of-the-box" detection model on a
+# collection of images.
+
+# %%
+# Create the data directory
+# ~~~~~~~~~~~~~~~~~~~~~~~~~
+# The snippet shown below will create the ``data`` directory where all our data will be stored. The
+# code will create a directory structure as shown bellow:
+#
+# .. code-block:: bash
+#
+# data
+# ├── images
+# └── models
+#
+# where the ``images`` folder will contain the downlaoded test images, while ``models`` will
+# contain the downloaded models.
+import os
+
+DATA_DIR = os.path.join(os.getcwd(), 'data')
+IMAGES_DIR = os.path.join(DATA_DIR, 'images')
+MODELS_DIR = os.path.join(DATA_DIR, 'models')
+for dir in [DATA_DIR, IMAGES_DIR, MODELS_DIR]:
+ if not os.path.exists(dir):
+ os.mkdir(dir)
+
+# %%
+# Download the test images
+# ~~~~~~~~~~~~~~~~~~~~~~~~
+# First we will download the images that we will use throughout this tutorial. The code snippet
+# shown bellow will download the test images from the `TensorFlow Model Garden `_
+# and save them inside the ``data/images`` folder.
+import urllib.request
+
+IMAGE_FILENAMES = ['image1.jpg', 'image2.jpg']
+IMAGES_DOWNLOAD_BASE = \
+ 'https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/test_images/'
+
+for image_filename in IMAGE_FILENAMES:
+
+ image_path = os.path.join(IMAGES_DIR, image_filename)
+
+ # Download image
+ if not os.path.exists(image_path):
+ print('Downloading {}... '.format(image_filename), end='')
+ urllib.request.urlretrieve(IMAGES_DOWNLOAD_BASE + image_filename, image_path)
+ print('Done')
+
+
+# %%
+# Download the model
+# ~~~~~~~~~~~~~~~~~~
+# The code snippet shown below is used to download the object detection model checkpoint file,
+# as well as the labels file (.pbtxt) which contains a list of strings used to add the correct
+# label to each detection (e.g. person). Once downloaded the files will be stored under the
+# ``data/models`` folder.
+#
+# The particular detection algorithm we will use is the `CenterNet HourGlass104 1024x1024`. More
+# models can be found in the `TensorFlow 2 Detection Model Zoo `_.
+# To use a different model you will need the URL name of the specific model. This can be done as
+# follows:
+#
+# 1. Right click on the `Model name` of the model you would like to use;
+# 2. Click on `Copy link address` to copy the download link of the model;
+# 3. Paste the link in a text editor of your choice. You should observe a link similar to ``download.tensorflow.org/models/object_detection/tf2/YYYYYYYY/XXXXXXXXX.tar.gz``;
+# 4. Copy the ``XXXXXXXXX`` part of the link and use it to replace the value of the ``MODEL_NAME`` variable in the code shown below;
+# 5. Copy the ``YYYYYYYY`` part of the link and use it to replace the value of the ``MODEL_DATE`` variable in the code shown below.
+#
+# For example, the download link for the model used below is: ``download.tensorflow.org/models/object_detection/tf2/20200711/centernet_hg104_1024x1024_coco17_tpu-32.tar.gz``
+
+import tarfile
+
+# Download and extract model
+MODEL_DATE = '20200711'
+MODEL_NAME = 'centernet_hg104_1024x1024_coco17_tpu-32'
+MODEL_TAR_FILENAME = MODEL_NAME + '.tar.gz'
+MODELS_DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/tf2/'
+MODEL_DOWNLOAD_LINK = MODELS_DOWNLOAD_BASE + MODEL_DATE + '/' + MODEL_TAR_FILENAME
+PATH_TO_MODEL_TAR = os.path.join(MODELS_DIR, MODEL_TAR_FILENAME)
+PATH_TO_CKPT = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, 'checkpoint/'))
+PATH_TO_CFG = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, 'pipeline.config'))
+if not os.path.exists(PATH_TO_CKPT):
+ print('Downloading model. This may take a while... ', end='')
+ urllib.request.urlretrieve(MODEL_DOWNLOAD_LINK, PATH_TO_MODEL_TAR)
+ tar_file = tarfile.open(PATH_TO_MODEL_TAR)
+ tar_file.extractall(MODELS_DIR)
+ tar_file.close()
+ os.remove(PATH_TO_MODEL_TAR)
+ print('Done')
+
+# Download labels file
+LABEL_FILENAME = 'mscoco_label_map.pbtxt'
+LABELS_DOWNLOAD_BASE = \
+ 'https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/data/'
+PATH_TO_LABELS = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, LABEL_FILENAME))
+if not os.path.exists(PATH_TO_LABELS):
+ print('Downloading label file... ', end='')
+ urllib.request.urlretrieve(LABELS_DOWNLOAD_BASE + LABEL_FILENAME, PATH_TO_LABELS)
+ print('Done')
+
+# %%
+# Load the model
+# ~~~~~~~~~~~~~~
+# Next we load the downloaded model
+os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging (1)
+import tensorflow as tf
+from object_detection.utils import label_map_util
+from object_detection.utils import config_util
+from object_detection.utils import visualization_utils as viz_utils
+from object_detection.builders import model_builder
+
+tf.get_logger().setLevel('ERROR') # Suppress TensorFlow logging (2)
+
+# Enable GPU dynamic memory allocation
+gpus = tf.config.experimental.list_physical_devices('GPU')
+for gpu in gpus:
+ tf.config.experimental.set_memory_growth(gpu, True)
+
+# Load pipeline config and build a detection model
+configs = config_util.get_configs_from_pipeline_file(PATH_TO_CFG)
+model_config = configs['model']
+detection_model = model_builder.build(model_config=model_config, is_training=False)
+
+# Restore checkpoint
+ckpt = tf.compat.v2.train.Checkpoint(
+ model=detection_model)
+ckpt.restore(os.path.join(PATH_TO_CKPT, 'ckpt-0')).expect_partial()
+
+@tf.function
+def detect_fn(image):
+ """Detect objects in image."""
+
+ image, shapes = detection_model.preprocess(image)
+ prediction_dict = detection_model.predict(image, shapes)
+ detections = detection_model.postprocess(prediction_dict, shapes)
+
+ return detections, prediction_dict, tf.reshape(shapes, [-1])
+
+
+# %%
+# Load label map data (for plotting)
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+# Label maps correspond index numbers to category names, so that when our convolution network
+# predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility
+# functions, but anything that returns a dictionary mapping integers to appropriate string labels
+# would be fine.
+
+category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,
+ use_display_name=True)
+
+# %%
+# Putting everything together
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+# The code shown below loads an image, runs it through the detection model and visualizes the
+# detection results, including the keypoints.
+#
+# Note that this will take a long time (several minutes) the first time you run this code due to
+# tf.function's trace-compilation --- on subsequent runs (e.g. on new images), things will be
+# faster.
+#
+# Here are some simple things to try out if you are curious:
+#
+# * Modify some of the input images and see if detection still works. Some simple things to try out here (just uncomment the relevant portions of code) include flipping the image horizontally, or converting to grayscale (note that we still expect the input image to have 3 channels).
+# * Print out `detections['detection_boxes']` and try to match the box locations to the boxes in the image. Notice that coordinates are given in normalized form (i.e., in the interval [0, 1]).
+# * Set ``min_score_thresh`` to other values (between 0 and 1) to allow more detections in or to filter out more detections.
+import numpy as np
+from six import BytesIO
+from PIL import Image
+import matplotlib.pyplot as plt
+import warnings
+warnings.filterwarnings('ignore') # Suppress Matplotlib warnings
+
+def load_image_into_numpy_array(path):
+ """Load an image from file into a numpy array.
+
+ Puts image into numpy array to feed into tensorflow graph.
+ Note that by convention we put it into a numpy array with shape
+ (height, width, channels), where channels=3 for RGB.
+
+ Args:
+ path: the file path to the image
+
+ Returns:
+ uint8 numpy array with shape (img_height, img_width, 3)
+ """
+ img_data = tf.io.gfile.GFile(path, 'rb').read()
+ image = Image.open(BytesIO(img_data))
+ (im_width, im_height) = image.size
+ return np.array(image.getdata()).reshape(
+ (im_height, im_width, 3)).astype(np.uint8)
+
+
+for image_filename in IMAGE_FILENAMES:
+
+ print('Running inference for {}... '.format(image_filename), end='')
+
+ image_path = os.path.join(IMAGES_DIR, image_filename)
+ image_np = load_image_into_numpy_array(image_path)
+
+ # Things to try:
+ # Flip horizontally
+ # image_np = np.fliplr(image_np).copy()
+
+ # Convert image to grayscale
+ # image_np = np.tile(
+ # np.mean(image_np, 2, keepdims=True), (1, 1, 3)).astype(np.uint8)
+
+ input_tensor = tf.convert_to_tensor(
+ np.expand_dims(image_np, 0), dtype=tf.float32)
+ detections, predictions_dict, shapes = detect_fn(input_tensor)
+
+ label_id_offset = 1
+ image_np_with_detections = image_np.copy()
+
+ viz_utils.visualize_boxes_and_labels_on_image_array(
+ image_np_with_detections,
+ detections['detection_boxes'][0].numpy(),
+ (detections['detection_classes'][0].numpy() + label_id_offset).astype(int),
+ detections['detection_scores'][0].numpy(),
+ category_index,
+ use_normalized_coordinates=True,
+ max_boxes_to_draw=200,
+ min_score_thresh=.30,
+ agnostic_mode=False)
+
+ plt.figure()
+ plt.imshow(image_np_with_detections)
+ print('Done')
+plt.show()
+
+# sphinx_gallery_thumbnail_number = 2
\ No newline at end of file
diff --git a/docs/build/_downloads/d0e545609c5f7f49f39abc7b6a38cec3/partition_dataset.py b/docs/build/_downloads/d0e545609c5f7f49f39abc7b6a38cec3/partition_dataset.py
new file mode 100644
index 0000000..a9d0c11
--- /dev/null
+++ b/docs/build/_downloads/d0e545609c5f7f49f39abc7b6a38cec3/partition_dataset.py
@@ -0,0 +1,98 @@
+""" usage: partition_dataset.py [-h] [-i IMAGEDIR] [-o OUTPUTDIR] [-r RATIO] [-x]
+
+Partition dataset of images into training and testing sets
+
+optional arguments:
+ -h, --help show this help message and exit
+ -i IMAGEDIR, --imageDir IMAGEDIR
+ Path to the folder where the image dataset is stored. If not specified, the CWD will be used.
+ -o OUTPUTDIR, --outputDir OUTPUTDIR
+ Path to the output folder where the train and test dirs should be created. Defaults to the same directory as IMAGEDIR.
+ -r RATIO, --ratio RATIO
+ The ratio of the number of test images over the total number of images. The default is 0.1.
+ -x, --xml Set this flag if you want the xml annotation files to be processed and copied over.
+"""
+import os
+import re
+from shutil import copyfile
+import argparse
+import math
+import random
+
+
+def iterate_dir(source, dest, ratio, copy_xml):
+ source = source.replace('\\', '/')
+ dest = dest.replace('\\', '/')
+ train_dir = os.path.join(dest, 'train')
+ test_dir = os.path.join(dest, 'test')
+
+ if not os.path.exists(train_dir):
+ os.makedirs(train_dir)
+ if not os.path.exists(test_dir):
+ os.makedirs(test_dir)
+
+ images = [f for f in os.listdir(source)
+ if re.search(r'([a-zA-Z0-9\s_\\.\-\(\):])+(.jpg|.jpeg|.png)$', f)]
+
+ num_images = len(images)
+ num_test_images = math.ceil(ratio*num_images)
+
+ for i in range(num_test_images):
+ idx = random.randint(0, len(images)-1)
+ filename = images[idx]
+ copyfile(os.path.join(source, filename),
+ os.path.join(test_dir, filename))
+ if copy_xml:
+ xml_filename = os.path.splitext(filename)[0]+'.xml'
+ copyfile(os.path.join(source, xml_filename),
+ os.path.join(test_dir,xml_filename))
+ images.remove(images[idx])
+
+ for filename in images:
+ copyfile(os.path.join(source, filename),
+ os.path.join(train_dir, filename))
+ if copy_xml:
+ xml_filename = os.path.splitext(filename)[0]+'.xml'
+ copyfile(os.path.join(source, xml_filename),
+ os.path.join(train_dir, xml_filename))
+
+
+def main():
+
+ # Initiate argument parser
+ parser = argparse.ArgumentParser(description="Partition dataset of images into training and testing sets",
+ formatter_class=argparse.RawTextHelpFormatter)
+ parser.add_argument(
+ '-i', '--imageDir',
+ help='Path to the folder where the image dataset is stored. If not specified, the CWD will be used.',
+ type=str,
+ default=os.getcwd()
+ )
+ parser.add_argument(
+ '-o', '--outputDir',
+ help='Path to the output folder where the train and test dirs should be created. '
+ 'Defaults to the same directory as IMAGEDIR.',
+ type=str,
+ default=None
+ )
+ parser.add_argument(
+ '-r', '--ratio',
+ help='The ratio of the number of test images over the total number of images. The default is 0.1.',
+ default=0.1,
+ type=float)
+ parser.add_argument(
+ '-x', '--xml',
+ help='Set this flag if you want the xml annotation files to be processed and copied over.',
+ action='store_true'
+ )
+ args = parser.parse_args()
+
+ if args.outputDir is None:
+ args.outputDir = args.imageDir
+
+ # Now we are ready to start the iteration
+ iterate_dir(args.imageDir, args.outputDir, args.ratio, args.xml)
+
+
+if __name__ == '__main__':
+ main()
\ No newline at end of file
diff --git a/docs/build/_downloads/da4babe668a8afb093cc7776d7e630f3/generate_tfrecord.py b/docs/build/_downloads/da4babe668a8afb093cc7776d7e630f3/generate_tfrecord.py
new file mode 100644
index 0000000..caad456
--- /dev/null
+++ b/docs/build/_downloads/da4babe668a8afb093cc7776d7e630f3/generate_tfrecord.py
@@ -0,0 +1,168 @@
+""" Sample TensorFlow XML-to-TFRecord converter
+
+usage: generate_tfrecord.py [-h] [-x XML_DIR] [-l LABELS_PATH] [-o OUTPUT_PATH] [-i IMAGE_DIR] [-c CSV_PATH]
+
+optional arguments:
+ -h, --help show this help message and exit
+ -x XML_DIR, --xml_dir XML_DIR
+ Path to the folder where the input .xml files are stored.
+ -l LABELS_PATH, --labels_path LABELS_PATH
+ Path to the labels (.pbtxt) file.
+ -o OUTPUT_PATH, --output_path OUTPUT_PATH
+ Path of output TFRecord (.record) file.
+ -i IMAGE_DIR, --image_dir IMAGE_DIR
+ Path to the folder where the input image files are stored. Defaults to the same directory as XML_DIR.
+ -c CSV_PATH, --csv_path CSV_PATH
+ Path of output .csv file. If none provided, then no file will be written.
+"""
+
+import os
+import glob
+import pandas as pd
+import io
+import xml.etree.ElementTree as ET
+import argparse
+
+os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging (1)
+import tensorflow.compat.v1 as tf
+from PIL import Image
+from object_detection.utils import dataset_util, label_map_util
+from collections import namedtuple
+
+# Initiate argument parser
+parser = argparse.ArgumentParser(
+ description="Sample TensorFlow XML-to-TFRecord converter")
+parser.add_argument("-x",
+ "--xml_dir",
+ help="Path to the folder where the input .xml files are stored.",
+ type=str)
+parser.add_argument("-l",
+ "--labels_path",
+ help="Path to the labels (.pbtxt) file.", type=str)
+parser.add_argument("-o",
+ "--output_path",
+ help="Path of output TFRecord (.record) file.", type=str)
+parser.add_argument("-i",
+ "--image_dir",
+ help="Path to the folder where the input image files are stored. "
+ "Defaults to the same directory as XML_DIR.",
+ type=str, default=None)
+parser.add_argument("-c",
+ "--csv_path",
+ help="Path of output .csv file. If none provided, then no file will be "
+ "written.",
+ type=str, default=None)
+
+args = parser.parse_args()
+
+if args.image_dir is None:
+ args.image_dir = args.xml_dir
+
+label_map = label_map_util.load_labelmap(args.labels_path)
+label_map_dict = label_map_util.get_label_map_dict(label_map)
+
+
+def xml_to_csv(path):
+ """Iterates through all .xml files (generated by labelImg) in a given directory and combines
+ them in a single Pandas dataframe.
+
+ Parameters:
+ ----------
+ path : str
+ The path containing the .xml files
+ Returns
+ -------
+ Pandas DataFrame
+ The produced dataframe
+ """
+
+ xml_list = []
+ for xml_file in glob.glob(path + '/*.xml'):
+ tree = ET.parse(xml_file)
+ root = tree.getroot()
+ for member in root.findall('object'):
+ value = (root.find('filename').text,
+ int(root.find('size')[0].text),
+ int(root.find('size')[1].text),
+ member[0].text,
+ int(member[4][0].text),
+ int(member[4][1].text),
+ int(member[4][2].text),
+ int(member[4][3].text)
+ )
+ xml_list.append(value)
+ column_name = ['filename', 'width', 'height',
+ 'class', 'xmin', 'ymin', 'xmax', 'ymax']
+ xml_df = pd.DataFrame(xml_list, columns=column_name)
+ return xml_df
+
+
+def class_text_to_int(row_label):
+ return label_map_dict[row_label]
+
+
+def split(df, group):
+ data = namedtuple('data', ['filename', 'object'])
+ gb = df.groupby(group)
+ return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
+
+
+def create_tf_example(group, path):
+ with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
+ encoded_jpg = fid.read()
+ encoded_jpg_io = io.BytesIO(encoded_jpg)
+ image = Image.open(encoded_jpg_io)
+ width, height = image.size
+
+ filename = group.filename.encode('utf8')
+ image_format = b'jpg'
+ xmins = []
+ xmaxs = []
+ ymins = []
+ ymaxs = []
+ classes_text = []
+ classes = []
+
+ for index, row in group.object.iterrows():
+ xmins.append(row['xmin'] / width)
+ xmaxs.append(row['xmax'] / width)
+ ymins.append(row['ymin'] / height)
+ ymaxs.append(row['ymax'] / height)
+ classes_text.append(row['class'].encode('utf8'))
+ classes.append(class_text_to_int(row['class']))
+
+ tf_example = tf.train.Example(features=tf.train.Features(feature={
+ 'image/height': dataset_util.int64_feature(height),
+ 'image/width': dataset_util.int64_feature(width),
+ 'image/filename': dataset_util.bytes_feature(filename),
+ 'image/source_id': dataset_util.bytes_feature(filename),
+ 'image/encoded': dataset_util.bytes_feature(encoded_jpg),
+ 'image/format': dataset_util.bytes_feature(image_format),
+ 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
+ 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
+ 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
+ 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
+ 'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
+ 'image/object/class/label': dataset_util.int64_list_feature(classes),
+ }))
+ return tf_example
+
+
+def main(_):
+
+ writer = tf.python_io.TFRecordWriter(args.output_path)
+ path = os.path.join(args.image_dir)
+ examples = xml_to_csv(args.xml_dir)
+ grouped = split(examples, 'filename')
+ for group in grouped:
+ tf_example = create_tf_example(group, path)
+ writer.write(tf_example.SerializeToString())
+ writer.close()
+ print('Successfully created the TFRecord file: {}'.format(args.output_path))
+ if args.csv_path is not None:
+ examples.to_csv(args.csv_path, index=None)
+ print('Successfully created the CSV file: {}'.format(args.csv_path))
+
+
+if __name__ == '__main__':
+ tf.app.run()
diff --git a/docs/build/_images/object_detection_tutorial_err.PNG b/docs/build/_images/object_detection_tutorial_err.PNG
deleted file mode 100644
index 1e1255a..0000000
Binary files a/docs/build/_images/object_detection_tutorial_err.PNG and /dev/null differ
diff --git a/docs/build/_images/object_detection_tutorial_output.PNG b/docs/build/_images/object_detection_tutorial_output.PNG
deleted file mode 100644
index dea977a..0000000
Binary files a/docs/build/_images/object_detection_tutorial_output.PNG and /dev/null differ
diff --git a/docs/build/_images/sphx_glr_object_detection_camera_thumb.png b/docs/build/_images/sphx_glr_object_detection_camera_thumb.png
new file mode 100644
index 0000000..233f8e6
Binary files /dev/null and b/docs/build/_images/sphx_glr_object_detection_camera_thumb.png differ
diff --git a/docs/build/_images/sphx_glr_plot_object_detection_simple_001.png b/docs/build/_images/sphx_glr_plot_object_detection_simple_001.png
new file mode 100644
index 0000000..24c9bbe
Binary files /dev/null and b/docs/build/_images/sphx_glr_plot_object_detection_simple_001.png differ
diff --git a/docs/build/_images/sphx_glr_plot_object_detection_simple_002.png b/docs/build/_images/sphx_glr_plot_object_detection_simple_002.png
new file mode 100644
index 0000000..ed422b7
Binary files /dev/null and b/docs/build/_images/sphx_glr_plot_object_detection_simple_002.png differ
diff --git a/docs/build/_images/sphx_glr_plot_object_detection_simple_thumb.png b/docs/build/_images/sphx_glr_plot_object_detection_simple_thumb.png
new file mode 100644
index 0000000..40fcf69
Binary files /dev/null and b/docs/build/_images/sphx_glr_plot_object_detection_simple_thumb.png differ
diff --git a/docs/build/_sources/auto_examples/index.rst.txt b/docs/build/_sources/auto_examples/index.rst.txt
new file mode 100644
index 0000000..d835a74
--- /dev/null
+++ b/docs/build/_sources/auto_examples/index.rst.txt
@@ -0,0 +1,83 @@
+:orphan:
+
+
+
+.. _sphx_glr_auto_examples:
+
+.. _examples:
+
+Examples
+========
+
+Below is a gallery of examples
+
+
+.. raw:: html
+
+
+
+.. only:: html
+
+ .. figure:: /auto_examples/images/thumb/sphx_glr_object_detection_camera_thumb.png
+ :alt: Detect Objects Using Your Webcam
+
+ :ref:`sphx_glr_auto_examples_object_detection_camera.py`
+
+.. raw:: html
+
+
+
+.. only:: html
+
+ .. figure:: /auto_examples/images/thumb/sphx_glr_plot_object_detection_simple_thumb.png
+ :alt: Object Detection Test
+
+ :ref:`sphx_glr_auto_examples_plot_object_detection_simple.py`
+
+.. raw:: html
+
+
+
+
+.. toctree::
+ :hidden:
+
+ /auto_examples/plot_object_detection_simple
+.. raw:: html
+
+
+
+
+
+.. only :: html
+
+ .. container:: sphx-glr-footer
+ :class: sphx-glr-footer-gallery
+
+
+ .. container:: sphx-glr-download sphx-glr-download-python
+
+ :download:`Download all examples in Python source code: auto_examples_python.zip `
+
+
+
+ .. container:: sphx-glr-download sphx-glr-download-jupyter
+
+ :download:`Download all examples in Jupyter notebooks: auto_examples_jupyter.zip `
+
+
+.. only:: html
+
+ .. rst-class:: sphx-glr-signature
+
+ `Gallery generated by Sphinx-Gallery `_
diff --git a/docs/build/_sources/auto_examples/object_detection_camera.rst.txt b/docs/build/_sources/auto_examples/object_detection_camera.rst.txt
new file mode 100644
index 0000000..655a07a
--- /dev/null
+++ b/docs/build/_sources/auto_examples/object_detection_camera.rst.txt
@@ -0,0 +1,257 @@
+.. only:: html
+
+ .. note::
+ :class: sphx-glr-download-link-note
+
+ Click :ref:`here ` to download the full example code
+ .. rst-class:: sphx-glr-example-title
+
+ .. _sphx_glr_auto_examples_object_detection_camera.py:
+
+
+Detect Objects Using Your Webcam
+================================
+
+This demo will take you through the steps of running an "out-of-the-box" detection model to
+detect objects in the video stream extracted from your camera.
+
+Create the data directory
+~~~~~~~~~~~~~~~~~~~~~~~~~
+The snippet shown below will create the ``data`` directory where all our data will be stored. The
+code will create a directory structure as shown bellow:
+
+.. code-block:: bash
+
+ data
+ └── models
+
+where the ``models`` folder will will contain the downloaded models.
+
+
+.. code-block:: default
+
+ import os
+
+ DATA_DIR = os.path.join(os.getcwd(), 'data')
+ MODELS_DIR = os.path.join(DATA_DIR, 'models')
+ for dir in [DATA_DIR, MODELS_DIR]:
+ if not os.path.exists(dir):
+ os.mkdir(dir)
+
+
+Download the model
+~~~~~~~~~~~~~~~~~~
+The code snippet shown below is used to download the object detection model checkpoint file,
+as well as the labels file (.pbtxt) which contains a list of strings used to add the correct
+label to each detection (e.g. person).
+
+The particular detection algorithm we will use is the `SSD ResNet101 V1 FPN 640x640`. More
+models can be found in the `TensorFlow 2 Detection Model Zoo `_.
+To use a different model you will need the URL name of the specific model. This can be done as
+follows:
+
+1. Right click on the `Model name` of the model you would like to use;
+2. Click on `Copy link address` to copy the download link of the model;
+3. Paste the link in a text editor of your choice. You should observe a link similar to ``download.tensorflow.org/models/object_detection/tf2/YYYYYYYY/XXXXXXXXX.tar.gz``;
+4. Copy the ``XXXXXXXXX`` part of the link and use it to replace the value of the ``MODEL_NAME`` variable in the code shown below;
+5. Copy the ``YYYYYYYY`` part of the link and use it to replace the value of the ``MODEL_DATE`` variable in the code shown below.
+
+For example, the download link for the model used below is: ``download.tensorflow.org/models/object_detection/tf2/20200711/ssd_resnet101_v1_fpn_640x640_coco17_tpu-8.tar.gz``
+
+
+.. code-block:: default
+
+ import tarfile
+ import urllib.request
+
+ # Download and extract model
+ MODEL_DATE = '20200711'
+ MODEL_NAME = 'ssd_resnet101_v1_fpn_640x640_coco17_tpu-8'
+ MODEL_TAR_FILENAME = MODEL_NAME + '.tar.gz'
+ MODELS_DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/tf2/'
+ MODEL_DOWNLOAD_LINK = MODELS_DOWNLOAD_BASE + MODEL_DATE + '/' + MODEL_TAR_FILENAME
+ PATH_TO_MODEL_TAR = os.path.join(MODELS_DIR, MODEL_TAR_FILENAME)
+ PATH_TO_CKPT = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, 'checkpoint/'))
+ PATH_TO_CFG = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, 'pipeline.config'))
+ if not os.path.exists(PATH_TO_CKPT):
+ print('Downloading model. This may take a while... ', end='')
+ urllib.request.urlretrieve(MODEL_DOWNLOAD_LINK, PATH_TO_MODEL_TAR)
+ tar_file = tarfile.open(PATH_TO_MODEL_TAR)
+ tar_file.extractall(MODELS_DIR)
+ tar_file.close()
+ os.remove(PATH_TO_MODEL_TAR)
+ print('Done')
+
+ # Download labels file
+ LABEL_FILENAME = 'mscoco_label_map.pbtxt'
+ LABELS_DOWNLOAD_BASE = \
+ 'https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/data/'
+ PATH_TO_LABELS = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, LABEL_FILENAME))
+ if not os.path.exists(PATH_TO_LABELS):
+ print('Downloading label file... ', end='')
+ urllib.request.urlretrieve(LABELS_DOWNLOAD_BASE + LABEL_FILENAME, PATH_TO_LABELS)
+ print('Done')
+
+
+Load the model
+~~~~~~~~~~~~~~
+Next we load the downloaded model
+
+
+.. code-block:: default
+
+
+ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging
+ import tensorflow as tf
+ from object_detection.utils import label_map_util
+ from object_detection.utils import config_util
+ from object_detection.utils import visualization_utils as viz_utils
+ from object_detection.builders import model_builder
+
+ tf.get_logger().setLevel('ERROR') # Suppress TensorFlow logging (2)
+
+ # Enable GPU dynamic memory allocation
+ gpus = tf.config.experimental.list_physical_devices('GPU')
+ for gpu in gpus:
+ tf.config.experimental.set_memory_growth(gpu, True)
+
+ # Load pipeline config and build a detection model
+ configs = config_util.get_configs_from_pipeline_file(PATH_TO_CFG)
+ model_config = configs['model']
+ detection_model = model_builder.build(model_config=model_config, is_training=False)
+
+ # Restore checkpoint
+ ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
+ ckpt.restore(os.path.join(PATH_TO_CKPT, 'ckpt-0')).expect_partial()
+
+ @tf.function
+ def detect_fn(image):
+ """Detect objects in image."""
+
+ image, shapes = detection_model.preprocess(image)
+ prediction_dict = detection_model.predict(image, shapes)
+ detections = detection_model.postprocess(prediction_dict, shapes)
+
+ return detections, prediction_dict, tf.reshape(shapes, [-1])
+
+
+
+Load label map data (for plotting)
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Label maps correspond index numbers to category names, so that when our convolution network
+predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility
+functions, but anything that returns a dictionary mapping integers to appropriate string labels
+would be fine.
+
+
+.. code-block:: default
+
+ category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,
+ use_display_name=True)
+
+
+Define the video stream
+~~~~~~~~~~~~~~~~~~~~~~~
+We will use `OpenCV `_ to capture the video stream
+generated by our webcam. For more information you can refer to the `OpenCV-Python Tutorials `_
+
+
+.. code-block:: default
+
+ import cv2
+
+ cap = cv2.VideoCapture(0)
+
+
+Putting everything together
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+The code shown below loads an image, runs it through the detection model and visualizes the
+detection results, including the keypoints.
+
+Note that this will take a long time (several minutes) the first time you run this code due to
+tf.function's trace-compilation --- on subsequent runs (e.g. on new images), things will be
+faster.
+
+Here are some simple things to try out if you are curious:
+
+* Modify some of the input images and see if detection still works. Some simple things to try out here (just uncomment the relevant portions of code) include flipping the image horizontally, or converting to grayscale (note that we still expect the input image to have 3 channels).
+* Print out `detections['detection_boxes']` and try to match the box locations to the boxes in the image. Notice that coordinates are given in normalized form (i.e., in the interval [0, 1]).
+* Set ``min_score_thresh`` to other values (between 0 and 1) to allow more detections in or to filter out more detections.
+
+
+.. code-block:: default
+
+ import numpy as np
+
+ while True:
+ # Read frame from camera
+ ret, image_np = cap.read()
+
+ # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
+ image_np_expanded = np.expand_dims(image_np, axis=0)
+
+ # Things to try:
+ # Flip horizontally
+ # image_np = np.fliplr(image_np).copy()
+
+ # Convert image to grayscale
+ # image_np = np.tile(
+ # np.mean(image_np, 2, keepdims=True), (1, 1, 3)).astype(np.uint8)
+
+ input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
+ detections, predictions_dict, shapes = detect_fn(input_tensor)
+
+ label_id_offset = 1
+ image_np_with_detections = image_np.copy()
+
+ viz_utils.visualize_boxes_and_labels_on_image_array(
+ image_np_with_detections,
+ detections['detection_boxes'][0].numpy(),
+ (detections['detection_classes'][0].numpy() + label_id_offset).astype(int),
+ detections['detection_scores'][0].numpy(),
+ category_index,
+ use_normalized_coordinates=True,
+ max_boxes_to_draw=200,
+ min_score_thresh=.30,
+ agnostic_mode=False)
+
+ # Display output
+ cv2.imshow('object detection', cv2.resize(image_np_with_detections, (800, 600)))
+
+ if cv2.waitKey(25) & 0xFF == ord('q'):
+ break
+
+ cap.release()
+ cv2.destroyAllWindows()
+
+.. rst-class:: sphx-glr-timing
+
+ **Total running time of the script:** ( 0 minutes 0.000 seconds)
+
+
+.. _sphx_glr_download_auto_examples_object_detection_camera.py:
+
+
+.. only :: html
+
+ .. container:: sphx-glr-footer
+ :class: sphx-glr-footer-example
+
+
+
+ .. container:: sphx-glr-download sphx-glr-download-python
+
+ :download:`Download Python source code: object_detection_camera.py `
+
+
+
+ .. container:: sphx-glr-download sphx-glr-download-jupyter
+
+ :download:`Download Jupyter notebook: object_detection_camera.ipynb `
+
+
+.. only:: html
+
+ .. rst-class:: sphx-glr-signature
+
+ `Gallery generated by Sphinx-Gallery `_
diff --git a/docs/build/_sources/auto_examples/plot_object_detection_simple.rst.txt b/docs/build/_sources/auto_examples/plot_object_detection_simple.rst.txt
new file mode 100644
index 0000000..cc6818a
--- /dev/null
+++ b/docs/build/_sources/auto_examples/plot_object_detection_simple.rst.txt
@@ -0,0 +1,360 @@
+.. only:: html
+
+ .. note::
+ :class: sphx-glr-download-link-note
+
+ Click :ref:`here ` to download the full example code
+ .. rst-class:: sphx-glr-example-title
+
+ .. _sphx_glr_auto_examples_plot_object_detection_simple.py:
+
+
+Object Detection Test
+=====================
+
+This demo will take you through the steps of running an "out-of-the-box" detection model on a
+collection of images.
+
+Create the data directory
+~~~~~~~~~~~~~~~~~~~~~~~~~
+The snippet shown below will create the ``data`` directory where all our data will be stored. The
+code will create a directory structure as shown bellow:
+
+.. code-block:: bash
+
+ data
+ ├── images
+ └── models
+
+where the ``images`` folder will contain the downlaoded test images, while ``models`` will
+contain the downloaded models.
+
+
+.. code-block:: default
+
+ import os
+
+ DATA_DIR = os.path.join(os.getcwd(), 'data')
+ IMAGES_DIR = os.path.join(DATA_DIR, 'images')
+ MODELS_DIR = os.path.join(DATA_DIR, 'models')
+ for dir in [DATA_DIR, IMAGES_DIR, MODELS_DIR]:
+ if not os.path.exists(dir):
+ os.mkdir(dir)
+
+
+
+
+
+
+
+
+Download the test images
+~~~~~~~~~~~~~~~~~~~~~~~~
+First we will download the images that we will use throughout this tutorial. The code snippet
+shown bellow will download the test images from the `TensorFlow Model Garden `_
+and save them inside the ``data/images`` folder.
+
+
+.. code-block:: default
+
+ import urllib.request
+
+ IMAGE_FILENAMES = ['image1.jpg', 'image2.jpg']
+ IMAGES_DOWNLOAD_BASE = \
+ 'https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/test_images/'
+
+ for image_filename in IMAGE_FILENAMES:
+
+ image_path = os.path.join(IMAGES_DIR, image_filename)
+
+ # Download image
+ if not os.path.exists(image_path):
+ print('Downloading {}... '.format(image_filename), end='')
+ urllib.request.urlretrieve(IMAGES_DOWNLOAD_BASE + image_filename, image_path)
+ print('Done')
+
+
+
+
+
+
+
+
+
+Download the model
+~~~~~~~~~~~~~~~~~~
+The code snippet shown below is used to download the object detection model checkpoint file,
+as well as the labels file (.pbtxt) which contains a list of strings used to add the correct
+label to each detection (e.g. person). Once downloaded the files will be stored under the
+``data/models`` folder.
+
+The particular detection algorithm we will use is the `CenterNet HourGlass104 1024x1024`. More
+models can be found in the `TensorFlow 2 Detection Model Zoo `_.
+To use a different model you will need the URL name of the specific model. This can be done as
+follows:
+
+1. Right click on the `Model name` of the model you would like to use;
+2. Click on `Copy link address` to copy the download link of the model;
+3. Paste the link in a text editor of your choice. You should observe a link similar to ``download.tensorflow.org/models/object_detection/tf2/YYYYYYYY/XXXXXXXXX.tar.gz``;
+4. Copy the ``XXXXXXXXX`` part of the link and use it to replace the value of the ``MODEL_NAME`` variable in the code shown below;
+5. Copy the ``YYYYYYYY`` part of the link and use it to replace the value of the ``MODEL_DATE`` variable in the code shown below.
+
+For example, the download link for the model used below is: ``download.tensorflow.org/models/object_detection/tf2/20200711/centernet_hg104_1024x1024_coco17_tpu-32.tar.gz``
+
+
+.. code-block:: default
+
+
+ import tarfile
+
+ # Download and extract model
+ MODEL_DATE = '20200711'
+ MODEL_NAME = 'centernet_hg104_1024x1024_coco17_tpu-32'
+ MODEL_TAR_FILENAME = MODEL_NAME + '.tar.gz'
+ MODELS_DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/tf2/'
+ MODEL_DOWNLOAD_LINK = MODELS_DOWNLOAD_BASE + MODEL_DATE + '/' + MODEL_TAR_FILENAME
+ PATH_TO_MODEL_TAR = os.path.join(MODELS_DIR, MODEL_TAR_FILENAME)
+ PATH_TO_CKPT = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, 'checkpoint/'))
+ PATH_TO_CFG = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, 'pipeline.config'))
+ if not os.path.exists(PATH_TO_CKPT):
+ print('Downloading model. This may take a while... ', end='')
+ urllib.request.urlretrieve(MODEL_DOWNLOAD_LINK, PATH_TO_MODEL_TAR)
+ tar_file = tarfile.open(PATH_TO_MODEL_TAR)
+ tar_file.extractall(MODELS_DIR)
+ tar_file.close()
+ os.remove(PATH_TO_MODEL_TAR)
+ print('Done')
+
+ # Download labels file
+ LABEL_FILENAME = 'mscoco_label_map.pbtxt'
+ LABELS_DOWNLOAD_BASE = \
+ 'https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/data/'
+ PATH_TO_LABELS = os.path.join(MODELS_DIR, os.path.join(MODEL_NAME, LABEL_FILENAME))
+ if not os.path.exists(PATH_TO_LABELS):
+ print('Downloading label file... ', end='')
+ urllib.request.urlretrieve(LABELS_DOWNLOAD_BASE + LABEL_FILENAME, PATH_TO_LABELS)
+ print('Done')
+
+
+
+
+
+
+
+
+Load the model
+~~~~~~~~~~~~~~
+Next we load the downloaded model
+
+
+.. code-block:: default
+
+ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging (1)
+ import tensorflow as tf
+ from object_detection.utils import label_map_util
+ from object_detection.utils import config_util
+ from object_detection.utils import visualization_utils as viz_utils
+ from object_detection.builders import model_builder
+
+ tf.get_logger().setLevel('ERROR') # Suppress TensorFlow logging (2)
+
+ # Enable GPU dynamic memory allocation
+ gpus = tf.config.experimental.list_physical_devices('GPU')
+ for gpu in gpus:
+ tf.config.experimental.set_memory_growth(gpu, True)
+
+ # Load pipeline config and build a detection model
+ configs = config_util.get_configs_from_pipeline_file(PATH_TO_CFG)
+ model_config = configs['model']
+ detection_model = model_builder.build(model_config=model_config, is_training=False)
+
+ # Restore checkpoint
+ ckpt = tf.compat.v2.train.Checkpoint(
+ model=detection_model)
+ ckpt.restore(os.path.join(PATH_TO_CKPT, 'ckpt-0')).expect_partial()
+
+ @tf.function
+ def detect_fn(image):
+ """Detect objects in image."""
+
+ image, shapes = detection_model.preprocess(image)
+ prediction_dict = detection_model.predict(image, shapes)
+ detections = detection_model.postprocess(prediction_dict, shapes)
+
+ return detections, prediction_dict, tf.reshape(shapes, [-1])
+
+
+
+
+
+
+
+
+
+Load label map data (for plotting)
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Label maps correspond index numbers to category names, so that when our convolution network
+predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility
+functions, but anything that returns a dictionary mapping integers to appropriate string labels
+would be fine.
+
+
+.. code-block:: default
+
+
+ category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS,
+ use_display_name=True)
+
+
+
+
+
+
+
+
+Putting everything together
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+The code shown below loads an image, runs it through the detection model and visualizes the
+detection results, including the keypoints.
+
+Note that this will take a long time (several minutes) the first time you run this code due to
+tf.function's trace-compilation --- on subsequent runs (e.g. on new images), things will be
+faster.
+
+Here are some simple things to try out if you are curious:
+
+* Modify some of the input images and see if detection still works. Some simple things to try out here (just uncomment the relevant portions of code) include flipping the image horizontally, or converting to grayscale (note that we still expect the input image to have 3 channels).
+* Print out `detections['detection_boxes']` and try to match the box locations to the boxes in the image. Notice that coordinates are given in normalized form (i.e., in the interval [0, 1]).
+* Set ``min_score_thresh`` to other values (between 0 and 1) to allow more detections in or to filter out more detections.
+
+
+.. code-block:: default
+
+ import numpy as np
+ from six import BytesIO
+ from PIL import Image
+ import matplotlib.pyplot as plt
+ import warnings
+ warnings.filterwarnings('ignore') # Suppress Matplotlib warnings
+
+ def load_image_into_numpy_array(path):
+ """Load an image from file into a numpy array.
+
+ Puts image into numpy array to feed into tensorflow graph.
+ Note that by convention we put it into a numpy array with shape
+ (height, width, channels), where channels=3 for RGB.
+
+ Args:
+ path: the file path to the image
+
+ Returns:
+ uint8 numpy array with shape (img_height, img_width, 3)
+ """
+ img_data = tf.io.gfile.GFile(path, 'rb').read()
+ image = Image.open(BytesIO(img_data))
+ (im_width, im_height) = image.size
+ return np.array(image.getdata()).reshape(
+ (im_height, im_width, 3)).astype(np.uint8)
+
+
+ for image_filename in IMAGE_FILENAMES:
+
+ print('Running inference for {}... '.format(image_filename), end='')
+
+ image_path = os.path.join(IMAGES_DIR, image_filename)
+ image_np = load_image_into_numpy_array(image_path)
+
+ # Things to try:
+ # Flip horizontally
+ # image_np = np.fliplr(image_np).copy()
+
+ # Convert image to grayscale
+ # image_np = np.tile(
+ # np.mean(image_np, 2, keepdims=True), (1, 1, 3)).astype(np.uint8)
+
+ input_tensor = tf.convert_to_tensor(
+ np.expand_dims(image_np, 0), dtype=tf.float32)
+ detections, predictions_dict, shapes = detect_fn(input_tensor)
+
+ label_id_offset = 1
+ image_np_with_detections = image_np.copy()
+
+ viz_utils.visualize_boxes_and_labels_on_image_array(
+ image_np_with_detections,
+ detections['detection_boxes'][0].numpy(),
+ (detections['detection_classes'][0].numpy() + label_id_offset).astype(int),
+ detections['detection_scores'][0].numpy(),
+ category_index,
+ use_normalized_coordinates=True,
+ max_boxes_to_draw=200,
+ min_score_thresh=.30,
+ agnostic_mode=False)
+
+ plt.figure()
+ plt.imshow(image_np_with_detections)
+ print('Done')
+ plt.show()
+
+ # sphinx_gallery_thumbnail_number = 2
+
+
+.. rst-class:: sphx-glr-horizontal
+
+
+ *
+
+ .. image:: /auto_examples/images/sphx_glr_plot_object_detection_simple_001.png
+ :alt: plot object detection simple
+ :class: sphx-glr-multi-img
+
+ *
+
+ .. image:: /auto_examples/images/sphx_glr_plot_object_detection_simple_002.png
+ :alt: plot object detection simple
+ :class: sphx-glr-multi-img
+
+
+.. rst-class:: sphx-glr-script-out
+
+ Out:
+
+ .. code-block:: none
+
+ Running inference for image1.jpg... Done
+ Running inference for image2.jpg... Done
+
+
+
+
+
+.. rst-class:: sphx-glr-timing
+
+ **Total running time of the script:** ( 0 minutes 31.329 seconds)
+
+
+.. _sphx_glr_download_auto_examples_plot_object_detection_simple.py:
+
+
+.. only :: html
+
+ .. container:: sphx-glr-footer
+ :class: sphx-glr-footer-example
+
+
+
+ .. container:: sphx-glr-download sphx-glr-download-python
+
+ :download:`Download Python source code: plot_object_detection_simple.py `
+
+
+
+ .. container:: sphx-glr-download sphx-glr-download-jupyter
+
+ :download:`Download Jupyter notebook: plot_object_detection_simple.ipynb `
+
+
+.. only:: html
+
+ .. rst-class:: sphx-glr-signature
+
+ `Gallery generated by Sphinx-Gallery `_
diff --git a/docs/build/_sources/auto_examples/sg_execution_times.rst.txt b/docs/build/_sources/auto_examples/sg_execution_times.rst.txt
new file mode 100644
index 0000000..87d5177
--- /dev/null
+++ b/docs/build/_sources/auto_examples/sg_execution_times.rst.txt
@@ -0,0 +1,14 @@
+
+:orphan:
+
+.. _sphx_glr_auto_examples_sg_execution_times:
+
+Computation times
+=================
+**00:31.329** total execution time for **auto_examples** files:
+
++-----------------------------------------------------------------------------------------------------+-----------+--------+
+| :ref:`sphx_glr_auto_examples_plot_object_detection_simple.py` (``plot_object_detection_simple.py``) | 00:31.329 | 0.0 MB |
++-----------------------------------------------------------------------------------------------------+-----------+--------+
+| :ref:`sphx_glr_auto_examples_object_detection_camera.py` (``object_detection_camera.py``) | 00:00.000 | 0.0 MB |
++-----------------------------------------------------------------------------------------------------+-----------+--------+
diff --git a/docs/build/_sources/camera.rst.txt b/docs/build/_sources/camera.rst.txt
deleted file mode 100644
index 5a1adca..0000000
--- a/docs/build/_sources/camera.rst.txt
+++ /dev/null
@@ -1,119 +0,0 @@
-Detect Objects Using Your Webcam
-===============================
-
-Hereby you can find an example which allows you to use your camera to generate a video stream, based on which you can perform object_detection.
-
-To run the example, simply create a new file under ``/TensorFlow/models/research/object_detection`` and paste the code below.
-
-.. code-block:: python
-
- import numpy as np
- import os
- import six.moves.urllib as urllib
- import sys
- import tarfile
- import tensorflow as tf
- import zipfile
- import cv2
-
- from collections import defaultdict
- from io import StringIO
- from matplotlib import pyplot as plt
- from PIL import Image
- from object_detection.utils import label_map_util
- from object_detection.utils import visualization_utils as vis_util
-
- # Define the video stream
- cap = cv2.VideoCapture(0) # Change only if you have more than one webcams
-
- # What model to download.
- # Models can bee found here: https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
- MODEL_NAME = 'ssd_inception_v2_coco_2017_11_17'
- MODEL_FILE = MODEL_NAME + '.tar.gz'
- DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
-
- # Path to frozen detection graph. This is the actual model that is used for the object detection.
- PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
-
- # List of the strings that is used to add correct label for each box.
- PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
-
- # Number of classes to detect
- NUM_CLASSES = 90
-
- # Download Model
- if not os.path.exists(os.path.join(os.getcwd(), MODEL_FILE)):
- print("Downloading model")
- opener = urllib.request.URLopener()
- opener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)
- tar_file = tarfile.open(MODEL_FILE)
- for file in tar_file.getmembers():
- file_name = os.path.basename(file.name)
- if 'frozen_inference_graph.pb' in file_name:
- tar_file.extract(file, os.getcwd())
-
-
- # Load a (frozen) Tensorflow model into memory.
- detection_graph = tf.Graph()
- with detection_graph.as_default():
- od_graph_def = tf.compat.v1.GraphDef()
- with tf.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
- serialized_graph = fid.read()
- od_graph_def.ParseFromString(serialized_graph)
- tf.import_graph_def(od_graph_def, name='')
-
-
- # Loading label map
- # Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine
- label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
- categories = label_map_util.convert_label_map_to_categories(
- label_map, max_num_classes=NUM_CLASSES, use_display_name=True)
- category_index = label_map_util.create_category_index(categories)
-
-
- # Helper code
- def load_image_into_numpy_array(image):
- (im_width, im_height) = image.size
- return np.array(image.getdata()).reshape(
- (im_height, im_width, 3)).astype(np.uint8)
-
-
- # Detection
- with detection_graph.as_default():
- with tf.compat.v1.Session(graph=detection_graph) as sess:
- while True:
- # Read frame from camera
- ret, image_np = cap.read()
- # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
- image_np_expanded = np.expand_dims(image_np, axis=0)
- # Extract image tensor
- image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
- # Extract detection boxes
- boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
- # Extract detection scores
- scores = detection_graph.get_tensor_by_name('detection_scores:0')
- # Extract detection classes
- classes = detection_graph.get_tensor_by_name('detection_classes:0')
- # Extract number of detectionsd
- num_detections = detection_graph.get_tensor_by_name(
- 'num_detections:0')
- # Actual detection.
- (boxes, scores, classes, num_detections) = sess.run(
- [boxes, scores, classes, num_detections],
- feed_dict={image_tensor: image_np_expanded})
- # Visualization of the results of a detection.
- vis_util.visualize_boxes_and_labels_on_image_array(
- image_np,
- np.squeeze(boxes),
- np.squeeze(classes).astype(np.int32),
- np.squeeze(scores),
- category_index,
- use_normalized_coordinates=True,
- line_thickness=8)
-
- # Display output
- cv2.imshow('object detection', cv2.resize(image_np, (800, 600)))
-
- if cv2.waitKey(25) & 0xFF == ord('q'):
- cv2.destroyAllWindows()
- break
diff --git a/docs/build/_sources/index.rst.txt b/docs/build/_sources/index.rst.txt
index bf6eb21..7dd7df2 100644
--- a/docs/build/_sources/index.rst.txt
+++ b/docs/build/_sources/index.rst.txt
@@ -3,16 +3,12 @@
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
-TensorFlow Object Detection API tutorial
-============================================
+TensorFlow 2 Object Detection API tutorial
+==========================================
-.. important:: This tutorial is intended for TensorFlow 1.14, which (at the time of writing this tutorial) is the latest stable version before TensorFlow 2.x.
+.. important:: This tutorial is intended for TensorFlow 2.2, which (at the time of writing this tutorial) is the latest stable version of TensorFlow 2.x.
- Tensorflow 1.15 has also been released, but seems to be exhibiting `instability issues `_.
-
- A version for TensorFlow 1.9 can be found `here `_.
-
- At the time of righting this tutorial, Object Detection model training and evaluation was not migrated to TensorFlow 2.x (see `here `_). From personal tests, it seems that detection using pre-trained models works, however it is not yet possible to train and evaluate models. Once the migration has been completed, a version for TensorFlow 2.x will be produced.
+ A version for TensorFlow 1.14 can be found `here `_.
This is a step-by-step tutorial/guide to setting up and using TensorFlow's Object Detection API to perform, namely, object detection in images/video.
@@ -23,11 +19,11 @@ The software tools which we shall use throughout this tutorial are listed in the
+==============+==============================+
| OS | Windows, Linux |
+--------------+------------------------------+
-| Python | 3.7 |
+| Python | 3.8 |
+--------------+------------------------------+
-| TensorFlow | 1.14 |
+| TensorFlow | 2.2.0 |
+--------------+------------------------------+
-| CUDA Toolkit | 10.0 |
+| CUDA Toolkit | 10.1 |
+--------------+------------------------------+
| CuDNN | 7.6.5 |
+--------------+------------------------------+
@@ -39,8 +35,8 @@ The software tools which we shall use throughout this tutorial are listed in the
:caption: Contents:
install
- camera
training
+ auto_examples/index
issues
diff --git a/docs/build/_sources/install.rst.txt b/docs/build/_sources/install.rst.txt
index ffc788d..ab1c61e 100644
--- a/docs/build/_sources/install.rst.txt
+++ b/docs/build/_sources/install.rst.txt
@@ -4,181 +4,184 @@ Installation
General Remarks
---------------
-- There are two different variations of TensorFlow that you might wish to install, depending on whether you would like TensorFlow to run on your CPU or GPU, namely :ref:`tensorflow_cpu` and :ref:`tensorflow_gpu`. I will proceed to document both and you can choose which one you wish to install.
+- In contrast to TensorFlow 1.x, where different Python packages needed to be installed for one to run TensorFlow on either their CPU or GPU (namely ``tensorflow`` and ``tensorflow-gpu``), TensorFlow 2.x only requires that the ``tensorflow`` package is installed and automatically checks to see if a GPU can be successfully registered.
-- If you wish to install both TensorFlow variants on your machine, ideally you should install each variant under a different (virtual) environment. If you attempt to install both :ref:`tensorflow_cpu` and :ref:`tensorflow_gpu`, without making use of virtual environments, you will either end up failing, or when we later start running code there will always be an uncertainty as to which variant is being used to execute your code.
-- To ensure that we have no package conflicts and/or that we can install several different versions/variants of TensorFlow (e.g. CPU and GPU), it is generally recommended to use a virtual environment of some sort. For the purposes of this tutorial we will be creating and managing our virtual environments using Anaconda, but you are welcome to use the virtual environment manager of your choice (e.g. virtualenv).
+Anaconda Python 3.7 (Optional)
+------------------------------
-Install Anaconda Python 3.7 (Optional)
---------------------------------------
Although having Anaconda is not a requirement in order to install and use TensorFlow, I suggest doing so, due to it's intuitive way of managing packages and setting up new virtual environments. Anaconda is a pretty useful tool, not only for working with TensorFlow, but in general for anyone working in Python, so if you haven't had a chance to work with it, now is a good chance.
+Install Anaconda Python 3.7
+***************************
+
.. tabs::
.. tab:: Windows
- - Go to ``_
- - Download `Anaconda Python 3.7 version for Windows `_
- - Run the downloaded executable (``.exe``) file to begin the installation. See `here `_ for more details.
- - (Optional) In the next step, check the box "Add Anaconda to my PATH environment variable". This will make Anaconda your default Python distribution, which should ensure that you have the same default Python distribution across all editors.
+ - Go to ``_ and click the "Download" button
+ - Download the `Python 3.7 64-Bit Graphical Installer `_ or the `32-Bit Graphical Installer `_ installer, per your system requirements
+ - Run the downloaded executable (``.exe``) file to begin the installation. See `here `_ for more details
+ - (Optional) In the next step, check the box "Add Anaconda3 to my PATH environment variable". This will make Anaconda your default Python distribution, which should ensure that you have the same default Python distribution across all editors.
.. tab:: Linux
- - Go to ``_
- - Download `Anaconda Python 3.7 version for Linux `_
+ - Go to ``_ and click the "Download" button
+ - Download the `Python 3.7 64-Bit (x86) Installer `_
- Run the downloaded bash script (``.sh``) file to begin the installation. See `here `_ for more details.
- When prompted with the question "Do you wish the installer to prepend the Anaconda<2 or 3> install location to PATH in your /home//.bashrc ?", answer "Yes". If you enter "No", you must manually add the path to Anaconda or conda will not work.
-.. _tf_install:
-
-TensorFlow Installation
------------------------
-
-As mentioned in the Remarks section, there exist two generic variants of TensorFlow, which utilise different hardware on your computer to run their computationally heavy Machine Learning algorithms.
-
- 1. The simplest to install, but also in most cases the slowest in terms of performance, is :ref:`tensorflow_cpu`, which runs directly on the CPU of your machine.
- 2. Alternatively, if you own a (compatible) Nvidia graphics card, you can take advantage of the available CUDA cores to speed up the computations performed by TesnsorFlow, in which case you should follow the guidelines for installing :ref:`tensorflow_gpu`.
+Create a new Anaconda virtual environment
+*****************************************
+- Open a new `Terminal` window
+- Type the following command:
-.. _tensorflow_cpu:
+ .. code-block:: posh
-TensorFlow CPU
-~~~~~~~~~~~~~~
+ conda create -n tensorflow pip python=3.8
-Getting setup with an installation of TensorFlow CPU can be done in 3 simple steps.
+- The above will create a new virtual environment with name ``tensorflow``
.. important:: The term `Terminal` will be used to refer to the Terminal of your choice (e.g. Command Prompt, Powershell, etc.)
-Create a new Conda virtual environment (Optional)
-*************************************************
-* Open a new `Terminal` window
-* Type the following command:
+Activate the Anaconda virtual environment
+*****************************************
+- Activating the newly created virtual environment is achieved by running the following in the `Terminal` window:
.. code-block:: posh
- conda create -n tensorflow_cpu pip python=3.7
+ conda activate tensorflow
-* The above will create a new virtual environment with name ``tensorflow_cpu``
-* Now lets activate the newly created virtual environment by running the following in the `Terminal` window:
+- Once you have activated your virtual environment, the name of the environment should be displayed within brackets at the beggining of your cmd path specifier, e.g.:
- .. code-block:: posh
+ .. code-block:: ps1con
- activate tensorflow_cpu
+ (tensorflow) C:\Users\sglvladi>
-Once you have activated your virtual environment, the name of the environment should be displayed within brackets at the beggining of your cmd path specifier, e.g.:
-
-.. code-block:: ps1con
-
- (tensorflow_cpu) C:\Users\sglvladi>
+.. important::
-Install TensorFlow CPU for Python
-*********************************
-- Open a new `Terminal` window and activate the `tensorflow_cpu` environment (if you have not done so already)
-- Once open, type the following on the command line:
+ Throughout the rest of the tutorial, execution of any commands in a `Terminal` window should be done after the Anaconda virtual environment has been activated!
- .. code-block:: posh
+.. _tf_install:
- pip install --ignore-installed --upgrade tensorflow==1.14
+TensorFlow Installation
+-----------------------
-- Wait for the installation to finish
+Getting setup with an installation of TensorFlow can be done in 3 simple steps.
-Test your Installation
-**********************
-- Open a new `Terminal` window and activate the `tensorflow_cpu` environment (if you have not done so already)
-- Start a new Python interpreter session by running:
+Install the TensorFlow PIP package
+**********************************
+- Run the following command in a `Terminal` window:
.. code-block:: posh
- python
-
-- Once the interpreter opens up, type:
+ pip install --ignore-installed --upgrade tensorflow==2.2.0
- .. code-block:: python
-
- >>> import tensorflow as tf
+Verify your Installation
+************************
+- Run the following command in a `Terminal` window:
-- If the above code shows an error, then check to make sure you have activated the `tensorflow_cpu` environment and that tensorflow_cpu was successfully installed within it in the previous step.
-- Then run the following:
-
- .. code-block:: python
-
- >>> hello = tf.constant('Hello, TensorFlow!')
- >>> sess = tf.Session()
-
-- Once the above is run, if you see a print-out similar (or identical) to the one below, it means that you could benefit from installing TensorFlow by building the sources that correspond to you specific CPU. Everything should still run as normal, but potentially slower than if you had built TensorFlow from source.
-
- .. code-block:: python
+ .. code-block:: posh
- 2019-02-28 11:59:25.810663: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
+ python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
-- Finally, run the following:
+- Once the above is run, you should see a print-out similar to the one bellow:
- .. code-block:: python
+ .. code-block:: posh
- >>> print(sess.run(hello))
- b'Hello, TensorFlow!'
+ 2020-06-22 19:20:32.614181: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
+ 2020-06-22 19:20:32.620571: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
+ 2020-06-22 19:20:35.027232: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
+ 2020-06-22 19:20:35.060549: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
+ pciBusID: 0000:02:00.0 name: GeForce GTX 1070 Ti computeCapability: 6.1
+ coreClock: 1.683GHz coreCount: 19 deviceMemorySize: 8.00GiB deviceMemoryBandwidth: 238.66GiB/s
+ 2020-06-22 19:20:35.074967: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
+ 2020-06-22 19:20:35.084458: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cublas64_10.dll'; dlerror: cublas64_10.dll not found
+ 2020-06-22 19:20:35.094112: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
+ 2020-06-22 19:20:35.103571: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found
+ 2020-06-22 19:20:35.113102: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusolver64_10.dll'; dlerror: cusolver64_10.dll not found
+ 2020-06-22 19:20:35.123242: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cusparse64_10.dll'; dlerror: cusparse64_10.dll not found
+ 2020-06-22 19:20:35.140987: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
+ 2020-06-22 19:20:35.146285: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1598] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
+ Skipping registering GPU devices...
+ 2020-06-22 19:20:35.162173: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
+ 2020-06-22 19:20:35.178588: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x15140db6390 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
+ 2020-06-22 19:20:35.185082: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
+ 2020-06-22 19:20:35.191117: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
+ 2020-06-22 19:20:35.196815: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]
+ tf.Tensor(1620.5817, shape=(), dtype=float32)
.. _tensorflow_gpu:
-TensorFlow GPU
-~~~~~~~~~~~~~~
+GPU Support (Optional)
+**********************
+
+Although using a GPU to run TensorFlow is not necessary, the computational gains are substantial.
+Therefore, if your machine is equipped with a compatible CUDA-enabled GPU, it is recommended that
+you follow the steps listed below to install the relevant libraries necessary to enable TensorFlow
+to make use of your GPU.
-The installation of `TesnorFlow GPU` is slightly more involved than that of `TensorFlow CPU`, mainly due to the need of installing the relevant Graphics and CUDE drivers. There's a nice Youtube tutorial (see `here `_), explaining how to install TensorFlow GPU. Although it describes different versions of the relevant components (including TensorFlow itself), the installation steps are generally the same with this tutorial.
+By default, when TensorFlow is run it will attempt to register compatible GPU devices. If this
+fails, TensorFlow will resort to running on the platform's CPU. This can also be observed in the
+printout shown in the previous section, under the "Verify the install" bullet-point, where there
+are a number of messages which report missing library files (e.g. ``Could not load dynamic library
+'cudart64_101.dll'; dlerror: cudart64_101.dll not found``).
-Before proceeding to install TesnsorFlow GPU, you need to make sure that your system can satisfy the following requirements:
+In order for TensorFlow to run on your GPU, the following requirements must be met:
+-------------------------------------+
| Prerequisites |
+=====================================+
| Nvidia GPU (GTX 650 or newer) |
+-------------------------------------+
-| CUDA Toolkit v10.0 |
+| CUDA Toolkit v10.1 |
+-------------------------------------+
| CuDNN 7.6.5 |
-+-------------------------------------+
-| Anaconda with Python 3.7 (Optional) |
+-------------------------------------+
.. _cuda_install:
Install CUDA Toolkit
-***********************
+~~~~~~~~~~~~~~~~~~~~
.. tabs::
.. tab:: Windows
- Follow this `link `_ to download and install CUDA Toolkit 10.0.
+ - Follow this `link `_ to download and install CUDA Toolkit 10.1
+ - Installation instructions can be found `here `_
.. tab:: Linux
- Follow this `link `_ to download and install CUDA Toolkit 10.0 for your Linux distribution.
+ - Follow this `link `_ to download and install CUDA Toolkit 10.1 for your Linux distribution.
+ - Installation instructions can be found `here `_
+
.. _cudnn_install:
Install CUDNN
-****************
+~~~~~~~~~~~~~
.. tabs::
.. tab:: Windows
- Go to ``_
- Create a user profile if needed and log in
- - Select `cuDNN v7.6.5 (Nov 5, 2019), for CUDA 10.0 `_
- - Download `cuDNN v7.6.5 Library for Windows 10 `_
- - Extract the contents of the zip file (i.e. the folder named ``cuda``) inside ``\NVIDIA GPU Computing Toolkit\CUDA\v10.0\``, where ```` points to the installation directory specified during the installation of the CUDA Toolkit. By default ```` = ``C:\Program Files``.
+ - Select `cuDNN v7.6.5 (Nov 5, 2019), for CUDA 10.1 `_
+ - Download `cuDNN v7.6.5 Library for Windows 10 `_
+ - Extract the contents of the zip file (i.e. the folder named ``cuda``) inside ``\NVIDIA GPU Computing Toolkit\CUDA\v10.1\``, where ```` points to the installation directory specified during the installation of the CUDA Toolkit. By default ```` = ``C:\Program Files``.
.. tab:: Linux
- Go to ``_
- Create a user profile if needed and log in
- - Select `cuDNN v7.6.5 (Nov 5, 2019), for CUDA 10.0 `_
- - Download `cuDNN v7.6.5 Library for Linux `_
+ - Select `cuDNN v7.6.5 (Nov 5, 2019), for CUDA 10.1 `_
+ - Download `cuDNN v7.6.5 Library for Linux `_
- Follow the instructions under Section 2.3.1 of the `CuDNN Installation Guide `_ to install CuDNN.
.. _set_env:
Environment Setup
-*****************
+~~~~~~~~~~~~~~~~~
.. tabs::
.. tab:: Windows
@@ -188,184 +191,113 @@ Environment Setup
- In the opened window, click the "Environment Variables..." button to open the "Environment Variables" window.
- Under "System variables", search for and click on the ``Path`` system variable, then click "Edit..."
- Add the following paths, then click "OK" to save the changes:
-
- - ``\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin``
- - ``\NVIDIA GPU Computing Toolkit\CUDA\v10.0\libnvvp``
- - ``\NVIDIA GPU Computing Toolkit\CUDA\v10.0\extras\CUPTI\libx64``
- - ``\NVIDIA GPU Computing Toolkit\CUDA\v10.0\cuda\bin``
- .. tab:: Linux
+ - ``\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin``
+ - ``\NVIDIA GPU Computing Toolkit\CUDA\v10.1\libnvvp``
+ - ``\NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\CUPTI\libx64``
+ - ``\NVIDIA GPU Computing Toolkit\CUDA\v10.1\cuda\bin``
+
+ .. tab:: Linux
As per Section 7.1.1 of the `CUDA Installation Guide for Linux `_, append the following lines to ``~/.bashrc``:
.. code-block:: bash
# CUDA related exports
- export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}
- export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
+ export PATH=/usr/local/cuda-10.1/bin${PATH:+:${PATH}}
+ export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
Update your GPU drivers (Optional)
-**********************************
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If during the installation of the CUDA Toolkit (see :ref:`cuda_install`) you selected the `Express Installation` option, then your GPU drivers will have been overwritten by those that come bundled with the CUDA toolkit. These drivers are typically NOT the latest drivers and, thus, you may wish to updte your drivers.
- Go to ``_
- Select your GPU version to download
- Install the driver for your chosen OS
-Create a new Conda virtual environment
-**************************************
-* Open a new `Terminal` window
-* Type the following command:
+Verify the installation
+~~~~~~~~~~~~~~~~~~~~~~~
- .. code-block:: posh
-
- conda create -n tensorflow_gpu pip python=3.7
-
-* The above will create a new virtual environment with name ``tensorflow_gpu``
-* Now lets activate the newly created virtual environment by running the following in the `Anaconda Promt` window:
+- Run the following command in a **NEW** `Terminal` window:
.. code-block:: posh
- activate tensorflow_gpu
+ python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
-Once you have activated your virtual environment, the name of the environment should be displayed within brackets at the beggining of your cmd path specifier, e.g.:
+ .. important::
-.. code-block:: ps1con
+ A new terminal window must be opened for the changes to the Environmental variables to take effect!!
- (tensorflow_gpu) C:\Users\sglvladi>
-
-Install TensorFlow GPU for Python
-*********************************
-- Open a new `Terminal` window and activate the `tensorflow_gpu` environment (if you have not done so already)
-- Once open, type the following on the command line:
+- Once the above is run, you should see a print-out similar to the one bellow:
.. code-block:: posh
+ :emphasize-lines: 1,2,6,7,8,9,10,11,12,20,21,22,23,24,25,26,31
+
+ 2020-06-22 20:24:31.355541: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
+ 2020-06-22 20:24:33.650692: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
+ 2020-06-22 20:24:33.686846: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
+ pciBusID: 0000:02:00.0 name: GeForce GTX 1070 Ti computeCapability: 6.1
+ coreClock: 1.683GHz coreCount: 19 deviceMemorySize: 8.00GiB deviceMemoryBandwidth: 238.66GiB/s
+ 2020-06-22 20:24:33.697234: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
+ 2020-06-22 20:24:33.747540: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
+ 2020-06-22 20:24:33.787573: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
+ 2020-06-22 20:24:33.810063: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
+ 2020-06-22 20:24:33.841474: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
+ 2020-06-22 20:24:33.862787: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
+ 2020-06-22 20:24:33.907318: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
+ 2020-06-22 20:24:33.913612: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
+ 2020-06-22 20:24:33.918093: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
+ 2020-06-22 20:24:33.932784: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x2382acc1c40 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
+ 2020-06-22 20:24:33.939473: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
+ 2020-06-22 20:24:33.944570: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
+ pciBusID: 0000:02:00.0 name: GeForce GTX 1070 Ti computeCapability: 6.1
+ coreClock: 1.683GHz coreCount: 19 deviceMemorySize: 8.00GiB deviceMemoryBandwidth: 238.66GiB/s
+ 2020-06-22 20:24:33.953910: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
+ 2020-06-22 20:24:33.958772: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
+ 2020-06-22 20:24:33.963656: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
+ 2020-06-22 20:24:33.968210: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
+ 2020-06-22 20:24:33.973389: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
+ 2020-06-22 20:24:33.978058: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
+ 2020-06-22 20:24:33.983547: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
+ 2020-06-22 20:24:33.990380: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
+ 2020-06-22 20:24:35.338596: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
+ 2020-06-22 20:24:35.344643: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] 0
+ 2020-06-22 20:24:35.348795: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0: N
+ 2020-06-22 20:24:35.353853: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6284 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1070 Ti, pci bus id: 0000:02:00.0, compute capability: 6.1)
+ 2020-06-22 20:24:35.369758: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x2384aa9f820 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
+ 2020-06-22 20:24:35.376320: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): GeForce GTX 1070 Ti, Compute Capability 6.1
+ tf.Tensor(122.478485, shape=(), dtype=float32)
+
+- Notice from the lines highlighted above that the library files are now ``Successfully opened`` and a debugging message is presented to confirm that TensorFlow has successfully ``Created TensorFlow device``.
- pip install --upgrade tensorflow-gpu==1.14
-
-- Wait for the installation to finish
-
-Test your Installation
-**********************
-- Open a new `Terminal` window and activate the `tensorflow_gpu` environment (if you have not done so already)
-- Start a new Python interpreter session by running:
-
- .. code-block:: posh
-
- python
-
-- Once the interpreter opens up, type:
-
- .. code-block:: python
-
- >>> import tensorflow as tf
-
-- If the above code shows an error, then check to make sure you have activated the `tensorflow_gpu` environment and that tensorflow_gpu was successfully installed within it in the previous step.
-- Then run the following:
-
- .. code-block:: python
-
- >>> hello = tf.constant('Hello, TensorFlow!')
- >>> sess = tf.Session()
-- Once the above is run, you should see a print-out similar (but not identical) to the one bellow:
-
- .. code-block:: python
-
- 2019-11-25 07:20:32.415386: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
- 2019-11-25 07:20:32.449116: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
- name: GeForce GTX 1070 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.683
- pciBusID: 0000:01:00.0
- 2019-11-25 07:20:32.455223: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
- 2019-11-25 07:20:32.460799: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
- 2019-11-25 07:20:32.464391: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
- 2019-11-25 07:20:32.472682: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
- name: GeForce GTX 1070 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.683
- pciBusID: 0000:01:00.0
- 2019-11-25 07:20:32.478942: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
- 2019-11-25 07:20:32.483948: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
- 2019-11-25 07:20:33.181565: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
- 2019-11-25 07:20:33.185974: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165] 0
- 2019-11-25 07:20:33.189041: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0: N
- 2019-11-25 07:20:33.193290: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6358 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1070 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
-
-- Finally, run the following:
-
- .. code-block:: python
-
- >>> print(sess.run(hello))
- b'Hello, TensorFlow!'
.. _tf_models_install:
-TensorFlow Models Installation
-------------------------------
-
-Now that you have installed TensorFlow, it is time to install the models used by TensorFlow to do its magic.
-
-Install Prerequisites
-~~~~~~~~~~~~~~~~~~~~~
-
-Building on the assumption that you have just created your new virtual environment (whether that's `tensorflow_cpu`, `tensorflow_gpu` or whatever other name you might have used), there are some packages which need to be installed before installing the models.
-
-+---------------------------------------------+
-| Prerequisite packages |
-+--------------+------------------------------+
-| Name | Tutorial version-build |
-+==============+==============================+
-| pillow | 6.2.1-py37hdc69c19_0 |
-+--------------+------------------------------+
-| lxml | 4.4.1-py37h1350720_0 |
-+--------------+------------------------------+
-| jupyter | 1.0.0-py37_7 |
-+--------------+------------------------------+
-| matplotlib | 3.1.1-py37hc8f65d3_0 |
-+--------------+------------------------------+
-| opencv | 3.4.2-py37hc319ecb_0 |
-+--------------+------------------------------+
-| pathlib | 1.0.1-cp37 |
-+--------------+------------------------------+
-
-The packages can be installed using ``conda`` by running:
-
-.. code-block:: posh
-
- conda install (=), (=), ..., (=)
-
-where ```` can be replaced with the name of the package, and optionally the package version can be specified by adding the optional specifier ``=`` after ````. For example, to simply install all packages at their latest versions you can run:
+TensorFlow Object Detection API Installation
+--------------------------------------------
-.. code-block:: posh
+Now that you have installed TensorFlow, it is time to install the TensorFlow Object Detection API.
- conda install pillow, lxml, jupyter, matplotlib, opencv, cython
-
-Alternatively, if you don't want to use Anaconda you can install the packages using ``pip``:
-
-.. code-block:: posh
-
- pip install (==) (==) ... (==)
-
-but you will need to install ``opencv-python`` instead of ``opencv``.
-
-Downloading the TensorFlow Models
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-.. note:: To ensure compatibility with the chosen version of Tensorflow (i.e. ``1.14.0``), it is generally recommended to use one of the `Tensorflow Models releases `_, as they are most likely to be stable. Release ``v1.13.0`` is the last unofficial release before ``v2.0`` and therefore is the one used here.
+Downloading the TensorFlow Model Garden
+***************************************
- Create a new folder under a path of your choice and name it ``TensorFlow``. (e.g. ``C:\Users\sglvladi\Documents\TensorFlow``).
- From your `Terminal` ``cd`` into the ``TensorFlow`` directory.
-- To download the models you can either use `Git `_ to clone the `TensorFlow Models v.1.13.0 release `_ inside the ``TensorFlow`` folder, or you can simply download it as a `ZIP `_ and extract it's contents inside the ``TensorFlow`` folder. To keep things consistent, in the latter case you will have to rename the extracted folder ``models-r1.13.0`` to ``models``.
+- To download the models you can either use `Git `_ to clone the `TensorFlow Models repository `_ inside the ``TensorFlow`` folder, or you can simply download it as a `ZIP `_ and extract its contents inside the ``TensorFlow`` folder. To keep things consistent, in the latter case you will have to rename the extracted folder ``models-master`` to ``models``.
- You should now have a single folder named ``models`` under your ``TensorFlow`` folder, which contains another 4 folders as such:
.. code-block:: bash
- TensorFlow
- └─ models
- ├── official
- ├── research
- ├── samples
- └── tutorials
+ TensorFlow/
+ └─ models/
+ ├─ community/
+ ├─ official/
+ ├─ orbit/
+ ├─ research/
+ └── ...
Protobuf Installation/Compilation
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+*********************************
The Tensorflow Object Detection API uses Protobufs to configure model and
training parameters. Before the framework can be used, the Protobuf libraries
@@ -374,9 +306,8 @@ must be downloaded and compiled.
This should be done as follows:
- Head to the `protoc releases page `_
-- Download the latest ``protoc-*-*.zip`` release (e.g. ``protoc-3.11.0-win64.zip`` for 64-bit Windows)
+- Download the latest ``protoc-*-*.zip`` release (e.g. ``protoc-3.12.3-win64.zip`` for 64-bit Windows)
- Extract the contents of the downloaded ``protoc-*-*.zip`` in a directory ```` of your choice (e.g. ``C:\Program Files\Google Protobuf``)
-- Extract the contents of the downloaded ``protoc-*-*.zip``, inside ``C:\Program Files\Google Protobuf``
- Add ```` to your ``Path`` environment variable (see :ref:`set_env`)
- In a new `Terminal` [#]_, ``cd`` into ``TensorFlow/models/research/`` directory and run the following command:
@@ -393,7 +324,7 @@ This should be done as follows:
.. tab:: Windows Powershell
- .. code-block:: python
+ .. code-block::
# From within TensorFlow/models/research/
Get-ChildItem object_detection/protos/*.proto | foreach {protoc "object_detection/protos/$($_.Name)" --python_out=.}
@@ -401,7 +332,7 @@ This should be done as follows:
.. tab:: Command Prompt
- .. code-block:: python
+ .. code-block::
# From within TensorFlow/models/research/
for /f %i in ('dir /b object_detection\protos\*.proto') do protoc object_detection\protos\%i --python_out=.
@@ -410,49 +341,12 @@ This should be done as follows:
.. [#] NOTE: You MUST open a new `Terminal` for the changes in the environment variables to take effect.
-Adding necessary Environment Variables
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-1. Install the ``Tensorflow\models\research\object_detection`` package by running the following from ``Tensorflow\models\research``:
-
- .. code-block:: python
-
- # From within TensorFlow/models/research/
- pip install .
-
-2. Add `research/slim` to your ``PYTHONPATH``:
-
-.. tabs::
-
- .. tab:: Windows
-
- - Go to `Start` and Search "environment variables"
- - Click "Edit the system environment variables". This should open the "System Properties" window
- - In the opened window, click the "Environment Variables..." button to open the "Environment Variables" window.
- - Under "System variables", search for and click on the ``PYTHONPATH`` system variable,
-
- - If it exists then click "Edit..." and add ``\TensorFlow\models\research\slim`` to the list
- - If it doesn't already exist, then click "New...", under "Variable name" type ``PYTHONPATH`` and under "Variable value" enter ``\TensorFlow\models\research\slim``
-
- - Then click "OK" to save the changes:
-
- .. tab:: Linux
-
- The `Installation docs `_ suggest that you either run, or add to ``~/.bashrc`` file, the following command, which adds these packages to your PYTHONPATH:
-
- .. code-block:: bash
-
- # From within tensorflow/models/research/
- export PYTHONPATH=$PYTHONPATH:/TensorFlow/models/research/slim
-
- where, in both cases, ```` replaces the absolute path to your ``TesnorFlow`` folder. (e.g. ```` = ``C:\Users\sglvladi\Documents`` if ``TensorFlow`` resides within your ``Documents`` folder)
-
.. _tf_models_install_coco:
-COCO API installation (Optional)
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+COCO API installation
+*********************
-The ``pycocotools`` package should be installed if you are interested in using COCO evaluation metrics, as discussed in :ref:`evaluation_sec`.
+As of TensorFlow 2.x, the ``pycocotools`` package is listed as `a dependency of the Object Detection API `_. Ideally, this package should get installed when installing the Object Detection API as documented in the :ref:`tf_models_install_object_detection` section below, however the installation can fail for various reasons and therefore it is simpler to just install the package beforehand, in which case later installation will be skipped.
.. tabs::
@@ -462,14 +356,15 @@ The ``pycocotools`` package should be installed if you are interested in using C
.. code-block:: bash
+ pip install cython
pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
Note that, according to the `package's instructions `_, Visual C++ 2015 build tools must be installed and on your path. If they are not, make sure to install them from `here `_.
.. tab:: Linux
-
- Download `cocoapi `_ to a directory of your choice, then ``make`` and copy the pycocotools subfolder to the ``Tensorflow/models/research`` directory, as such:
+
+ Download `cocoapi `_ to a directory of your choice, then ``make`` and copy the pycocotools subfolder to the ``Tensorflow/models/research`` directory, as such:
.. code-block:: bash
@@ -485,49 +380,96 @@ The ``pycocotools`` package should be installed if you are interested in using C
- To use the COCO instance segmentation metrics add ``metrics_set: "coco_mask_metrics"`` to the ``eval_config`` message in the config file.
-.. _test_tf_models:
+.. _tf_models_install_object_detection:
-Test your Installation
-~~~~~~~~~~~~~~~~~~~~~~
+Install the Object Detection API
+********************************
+Installation of the Object Detection API is achieved by installing the ``object_detection`` package. This is done by running the following commands from within ``Tensorflow\models\research``:
-- Open a new `Terminal` window and activate the `tensorflow_gpu` environment (if you have not done so already)
-- ``cd`` into ``TensorFlow\models\research\object_detection`` and run the following command:
+.. code-block::
- .. code-block:: posh
+ # From within TensorFlow/models/research/
+ cp object_detection/packages/tf2/setup.py .
+ python -m pip install .
- # From within TensorFlow/models/research/object_detection
- jupyter notebook
+.. note::
-- This should start a new ``jupyter notebook`` server on your machine and you should be redirected to a new tab of your default browser.
+ During the above installation, you may observe the following error:
-- Once there, simply follow `sentdex's Youtube video `_ to ensure that everything is running smoothly.
+ .. code-block::
-- When done, your notebook should look similar to the image bellow:
+ ERROR: Command errored out with exit status 1:
+ command: 'C:\Users\sglvladi\Anaconda3\envs\tf2\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\sglvladi\\AppData\\Local\\Temp\\pip-install-yn46ecei\\pycocotools\\setup.py'"'"'; __file__='"'"'C:\\Users\\sglvladi\\AppData\\Local\\Temp\\pip-install-yn46ecei\\pycocotools\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\sglvladi\AppData\Local\Temp\pip-record-wpn7b6qo\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\Users\sglvladi\Anaconda3\envs\tf2\Include\pycocotools'
+ cwd: C:\Users\sglvladi\AppData\Local\Temp\pip-install-yn46ecei\pycocotools\
+ Complete output (14 lines):
+ running install
+ running build
+ running build_py
+ creating build
+ creating build\lib.win-amd64-3.8
+ creating build\lib.win-amd64-3.8\pycocotools
+ copying pycocotools\coco.py -> build\lib.win-amd64-3.8\pycocotools
+ copying pycocotools\cocoeval.py -> build\lib.win-amd64-3.8\pycocotools
+ copying pycocotools\mask.py -> build\lib.win-amd64-3.8\pycocotools
+ copying pycocotools\__init__.py -> build\lib.win-amd64-3.8\pycocotools
+ running build_ext
+ skipping 'pycocotools\_mask.c' Cython extension (up-to-date)
+ building 'pycocotools._mask' extension
+ error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/
+ ----------------------------------------
+ ERROR: Command errored out with exit status 1: 'C:\Users\sglvladi\Anaconda3\envs\tf2\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\sglvladi\\AppData\\Local\\Temp\\pip-install-yn46ecei\\pycocotools\\setup.py'"'"'; __file__='"'"'C:\\Users\\sglvladi\\AppData\\Local\\Temp\\pip-install-yn46ecei\\pycocotools\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\sglvladi\AppData\Local\Temp\pip-record-wpn7b6qo\install-record.txt' --single-version-externally-managed --compile --install-headers 'C:\Users\sglvladi\Anaconda3\envs\tf2\Include\pycocotools' Check the logs for full command output.
- .. image:: ./_static/object_detection_tutorial_output.png
- :width: 90%
- :alt: alternate text
- :align: center
+ This is caused because installation of the ``pycocotools`` package has failed. To fix this have a look at the :ref:`tf_models_install_coco` section and rerun the above commands.
-.. important::
- 1. If no errors appear, but also no images are shown in the notebook, try adding ``%matplotlib inline`` at the start of the last cell, as shown by the highlighted text in the image bellow:
- .. image:: ./_static/object_detection_tutorial_err.png
- :width: 90%
- :alt: alternate text
- :align: center
+.. _test_tf_models:
+
+Test your Installation
+**********************
+
+To test the installation, run the following command from within ``Tensorflow\models\research``:
+.. code-block::
- 2. If Python crashes when running the last cell, have a look at the `Terminal` window you used to run ``jupyter notebook`` and check for an error similar (maybe identical) to the one below:
+ # From within TensorFlow/models/research/
+ python object_detection/builders/model_builder_tf2_test.py
- .. code-block:: python
+Once the above is run, allow some time for the test to complete and once done you should observe a
+printout similar to the one below:
- 2018-03-22 03:07:54.623130: E C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\stream_executor\cuda\cuda_dnn.cc:378] Loaded runtime CuDNN library: 7101 (compatibility version 7100) but source was compiled with 7003 (compatibility version 7000). If using a binary install, upgrade your CuDNN library to match. If building from sources, make sure the library loaded at runtime matches a compatible version specified during compile configuration.
+.. code-block::
- - If the above line is present in the printed debugging, it means that you have not installed the correct version of the cuDNN libraries. In this case make sure you re-do the :ref:`cudnn_install` step, making sure you instal cuDNN v7.6.5.
+ ...
+ [ OK ] ModelBuilderTF2Test.test_create_ssd_models_from_config
+ [ RUN ] ModelBuilderTF2Test.test_invalid_faster_rcnn_batchnorm_update
+ [ OK ] ModelBuilderTF2Test.test_invalid_faster_rcnn_batchnorm_update
+ [ RUN ] ModelBuilderTF2Test.test_invalid_first_stage_nms_iou_threshold
+ [ OK ] ModelBuilderTF2Test.test_invalid_first_stage_nms_iou_threshold
+ [ RUN ] ModelBuilderTF2Test.test_invalid_model_config_proto
+ [ OK ] ModelBuilderTF2Test.test_invalid_model_config_proto
+ [ RUN ] ModelBuilderTF2Test.test_invalid_second_stage_batch_size
+ [ OK ] ModelBuilderTF2Test.test_invalid_second_stage_batch_size
+ [ RUN ] ModelBuilderTF2Test.test_session
+ [ SKIPPED ] ModelBuilderTF2Test.test_session
+ [ RUN ] ModelBuilderTF2Test.test_unknown_faster_rcnn_feature_extractor
+ [ OK ] ModelBuilderTF2Test.test_unknown_faster_rcnn_feature_extractor
+ [ RUN ] ModelBuilderTF2Test.test_unknown_meta_architecture
+ [ OK ] ModelBuilderTF2Test.test_unknown_meta_architecture
+ [ RUN ] ModelBuilderTF2Test.test_unknown_ssd_feature_extractor
+ [ OK ] ModelBuilderTF2Test.test_unknown_ssd_feature_extractor
+ ----------------------------------------------------------------------
+ Ran 20 tests in 68.510s
+ OK (skipped=1)
+
+Try out the examples
+********************
+If the previous step completed successfully it means you have successfully installed all the
+components necessary to perform object detection using pre-trained models.
+
+If you want to play around with some examples to see how this can be done, now would be a good
+time to have a look at the :ref:`examples` section.
-.. n
.. _labelImg_install:
@@ -537,7 +479,7 @@ LabelImg Installation
There exist several ways to install ``labelImg``. Below are 3 of the most common.
Get from PyPI (Recommended)
-~~~~~~~~~~~~~~~~~~~~~~~~~~~
+***************************
1. Open a new `Terminal` window and activate the `tensorflow_gpu` environment (if you have not done so already)
2. Run the following command to install ``labelImg``:
@@ -554,7 +496,7 @@ Get from PyPI (Recommended)
labelImg [IMAGE_PATH] [PRE-DEFINED CLASS FILE]
Use precompiled binaries (Easy)
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+*******************************
Precompiled binaries for both Windows and Linux can be found `here `_ .
Installation is the done in three simple steps:
@@ -563,18 +505,19 @@ Installation is the done in three simple steps:
2. Download the latest binary for your OS from `here `_. and extract its contents under ``Tensorflow/addons/labelImg``.
-3. You should now have a single folder named ``addons\labelImg`` under your ``TensorFlow`` folder, which contains another 4 folders as such:
+3. You should now have a single folder named ``addons/labelImg`` under your ``TensorFlow`` folder, which contains another 4 folders as such:
.. code-block:: bash
- TensorFlow
- ├─ addons
- │ └── labelImg
- └─ models
- ├── official
- ├── research
- ├── samples
- └── tutorials
+ TensorFlow/
+ ├─ addons/
+ │ └─ labelImg/
+ └─ models/
+ ├─ community/
+ ├─ official/
+ ├─ orbit/
+ ├─ research/
+ └─ ...
4. ``labelImg`` can then be run as follows:
@@ -586,7 +529,7 @@ Installation is the done in three simple steps:
labelImg [IMAGE_PATH] [PRE-DEFINED CLASS FILE]
Build from source (Hard)
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+************************
The steps for installing from source follow below.
**1. Download labelImg**
@@ -597,21 +540,22 @@ The steps for installing from source follow below.
.. code-block:: bash
- TensorFlow
+ TensorFlow/
├─ addons
- │ └── labelImg
- └─ models
- ├── official
- ├── research
- ├── samples
- └── tutorials
+ │ └─ labelImg/
+ └─ models/
+ ├─ community/
+ ├─ official/
+ ├─ orbit/
+ ├─ research/
+ └─ ...
.. [#] The latest repo commit when writing this tutorial is `8d1bd68 `_.
**2. Install dependencies and compiling package**
- Open a new `Terminal` window and activate the `tensorflow_gpu` environment (if you have not done so already)
-- ``cd`` into ``TensorFlow\addons\labelImg`` and run the following commands:
+- ``cd`` into ``TensorFlow/addons/labelImg`` and run the following commands:
.. tabs::
@@ -634,7 +578,7 @@ The steps for installing from source follow below.
**3. Test your installation**
- Open a new `Terminal` window and activate the `tensorflow_gpu` environment (if you have not done so already)
-- ``cd`` into ``TensorFlow\addons\labelImg`` and run the following command:
+- ``cd`` into ``TensorFlow/addons/labelImg`` and run the following command:
.. code-block:: posh
diff --git a/docs/build/_sources/issues.rst.txt b/docs/build/_sources/issues.rst.txt
index 3358439..6138799 100644
--- a/docs/build/_sources/issues.rst.txt
+++ b/docs/build/_sources/issues.rst.txt
@@ -38,13 +38,6 @@ To solve such issues in Windows, open a `Task Manager` windows, look for Tasks w
If the issue persists, then you're probably running out of memory. Try closing down anything else that might be eating up your GPU memory (e.g. Youtube videos, webpages etc.)
-labelImg saves annotation files with ``.xml.xml`` extension
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-At the time of writing up this document, I haven't managed to identify why this might be happening. I have joined a `GitHub issue `_, at which you can refer in case there are any updates.
-
-One way I managed to fix the issue was by clicking on the "Change Save Dir" button and selecting the directory where the annotations files should be stores. By doing so, you should not longer get a pop-up dialog when you click "Save" (or Ctrl+s), but you can always check if the file was saved by looking at the bottom left corner of ``labelImg``.
-
"WARNING:tensorflow:Entity ``>`` could not be transformed ..."
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -59,8 +52,69 @@ In some versions of Tensorflow, you may see errors that look similar to the ones
These warnings appear to be harmless form my experience, however they can saturate the console with unnecessary messages, which makes it hard to scroll through the output of the training/evaluation process.
-As reported `here `_, this issue seems to be caused by a mismatched version of `gast `_. Simply downgrading gast to version ``0.2.2`` seems to remove the warnings. This can be done by running:
+As reported `here `_, this issue seems to
+be caused by a mismatched version of `gast `_. Simply
+downgrading gast to version ``0.2.2`` seems to remove the warnings. This can be done by running:
.. code-block:: bash
- pip install gast==0.2.2
\ No newline at end of file
+ pip install gast==0.2.2
+
+"AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key"
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+It is possible that when executing ``from object_detection.utils import label_map_util`` you may
+get the above error. As per the discussion is in `this Stack Overflow thread `_,
+upgrading the Python protobuf version seems to solve this issue:
+
+.. code-block::
+
+ pip install --upgrade protobuf
+
+.. _export_error:
+
+"TypeError: Expected Operation, Variable, or Tensor, got level_5"
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+When trying to export oyu trained model using the ``exporter_main_v2.py`` script, you may come
+across an error that looks like this:
+
+.. code-block::
+ :linenos:
+ :emphasize-lines: 9
+
+ Traceback (most recent call last):
+ File ".\exporter_main_v2.py", line 126, in
+ app.run(main)
+ File "C:\Users\sglvladi\Anaconda3\envs\tf2\lib\site-packages\absl\app.py", line 299, in run
+ _run_main(main, args)
+ ...
+ File "C:\Users\sglvladi\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1627, in get_losses_for
+ reachable = tf_utils.get_reachable_from_inputs(inputs, losses)
+ File "C:\Users\sglvladi\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\keras\utils\tf_utils.py", line 140, in get_reachable_from_inputs
+ raise TypeError('Expected Operation, Variable, or Tensor, got ' + str(x))
+ TypeError: Expected Operation, Variable, or Tensor, got level_5
+
+This error seems to come from TensorFlow itself and a discussion on the issue can be found
+`here `_. As discussed there, a fix to the above
+issue can be achieved by opening the ``tf_utils.py`` file and adding a line of code. Below is a
+summary of how this can be done:
+
+- Look at the line that corresponds to line 9 (highlighted) in the above error print out.
+- Copy the path to the ``tf_utils.py`` file; in my case this was ``C:\Users\sglvladi\Anaconda3\envs\tf2\lib\site-packages\tensorflow\python\keras\utils\tf_utils.py``
+- Open the file and replace line 140 of the file as follows:
+
+ - Change:
+
+ .. code-block:: python
+
+ raise TypeError('Expected Operation, Variable, or Tensor, got ' + str(x))
+
+ to:
+
+ .. code-block:: python
+
+ if not isinstance(x, str):
+ raise TypeError('Expected Operation, Variable, or Tensor, got ' + str(x))
+
+At the time of writting this tutorial, a fix to the issue had not been implemented in the version
+of TensorFlow installed using ``pip``. It is possible that this will get incorporated at some later
+point.
\ No newline at end of file
diff --git a/docs/build/_sources/training.rst.txt b/docs/build/_sources/training.rst.txt
index 3167297..79d7e17 100644
--- a/docs/build/_sources/training.rst.txt
+++ b/docs/build/_sources/training.rst.txt
@@ -3,8 +3,8 @@ Training Custom Object Detector
So, up to now you should have done the following:
-- Installed TensorFlow, either CPU or GPU (See :ref:`tf_install`)
-- Installed TensorFlow Models (See :ref:`tf_models_install`)
+- Installed TensorFlow (See :ref:`tf_install`)
+- Installed TensorFlow Object Detection API (See :ref:`tf_models_install`)
- Installed labelImg (See :ref:`labelImg_install`)
Now that we have done all the above, we can start doing some cool stuff. Here we will see how you can train your own object detector, and since it is not as simple as it sounds, we will have a look at:
@@ -16,48 +16,50 @@ Now that we have done all the above, we can start doing some cool stuff. Here we
5. How to train a model and monitor it's progress
6. How to export the resulting model and use it to detect objects.
-Preparing workspace
-~~~~~~~~~~~~~~~~~~~
+Preparing the Workspace
+-----------------------
-1. If you have followed the tutorial, you should by now have a folder ``Tensorflow``, placed under ```` (e.g. ``C:\Users\sglvladi\Documents``), with the following directory tree:
+1. If you have followed the tutorial, you should by now have a folder ``Tensorflow``, placed under ```` (e.g. ``C:/Users/sglvladi/Documents``), with the following directory tree:
.. code-block:: bash
- TensorFlow
- ├─ addons
- │ └── labelImg
- └─ models
- ├── official
- ├── research
- ├── samples
- └── tutorials
+ TensorFlow/
+ ├─ addons/ (Optional)
+ │ └─ labelImg/
+ └─ models/
+ ├─ community/
+ ├─ official/
+ ├─ orbit/
+ ├─ research/
+ └─ ...
2. Now create a new folder under ``TensorFlow`` and call it ``workspace``. It is within the ``workspace`` that we will store all our training set-ups. Now let's go under workspace and create another folder named ``training_demo``. Now our directory structure should be as so:
.. code-block:: bash
- TensorFlow
- ├─ addons
- │ └─ labelImg
- ├─ models
- │ ├─ official
- │ ├─ research
- │ ├─ samples
- │ └─ tutorials
- └─ workspace
- └─ training_demo
+ TensorFlow/
+ ├─ addons/ (Optional)
+ │ └─ labelImg/
+ ├─ models/
+ │ ├─ community/
+ │ ├─ official/
+ │ ├─ orbit/
+ │ ├─ research/
+ │ └─ ...
+ └─ workspace/
+ └─ training_demo/
3. The ``training_demo`` folder shall be our `training folder`, which will contain all files related to our model training. It is advisable to create a separate training folder each time we wish to train a different model. The typical structure for training folders is shown below.
.. code-block:: bash
- training_demo
- ├─ annotations
- ├─ images
- │ ├─ test
- │ └─ train
- ├─ pre-trained-model
- ├─ training
+ training_demo/
+ ├─ annotations/
+ ├─ images/
+ │ ├─ test/
+ │ └─ train/
+ ├─ models/
+ ├─ pre-trained-models/
└─ README.md
Here's an explanation for each of the folders/filer shown in the above tree:
@@ -65,35 +67,39 @@ Here's an explanation for each of the folders/filer shown in the above tree:
- ``annotations``: This folder will be used to store all ``*.csv`` files and the respective TensorFlow ``*.record`` files, which contain the list of annotations for our dataset images.
- ``images``: This folder contains a copy of all the images in our dataset, as well as the respective ``*.xml`` files produced for each one, once ``labelImg`` is used to annotate objects.
- * ``images\train``: This folder contains a copy of all images, and the respective ``*.xml`` files, which will be used to train our model.
- * ``images\test``: This folder contains a copy of all images, and the respective ``*.xml`` files, which will be used to test our model.
+ * ``images/train``: This folder contains a copy of all images, and the respective ``*.xml`` files, which will be used to train our model.
+ * ``images/test``: This folder contains a copy of all images, and the respective ``*.xml`` files, which will be used to test our model.
-- ``pre-trained-model``: This folder will contain the pre-trained model of our choice, which shall be used as a starting checkpoint for our training job.
-- ``training``: This folder will contain the training pipeline configuration file ``*.config``, as well as a ``*.pbtxt`` label map file and all files generated during the training of our model.
+- ``models``: This folder will contain a sub-folder for each of training job. Each subfolder will contain the training pipeline configuration file ``*.config``, as well as all files generated during the training and evaluation of our model.
+- ``pre-trained-models``: This folder will contain the downloaded pre-trained models, which shall be used as a starting checkpoint for our training jobs.
- ``README.md``: This is an optional file which provides some general information regarding the training conditions of our model. It is not used by TensorFlow in any way, but it generally helps when you have a few training folders and/or you are revisiting a trained model after some time.
If you do not understand most of the things mentioned above, no need to worry, as we'll see how all the files are generated further down.
-Annotating images
-~~~~~~~~~~~~~~~~~
+
+Preparing the Dataset
+---------------------
+
+Annotate Images
+~~~~~~~~~~~~~~~
To annotate images we will be using the `labelImg `_ package. If you haven't installed the package yet, then have a look at :ref:`labelImg_install`.
-- Once you have collected all the images to be used to test your model (ideally more than 100 per class), place them inside the folder ``training_demo\images``.
-- Open a new `Anaconda/Command Prompt` window and ``cd`` into ``Tensorflow\addons\labelImg``.
+- Once you have collected all the images to be used to test your model (ideally more than 100 per class), place them inside the folder ``training_demo/images``.
+- Open a new `Anaconda/Command Prompt` window and ``cd`` into ``Tensorflow/addons/labelImg``.
- If (as suggested in :ref:`labelImg_install`) you created a separate Conda environment for ``labelImg`` then go ahead and activate it by running:
.. code-block:: bash
activate labelImg
-- Next go ahead and start ``labelImg``, pointing it to your ``training_demo\images`` folder.
+- Next go ahead and start ``labelImg``, pointing it to your ``training_demo/images`` folder.
.. code-block:: bash
- python labelImg.py ..\..\workspace\training_demo\images
+ python labelImg.py ../../workspace/training_demo/images
-- A File Explorer Dialog windows should open, which points to the ``training_demo\images`` folder.
+- A File Explorer Dialog windows should open, which points to the ``training_demo/images`` folder.
- Press the "Select Folder" button, to start annotating your images.
Once open, you should see a window similar to the one below:
@@ -103,141 +109,46 @@ Once open, you should see a window similar to the one below:
:alt: alternate text
:align: center
-I won't be covering a tutorial on how to use ``labelImg``, but you can have a look at `labelImg's repo `_ for more details. A nice Youtube video demonstrating how to use ``labelImg`` is also available `here `_. What is important is that once you annotate all your images, a set of new ``*.xml`` files, one for each image, should be generated inside your ``training_demo\images`` folder.
+I won't be covering a tutorial on how to use ``labelImg``, but you can have a look at `labelImg's repo `_ for more details. A nice Youtube video demonstrating how to use ``labelImg`` is also available `here `_. What is important is that once you annotate all your images, a set of new ``*.xml`` files, one for each image, should be generated inside your ``training_demo/images`` folder.
.. _image_partitioning_sec:
-Partitioning the images
-~~~~~~~~~~~~~~~~~~~~~~~
+Partition the Dataset
+~~~~~~~~~~~~~~~~~~~~~
Once you have finished annotating your image dataset, it is a general convention to use only part of it for training, and the rest is used for evaluation purposes (e.g. as discussed in :ref:`evaluation_sec`).
Typically, the ratio is 90%/10%, i.e. 90% of the images are used for training and the rest 10% is maintained for testing, but you can chose whatever ratio suits your needs.
-Once you have decided how you will be splitting your dataset, copy all training images, together with their corresponding ``*.xml`` files, and place them inside the ``training_demo\images\train`` folder. Similarly, copy all testing images, with their ``*.xml`` files, and paste them inside ``training_demo\images\test``.
+Once you have decided how you will be splitting your dataset, copy all training images, together with their corresponding ``*.xml`` files, and place them inside the ``training_demo/images/train`` folder. Similarly, copy all testing images, with their ``*.xml`` files, and paste them inside ``training_demo/images/test``.
For lazy people like myself, who cannot be bothered to do the above, I have put tugether a simple script that automates the above process:
-.. code-block:: python
-
- """ usage: partition_dataset.py [-h] [-i IMAGEDIR] [-o OUTPUTDIR] [-r RATIO] [-x]
-
- Partition dataset of images into training and testing sets
-
- optional arguments:
- -h, --help show this help message and exit
- -i IMAGEDIR, --imageDir IMAGEDIR
- Path to the folder where the image dataset is stored. If not specified, the CWD will be used.
- -o OUTPUTDIR, --outputDir OUTPUTDIR
- Path to the output folder where the train and test dirs should be created. Defaults to the same directory as IMAGEDIR.
- -r RATIO, --ratio RATIO
- The ratio of the number of test images over the total number of images. The default is 0.1.
- -x, --xml Set this flag if you want the xml annotation files to be processed and copied over.
- """
- import os
- import re
- import shutil
- from PIL import Image
- from shutil import copyfile
- import argparse
- import glob
- import math
- import random
- import xml.etree.ElementTree as ET
-
-
- def iterate_dir(source, dest, ratio, copy_xml):
- source = source.replace('\\', '/')
- dest = dest.replace('\\', '/')
- train_dir = os.path.join(dest, 'train')
- test_dir = os.path.join(dest, 'test')
-
- if not os.path.exists(train_dir):
- os.makedirs(train_dir)
- if not os.path.exists(test_dir):
- os.makedirs(test_dir)
-
- images = [f for f in os.listdir(source)
- if re.search(r'([a-zA-Z0-9\s_\\.\-\(\):])+(.jpg|.jpeg|.png)$', f)]
-
- num_images = len(images)
- num_test_images = math.ceil(ratio*num_images)
-
- for i in range(num_test_images):
- idx = random.randint(0, len(images)-1)
- filename = images[idx]
- copyfile(os.path.join(source, filename),
- os.path.join(test_dir, filename))
- if copy_xml:
- xml_filename = os.path.splitext(filename)[0]+'.xml'
- copyfile(os.path.join(source, xml_filename),
- os.path.join(test_dir,xml_filename))
- images.remove(images[idx])
-
- for filename in images:
- copyfile(os.path.join(source, filename),
- os.path.join(train_dir, filename))
- if copy_xml:
- xml_filename = os.path.splitext(filename)[0]+'.xml'
- copyfile(os.path.join(source, xml_filename),
- os.path.join(train_dir, xml_filename))
-
-
- def main():
-
- # Initiate argument parser
- parser = argparse.ArgumentParser(description="Partition dataset of images into training and testing sets",
- formatter_class=argparse.RawTextHelpFormatter)
- parser.add_argument(
- '-i', '--imageDir',
- help='Path to the folder where the image dataset is stored. If not specified, the CWD will be used.',
- type=str,
- default=os.getcwd()
- )
- parser.add_argument(
- '-o', '--outputDir',
- help='Path to the output folder where the train and test dirs should be created. '
- 'Defaults to the same directory as IMAGEDIR.',
- type=str,
- default=None
- )
- parser.add_argument(
- '-r', '--ratio',
- help='The ratio of the number of test images over the total number of images. The default is 0.1.',
- default=0.1,
- type=float)
- parser.add_argument(
- '-x', '--xml',
- help='Set this flag if you want the xml annotation files to be processed and copied over.',
- action='store_true'
- )
- args = parser.parse_args()
-
- if args.outputDir is None:
- args.outputDir = args.imageDir
-
- # Now we are ready to start the iteration
- iterate_dir(args.imageDir, args.outputDir, args.ratio, args.xml)
-
-
- if __name__ == '__main__':
- main()
-
-To use the script, simply copy and paste the code above in a script named ``partition_dataset.py``. Then, assuming you have all your images and ``*.xml`` files inside ``training_demo\images``, just run the following command:
+.. literalinclude:: scripts/partition_dataset.py
-.. code-block:: bash
+- Click :download:`here ` to download the above script and save it inside ``TensorFlow/scripts/preprocessing``.
+- Then, ``cd`` into ``TensorFlow/scripts/preprocessing`` and run:
- python partition_dataser.py -x -i training_demo\images -r 0.1
+ .. code-block::
+
+ python partition_dataset.py -x -i [PATH_TO_IMAGES_FOLDER] -r 0.1
+
+ # For example
+ # python partition_dataset.py -x -i C:/Users/sglvladi/Documents/Tensorflow/workspace/training_demo/images -r 0.1
-Once the script has finished, there should exist two new folders under ``training_demo\images``, namely ``training_demo\images\train`` and ``training_demo\images\test``, containing 90% and 10% of the images (and ``*.xml`` files), respectively. To avoid loss of any files, the script will not delete the images under ``training_demo\images``. Once you have checked that your images have been safely copied over, you can delete the images under ``training_demo\images`` manually.
+Once the script has finished, two new folders should have been created under ``training_demo/images``,
+namely ``training_demo/images/train`` and ``training_demo/images/test``, containing 90% and 10% of
+the images (and ``*.xml`` files), respectively. To avoid loss of any files, the script will not
+delete the images under ``training_demo/images``. Once you have checked that your images have been
+safely copied over, you can delete the images under ``training_demo/images`` manually.
-Creating Label Map
-~~~~~~~~~~~~~~~~~~
+Create Label Map
+~~~~~~~~~~~~~~~~
TensorFlow requires a label map, which namely maps each of the used labels to an integer values. This label map is used both by the training and detection processes.
-Below I show an example label map (e.g ``label_map.pbtxt``), assuming that our dataset containes 2 labels, ``dogs`` and ``cats``:
+Below we show an example label map (e.g ``label_map.pbtxt``), assuming that our dataset containes 2 labels, ``dogs`` and ``cats``:
.. code-block:: json
@@ -251,622 +162,248 @@ Below I show an example label map (e.g ``label_map.pbtxt``), assuming that our d
name: 'dog'
}
-Label map files have the extention ``.pbtxt`` and should be placed inside the ``training_demo\annotations`` folder.
+Label map files have the extention ``.pbtxt`` and should be placed inside the ``training_demo/annotations`` folder.
-Creating TensorFlow Records
-~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Create TensorFlow Records
+~~~~~~~~~~~~~~~~~~~~~~~~~
-Now that we have generated our annotations and split our dataset into the desired training and testing subsets, it is time to convert our annotations into the so called ``TFRecord`` format.
+Now that we have generated our annotations and split our dataset into the desired training and
+testing subsets, it is time to convert our annotations into the so called ``TFRecord`` format.
-There are two steps in doing so:
-
-- Converting the individual ``*.xml`` files to a unified ``*.csv`` file for each dataset.
-- Converting the ``*.csv`` files of each dataset to ``*.record`` files (TFRecord format).
-
-Before we proceed to describe the above steps, let's create a directory where we can store some scripts. Under the ``TensorFlow`` folder, create a new folder ``TensorFlow\scripts``, which we can use to store some useful scripts. To make things even tidier, let's create a new folder ``TensorFlow\scripts\preprocessing``, where we shall store scripts that we can use to preprocess our training inputs. Below is out ``TensorFlow`` directory tree structure, up to now:
+Before we proceed to describe the above steps, let's create a directory where we can store some
+scripts. Under the ``TensorFlow`` folder, create a new folder ``TensorFlow/scripts``, which we can
+use to store some useful scripts. To make things even tidier, let's create a new folder
+``TensorFlow/scripts/preprocessing``, where we shall store scripts that we can use to preprocess
+our training inputs. Below is out ``TensorFlow`` directory tree structure, up to now:
.. code-block:: bash
- TensorFlow
- ├─ addons
- │ └─ labelImg
- ├─ models
- │ ├─ official
- │ ├─ research
- │ ├─ samples
- │ └─ tutorials
- ├─ scripts
- │ └─ preprocessing
- └─ workspace
- └─ training_demo
+ TensorFlow/
+ ├─ addons/ (Optional)
+ │ └─ labelImg/
+ ├─ models/
+ │ ├─ community/
+ │ ├─ official/
+ │ ├─ orbit/
+ │ ├─ research/
+ │ └─ ...
+ ├─ scripts/
+ │ └─ preprocessing/
+ └─ workspace/
+ └─ training_demo/
+
-Converting ``*.xml`` to ``*.csv``
----------------------------------
+Convert ``*.xml`` to ``*.record``
+*********************************
-To do this we can write a simple script that iterates through all ``*.xml`` files in the ``training_demo\images\train`` and ``training_demo\images\test`` folders, and generates a ``*.csv`` for each of the two.
+To do this we can write a simple script that iterates through all ``*.xml`` files in the ``training_demo/images/train`` and ``training_demo/images/test`` folders, and generates a ``*.record`` file for each of the two.
Here is an example script that allows us to do just that:
-.. code-block:: python
-
- """
- Usage:
- # Create train data:
- python xml_to_csv.py -i [PATH_TO_IMAGES_FOLDER]/train -o [PATH_TO_ANNOTATIONS_FOLDER]/train_labels.csv
-
- # Create test data:
- python xml_to_csv.py -i [PATH_TO_IMAGES_FOLDER]/test -o [PATH_TO_ANNOTATIONS_FOLDER]/test_labels.csv
- """
-
- import os
- import glob
- import pandas as pd
- import argparse
- import xml.etree.ElementTree as ET
-
-
- def xml_to_csv(path):
- """Iterates through all .xml files (generated by labelImg) in a given directory and combines them in a single Pandas datagrame.
-
- Parameters:
- ----------
- path : {str}
- The path containing the .xml files
- Returns
- -------
- Pandas DataFrame
- The produced dataframe
- """
-
- xml_list = []
- for xml_file in glob.glob(path + '/*.xml'):
- tree = ET.parse(xml_file)
- root = tree.getroot()
- for member in root.findall('object'):
- value = (root.find('filename').text,
- int(root.find('size')[0].text),
- int(root.find('size')[1].text),
- member[0].text,
- int(member[4][0].text),
- int(member[4][1].text),
- int(member[4][2].text),
- int(member[4][3].text)
- )
- xml_list.append(value)
- column_name = ['filename', 'width', 'height',
- 'class', 'xmin', 'ymin', 'xmax', 'ymax']
- xml_df = pd.DataFrame(xml_list, columns=column_name)
- return xml_df
-
-
- def main():
- # Initiate argument parser
- parser = argparse.ArgumentParser(
- description="Sample TensorFlow XML-to-CSV converter")
- parser.add_argument("-i",
- "--inputDir",
- help="Path to the folder where the input .xml files are stored",
- type=str)
- parser.add_argument("-o",
- "--outputFile",
- help="Name of output .csv file (including path)", type=str)
- args = parser.parse_args()
-
- if(args.inputDir is None):
- args.inputDir = os.getcwd()
- if(args.outputFile is None):
- args.outputFile = args.inputDir + "/labels.csv"
-
- assert(os.path.isdir(args.inputDir))
-
- xml_df = xml_to_csv(args.inputDir)
- xml_df.to_csv(
- args.outputFile, index=None)
- print('Successfully converted xml to csv.')
-
-
- if __name__ == '__main__':
- main()
-
-
-- Create a new file with name ``xml_to_csv.py`` under ``TensorFlow\scripts\preprocessing``, open it, paste the above code inside it and save.
+.. literalinclude:: scripts/generate_tfrecord.py
+
+
+- Click :download:`here ` to download the above script and save it inside ``TensorFlow/scripts/preprocessing``.
- Install the ``pandas`` package:
- .. code-block:: bash
+ .. code-block::
conda install pandas # Anaconda
# or
pip install pandas # pip
-- Finally, ``cd`` into ``TensorFlow\scripts\preprocessing`` and run:
+- Finally, ``cd`` into ``TensorFlow/scripts/preprocessing`` and run:
- .. code-block:: bash
+ .. code-block::
# Create train data:
- python xml_to_csv.py -i [PATH_TO_IMAGES_FOLDER]/train -o [PATH_TO_ANNOTATIONS_FOLDER]/train_labels.csv
+ python generate_tfrecord.py -x [PATH_TO_IMAGES_FOLDER]/train -l [PATH_TO_ANNOTATIONS_FOLDER]/label_map.pbtxt -o [PATH_TO_ANNOTATIONS_FOLDER]/train.record
# Create test data:
- python xml_to_csv.py -i [PATH_TO_IMAGES_FOLDER]/test -o [PATH_TO_ANNOTATIONS_FOLDER]/test_labels.csv
+ python generate_tfrecord.py -x [PATH_TO_IMAGES_FOLDER]/test -l [PATH_TO_ANNOTATIONS_FOLDER]/label_map.pbtxt -o [PATH_TO_ANNOTATIONS_FOLDER]/test.record
# For example
- # python xml_to_csv.py -i C:\Users\sglvladi\Documents\TensorFlow\workspace\training_demo\images\train -o C:\Users\sglvladi\Documents\TensorFlow\workspace\training_demo\annotations\train_labels.csv
- # python xml_to_csv.py -i C:\Users\sglvladi\Documents\TensorFlow\workspace\training_demo\images\test -o C:\Users\sglvladi\Documents\TensorFlow\workspace\training_demo\annotations\test_labels.csv
-
-Once the above is done, there should be 2 new files under the ``training_demo\annotations`` folder, named ``test_labels.csv`` and ``train_labels.csv``, respectively.
-
-Converting from ``*.csv`` to ``*.record``
------------------------------------------
-
-Now that we have obtained our ``*.csv`` annotation files, we will need to convert them into TFRecords. Below is an example script that allows us to do just that:
-
-.. code-block:: python
-
- """
- Usage:
-
- # Create train data:
- python generate_tfrecord.py --label=