在用imageAI时,遇到一下报错,查了说版本不一样,但换了好几个版本都没解决
ModuleNotFoundError: No module named 'tensorflow.python.keras.preprocessing'
我是按照这篇文章来的,代码如下
# 导入ImageAI库和python os类
from imageai.Prediction import ImagePrediction
import os
execution_path = 'E:\imageAI' # imageAI 根目录
prediction = ImagePrediction() # 图片预测预备
# 模型文件下载在根目录的models子目录下
# 选择一个神经网络
# 1 DenseNet 预测时间较慢,精度最高
# prediction.setModelTypeAsDenseNet()
# prediction.setModelPath(
# os.path.join(execution_path, 'models',
# "DenseNet-BC-121-32.h5")) # 加载模型文件地址
# 2 InceptionV3 预测速度慢且精度更高
# prediction.setModelTypeAsInceptionV3()
# prediction.setModelPath(
# os.path.join(execution_path, 'models',
# 'inception_v3_weights_tf_dim_ordering_tf_kernels.h5'))
# 3 ResNet 预测时间快,精度高
prediction.setModelTypeAsResNet()
prediction.setModelPath(
os.path.join(execution_path, 'models',
'resnet50_weights_tf_dim_ordering_tf_kernels.h5'))
# 4 squeezenet 预测时间最快,精度中等
# prediction.setModelTypeAsSqueezeNet()
# prediction.setModelPath(
# os.path.join(execution_path, 'models',
# 'squeezenet_weights_tf_dim_ordering_tf_kernels.h5'))
prediction.loadModel() # 加载模型
# 图片文件在执行目录的images子目录下
redictions, probabilities = prediction.predictImage(
os.path.join(execution_path, 'images', "1.jpg"),
result_count=5) # 加载待预测的图片地址,输出5个最高可能的类型
for eachPrediction, eachProbability in zip(predictions,
probabilities): # 输出格式 预测类型 : 可能性大小
print(eachPrediction, ' : ', eachProbability)