无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。人工智能教程
ImageAI 提供4种不同的算法和模型,使你可以用您自定义的模型执行图像预测。您将使用 ImageAI 已训练的模型和相应的 JSON 文件来预测自定义对象。下面我们通过我们训练的一个例子来进行预测。
先看项目目录:
dataset
resnet_train.py
resnet_predict.py
模型分为5类,训练好的模型最高测试集准确率97.48%左右。
dataset 文件夹下面结构如下:
.
└── dataset
├── json
├── logs
│ └── resnet_lr-0.001_2019-09-26-18-01-18
│ ├── train
│ └── validation
├── models
├── test
│ ├── animal
│ ├── flower
│ ├── guitar
│ ├── houses
│ └── plane
└── train
├── animal
├── flower
├── guitar
├── houses
└── plane
json文件夹 里面 有一个 model_class.json,内容如下:
{
"0" : "animal",
"1" : "flower",
"2" : "guitar",
"3" : "houses",
"4" : "plane"
}
把训练好的model_ex-076_acc-0.974864.h5(90.4M) 和 model_class.json文件下载下来
下面我们进行编写resnet_predict.py 进行预测
# -*- coding: utf-8 -*-
from imageai.Prediction.Custom import CustomImagePrediction
import os
import time
execution_path = 'E:/2019年/图像算法/imageAI_dongli/'
prediction = CustomImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath(os.path.join(execution_path,"model_ex-076_acc-0.974864.h5"))
prediction.setJsonPath(os.path.join(execution_path, "model_class.json"))
prediction.loadModel(num_objects=5)
time1=time.time()
predictions, probabilities = prediction.predictImage(os.path.join(execution_path, "flower.jpg"), result_count=5)
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction + " : " + str(eachProbability))
time2=time.time()
print('总共耗时:' + str(time2 - time1) + 's')
预测此玫瑰花 是属于哪种类别。
运行结果,属于花的类别最大:
flower : 99.92861151695251
animal : 0.06757107912562788
guitar : 0.0034803331800503656
plane : 0.0003505825361571624
houses : 7.271949709064529e-07
总共耗时:0.8197042942047119s
Process finished with exit code 0
再来预测一张房子:
houses : 98.91605973243713
plane : 0.7265790831297636
guitar : 0.18177760066464543
animal : 0.1166991307400167
flower : 0.0588802678976208
总共耗时:0.7971503734588623s
Process finished with exit code 0
guitar : 99.53780174255371
animal : 0.2148298779502511
plane : 0.1628887257538736
flower : 0.058067135978490114
houses : 0.026400317437946796
总共耗时:0.7780678272247314s
animal : 99.88353848457336
guitar : 0.10665059089660645
houses : 0.007412067498080432
plane : 0.002398901415290311
flower : 8.875867507640578e-06
总共耗时:0.8121817111968994s
Process finished with exit code 0
plane : 98.74292016029358
animal : 1.198401115834713
flower : 0.05224089836701751
guitar : 0.00422090852225665
houses : 0.002220892383775208
总共耗时:0.8304483890533447s
Process finished with exit code 0
版本记录:
python 3.5.2
pip install tensorflow==1.12.0
pip install numpy==1.14.6