Imageai: ValueError: Shapes (1, 1, 1024, 255) and (21, 1024, 1, 1) are incompatible

Created on 4 Oct 2020  路  5Comments  路  Source: OlafenwaMoses/ImageAI

So basically I trained a YOLOv3 Model with ImageAI.
During the load() function it crashes and gives back this traceback:

Traceback (most recent call last): File "C:/Users/aless/PycharmProjects/Mikulyzer_Locate/firstDetection.py", line 10, in <module> detector.loadModel() File "C:\Users\aless\anaconda3\envs\Mikulyzer_Locate\lib\site-packages\imageai\Detection\__init__.py", line 197, in loadModel model.load_weights(self.modelPath) File "C:\Users\aless\anaconda3\envs\Mikulyzer_Locate\lib\site-packages\keras\engine\saving.py", line 492, in load_wrapper return load_function(*args, **kwargs) File "C:\Users\aless\anaconda3\envs\Mikulyzer_Locate\lib\site-packages\keras\engine\network.py", line 1230, in load_weights f, self.layers, reshape=reshape) File "C:\Users\aless\anaconda3\envs\Mikulyzer_Locate\lib\site-packages\keras\engine\saving.py", line 1237, in load_weights_from_hdf5_group K.batch_set_value(weight_value_tuples) File "C:\Users\aless\anaconda3\envs\Mikulyzer_Locate\lib\site-packages\keras\backend\tensorflow_backend.py", line 2960, in batch_set_value tf_keras_backend.batch_set_value(tuples) File "C:\Users\aless\anaconda3\envs\Mikulyzer_Locate\lib\site-packages\tensorflow\python\keras\backend.py", line 3066, in batch_set_value assign_op = x.assign(assign_placeholder) File "C:\Users\aless\anaconda3\envs\Mikulyzer_Locate\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 1141, in assign self._shape.assert_is_compatible_with(value_tensor.shape) File "C:\Users\aless\anaconda3\envs\Mikulyzer_Locate\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 1103, in assert_is_compatible_with raise ValueError("Shapes %s and %s are incompatible" % (self, other))

My Code is:

execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path, "detection_model-ex-013--loss-0012.906.h5"))
detector.loadModel() # crashes here

Basically, did I not train it right? Or do I not load it correctly?

All 5 comments

Hi @aless2003 , please use the CustomObjectDetection class as seen in the sample code below

from imageai.Detection.Custom import CustomObjectDetection

detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("hololens-ex-60--loss-2.76.h5")
detector.setJsonPath("detection_config.json")
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image="holo1.jpg", output_image_path="holo1-detected.jpg")
for detection in detections:
    print(detection["name"], " : ", detection["percentage_probability"], " : ", detection["box_points"])

Thanks for the fast reply.
Unfortunately Python says ObjectDetection doesn't have something called setJsonPath.
I seem to be on the most recent version though.

Remove from imageai.Detection import ObjectDetection and use the import below instead.

from imageai.Detection.Custom import CustomObjectDetection

Oh! Alright now I understand. Thank you very much! I'm sorry I took your time for that, it works now.

Have a beautiful day!

Was this page helpful?
0 / 5 - 0 ratings