用10行Python代碼進(jìn)行圖像識(shí)別
隨著深度學(xué)習(xí)算法的興起和普及,人工智能領(lǐng)域取得了令人矚目的進(jìn)步,特別是在計(jì)算機(jī)視覺領(lǐng)域。21世紀(jì)的第二個(gè)十年迅速采用卷積神經(jīng)網(wǎng)絡(luò),發(fā)明了***進(jìn)的算法,大量訓(xùn)練數(shù)據(jù)的可用性以及高性能和高性價(jià)比計(jì)算的發(fā)明。計(jì)算機(jī)視覺中的一個(gè)關(guān)鍵概念是圖像分類; 這是軟件系統(tǒng)正確標(biāo)記圖像中主導(dǎo)對(duì)象的能力。
ImageAI是一個(gè)Python庫,旨在幫助開發(fā)人員構(gòu)建具有自包含計(jì)算機(jī)視覺功能的應(yīng)用程序和系統(tǒng)。
1. 安裝Python 3.5.1或更高版本和pip
(如果您已經(jīng)安裝了Python 3.5.1或更高版本,請(qǐng)?zhí)^本節(jié))
https://www.python.org/downloads/
2. 安裝ImageAI依賴項(xiàng)
- Tensorflow
- pip3 install --upgrade tensorflow
- Numpy
- pip3 install numpy
- SciPy
- pip3 install scipy
- OpenCV
- pip3 install opencv-python
- Matplotlib
- pip3 install matplotlib
- h5py
- pip3 install h5py
- Keras
- pip3 install keras
3. 安裝ImageAI庫
pip3 install https://github.com/OlafenwaMoses/ImageAI/raw/master/dist/imageai-1.0.2-py3-none-any.whl
4. 下載經(jīng)過ImageNet-1000數(shù)據(jù)集訓(xùn)練的ResNet Model文件,并將文件復(fù)制到您的python項(xiàng)目文件夾。
https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5
5. 創(chuàng)建一個(gè)名稱為python的文件(例如“FirstPrediction.py”),并將下面的代碼寫入其中。
- from imageai.Prediction import ImagePrediction
- import os
- execution_path = os.getcwd()
- prediction = ImagePrediction()
- prediction.setModelTypeAsResNet()
- prediction.setModelPath( execution_path + " esnet50_weights_tf_dim_ordering_tf_kernels.h5")
- prediction.loadModel()
- predictions, percentage_probabilities = prediction.predictImage("C:UsersMyUserDownloadssample.jpg", result_count=5)
- for index in range(len(predictions)):
- print(predictions[index] + " : " + percentage_probabilities[index])
sample.jpg
代碼結(jié)果:
- sports_car : 90.61029553413391
- car_wheel : 5.9294357895851135
- racer : 0.9972884319722652
- convertible : 0.8457873947918415
- grille : 0.581052340567112
代碼說明
現(xiàn)在讓我們分解代碼,以便了解它是如何工作的。上面的代碼工作如下:
- from imageai.Prediction import ImagePrediction
- import os
上面的代碼導(dǎo)入了ImageAI ImagePrediction類和python os類。
- execution_path = os.getcwd()
上面的代碼創(chuàng)建一個(gè)變量,它保存對(duì)包含python文件(在本例中為FirstPrediction.py)和ResNet模型文件的路徑的引用。
- prediction = ImagePrediction()
- prediction.setModelTypeAsResNet()
- prediction.setModelPath(execution_path +“ resnet50_weights_tf_dim_ordering_tf_kernels.h5”)
在上面的代碼中,我們?cè)?**行創(chuàng)建了一個(gè)ImagePrediction()類的實(shí)例,然后通過在第二行中調(diào)用.setModelTypeAsResNet(),將預(yù)測(cè)對(duì)象的模型類型設(shè)置為ResNet ,然后設(shè)置模型路徑將預(yù)測(cè)對(duì)象復(fù)制到模型文件(resnet50_weights_tf_dim_ordering_tf_kernels.h5)的路徑中,并將其復(fù)制到第三行的項(xiàng)目文件夾文件夾中。
- predictions, percentage_probabilities = prediction.predictImage("C:UsersMyUserDownloadssample.jpg", result_count=5)
在上面的行中,我們定義了2個(gè)變量,它等于被調(diào)用來預(yù)測(cè)圖像的函數(shù),這個(gè)函數(shù)是 .predictImage()函數(shù),我們?cè)谄渲薪馕隽藞D像的路徑,并且還指出了我們想要的預(yù)測(cè)結(jié)果的數(shù)量有(從1到1000的值)解析result_count = 5 。所述 .predictImage()函數(shù)將返回與所述***(2級(jí)陣列的對(duì)象的預(yù)測(cè))是預(yù)測(cè)和所述第二(陣列percentage_probabilities)是相應(yīng)的百分比概率為每個(gè)預(yù)測(cè)的陣列。
- for index in range(len(predictions)):
- print(predictions[index] + " : " + percentage_probabilities[index])
上述行獲取中的每個(gè)對(duì)象的預(yù)測(cè)陣列,并且還獲得從相應(yīng)百分比概率percentage_probabilities,***打印二者的結(jié)果到控制臺(tái)。
該 .predictImage()函數(shù)將在路徑中的圖像,也可以說明我們預(yù)計(jì)函數(shù)返回預(yù)測(cè)的數(shù)量(可選,默認(rèn)為5)。ImageNet-1000數(shù)據(jù)集中有1000個(gè)項(xiàng)目,ResNet模型在該數(shù)據(jù)集上進(jìn)行了訓(xùn)練,這意味著 .predictImage函數(shù)將返回1000個(gè)可能的預(yù)測(cè)值,并按其概率排列。
借助ImageAI,您可以輕松方便地將圖像預(yù)測(cè)代碼集成到您在python中構(gòu)建的任何應(yīng)用程序,網(wǎng)站或系統(tǒng)中。ImageAI庫支持其他算法和模型類型,其中一些針對(duì)速度進(jìn)行了優(yōu)化,另一些針對(duì)精度進(jìn)行了優(yōu)化。借助ImageAI,我們希望支持計(jì)算機(jī)視覺的更多專業(yè)方面,包括但不限于特殊環(huán)境和特殊領(lǐng)域的圖像識(shí)別以及自定義圖像預(yù)測(cè)。