TensorFlow Chicken Recognition in Google Colaboratory
TensorFlow Chicken Recognition in Google Colaboratory
I am working with a healthcare customer at the moment to improve machine learning for the benefit of subscriber physical and financial health. The goal is to run an increasing number of models and to shorten the time to discovery and health intervention. This was the perfect push for me to get my hands back on machine learning so that I could better understand the work our experts are delivering. I thought that sharing my steps to learning might be helpful to others.
Acknowledgements
As I am very new to both Python and machine learning, I could not have been successful without the assistance of these people and resources:
- Paige Bailey, Google AI Developer Advocate (@dynamicwebpaige). Paige is very talented and equally generous with her talent and time.
- Lin JungHsuan and his Medium article that my chicken recognition project is based on.
- Google Colaboratory. A working, hosted, free, Python/TensorFlow notebook environment with CPU, GPU, and TPU runtime options.
Google Colaboratory
Most examples of machine learning are written in Python. Python can sometimes be a bear to get running. My daily desktop is a Mac which has Python 2 installed as part of the OS which cannot be removed. Python 3 is the version of choice for machine learning compatibility. Getting both versions to behave on a Mac is doable but proved to be very difficult for me. Google Colaboratory let me just get to work with Python and to learn – exactly what Platform as a Service (PaaS) is meant for.
I read and played with every single Colaboratory tutorial in the Getting Started section on this page before beginning my own project. I’m glad I did. The section on using Google Drive for storage proved very useful in my project.
An Excellent Image Recognition Sample Project
Lin JungHsuan’s article laid out all the steps for me. I’m not sure I could have done this without the guidance provided by that article. My project is a copy of Lin’s project with some small changes.
Chickens
I needed something to recognize. In May 2018, we started raising six chicks who are now fully grown, quite social and laying eggs every day. I have plenty of photos of them to use as test images plus recognizing chickens just sounded fun. Fun definitely helps with learning.
Input
For TensorFlow image recognition to work, you need to feed the training program multiple folders of images to learn from. Each folder holds similar images and the folder name is used for that image class. I used Google Image Search to search and download images of chickens and huskies to create folders of training data. I used this Chrome Extension to make bulk image downloading easier.
Output
Once the model has been trained, a new image is compared to the model for classification and a classification confidence score. For example, the above photo of me with Zaja on my shoulder was classified as a photo of a chicken with 92.6% confidence.
Description of My Colaboratory Notebook
You should be able to access my notebook on Google Drive here. I will step through the notebook section by section.
TensorFlow and TPU Check
It turns out that a few of the TensorFlow (TF) 1.12 functions used in Lin JungHsuan’s example have been deprecated and do not work in TF 2.0. I wanted to make sure I was using TF 1.12.
Also, Colaboratory generously allows one to choose to run code on CPU, GPU or TPU runtimes. I chose TPU as my runtime in Colaboratory and checked TPU use in the code.
Google Drive for Persistent Image Store
Google Colaboratory is free and awesome, but not persistent. Google will reset your runtime when you are not using it including any files you have uploaded into the runtime. Thankfully, Google Drive is supported as persistent storage. When your runtime gets reset, just reconnect your Google Drive. I am using Google drive to hold my training images as well as my python scripts.
Get the retrain.py script
Use the retrain.py code to classify the images in your image folders
- The Inception-V3 model is downloaded
- “Bottlenecks” are created for each image.
- Train the image recognition model in the number of steps specified and periodically report on accuracy
Display the image to be classified
Classify the new image against the model just created
Thank You for Reading
Bravo to you for making it this far. Machine Learning is heavy stuff full of PhD-level concepts that I do not completely understand. It is nice to know that there are good examples and free, hosted environments out there to make experimentation easier. Oh, and the reason there are TensorFlow socks in the title image is that Paige Bailey was kind enough to provide me a discount on some stylin’ TF socks for challenging myself and sharing the results. Thanks Paige.
As always, I truly welcome your feedback.