【深度学习--图像分类】imageAI自定义模型预测

无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。人工智能教程

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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东华果汁哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值