With the help of powerful computers such as NVIDIA GPUs and state of the art Deep Learning algorithms for image recognition such as :
- AlexNet by Alex Krizhevsky et al in 2012
- SqueezeNet by Forrest Landola et al in 2016
- DenseNet by Gao Huang et al in 2016
Before, to train an AI model that can recognize whatever you want it to recognize in pictures, involves lots of expertise in Applied Mathematics and use Deep Learning Libraries. To write the code for the algorithm and fit the code to your images involves lots of time and stress.
So, AI Commons has developed a python library that can let you train an AI model that can recognize images in just 5 simple lines of code. The python library is ImageAI, a library built to let students, developers and researchers with all levels of expertise to build systems and applications with state-of-the-artcomputer vision capabilities using between 5 to 15 simple lines of code.
Now, lets walk through the code...
To train AI model we need collection of images called datasets. Dataset contains hundreds to thousands of sample images of objects you want your AI model to recognize.
For this tutorial, we have a dataset called IdenProf. IdenProf (Identifiable Professionals) is a dataset that contains 11,000 pictures of 10 different professionals that humans can see and recognize their jobs by their mode of dressing. The classes of professionals whose pictures are in this dataset are as below:
This dataset is split into 9000 (900 pictures for each profession)pictures to train the artificial intelligence model and 2000 (200 pictures for each profession)pictures to test the performance of the artificial intelligence model as it is training.IdenProf has been properly arranged and made ready for training your artificial intelligence model to recognize professionals by their mode of dressing.
- First you must download the zip of IdenProf dataset via this link.
2. Because training artificial intelligence models require high performance computer systems, I strongly advice that you ensure your computer/laptop that you want to use for this training has NVIDIA GPU. Alternatively, you can use Google Colab for this experiment has it offers a free NVIDIA K80 GPU for experiments.
· Then you have to install ImageAI and its dependencies.
1) Download and install Python 3 from official Python Language website
2) Install the following dependencies via pip:
pip install tensorflow
pip install numpy
pip install scipy
pip install opencv-python
pip install pillow
pip install matplotlib
pip install h5py
pip install keras
-> Create a python file with any name you want to give it, for example “FirstTraining.py”.
-> Copy the zip of the IdenProf dataset into the folder where your Python file is. Then unzip it into the same folder.
-> Then copy the code below into the python file (e.g FirstTraining.py).
from imageai.Prediction.Custom import ModelTraining model_trainer = ModelTraining() model_trainer.setModelTypeAsResNet() model_trainer.setDataDirectory("idenprof") model_trainer.trainModel(num_objects=10, num_experiments=200, enhance_data=True, batch_size=32, show_network_summary=True)
That’s it! That’s all the code you need to train your artificial intelligence model. Before you run the code to start the training, let us explain the code.
In the first line, we imported ImageAI’s model training class. In the second line we, created an instance of the model training class. In the third line, we set the model type to ResNet (there are four model types available which are SqueezeNet, ResNet, InceptionV3 and DenseNet). In the fourth line, we set the data directory (dataset directory) to the folder of the dataset zip file you unzipped. Then in the fifth line, we call the trainModel function and specified the following values:
• number_objects : This refers to the number of different types of professionals in the IdenProf dataset.
• num_experiments : This is the number of times the model trainer will study all the images in the idenprof dataset in order to achieve maximum accuracy.
• Enhance_data (Optional) : This is to tell the model trainer to create modified copies of the images in the IdenProf dataset to ensure maximum accuracy is achieved.
• batch_size: This refers to the number of images the set that the model trainer will study at once, until it has studied all the images in the IdenProf dataset.
• Show_network_summary (Optional) : This is to show the structure of the model type you are using to train the artificial intelligence model.
Now you can start run the Python file and start the training. When the training starts, you will see results like the one below:
===================================== Total params: 23,608,202 Trainable params: 23,555,082 Non-trainable params: 53,120 ______________________________________ Using Enhanced Data GenerationFound 4000 images belonging to 4 classes. Found 800 images belonging to 4 classes. JSON Mapping for the model classes saved to C:\Users\User\PycharmProjects\FirstTraining\idenprof\json\model_class.json Number of experiments (Epochs) : 200 Epoch 1/100 1/280 [>.............................] - ETA: 52s - loss: 2.3026 - acc: 0.2500 2/280 [>.............................] - ETA: 52s - loss: 2.3026 - acc: 0.2500 3/280 [>.............................] - ETA: 52s - loss: 2.3026 - acc: 0.2500 .............................., .............................., .............................., 279/280 [===========================>..] - ETA: 1s - loss: 2.3097 - acc: 0.0625Epoch 00000: saving model to C:\Users\User\PycharmProjects\FirstTraining\idenprof\models\model_ex-000_acc-0.100000.h5 280/280 [==============================] - 51s - loss: 2.3095 - acc: 0.0600 - val_loss: 2.3026 - val_acc: 0.1000
Let us explain the details shown above:
1. The statement “JSON Mapping for the model classes saved to C:\Users\User\PycharmProjects\FirstTraining\idenprof\json\model_class.json” means the model trainer has saved a JSON file for the idenprof dataset which you can use to recognize other pictures with the custom image prediction class (explanation available as you read further).
2. The line Epoch 1/200 means the network is performing the first training of the targeted 200
3. The line 1/280 [>………………………..] — ETA: 52s — loss: 2.3026 — acc: 0.2500 represents the number of batches that has been trained in the present experiment
4. The line Epoch 00000: saving model to C:\Users\User\PycharmProjects\FirstTraining\idenprof\models\model_ex-000_acc-0.100000.h5 refers to the model saved after the present training. The ex_000 represents the experiment at this stage while the acc0.100000 and valacc: 0.1000 represents the accuracy of the model on the test images after the present experiment (maximum value value of accuracy is 1.0). This result helps to know the best performed model you can use for custom image prediction.
Once you are done training your artificial intelligence model, you can use the “CustomImagePrediction” class to perform image prediction with you’re the model that achieved the highest accuracy.
Just in case you have not been able to train the artificial intelligence model yourself due to lack of accessing an NVIDIA GPU, for the purpose of this tutorial, we have provided an artificial intelligence model we have trained on the IdenProf dataset which you can use right now to predict new images of any of the 10 professionals that is in the dataset. This model achieved over 79%accuracy after 61 training experiments. Click this link to download the model. Also, if you have not perform the training yourself, also download the JSON file of the idenprof model via this link. Then, you are ready to start recognizing professionals using the trained artificial intelligence model. Just follow the instructions below.
Next, create another Python file and give it a name, for example FirstCustomImageRecognition.py . Copy the artificial intelligence model you downloaded above or the one you trained that achieved the highest accuracy and paste it to the folder where your new python file (e.g FirstCustomImageRecognition.py ) . Also copy the JSON file you downloaded or was generated by your training and paste it to the same folder as your new python file. Copy a sample image(s) of any professional that fall into the categories in the IdenProf dataset to the same folder as your new python file.
Then copy the code below and put it into your new python file
from imageai.Prediction.Custom import CustomImagePrediction import os execution_path = os.getcwd() prediction = CustomImagePrediction() prediction.setModelTypeAsResNet() prediction.setModelPath("idenprof_061-0.7933.h5") prediction.setJsonPath("idenprof_model_class.json") prediction.loadModel(num_objects=10) predictions, probabilities = prediction.predictImage("image.jpg", result_count=3) for eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction , " : " , eachProbability)
View sample image and result below.
waiter : 99.99997615814209 chef : 1.568847380895022e-05 judge : 1.0255866556008186e-05
That was easy! Now let’s explain the code above that produced this prediction result.
The first and second lines of code above imports the ImageAI’s CustomImagePrediction class for predicting and recognizing images with trained models and the python os class. The third line of code creates a variable which holds the reference to the path that contains your python file (in this example, your FirstCustomImageRecognition.py) and the ResNet model file you downloaded or trained yourself. In the above code, we created an instance of the ImagePrediction() class in the fourth line, then we set the model type of the prediction object to ResNet by calling the .setModelTypeAsResNet() in the fifth line and then we set the model path of the prediction object to the path of the artificial intelligence model file (idenprof_061–0.7933.h5) we copied to the project folder folder in the sixth line. In the seventh line, we set the path of the JSON file we copied to the folder in the seventh line and loaded the model in the eightieth line. Finally, we ran prediction on the image we copied to the folder and print out the result to the Command Line Interface.
So far, you have learnt how to use ImageAI to easily train your own artificial intelligence model that can predict any type of object or set of objects in an image.
You can find all the details and documentation use ImageAI for training custom artificial intelligence models, as well as other computer vision features contained in ImageAI on the official GitHub repository.
If you find this article helpful and enjoyed it, feel free to share it with friends and colleagues.
Made by @OlafenwaMoses you can contact him for the further updates.