ImageAI - Happy Canada Day

Yesterday is Canada Day_ Happy Canada Day everybody…

Happy Canada Day

Just notice this news about ImageAI today. So, I just had it tested for fun. I seriously don’t want to talk about ImageAI too much, you can follow the author’s github and it shouldn’t be that hard to have everything done in minutes.

1. Preparation

1.1 Prerequisite Dependencies

As described by on ImageAI’s Github, multiple Python dependencies need to be installed:

  • Tensorflow
  • Numpy
  • SciPy
  • OpenCV
  • Pillow
  • Matplotlib
  • h5py
  • Keras

All packages can be easily installed by command:

1
pip3 install PackageName

Afterwards, ImageAI can be installed by a single command:

1
pip3 install https://github.com/OlafenwaMoses/ImageAI/releases/download/2.0.1/imageai-2.0.1-py3-none-any.whl

1.2 CNN Models

Two models are adopted as in the examples prediction and detection.

2. Examples

2.1 Prediction

Simple examples are given at https://github.com/OlafenwaMoses/ImageAI/tree/master/imageai/Prediction.

I modified FirstPrediction.py a bit as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
from imageai.Prediction import ImagePrediction
import os
import numpy as np

prediction = ImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath("/media/jiapei/Data/Programs/Python/ImageAI/resnet50_weights_tf_dim_ordering_tf_kernels.h5")
prediction.loadModel()


predictions, probabilities = prediction.predictImage(("./01.jpg"), result_count=5)
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction + " : " + np.format_float_positional(eachProbability, precision=3))

For the 1st image:

ImageAI Test Image 1

From bash, you will get:

1
2
3
4
5
6
7
$ python FirstPrediction.py 
2018-07-02 18:12:09.275412: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
convertible : 52.45954394340515
sports_car : 37.61279881000519
pickup : 3.1751133501529694
car_wheel : 1.817503571510315
minivan : 1.748703233897686

2.2 Detection

Simple examples are given at https://github.com/OlafenwaMoses/ImageAI/tree/master/imageai/Detection.

Trivial modification is also implemented upon FirstObjectDetection.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from imageai.Detection import ObjectDetection
import os
import numpy as np

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join("/media/jiapei/Data/Programs/Python/ImageAI/resnet50_coco_best_v2.1.0.h5"))
detector.loadModel()

input_image="./03.jpg"
output_image="./imageai_output_03.jpg"
detections = detector.detectObjectsFromImage(input_image, output_image)

for eachObject in detections:
print(eachObject["name"] + " : " + np.format_float_positional(eachObject["percentage_probability"], precision=3))

2.2.1 For the 2nd image:

ImageAI Test Image 2

From bash, you will get

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
$ python FirstDetection.py 
Using TensorFlow backend.
2018-07-02 18:23:09.634037: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2018-07-02 18:23:11.744790: W tensorflow/core/framework/allocator.cc:101] Allocation of 68300800 exceeds 10% of system memory.
2018-07-02 18:23:11.958081: W tensorflow/core/framework/allocator.cc:101] Allocation of 68403200 exceeds 10% of system memory.
2018-07-02 18:23:12.174739: W tensorflow/core/framework/allocator.cc:101] Allocation of 68403200 exceeds 10% of system memory.
2018-07-02 18:23:12.433540: W tensorflow/core/framework/allocator.cc:101] Allocation of 68403200 exceeds 10% of system memory.
2018-07-02 18:23:12.694631: W tensorflow/core/framework/allocator.cc:101] Allocation of 68403200 exceeds 10% of system memory.
2018-07-02 18:23:16.267111: W tensorflow/core/framework/allocator.cc:101] Allocation of 64224000 exceeds 10% of system memory.
2018-07-02 18:23:16.370939: W tensorflow/core/framework/allocator.cc:101] Allocation of 64224000 exceeds 10% of system memory.
2018-07-02 18:23:16.403353: W tensorflow/core/framework/allocator.cc:101] Allocation of 67435200 exceeds 10% of system memory.
person : 55.596935749053955
person : 66.90954566001892
person : 67.96322464942932
person : 50.80411434173584
bicycle : 64.87574577331543
bicycle : 72.0929205417633
person : 80.02063035964966
person : 85.82872748374939
truck : 59.56767797470093
person : 66.69963002204895
person : 79.37889695167542
person : 64.81361389160156
bus : 65.35580158233643
bus : 97.16107249259949
bus : 68.20474863052368
truck : 67.65954494476318
truck : 77.73774266242981
bus : 69.96590495109558
truck : 69.54039335250854
car : 61.26518249511719
car : 59.965676069259644

And, under the program folder, you will get an output image:

ImageAI Output Image for Test Image 2

2.2.2 For the 3rd image:

ImageAI Test Image 3

From bash, you will get

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ python FirstDetection.py 
Using TensorFlow backend.
2018-07-02 18:25:24.919351: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
person : 53.27633619308472
person : 52.71329879760742
person : 63.67729902267456
person : 55.00321388244629
person : 74.53054189682007
person : 51.54905915260315
motorcycle : 59.057921171188354
bus : 93.79504919052124
bus : 86.21828556060791
bus : 77.143394947052
person : 59.69809293746948
car : 71.79147601127625
car : 60.15858054161072
person : 62.758803367614746
person : 58.786213397979736
person : 76.49624943733215
car : 56.977421045303345
person : 67.86248683929443
person : 50.977784395217896
person : 52.3215651512146
motorcycle : 52.81376242637634
person : 76.79281234741211
motorcycle : 74.65972304344177
person : 55.96961975097656
person : 68.15704107284546
motorcycle : 56.21282458305359
bicycle : 71.78951501846313
motorcycle : 69.68616843223572
bicycle : 91.09067916870117
motorcycle : 83.16765427589417
motorcycle : 61.57424449920654

And, under the program folder, you will get an output image:

ImageAI Output Image for Test Image 3