Skip to main content

Tensorflow Crash Course - Part I

Mong Kok, Hongkong

This set of Notebooks provides a complete set of code to be able to train and leverage your own custom object detection model using the Tensorflow Object Detection API.

This article is based on a Tutorial by @nicknochnack.

Tensorflow Object Detection Walkthrough

Github Repository

Setting up Tensorflow

See Installation Guide

Arch Linux

0 python-tensorflow

If you want to have CUDA acceleration with __nVidia GPUs:

sudo pacman -Syu tensorflow-cuda python-tensorflow-cuda cuda cudnn

Packages (51) abseil-cpp-20211102.0-1 absl-py-0.14.1-1 cblas-3.10.0-1 hdf5-1.12.1-1 intel-mkl-2020.4.304-1 libaec-1.0.6-1 liblzf-3.6-3 nccl-2.11.4-2 onednn-2.4.4-1 pybind11-2.8.1-1 python-astor-0.8.1-3 python-astunparse-1.6.3-3 python-bleach-4.1.0-1 python-cachetools-4.2.4-1 python-dateutil-2.8.2-2 python-decorator-5.1.0-1 python-flatbuffers-2.0.5-1 python-gast03-0.3.3-1 python-google-auth-1.34.0-1 python-google-auth-oauthlib-0.4.4-1 python-grpcio-1.42.0-1 python-h5py-3.6.0-1 python-keras-2.7.0-1 python-keras-applications-1.0.8-6 python-keras-preprocessing-1.1.2-4 python-numpy-1.21.3-1 python-oauthlib-3.1.1-1 python-opt_einsum-3.3.0-3 python-pandas-1.3.4-1 python-pasta-0.2.0-4 python-protobuf-3.17.3-1 python-pyasn1-0.4.8-5 python-pyasn1-modules-0.2.8-4 python-pycuda-2021.1-5 python-pydot-1.4.2-1 python-pytools-2021.2.9-1 python-pytz-2021.3-2 python-requests-oauthlib-1.3.0-4 python-rsa-4.8-1 python-scipy-1.7.3-1 python-tensorboard_plugin_wit-1.8.1-1 python-tensorflow-estimator-2.6.0-1 python-termcolor-1.1.0-10 python-werkzeug-2.0.2-1 python-wheel-0.37.0-2 python-wrapt-1.12.1-4 tensorboard-2.7.0-1 cuda-11.5.1-1 cudnn-8.3.0.98-1 python-tensorflow-cuda-2.7.0-3 tensorflow-cuda-2.7.0-3

Total Download Size: 3137.74 MiB
Total Installed Size: 10745.45 MiB

Installing Bazel

Bazel is a tool that builds and tests Java, C++, Android, iOS, Go and a wide variety of other language platforms. Bazel runs on Windows, macOS, and Linux.

Arch Linux

sudo pacman -Syu bazel

Packages (7) java-environment-common-3-3 java-runtime-common-3-3 jdk11-openjdk-11.0.13.u8-1 jre11-openjdk-11.0.13.u8-1 jre11-openjdk-headless-11.0.13.u8-1 libnet-1:1.1.6-1 bazel-4.2.2-1

Total Download Size: 193.97 MiB
Total Installed Size: 362.47 MiB

Project Setup

cd workdir
mkdir -p tf-course/Tensorflow/workspace/images/{train,test,collection}
cd tf-course
python -m venv tfod
  • Step 3: Activate your virtual environment
source tfod/bin/activate # Linux
.\tfod\Scripts\activate # Windows
  • Step 4: Install dependencies and add virtual environment to the Python Kernel
pip install -r dependencies.txt
python -m pip install --upgrade pip
pip install ipykernel
python -m ipykernel install --user --name=tfodj

Verify that Jupyter Notebooks was installed:

jupyter notebook

Make sure that you have the option to choose your virtual environment as interpreter:

Tensorflow Object Detection Walkthrough

Data Collection

Collect images using the Notebook collecting_training_data.ipynb:

Tensorflow Object Detection Walkthrough

For the image capturing I noticed that the timing seemed to be off. It got better by deactivating the preview. I am not sure what is going wrong there for now. Might be an issue with the RTSP stream - using a webcam instead seemed fine. Just run the capture step a couple of times and hold on to the "best" 5 images for all labels.

To lable the images we are going to use labelImg, which is a graphical image annotation tool.

