I thought I would start off the AI topic with a brief tutorial on how to get the Tensorflow object detection API up and running on Windows. The object detection API is a series of tools that allow us to quickly test or train state-of-the-art object detection algorithms; coding these from scratch would be incredibly time consuming and so this tool is immensely valuable.
Installing the API is one of those things that's best done by following a tutorial; however the Tensorflow documentation is sometimes a little lacking and is also focussed towards Linux or Mac.
I will try to provide a bit of insight into the reason the various steps are needed; and also give some tips on how to check if what you've done in each step has worked. Things sometimes fail silently and this can be frustrating...
I am using Windows 10 on a 64-bit system; I will be putting everything inside a root folder
Step 0 : Prerequesites
You will need a few things set up before beginning. I highly recommend Anaconda for dealing with Python code. To find out a bit more about Anaconda (as I am assuming you have some familiarity with it) go here.
Once installed you should have access to the "Anaconda Prompt" app. Open this up and it should like the image below
Step 1 : Install Dependencies
The first thing you *need* to do (or I highly recommend you do), is create a new conda environment so that you have a clean area to build up the dependencies. Use the following lines of code
In the first line the environment is created with the name "tf_detect" and python version 3.6; in the second line we move into the environment (you should notice the prompt change from base to tf_detect). The third line should return the location of pip, which is a python package manager.
This output is good; there are two pip executables, one in my new environment and one in the base environment. The first one will be the one that gets used with the pip command and this should be the environment one (the reason I highlight this is that if you use the wrong pip, you will install things into the wrong environment and end up very confused).
Next, we need to install Tensorflow version 1 (I suggest the latest version which is 1.15.2 as of writing) using pip. We should also install jupyter so that we can run notebooks
Now you can check the success of the Tensorflow installation by listing all the packages on your environment. You should use the command
which provides the following output on my machine (I have scrolled to the relevant section)
The fact that Tensorflow appears shows that it has been installed correctly (and is the 2.1.0 version, yours should be 1.15.2).
Another way to check is to open the python repr, and try to import the package. If it works without error then it is correctly installed; as shown below.
Step 2 : Install Object Detection API
Ok; now the dependencies are set up we can start with the Tensorflow object detection API. First off, we want to get the source code. There is a posh and a non-posh way to do this
2.1 Posh
Use a git client to clone the repository. My favourite is GitBash as this has many of the usual Linux commands and function basically like a linux terminal in Windows (you can use it for non-git things!). Once you have installed it, open the GitBash prompt.
Go to the directory that you want to put the Tensorflow object detector code and then clone it (see below)
You should see some sensible output and once complete, the folder will appear in tensorflow_detection.
2.2 Non-Posh
Go to https://github.com/tensorflow/models and select to download the zip file
Place the zip file in our root folder and extract it. That should have placed the code inside the "tensorflow_detection" folder.
Step 3 : Dealing with Protobuf
For some reason; many of the files in this code are "protobuf" (these are the ones with the .proto extension) and you need to convert them to python .py files. To do this we need to download the converter from
You should make sure you download the correct one for the operating system, for me it was "protoc-3.11.3-win64.zip" for 64-bit Windows. Place this zip file in /tensorflow_detection/ and extract it. You should have a ".exe" file inside `protoc-3.11.3-win64\bin` called protoc.exe. This will do the conversions for us.
Go back to the anaconda prompt; we are now going to run this executable on all the proto files (which handily are all in the same folder). First go to the correct folder, and then run the executable on the relevant files;
To check this, firstly there shouldn't be any warnings, and secondly there should be .py files in the protos/ folder. Now, we can install the object detection API onto our environment
To test this, try to import object_detection from within python, if it works, all good!
Step 4 : Testing the Installation
The quickest way to test the install is to use the example Jupyter notebook supplied with the Tensorflow object detection API; it can be found in
However; this is designed to be used with Google colab so I have simplified it a bit for running locally on Jupyter notebooks and you can download it here.
Place it in the root folder. To open jupyter do the following
this should open your preferred browser and start a tab with a Jupyter server on. You should see the tutorial file straight away since we opened the server in the directory containing it. Click it to open. I have left all the output in the Jupyter notebook except for the images so you can see the expected output.
In this notebook you will find an example run of an object detector as well as a segmentation algorithm. Actually it's quite fun to spend a few minutes shoving some of your own images in and testing them. To do that, put your images in .jpg form and put this files in
and rerun the relevant parts of the notebook
In the next blog; I will show you how to train an object detector on new data.
Left : An example of an object detector from the notebook. Right : A segmentation algorithm
References
Tensorflow Object Detection API on GitHub :
Other Tutorials
Comments