1

I am using the imageai.Detection.Custom.CustomObjectDetection class to label my test image based on the model that I trained. The model was trained using the imageai.Detection.Custom.DetectionModelTrainer class. I followed the instructions in the official documentation (Here) and I think I set up the dataset correctly, with all the labels. However, this error came out when I tried to run the detection code.

Traceback (most recent call last):
  File "./model_predict.py", line 25, in <module>
    detector.loadModel()
  File "/home/thecoder3281f/Documents/cca/env/lib/python3.7/site-packages/imageai/Detection/Custom/__init__.py", line 666, in loadModel
    self.__model.load_weights(self.__model_path)
  File "/home/thecoder3281f/Documents/cca/env/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 2234, in load_weights
    hdf5_format.load_weights_from_hdf5_group(f, self.layers)
  File "/home/thecoder3281f/Documents/cca/env/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 710, in load_weights_from_hdf5_group
    K.batch_set_value(weight_value_tuples)
  File "/home/thecoder3281f/Documents/cca/env/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py", line 201, in wrapper
    return target(*args, **kwargs)
  File "/home/thecoder3281f/Documents/cca/env/lib/python3.7/site-packages/tensorflow/python/keras/backend.py", line 3706, in batch_set_value
    x.assign(np.asarray(value, dtype=dtype(x)))
  File "/home/thecoder3281f/Documents/cca/env/lib/python3.7/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 891, in assign
    (tensor_name, self._shape, value_tensor.shape))
ValueError: Cannot assign to variable last1/kernel:0 due to variable shape (1, 1, 1024, 15) and value shape (45, 1024, 1, 1) are incompatible

The detection code is supposed to output a file with the bounding boxes of the labels around objects in the input image.

Is there something wrong with my detection code, or training code? Or is it my image dataset?

Details: OS: Ubuntu 22.04 Python version: 3.7.15 GPU: NVIDIA Geforce RTX 2060 CPU: AMD Ryzen 5 3600

Folder structure: Here

Model training code:

import os
from imageai.Detection.Custom import DetectionModelTrainer

os.environ["SM_FRAMEWORK"] = "tf.keras"
trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory="custom_image_db")
trainer.setTrainConfig(object_names_array=[], batch_size=4, num_experiments=20, train_from_pretrained_model="pretrained-yolov3.h5")
trainer.trainModel()

Detection code:

import os

execution_path = os.getcwd()                                       
                                        
from imageai.Detection.Custom import CustomObjectDetection

detector = CustomObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("custom_image_db/models (1)/detection_model-ex-020--loss-0030.816.h5")
detector.setJsonPath("custom_image_db/json (1)/detection_config.json")
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image="testimg1.jpg", output_image_path="testimg1-detected.jpg", extract_detected_objects=True)
for detection in detections:
    print(detection["name"], " : ", detection["percentage_probability"], " : ", detection["box_points"])

Sample xml file:

<?xml version="1.0" encoding="utf-8"?>
<annotation>
<folder>images</folder>
<filename>af278bd0-AdobeStock_15555240_Preview.jpeg</filename>
<source>
<database>MyDatabase</database>
<annotation>COCO2017</annotation>
<image>flickr</image>
<flickrid>NULL</flickrid>
<annotator>1</annotator>
</source>
<owner>
<flickrid>NULL</flickrid>
<name>Label Studio</name>
</owner>
<size>
<width>1000</width>
<height>667</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>Fall</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>35</xmin>
<ymin>100</ymin>
<xmax>921</xmax>
<ymax>603</ymax>
</bndbox>
</object>
<object>
<name>Pain</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>557</xmin>
<ymin>62</ymin>
<xmax>923</xmax>
<ymax>314</ymax>
</bndbox>
</object>
</annotation>

0 Answers0