Email Me: lezama at lacartita.com



ImageAI: Python Library For Recognizing Images

Ricardo Lezama — Image AI is an excellent, easy-to-use, Machine Learning wrapper that allows a python script to identify the dominant concept to describe an image. While this article covers a tiny usecase, I would recommend a user be aware of the need to install the right C++ dependencies.

Facebook Image AI

The developers are a group from a Facebook-backed outfit based in Nigeria. One of the principal developers is Moses Olafenwa, a founder of DeepQuest AI. Aside from this excellent python library, Olafenwa’s group develops AI servers for business applications.

Code Summary: ImageAI Predictions

In this summary, we will review the code examples here: https://github.com/OlafenwaMoses/ImageAI/tree/master/imageai/Prediction

Model Dependencies: ResNet

Aside from the libraries called through import statements, the more important dependencies for our test script using ImageAI’s python moduleare the different models that one can use to run a particular image against the model. In this particular example, we reference the RESNET model trained on ImageNet-1000 images. There is an annual competition in which various neural net models are compared against one another using the ImageNet libraries as a frame of reference.

ResNet is a model that uses ‘residual learning’ to create deeper learning.

According to the authors, ResNet “explicitly reformulate[s] the layers as learning residual functions with reference to the layer inputs, instead of learning unreferenced functions. We provide comprehensive empirical evidence showing that these residual networks are easier to optimize, and can gain accuracy from considerably increased depth.” 

Easy Ways To Interface

Instead of modifying the hardcoded line referencing an image, we modified the sample script to accept a simple command line argument. The script (posted originally here) has been modified slightly. I added a reference to the built-in sys library to pass on a command line argument.

Name the file “predicition.py”, then run the script (copy/paste) from wherever your image file is local. Also, the model is resnet50_weights_tf_dim_ordering_tf_kernels.h5, and is a Microsoft sponsored model developed by Kaiming He et al.

from imageai.Prediction import ImagePrediction
import sys

import os

prediction = ImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath("resnet50_weights_tf_dim_ordering_tf_kernels.h5")
prediction.loadModel()


predictions, percentage_probabilities = prediction.predictImage(sys.argv[1], result_count=5)
for index in range(len(predictions)):
    
	print(predictions[index] , " : " , percentage_probabilities[index])