OlafenwaMoses / ImageAI

A python library built to empower developers to build applications and systems with self-contained Computer Vision capabilities
https://www.genxr.co/#products
MIT License
8.48k stars 2.18k forks source link

Cannot assign to variable last1/kernel:0 due to variable shape #747

Open CarlChaaya opened 2 years ago

CarlChaaya commented 2 years ago

After training my model, I am trying to detect some objects in an image but this error keeps showing:

Training Code:

from imageai.Detection.Custom import DetectionModelTrainer

trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory=r"Dataset")
trainer.setTrainConfig(object_names_array=[], batch_size=8, num_experiments=10, train_from_pretrained_model="pretrained-yolov3.h5", )
trainer.trainModel()

I then proceeded (after 3 experiments) to download the json file and model. Detection code:

from imageai.Detection.Custom import CustomObjectDetection

detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("/content/model.h5")
detector.setJsonPath("/content/detection_config.json")
detector.loadModel()

img = "/content/IP038000063.jpg"

detections = detector.detectObjectsFromImage(input_image=img, output_image_path="/content/detected.jpg")

for detection in detections:
    print(detection["name"], " : ", detection["percentage_probability"], " : ", detection["box_points"])

I keep receiving this error:

ValueError                                Traceback (most recent call last)
[<ipython-input-5-902c9f52d5b3>](https://localhost:8080/#) in <module>()
      5 detector.setModelPath("/content/model.h5")
      6 detector.setJsonPath("/content/detection_config.json")
----> 7 detector.loadModel()
      8 
      9 img = "/content/IP038000063.jpg"

5 frames
[/usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/resource_variable_ops.py](https://localhost:8080/#) in assign(self, value, use_locking, name, read_value)
    889             ("Cannot assign to variable%s due to variable shape %s and value "
    890              "shape %s are incompatible") %
--> 891             (tensor_name, self._shape, value_tensor.shape))
    892       assign_op = gen_resource_variable_ops.assign_variable_op(
    893           self.handle, value_tensor, name=name)

ValueError: Cannot assign to variable last1/kernel:0 due to variable shape (1, 1, 1024, 15) and value shape (306, 1024, 1, 1) are incompatible

Can anyone help out?