Today, only a few buzzwords have captivated our imagination as profoundly as Artificial Intelligence (AI) and Machine Learning (ML) which have now become integral components of our everyday lives. However, to many, the question remains, “What is AI or ML, and how can we use it to create something practical?
In this article, we take a foundational look at AI and ML, it’s basic concepts, and explore a use case of how we can leverage these technologies to build a simple, yet practical image prediction python script with the ImageAI library.
Background
First, let’s briefly demystify some essential concepts that we mention throughout this article.
Artificial Intelligence (AI)
Artificial Intelligence is the simulation of human intelligence in machines that are programmed to think and learn like humans. It encompasses various subfields, including machine learning.
Machine Learning (ML)
Machine Learning is a subset of AI that focuses on the development of algorithms and statistical models that enable computers to improve their performance on a specific task through learning from data.
Deep Learning(DL)
Deep learning is a subset of Machine Learning that revolves around artificial neural networks. A deep learning model typically refers to a neural network with multiple layers used for tasks such as image recognition, natural language processing and speech recognition.
Image Prediction
Image prediction is the process of using AI and ML techniques to identify and classify objects or content within images.
ImageAI
ImageAI is a Python library that simplifies image recognition and prediction tasks by providing pre-trained models and a user-friendly interface.
Prerequisites
- A basic understanding of Python programming with version 3 or greater installed
- Make sure to install the ImageAI library. You can install it by running the following pip command —
pip install imageai
- Gather some images you want to perform predictions on. You can use your own images or find some online.
- You can also clone my personal GitHub repo for additional resource to follow along with —
Objectives
- Understand the basic concepts of AI and ML
- Use ImageAI’s pre-trained models for image prediction
- Interpret the predictions and probabilities generated by the model
Step 0— Install Necessary Libraries and Dependencies
First, let’s set up our environment by installing the needed dependencies from the ‘requirements.txt’ and ‘requirements_gpu.txt’ docs. Run the following commands —
pip install -r requirements.txt
pip install -r requirements_gpu.txt
Step 1 — Import Necessary Libraries and Set Execution Path
Now, we need to import the required libraries into our script, including ImageAI and os, then set the execution path to your current working directory to ensure the script can locate and load the files and models in current directory when executed.
You can accomplish this by the code below —
from imageai.Classification import ImageClassification
import os
execution_path = os.getcwd()
Step 2 — Choose Your Algorithm and Load the Model
Now, we have the option to choose from the four different image processing algorithms provided by ImageAI. You can automatically download one of algorithm path files by clicking on one of the links below
- MobileNetV2 — 4.82 mb, fastest prediction time but moderately accurate
- ResNet50 — by Microsoft Research, 98 mb, fast prediction time and high accuracy
- InceptionV3 — by Google Brain team, 91.6 mb, slow prediction time and higher accuracy
- DenseNet121 — by Facebook AI Research, 31.6 mb, slower prediction time and highest accuracy
Each has its own characteristics in terms of file size, prediction speed and accuracy. You are free to choose the one that suits your needs best.
For example, if you prefer higher accuracy over speed, go with DenseNet121.
In the article we will use MobileNetV2 due to its compact size of 4.82mb —
prediction = ImageClassification()
prediction.setModelTypeAsDenseNet121() # Choose your desired algorithm here
prediction.setModelPath(os.path.join(execution_path, "model_file.pth")) # Path to the model file
prediction.loadModel()
In the code block above, we first create an instance of the ImageClassification class which will be used for the image classification tasks, then we specify the algorithm method the model type will use — setModelTypeAsMobileNetV2().
Next, we choose the MobileNetV2 architecture as our model of choice. However, feel free to explore other model options by utilizing the corresponding algorithm method that best aligns with your specific needs and preferences.
Then, set the path to the pre-trained model file which contains the pre-trained weights and architecture for the selected deep learning model, using prediction.setModelPath() as the os.path.join() function constructs the full file path based on the previously set execution path execution_path.
Finally, we call the loadModel() method to load the pre-trained model as it initializes the model from the model file, making it ready for making predictions on the provided images.
Replace model_file.pth with the actual path to the pre-trained model file that you intend to use for your image classification task.
Step 3: Perform Image Prediction
Now, it’s time to perform the image prediction. You can replace house.jpg with the path to the image you want to predict —
predictions, probabilities = prediction.classifyImage(os.path.join(execution_path, "house.jpg"), result_count=5)
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction, " : ", eachProbability)
The classifyImage() method takes an image file as input, in this case, house.jpg, which is located in the current working directory. It then, analyzes the image and generates predictions for its content. Specifically, it identifies objects or categories present in the image and assigns a confidence probability to each prediction.
The results of the image classification are stored in two variables, predictions and probabilities. Predictions contain the labels or names of the objects or categories that the model predicts to be in the image, while probabilities contain the corresponding confidence scores for each prediction.
The for loop iterates through these predictions and probabilities using the zip() function, allowing us to pair each prediction with its associated confidence score. For each prediction, it prints both the label and the confidence score, for e.g — cat : 90. This information provides insights into the model’s assessment of the image’s content, indicating which objects it recognizes and how confident it is in its predictions.
Step 4: Run the Python Script
We’re down to our final step! In your terminal/CLI, navigate to the directory that contains the python script and all other files, then run the following command using the name of your Python file to execute the image prediction script—
python3 ai-image-processsing.py
You will receive some warnings which we will ignore at this time. However, at the tail end of the output, you will the predictions of the image given as seen in the image below —
The image processing mode used was able to have a 97.1359% confidence score that the image in the house.jpg file is a boathouse.
Remember, this is according to the algorithm model we used which is not as accurate as the others. However, it does recognize that is it a house 🙂
Congratulations!
You’ve successfully completed “Pythonic Predictions”. We’ve utilized the power of AI/ML to create a simple image prediction Python script.
For more information about ImageAI check out the official GitHub repo below —
Enjoyed this article? Sign up for our newsletter to receive regular insights and stay connected.

