Python基於 ImageAI 模塊實踐 idenprof數據集識別預測分析

Python基於 ImageAI 模塊實踐 idenprof數據集識別預測分析
       圖像識別早已不是很新鮮的話題了,很多數據處理的任務到最後都會歸爲圖像識別中,在之前的很多工作中,我陸陸續續也接觸了很多相關的工作,從最開始數據處理,到模型搭建與最終上線也都經歷,大多數時候模型都是自己搭建的,雖然說現在keras的出現極大地簡化了模型的搭建工作,但是整個過程還是需要自己去實踐完成的,對於很多的初學者來說並不是很容易的。

      今天發現了一個好玩的庫——ImageAI,簡單的說一下我的理解就是對keras的又一層封裝,但又不全是這樣。ImageAI簡化了整個圖像識別和目標檢測的工作,今天想來簡單看看,整個模塊的能力。

     使用的使用網上公開的數據集 idenprof ,分爲train和test兩個數據集,每個集合裏面共有10個類別,數據集截圖如下:

       具體實踐如下:
 

!usr/bin/env python

encoding:utf-8

from future import division

"""
__Author__:沂水寒城
功能: python基於 ImageAI 模塊實現 idenprof 數據集預測識別
當前官網中 ImageAI 主要提供四種類型的預測模型分別如下:
SqueezeNet(預測速度最快 正確率中等)
ResNet50 (預測速度快 正確率較高)
InceptionV3(預測速度慢 正確率高)
DenseNet121(預測速度更慢 正確率最高)
"""

import os
import threading
from imageai.Prediction import ImagePrediction
from imageai.Prediction.Custom import ModelTraining
from imageai.Prediction.Custom import CustomImagePrediction

def modelPredictDemo(model_path='officeModels/resnet_model_ex-020_acc-0.651714_idenprof.h5',

                 class_path='officeModels/model_class_idenprof.json',
                 pic_dir='idenprofTestPic/',classNum=10,resNum=1,flag=True):
'''
圖像識別模型demo
''' 
right=0
if flag:
    prediction=CustomImagePrediction()  
else:
    prediction=ImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath(model_path)
prediction.setJsonPath(class_path)
prediction.loadModel(num_objects=classNum)
all_files=[]
pic_list=os.listdir(pic_dir)  
for one_pic in pic_list:
    all_files.append(pic_dir+one_pic)
for one_pic in all_files:
    predictions,probabilitys=prediction.predictImage(one_pic,result_count=resNum)
    true_label=one_pic.split('/')[-1].split('.')[0].strip()
    for predict,probability in zip(predictions,probabilitys):
        print(true_label,'===>',predict, " : ", str(probability))
        if true_label.split('-')[0].strip()==predict:
            right+=1
acc=right/len(all_files)
print('Accuracy: ',acc)

if __name__=='__main__':

modelPredictDemo(model_path='officeModels/resnet_model_ex-020_acc-0.651714_idenprof.h5',
                 class_path='officeModels/model_class_idenprof.json',
                 pic_dir='idenprofTestPic/',classNum=10,resNum=1,flag=True)

      模型輸出如下:

      由於自己的PC機太次了,我沒有選擇去自己訓練模型,直接下載了官方提供的預訓練模型,隨機抽取了250張圖片數據作爲測試使用,粗略地計算了一下準確度達到了55.64%以上。

      感覺ImageAI使用起來的確是簡單了很多,但是模型的種類和靈活性反而不如自己搭建模型了,這裏更像是一個社區,自己可以貢獻自己的數據集和預訓練模型供別人使用,總之,好的東西總是會爲開發者提供便利的,記錄學習!

作者:Together_CZ
來源:CSDN
原文:https://blog.csdn.net/Together_CZ/article/details/96714163
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章