简体   繁体   English

ImageAI/Keras 无法加载 ResNet model

[英]ImageAI/Keras can't load ResNet model

I'm trying to use the Image Prediction tool in the imageai library, and I'm getting the following two errors... As a beginner, I honestly can't make sense of the errors, and I couldn't find any answers online that worked.我正在尝试使用 imageai 库中的图像预测工具,但出现以下两个错误...作为初学者,老实说,我无法理解这些错误,也找不到任何答案在线有效。 If anyone could please help me solve it I would greatly appreciate it.如果有人可以帮我解决它,我将不胜感激。

1. 1.

ImportError: load_weights requires h5py when loading weights from HDF5.

But I do have h5py installed and updated.但我确实安装并更新了 h5py。 I also tried installing h5py==2.10.0 and cython, as others suggested in past questions, but that hasn't worked for me either.正如其他人在过去的问题中所建议的那样,我还尝试安装 h5py==2.10.0 和 cython,但这对我也不起作用。

2. 2.

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

I have tried several different ways of writing the path but this still won't work.我尝试了几种不同的方法来编写路径,但这仍然行不通。

This is the full error text:这是完整的错误文本:

Traceback (most recent call last):
  File "C:\Users\MYUSER\Miniconda3\envs\tensorflow\lib\site-packages\imageai\Prediction\__init__.py", line 125, in loadModel
    model = ResNet50(model_path=self.modelPath, model_input=image_input)
  File "C:\Users\MYUSER\Miniconda3\envs\tensorflow\lib\site-packages\imageai\Prediction\ResNet\resnet50.py", line 115, in ResNet50
    model.load_weights(weights_path)
  File "C:\Users\MYUSER\Miniconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\engine\training.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:\Users\MYUSER\Current_Working_Directory\brain.py", line 10, in <module>
    prediction.loadModel() 
  File "C:\Users\MYUSER\Miniconda3\envs\tensorflow\lib\site-packages\imageai\Prediction\__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.

This is my code:这是我的代码:

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)

Try this:尝试这个:

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("--------------------------------")

The Imagenet-algorithm didnt work for me either, but using the coco-algorithm (download on Github of ImageAI) saves me a lot of time and effort atm. Imagenet 算法对我也不起作用,但使用 coco 算法(在 ImageAI 的 Github 上下载)为我节省了大量时间和精力。

Also your code is not suited for windows, since it requires the direct pathings to the files, otherwise you will get that exact error.此外,您的代码不适合 windows,因为它需要文件的直接路径,否则您将得到确切的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM