机器学习之ImageAI:图像预测

以下内容是基于ImageAI的ResNet模型进行学习,所用的相关设置如下:

(具体教程可见文末推荐网址1号,如若有相关建议可分享共勉,加油)

 

工具:

JetBrains Pycharm Community Edition 2018.3.1

 

环境:

python 3.7 (Project Interpreter是用Anaconda3-5.3.1中的python.exe,另外为安装Keras,安装了Python3.5)

tensorflow 1.12.0 (深度学习框架,目前使用率最高)

numpy 1.15.4 (Python的一种开源的科学计算库,提供和处理N维数组对象)

scipy 1.1.0 (Python工具包,利用numpy做更高级的数学,信号处理,优化,统计等)

opencv 3.4.2 (开源的计算机视觉库,提供Python、Ruby、Matlab等语言的接口,实现图像处理和计算机视觉方面的很多算法)

pillow 5.3.0 (PIL,Python Image Library,图像处理标准库,功能强大,API简单易用)

matplotlib 3.0.2 (Python的2D绘图库,可实现散点图、折线图、直方图等)

h5py 2.8.0 (操作和使用HDF5数据。HDF,Hierarchical Data Format,一种为存储和处理大容量科学数据设计的文件格式及相应库文件)

keras 2.2.4 (高层神经网络API,基于Theano和Tensorflow的深度学习库,注意,适用的Python版本是2.7-3.5)

 

(建议初学者将以上模块安装好,上述模块的解释只是简单摘取了一部分,若想深入了解可自行搜索)

 

这是对输入的一张图片的预测:

OnePicture.py

# 通过imageai.Prediction做对象检测

# 对一张图像进行预测

from imageai.Prediction import ImagePrediction
import os
# 跳过警告
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# 获取当前路径【os.getcwd()用于返回当前工作目录】
execution_path = os.getcwd()

# 初始化预测器
prediction = ImagePrediction()

# 设置预测器的网络类型为resnet
prediction.setModelTypeAsResNet()

# 导入模型权值文件
prediction.setModelPath(execution_path + "\\resnet50_weights_tf_dim_ordering_tf_kernels.h5")

# 加载模型
prediction.loadModel()

# 对图片进行测试并输出测试结果
# result_count用于设置想要的预测结果的数量(参数范围[1,1000])
# predictImage()函数将返回预测的对象名和相应的百分比概率(percentage_probabilities)
predictions, probabilities = prediction.predictImage(os.path.join(execution_path, 'sample.jpg'), result_count=5)

#输出预测到的对象及相应的置信度
for prediction, probability in zip(predictions, probabilities):
    print(prediction, "  : ", probability)

ps:

1、其中的sample.jpg,可是任意一张车的图片或是动物图片,但它要放在与OnePicture.py相同目录下。

2、在OnePicture.py中,我曾遇到tensorflow使用时cpu编译下不支持的警告,添加下面两行即可。

(具体可看此网址:https://blog.csdn.net/Fourierrr_/article/details/79749899

# 跳过警告
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

 

这是对输入的多张图片的预测:

MultiplePictures.py

# 对多张图像进行预测
from imageai.Prediction import ImagePrediction
import os
import time

start_time = time.time()

execution_path = os.getcwd()

# 定义普通的ImagePrediction实例
multiple_prediction = ImagePrediction()

# 设置模型类型
multiple_prediction.setModelTypeAsResNet()

# 设置模型路径
multiple_prediction.setModelPath(os.path.join(execution_path, "resnet50_weights_tf_dim_ordering_tf_kernels.h5"))

# 载入模型
multiple_prediction.loadModel()
# 预测速度调节功能
# prediction_speed的值可为normal(default)、fast、faster、fastest;
# 但调整速度模式时,为了更能确保预测的准确度,最好使用具有更高精度的模型(DenseNet or InceptionV3),或是预测图像是标志性的(明显的)。
# multiple_prediction.loadModel(prediction_speed="fast")

# 创建一个数组
all_images_array = []

# 将所有要预测的图像的路径添加至数组中
all_files = os.listdir(execution_path)
for each_file in all_files:
    if(each_file.endswith(".jpg") or each_file.endswith(".png")):
        all_images_array.append(each_file)

# 调用.predictMultipleImages()函数解析包含图像路径的数组并执行图像预测,并输出5个预测的可能结果(result_count_per_image的默认值为2)
results_array = multiple_prediction.predictMultipleImages(all_images_array, result_count_per_image=5)

end_time = time.time()

for each_result in results_array:
    predictions, percentage_probabilities = each_result["predictions"], each_result["percentage_probabilities"]
    for index in range(len(predictions)):
        print(predictions[index], " : ", percentage_probabilities[index])
    print("\ncost time:", end_time - start_time)
    print("---------------------------------------------------------------")

 

若是想深入学习ImageAI,可查看以下网址,即 ImageAI中文文档:

(里面含有ImageAI图像检测中的其它模型(如DenseNet)的相关内容,及具体相关步骤,十分不错的学习网址)

网址1号:https://imageai-cn.readthedocs.io/zh_CN/latest/

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

住在阳光的心里

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

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

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

打赏作者

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

抵扣说明:

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

余额充值