ImageAI/Keras 无法加载 ResNet 模型



我正在尝试使用imageai库中的图像预测工具,但我得到了以下两个错误。。。作为一个初学者,我真的无法理解这些错误,而且我在网上也找不到任何有效的答案。如果有人能帮我解决这个问题,我将不胜感激

1.

ImportError: load_weights requires h5py when loading weights from HDF5.

但我确实安装并更新了h5py。我还试着安装h5py==2.10.0和cython,就像其他人在过去的问题中建议的那样,但这对我来说也不起作用。

2.

ValueError: You have specified an incorrect path to the ResNet model file.

我已经尝试了几种不同的方式来写路径,但这仍然不起作用。

这是完整的错误文本:

Traceback (most recent call last):
File "C:UsersMYUSERMiniconda3envstensorflowlibsite-packagesimageaiPrediction__init__.py", line 125, in loadModel
model = ResNet50(model_path=self.modelPath, model_input=image_input)
File "C:UsersMYUSERMiniconda3envstensorflowlibsite-packagesimageaiPredictionResNetresnet50.py", line 115, in ResNet50
model.load_weights(weights_path)
File "C:UsersMYUSERMiniconda3envstensorflowlibsite-packagestensorflowpythonkerasenginetraining.py", line 2341, in load_weights
raise ImportError(
ImportError: `load_weights` requires h5py when loading weights from HDF5.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:UsersMYUSERCurrent_Working_Directorybrain.py", line 10, in <module>
prediction.loadModel() 
File "C:UsersMYUSERMiniconda3envstensorflowlibsite-packagesimageaiPrediction__init__.py", line 129, in loadModel
raise ValueError("You have specified an incorrect path to the ResNet model file.")
ValueError: You have specified an incorrect path to the ResNet model file.

这是我的代码:

from imageai.Prediction import ImagePrediction 
import os 
execution_path=os.getcwd() 
prediction = ImagePrediction()
prediction.setModelTypeAsResNet() 
prediction.setModelPath(os.path.join(execution_path, "resnet50_imagenet_tf.2.0.h5")) 
prediction.loadModel() 
predictions, probabilities = prediction.predictImage(os.path.join(execution_path, "giraffe.jpg"), result_count=5 )
for eachPrediction, eachProbability in zip(predictions, probabilities): 
print(eachPrediction , " : " , eachProbability)

试试这个:

from imageai.Detection import ObjectDetection
import os
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.1.0.h5"))
detector.loadModel()
detections, objects_path = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "input.jpg"), output_image_path=os.path.join(execution_path , "output.jpg"), minimum_percentage_probability=10,  extract_detected_objects=True)
for eachObject, eachObjectPath in zip(detections, objects_path):
print(eachObject["name"] , " : " , eachObject["percentage_probability"], " : ", eachObject["box_points"] )
print("Object's image saved in " + eachObjectPath)
print("--------------------------------")

Imagenet算法也不适用于我,但使用coco算法(在ImageAI的Github上下载(为我节省了很多时间和精力。

此外,您的代码不适合windows,因为它需要文件的直接路径,否则您将得到确切的错误。

最新更新