diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..527699b --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +# Sphinx documentation +/docs/build/ +/docs/examples/data/ +/docs/source/auto_examples/ +!/docs/source/auto_examples/*.ipynb +!/docs/source/auto_examples/*.py.md5 +!/docs/source/auto_examples/*.rst +!/docs/source/auto_examples/images + +# Editors +.vscode/ +.idea/ + diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index fb3df05..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "python.pythonPath": "C:\\Users\\sglvladi\\Anaconda3\\envs\\sphinx\\python.exe" -} \ No newline at end of file diff --git a/README.md b/README.md index db7e3cd..9e8042c 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,22 @@ -# TensorFlowObjectDetectionTutorial [![Documentation Status](https://readthedocs.org/projects/tensorflow-object-detection-api-tutorial/badge/?version=latest)](http://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/?badge=latest) +# TensorFlowObjectDetectionTutorial This is a "short" (rather lengthy) tutorial that started off as self notes on how to set-up and get going with the TensorFlow Object Detection API. -To read the actual tutorial, you have the following 2 (more like 3) options: -1. Visit [http://tensorflow-object-detection-api-tutorial.readthedocs.io](http://tensorflow-object-detection-api-tutorial.readthedocs.io). (By far the easiest) -2. Clone/Download the repo: - - Open the "index.html" files under docs/build/ (Not guaranteed to be up-to-date) - - Build the docs using Sphinx and the open "index.html" (Guaranteed to be up-to-date) +There currently exist several versions of the tutorial, corresponding to the various different versions of TensorFlow. The two major versions are outlined below. + +## TensorFlow 2 Object Detection API tutorial + +[![TensorFlow 2.5](https://img.shields.io/badge/TensorFlow-2.5-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v2.5.0) [![Documentation Status](https://readthedocs.org/projects/tensorflow-object-detection-api-tutorial/badge/?version=latest)](http://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/?badge=latest) + +Since July 10, 2020 TensorFlow [announced that the Object Detection API officially supports TensorFlow 2](https://blog.tensorflow.org/2020/07/tensorflow-2-meets-object-detection-api.html). Therefore, an updated version of the tutorial was created to cover TensorFlow 2. + +To read the tutorial, visit [http://tensorflow-object-detection-api-tutorial.readthedocs.io](http://tensorflow-object-detection-api-tutorial.readthedocs.io). + +## TensorFlow Object Detection API tutorial + +[![TensorFlow 1.14](https://img.shields.io/badge/TensorFlow-1.14-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v1.14.0) [![Documentation Status](https://readthedocs.org/projects/tensorflow-object-detection-api-tutorial/badge/?version=tensorflow-1.14)](http://tensorflow-object-detection-api-tutorial.readthedocs.io/en/tensorflow-1.14/?badge=tensorflow-1.14) + +Originally, the tutorial was written with TensorFlow 1 in mind. Since it is possible that many users will still be interested in working with TensorFlow 1, a version of this tutorial will also be maintained for the foreseeable future. + +To read the tutorial, visit [https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/tensorflow-1.14/](https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/tensorflow-1.14/). diff --git a/docs/build/.buildinfo b/docs/build/.buildinfo deleted file mode 100644 index b3b3e26..0000000 --- a/docs/build/.buildinfo +++ /dev/null @@ -1,4 +0,0 @@ -# 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 -tags: 645f666f9bcd5a90fca523b33c5a78b7 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 deleted file mode 100644 index a38219f..0000000 Binary files a/docs/build/.doctrees/environment.pickle and /dev/null differ diff --git a/docs/build/.doctrees/index.doctree b/docs/build/.doctrees/index.doctree deleted file mode 100644 index 374d166..0000000 Binary files a/docs/build/.doctrees/index.doctree and /dev/null differ diff --git a/docs/build/.doctrees/install.doctree b/docs/build/.doctrees/install.doctree deleted file mode 100644 index 447ce6d..0000000 Binary files a/docs/build/.doctrees/install.doctree and /dev/null differ diff --git a/docs/build/.doctrees/issues.doctree b/docs/build/.doctrees/issues.doctree deleted file mode 100644 index 8edfd7c..0000000 Binary files a/docs/build/.doctrees/issues.doctree and /dev/null differ diff --git a/docs/build/.doctrees/training.doctree b/docs/build/.doctrees/training.doctree deleted file mode 100644 index 3cc8720..0000000 Binary files a/docs/build/.doctrees/training.doctree and /dev/null differ diff --git a/docs/build/_images/TensorBoard.JPG b/docs/build/_images/TensorBoard.JPG deleted file mode 100644 index 8493d2f..0000000 Binary files a/docs/build/_images/TensorBoard.JPG and /dev/null differ diff --git a/docs/build/_images/labelImg.JPG b/docs/build/_images/labelImg.JPG deleted file mode 100644 index c62009e..0000000 Binary files a/docs/build/_images/labelImg.JPG and /dev/null differ 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/_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 deleted file mode 100644 index bf6eb21..0000000 --- a/docs/build/_sources/index.rst.txt +++ /dev/null @@ -1,53 +0,0 @@ -.. TensorFlow setup documentation master file, created by - sphinx-quickstart on Wed Mar 21 19:03:08 2018. - You can adapt this file completely to your liking, but it should at least - contain the root `toctree` directive. - -TensorFlow 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. - - 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. - -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. - -The software tools which we shall use throughout this tutorial are listed in the table below: - -+---------------------------------------------+ -| Target Software versions | -+==============+==============================+ -| OS | Windows, Linux | -+--------------+------------------------------+ -| Python | 3.7 | -+--------------+------------------------------+ -| TensorFlow | 1.14 | -+--------------+------------------------------+ -| CUDA Toolkit | 10.0 | -+--------------+------------------------------+ -| CuDNN | 7.6.5 | -+--------------+------------------------------+ -| Anaconda | Python 3.7 (Optional) | -+--------------+------------------------------+ - -.. toctree:: - :maxdepth: 4 - :caption: Contents: - - install - camera - training - issues - - - -Indices and tables -================== - -* :ref:`genindex` -* :ref:`modindex` -* :ref:`search` diff --git a/docs/build/_sources/install.rst.txt b/docs/build/_sources/install.rst.txt deleted file mode 100644 index ffc788d..0000000 --- a/docs/build/_sources/install.rst.txt +++ /dev/null @@ -1,647 +0,0 @@ -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. - -- 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). - -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. - -.. 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. - - .. tab:: Linux - - - Go to ``_ - - Download `Anaconda Python 3.7 version for Linux `_ - - 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`. - -.. _tensorflow_cpu: - -TensorFlow CPU -~~~~~~~~~~~~~~ - -Getting setup with an installation of TensorFlow CPU can be done in 3 simple steps. - -.. 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: - - .. code-block:: posh - - conda create -n tensorflow_cpu pip python=3.7 - -* 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: - - .. code-block:: posh - - activate tensorflow_cpu - -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> - -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: - - .. code-block:: posh - - pip install --ignore-installed --upgrade tensorflow==1.14 - -- Wait for the installation to finish - -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: - - .. 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_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 - - 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 - -- Finally, run the following: - - .. code-block:: python - - >>> print(sess.run(hello)) - b'Hello, TensorFlow!' - -.. _tensorflow_gpu: - -TensorFlow 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. - -Before proceeding to install TesnsorFlow GPU, you need to make sure that your system can satisfy the following requirements: - -+-------------------------------------+ -| Prerequisites | -+=====================================+ -| Nvidia GPU (GTX 650 or newer) | -+-------------------------------------+ -| CUDA Toolkit v10.0 | -+-------------------------------------+ -| 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. - - .. tab:: Linux - - Follow this `link `_ to download and install CUDA Toolkit 10.0 for your Linux distribution. - -.. _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``. - - .. 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 `_ - - Follow the instructions under Section 2.3.1 of the `CuDNN Installation Guide `_ to install CuDNN. - -.. _set_env: - -Environment Setup -***************** -.. 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 ``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 - - 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}} - -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: - - .. 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: - - .. code-block:: posh - - activate tensorflow_gpu - -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_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: - - .. code-block:: posh - - 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: - -.. code-block:: posh - - 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. - -- 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``. -- 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 - -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 -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) -- 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: - - .. code-block:: python - - # From within TensorFlow/models/research/ - protoc object_detection/protos/*.proto --python_out=. - -.. important:: - - If you are on Windows and using Protobuf 3.5 or later, the multi-file selection wildcard (i.e ``*.proto``) may not work but you can do one of the following: - - .. tabs:: - - .. tab:: Windows Powershell - - .. code-block:: python - - # From within TensorFlow/models/research/ - Get-ChildItem object_detection/protos/*.proto | foreach {protoc "object_detection/protos/$($_.Name)" --python_out=.} - - - .. tab:: Command Prompt - - .. code-block:: python - - # From within TensorFlow/models/research/ - for /f %i in ('dir /b object_detection\protos\*.proto') do protoc object_detection\protos\%i --python_out=. - - -.. [#] 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) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -The ``pycocotools`` package should be installed if you are interested in using COCO evaluation metrics, as discussed in :ref:`evaluation_sec`. - -.. tabs:: - - .. tab:: Windows - - Run the following command to install ``pycocotools`` with Windows support: - - .. code-block:: bash - - 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: - - .. code-block:: bash - - git clone https://github.com/cocodataset/cocoapi.git - cd cocoapi/PythonAPI - make - cp -r pycocotools /TensorFlow/models/research/ - -.. note:: The default metrics are based on those used in Pascal VOC evaluation. - - - To use the COCO object detection metrics add ``metrics_set: "coco_detection_metrics"`` to the ``eval_config`` message in the config file. - - - 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: - -Test your Installation -~~~~~~~~~~~~~~~~~~~~~~ - -- 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:: posh - - # From within TensorFlow/models/research/object_detection - jupyter notebook - -- This should start a new ``jupyter notebook`` server on your machine and you should be redirected to a new tab of your default browser. - -- Once there, simply follow `sentdex's Youtube video `_ to ensure that everything is running smoothly. - -- When done, your notebook should look similar to the image bellow: - - .. image:: ./_static/object_detection_tutorial_output.png - :width: 90% - :alt: alternate text - :align: center - -.. 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 - - - 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: - - .. code-block:: python - - 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. - - - 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. - - -.. n - -.. _labelImg_install: - -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``: - -.. code-block:: bash - - pip install labelImg - -3. ``labelImg`` can then be run as follows: - -.. code-block:: bash - - labelImg - # or - 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: - -1. Inside you ``TensorFlow`` folder, create a new directory, name it ``addons`` and then ``cd`` into it. - -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: - -.. code-block:: bash - - TensorFlow - ├─ addons - │ └── labelImg - └─ models - ├── official - ├── research - ├── samples - └── tutorials - -4. ``labelImg`` can then be run as follows: - -.. code-block:: bash - - # From within Tensorflow/addons/labelImg - labelImg - # or - labelImg [IMAGE_PATH] [PRE-DEFINED CLASS FILE] - -Build from source (Hard) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -The steps for installing from source follow below. - -**1. Download labelImg** - -- Inside you ``TensorFlow`` folder, create a new directory, name it ``addons`` and then ``cd`` into it. -- To download the package you can either use `Git `_ to clone the `labelImg repo `_ inside the ``TensorFlow\addons`` folder, or you can simply download it as a `ZIP `_ and extract it's contents inside the ``TensorFlow\addons`` folder. To keep things consistent, in the latter case you will have to rename the extracted folder ``labelImg-master`` to ``labelImg``. [#]_ -- 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 - -.. [#] 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: - - .. tabs:: - - .. tab:: Windows - - .. code-block:: bash - - conda install pyqt=5 - pyrcc5 -o libs/resources.py resources.qrc - - .. tab:: Linux - - .. code-block:: bash - - sudo apt-get install pyqt5-dev-tools - sudo pip install -r requirements/requirements-linux-python3.txt - make qt5py3 - - -**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: - - .. code-block:: posh - - # From within Tensorflow/addons/labelImg - python labelImg.py - # or - python labelImg.py [IMAGE_PATH] [PRE-DEFINED CLASS FILE] - - - diff --git a/docs/build/_sources/issues.rst.txt b/docs/build/_sources/issues.rst.txt deleted file mode 100644 index 3358439..0000000 --- a/docs/build/_sources/issues.rst.txt +++ /dev/null @@ -1,66 +0,0 @@ -.. _issues: - -Common issues -============= - -Below is a list of common issues encountered while using TensorFlow for objects detection. - -Python crashes - TensorFlow GPU -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -If you are using :ref:`tensorflow_gpu` and when you try to run some Python object detection script (e.g. :ref:`test_tf_models`), after a few seconds, Windows reports that Python has crashed then have a look at the `Anaconda/Command Prompt` window you used to run the script and check for a line similar (maybe identical) to the one below: - - .. code-block:: python - - 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. - -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.0.5. - -Cleaning up Nvidia containers (TensorFlow GPU) -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Sometimes, when terminating a TensorFlow training process, the Nvidia containers associated to the process are not cleanly terminated. This can lead to bogus errors when we try to run a new TensorFlow process. - -Some known issues caused by the above are presented below: - -- Failure to restart training of a model. Look for the following errors in the debugging: - - .. code-block:: python - - 2018-03-23 03:03:10.326902: E C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\stream_executor\cuda\cuda_dnn.cc:385] could not create cudnn handle: CUDNN_STATUS_ALLOC_FAILED - 2018-03-23 03:03:10.330475: E C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\stream_executor\cuda\cuda_dnn.cc:352] could not destroy cudnn handle: CUDNN_STATUS_BAD_PARAM - 2018-03-23 03:03:10.333797: W C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow/stream_executor/stream.h:1983] attempting to perform DNN operation using StreamExecutor without DNN support - 2018-03-23 03:03:10.333807: I C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\stream_executor\stream.cc:1851] stream 00000216F05CB660 did not wait for stream: 00000216F05CA6E0 - 2018-03-23 03:03:10.340765: I C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\stream_executor\stream.cc:4637] stream 00000216F05CB660 did not memcpy host-to-device; source: 000000020DB37B00 - 2018-03-23 03:03:10.343752: F C:\tf_jenkins\workspace\rel-win\M\windows-gpu\PY\36\tensorflow\core\common_runtime\gpu\gpu_util.cc:343] CPU->GPU Memcpy failed - -To solve such issues in Windows, open a `Task Manager` windows, look for Tasks with name ``NVIDIA Container`` and kill them by selecting them and clicking the `End Task` button at the bottom left corner of the window. - -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 ..." -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -In some versions of Tensorflow, you may see errors that look similar to the ones below: - -.. code-block:: python - - ... - WARNING:tensorflow:Entity > could not be transformed and will be executed as-is. Please report this to the AutgoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: converting >: AssertionError: Bad argument number for Name: 3, expecting 4 - WARNING:tensorflow:Entity > could not be transformed and will be executed as-is. Please report this to the AutgoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: converting >: AssertionError: Bad argument number for Name: 3, expecting 4 - ... - -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: - -.. code-block:: bash - - pip install gast==0.2.2 \ No newline at end of file diff --git a/docs/build/_sources/training.rst.txt b/docs/build/_sources/training.rst.txt deleted file mode 100644 index 3167297..0000000 --- a/docs/build/_sources/training.rst.txt +++ /dev/null @@ -1,957 +0,0 @@ -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 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: - -1. How to organise your workspace/training files -2. How to prepare/annotate image datasets -3. How to generate tf records from such datasets -4. How to configure a simple training pipeline -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 -~~~~~~~~~~~~~~~~~~~ - -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 - -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 - -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 - └─ README.md - -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. - -- ``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. -- ``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 -~~~~~~~~~~~~~~~~~ - -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``. -- 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. - - .. code-block:: bash - - python labelImg.py ..\..\workspace\training_demo\images - -- 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: - -.. image:: ./_static/labelImg.JPG - :width: 90% - :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. - -.. _image_partitioning_sec: - -Partitioning the images -~~~~~~~~~~~~~~~~~~~~~~~ - -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``. - -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: - -.. code-block:: bash - - python partition_dataser.py -x -i 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. - - -Creating 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``: - -.. code-block:: json - - item { - id: 1 - name: 'cat' - } - - item { - id: 2 - name: 'dog' - } - -Label map files have the extention ``.pbtxt`` and should be placed inside the ``training_demo\annotations`` folder. - -Creating 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. - -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: - -.. code-block:: bash - - TensorFlow - ├─ addons - │ └─ labelImg - ├─ models - │ ├─ official - │ ├─ research - │ ├─ samples - │ └─ tutorials - ├─ scripts - │ └─ preprocessing - └─ workspace - └─ training_demo - -Converting ``*.xml`` to ``*.csv`` ---------------------------------- - -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. - -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. -- Install the ``pandas`` package: - - .. code-block:: bash - - conda install pandas # Anaconda - # or - pip install pandas # pip - -- Finally, ``cd`` into ``TensorFlow\scripts\preprocessing`` and run: - - .. code-block:: bash - - # 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 - - # 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=