ImageAI 學習

最近在學習ImageAI對圖片的處理,在此記錄下學習過程。

參考:ImageAI 的 Github 鏈接

目標:用自己的圖片庫實現液晶屏異常預測。

1. 將自己的圖片整理成224×224(模型默認)像素的大小,新建一個tf-train目錄(名字隨便起),在該目錄下分爲train和test兩個目錄,目錄下將正常的圖片放在normal文件夾下,將存在異常的圖片放在abnormal文件夾下,文件夾的名稱就是圖片類別的名稱。

——>——>在train和test目錄下都有這兩個文件夾,train目錄下共有512張圖片,test有64張。

2.新建一個模型訓練的.py文件

from imageai.Prediction.Custom import ModelTraining

model_trainer = ModelTraining()
model_trainer.setModelTypeAsResNet()
model_trainer.setDataDirectory("tf-train")
model_trainer.trainModel(num_objects=2, num_experiments=100, enhance_data=True, batch_size=4, show_network_summary=True)

由於本項目只有兩類目標,所以num_object = 2, 設置迭代訓練100次後停止,batch_size可以根據GPU內存大小適當設置,我一開始設置32GPU跑步起來,所以改爲4了。

開始等待模型迭代優化,共迭代100次,每次訓練次數:圖片數量/batch=512/4=128

3.訓練完成後,會在tf-train目錄下生產兩個文件夾,joson文件夾下放的是類別標籤,models文件夾下放的是100次的訓練模型。

           

4.實現預測

from imageai.Prediction.Custom import CustomImagePrediction
import os
import glob
import numpy as np

execution_path = os.getcwd()

# 指定訓練好的模型
prediction = CustomImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath(os.path.join(execution_path, "model_ex-100_acc-1.000000.h5"))
prediction.setJsonPath(os.path.join(execution_path, "model_class.json"))
prediction.loadModel(num_objects=2)

# 三種方式
# 1.預測當前路徑下的指定圖片
predictions, probabilities = prediction.predictImage(os.path.join(execution_path, "t4.jpg"), result_count=1)

for eachPrediction, eachProbability in zip(predictions, probabilities):
    print(eachPrediction, " : ", eachProbability)

# 2.預測當前路徑指定文件夾下所有圖片 predictImage
# for files in glob.glob(os.path.join(execution_path, r"tf-train\test\normal\*.jpg")):
#     filepath, filename = os.path.split(files)
#     predictions, probabilities = prediction.predictImage(files, result_count=1)
#     for eachPrediction, eachProbability in zip(predictions, probabilities):
#         print(filename, " predicted is -->", eachPrediction, " : ", eachProbability)

# # 3.預測當前路徑指定文件夾下所有圖片 predictMultipleImages
# imgpathls=[]
# imgnamels=[]
# for img in glob.glob(os.path.join(execution_path, r"tf-train\test\normal\*.jpg")):
#     imgpathls.append(img)
#     imgnamels.append(os.path.basename(img))
# output = prediction.predictMultipleImages(np.array(imgpathls))
# for k in np.arange(0, len(output)):
#     print(imgnamels[k], " predicted is -->", results_array[k]["predictions"][0], " : ", results_array[k]["percentage_probabilities"][0])

結果如下:

還有很好的資料值得學習:

  1. Somshubra Majumdar, DenseNet Implementation of the paper, Densely Connected Convolutional Networks in Keras
    https://github.com/titu1994/DenseNet/
  2. Broad Institute of MIT and Harvard, Keras package for deep residual networks
    https://github.com/broadinstitute/keras-resnet
  3. Fizyr, Keras implementation of RetinaNet object detection
    https://github.com/fizyr/keras-retinanet
  4. Francois Chollet, Keras code and weights files for popular deeplearning models
    https://github.com/fchollet/deep-learning-models
  5. Forrest N. et al, SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size
    https://arxiv.org/abs/1602.07360
  6. Kaiming H. et al, Deep Residual Learning for Image Recognition
    https://arxiv.org/abs/1512.03385
  7. Szegedy. et al, Rethinking the Inception Architecture for Computer Vision
    https://arxiv.org/abs/1512.00567
  8. Gao. et al, Densely Connected Convolutional Networks
    https://arxiv.org/abs/1608.06993
  9. Tsung-Yi. et al, Focal Loss for Dense Object Detection
    https://arxiv.org/abs/1708.02002
  10. O Russakovsky et al, ImageNet Large Scale Visual Recognition Challenge
    https://arxiv.org/abs/1409.0575
  11. TY Lin et al, Microsoft COCO: Common Objects in Context
    https://arxiv.org/abs/1405.0312
  12. Moses & John Olafenwa, A collection of images of identifiable professionals.
    https://github.com/OlafenwaMoses/IdenProf
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章