ImageAI/Keras can't load ResNet model

440 views Asked by At

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. If anyone could please help me solve it I would greatly appreciate it.

1.

ImportError: load_weights requires h5py when loading weights from HDF5.

But I do have h5py installed and updated. I also tried installing h5py==2.10.0 and cython, as others suggested in past questions, but that hasn't worked for me either.

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)
1

There are 1 answers

0
Eric Dirla On

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.

Also your code is not suited for windows, since it requires the direct pathings to the files, otherwise you will get that exact error.