Classifying images with Python and TensorFlow

Sometimes we look at things and wonder what it is. and when we try looking it up on google we can’t find it because we don’t know how to find a item with a text search if we don’t know what it is. A way to fix this is if you can take a picture of the thing and then use that in your search. Yes I know google is working on image search. But that’s no fun, at least for me it isn’t. and in my case i also wanted to create something that could give me more specific results and here is were Python and TensorFlow comes in.

In my case i wanted to create something that could tell me what kind of aquarium fish I’m looking at and later make it capable of telling me something about the fish. Now I’m not that far into the project yet so it can only recognize 11 different fish at the moment. but as I get more images to train on it will get better and i can add more fish. You can check it out here: sorry, hope you can live with the site being on danish.

Now what I’m going to talk about in the rest of the post here is not gonna be about fish but on how you can take my code and use it to set up a website to recognize what ever you want.

Note: I created this project on the Linux platform and don’t really know how well it converts to Windows or Mac, but if you can install all the dependencies on Windows and Mac you are properly going to be fine after some minor changes.


What you will need

Now I’m going to show you the simplest way to set this up but if you know your stuff and can setup flask for production go for it. It’s more stable and more shareable so I can only recommend it.

I should mention that if you just want a running classifier you should go and take a look at TensorFlow for Poets. but for what I’m going to do i will install everything myself.

First we need to install the dependencies that we need.

Use pip to install Flask and TensorFlow for python 2.7.
sudo pip install flask
sudo pip install TensorFlow

now I’m going to create a folder to keep everything in.
mkdir ImageClassifier

Now I’m going to clone the TensorFlow github repositorie into the folder i just made.
cd ImageClassifier
git clone

If everything went without problems you should now be ready for the next part where we are going to retrain Inception so we can use it on the site.


Time for some training

The first thing we should do now is to make two new folders tf_files and pictures.

mkdir pictures
mkdir tf_files

we will save the files from the retrained Inception in tf_files and all the images we are going to train on is going to be in pictures. So we will need to create more folders in pictures and name them after the thing the site should be able to recognize and then put some images of that thing into the folder. In my case i create 11 folders for different fish and put images of fish that match the folder name fx i put images of guppies in the folder names guppies.

Now while still based in the folder ImageClassifier we will need to run the following and wait, this is going to take some time.
python tensorflow/tensorflow/examples/image_retraining/ \ --bottleneck_dir=/tf_files/bottlenecks \ --how_many_training_steps 500 \ --model_dir=/tf_files/inception \ --output_graph=/tf_files/retrained_graph.pb \ --output_labels=/tf_files/retrained_labels.txt \ --image_dir pictures

CONGRATULATIONS!!! you have now trained Inception to recognize things you want it to recognize.

The only thing back to do is to get the code for the website, change a little in it and then run it.


Setting up the site

Ok. now we just have to get the code for the website, so you can host it. the code can be found on my github. or we can just do a clone command.
git clone

when that is done you properly wanna go into the tf_files folder that came with the site and switch the files out with the ones from your tf_files folder. the open in an editor. and I’m sorry I have to do this to you but because of my bad programming skills you will have to change the paths on line 9, 55, 59 and 93 so they match your system. but when that is done you can run it and flask will host the site on localhost:5000 where you now can upload your images and check out if your site can see what it is.

Leave a Reply

Your email address will not be published. Required fields are marked *