pip3 install labelImg

Successfully installed PyQt5-Qt5-5.15.2 PyQt5-sip-12.9.0 labelImg-1.8.3 pyqt5-5.15.6

Start the Software from your console with labelImg:

Tensorflow Object Detection Walkthrough

Open the directory where your captures were stored and press W and draw a box around your hand and label it according to the gesture.

Ctrl+s to save the annotation XML file for your image and repeat for all images.

Training Preparation

Manually divide collected images into two folders train and test. Copy 4 images (+ annotation file) of each label into the train folder and the remaining one into the test folder:

ls Tensorflow/workspace/images/test

metal.d31b4e19-6952-11ec-8d31-1c1b0dc5817f.jpg thumbsdown.b74e6640-6953-11ec-b6d3-1c1b0dc5817f.jpg
metal.d31b4e19-6952-11ec-8d31-1c1b0dc5817f.xml thumbsdown.b74e6640-6953-11ec-b6d3-1c1b0dc5817f.xml
ok.b9d7d438-6954-11ec-ae56-1c1b0dc5817f.jpg thumbsup.f1535776-6951-11ec-856e-1c1b0dc5817f.jpg
ok.b9d7d438-6954-11ec-ae56-1c1b0dc5817f.xml thumbsup.f1535776-6951-11ec-856e-1c1b0dc5817f.xml

ls Tensorflow/workspace/images/train

metal.25a3fdb5-6952-11ec-be18-1c1b0dc5817f.jpg thumbsdown.939cd20d-6954-11ec-b0ac-1c1b0dc5817f.jpg
metal.25a3fdb5-6952-11ec-be18-1c1b0dc5817f.xml thumbsdown.939cd20d-6954-11ec-b0ac-1c1b0dc5817f.xml
metal.c6fd71c4-6952-11ec-af7f-1c1b0dc5817f.jpg thumbsdown.9a973d84-6954-11ec-89bf-1c1b0dc5817f.jpg
metal.c6fd71c4-6952-11ec-af7f-1c1b0dc5817f.xml thumbsdown.9a973d84-6954-11ec-89bf-1c1b0dc5817f.xml
metal.cd667fbd-6953-11ec-a747-1c1b0dc5817f.jpg thumbsdown.a3a55b8c-6954-11ec-baf0-1c1b0dc5817f.jpg
metal.cd667fbd-6953-11ec-a747-1c1b0dc5817f.xml thumbsdown.a3a55b8c-6954-11ec-baf0-1c1b0dc5817f.xml
metal.d06a2256-6953-11ec-8d60-1c1b0dc5817f.jpg thumbsdown.aa4aa114-6953-11ec-a97c-1c1b0dc5817f.jpg
metal.d06a2256-6953-11ec-8d60-1c1b0dc5817f.xml thumbsdown.aa4aa114-6953-11ec-a97c-1c1b0dc5817f.xml
ok.adbee52a-6954-11ec-989c-1c1b0dc5817f.jpg thumbsdown.b3df44fe-6952-11ec-bee8-1c1b0dc5817f.jpg
ok.adbee52a-6954-11ec-989c-1c1b0dc5817f.xml thumbsdown.b3df44fe-6952-11ec-bee8-1c1b0dc5817f.xml
ok.b0c3a2e2-6954-11ec-8646-1c1b0dc5817f.jpg thumbsup.8dc99774-6952-11ec-a0a3-1c1b0dc5817f.jpg
ok.b0c3a2e2-6954-11ec-8646-1c1b0dc5817f.xml thumbsup.8dc99774-6952-11ec-a0a3-1c1b0dc5817f.xml
ok.b3c3f23a-6954-11ec-89b1-1c1b0dc5817f.jpg thumbsup.90d4df65-6952-11ec-b7d0-1c1b0dc5817f.jpg
ok.b3c3f23a-6954-11ec-89b1-1c1b0dc5817f.xml thumbsup.90d4df65-6952-11ec-b7d0-1c1b0dc5817f.xml
ok.b6ca07d3-6954-11ec-b156-1c1b0dc5817f.jpg thumbsup.96d98a6b-6952-11ec-8039-1c1b0dc5817f.jpg
ok.b6ca07d3-6954-11ec-b156-1c1b0dc5817f.xml thumbsup.96d98a6b-6952-11ec-8039-1c1b0dc5817f.xml

Make sure you always have an even number of images in both folders