david8862 / keras-YOLOv3-model-set

end-to-end YOLOv4/v3/v2 object detection pipeline, implemented on tf.keras with different technologies
MIT License
639 stars 221 forks source link

Inference on custom YOLO model #1

Closed tanmay-bhatnagar closed 4 years ago

tanmay-bhatnagar commented 5 years ago

I had previously trained a YOLO model on a custom dataset using this repo : https://github.com/qqwweee/keras-yolo3 This repo is quite similar to this one and the inference mode is exactly the same, as far as image mode is concerned. I give the following command in terminal python yolo.py --model_type=darknet --model_path=./weights/yoloFace.h5 --anchors_path=configs/yolo_anchors.txt --classes_path=configs/face_classes.txt --model_image_size=416x416 --image` This gives me this error :

`Call initializer instance with the dtype argument instead of passing it to the constructor
Traceback (most recent call last):
  File "yolo.py", line 75, in _generate_model
    yolo_model, _ = get_yolo3_model(self.model_type, num_feature_layers, num_anchors, num_classes, input_shape=self.model_image_size + (3,))
  File "/home/dk-tanmay/Desktop/keras-YOLOv3-model-set/yolo3/model.py", line 101, in get_yolo3_model
    model_body = model_function(input_tensor, num_anchors//3, num_classes, weights_path=weights_path)
  File "/home/dk-tanmay/Desktop/keras-YOLOv3-model-set/yolo3/models/yolo3_darknet.py", line 60, in yolo_body
    darknet.load_weights(weights_path, by_name=True)
  File "/home/dk-tanmay/.virtualenvs/cv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 162, in load_weights
    return super(Model, self).load_weights(filepath, by_name)
  File "/home/dk-tanmay/.virtualenvs/cv/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1412, in load_weights
    with h5py.File(filepath, 'r') as f:
  File "/home/dk-tanmay/.virtualenvs/cv/lib/python3.6/site-packages/h5py/_hl/files.py", line 408, in __init__
    swmr=swmr)
  File "/home/dk-tanmay/.virtualenvs/cv/lib/python3.6/site-packages/h5py/_hl/files.py", line 173, in make_fid
    fid = h5f.open(name, flags, fapl=fapl)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5f.pyx", line 88, in h5py.h5f.open
OSError: Unable to open file (unable to open file: name = 'weights/darknet53.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "yolo.py", line 379, in <module>
    yolo = YOLO_np(**vars(args))
  File "yolo.py", line 59, in __init__
    self.yolo_model = self._generate_model()
  File "yolo.py", line 79, in _generate_model
    assert yolo_model.layers[-1].output_shape[-1] == \
UnboundLocalError: local variable 'yolo_model' referenced before assignment

The code is going to the darknet53.h5 weights even though I specified the paths for my own weights. The anchors file is the same as full YOLOv3 and I have replaced the classes file. How to resolve this ?

david8862 commented 5 years ago

Yes. It's inherited from the code base of @qqwweee so generally your pretrained model should be compatible. The error you meet is because when generating the darknet backbone it will try to automatically load the pretrained weights from weights/ first. You can go through the QuickStart part to download the weights file to fix it. Or locally change logic to avoid the auto pre-load.

I had previously trained a YOLO model on a custom dataset using this repo : https://github.com/qqwweee/keras-yolo3 This repo is quite similar to this one and the inference mode is exactly the same, as far as image mode is concerned. I give the following command in terminal python yolo.py --model_type=darknet --model_path=./weights/yoloFace.h5 --anchors_path=configs/yolo_anchors.txt --classes_path=configs/face_classes.txt --model_image_size=416x416 --image` This gives me this error :

`Call initializer instance with the dtype argument instead of passing it to the constructor
Traceback (most recent call last):
  File "yolo.py", line 75, in _generate_model
    yolo_model, _ = get_yolo3_model(self.model_type, num_feature_layers, num_anchors, num_classes, input_shape=self.model_image_size + (3,))
  File "/home/dk-tanmay/Desktop/keras-YOLOv3-model-set/yolo3/model.py", line 101, in get_yolo3_model
    model_body = model_function(input_tensor, num_anchors//3, num_classes, weights_path=weights_path)
  File "/home/dk-tanmay/Desktop/keras-YOLOv3-model-set/yolo3/models/yolo3_darknet.py", line 60, in yolo_body
    darknet.load_weights(weights_path, by_name=True)
  File "/home/dk-tanmay/.virtualenvs/cv/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 162, in load_weights
    return super(Model, self).load_weights(filepath, by_name)
  File "/home/dk-tanmay/.virtualenvs/cv/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1412, in load_weights
    with h5py.File(filepath, 'r') as f:
  File "/home/dk-tanmay/.virtualenvs/cv/lib/python3.6/site-packages/h5py/_hl/files.py", line 408, in __init__
    swmr=swmr)
  File "/home/dk-tanmay/.virtualenvs/cv/lib/python3.6/site-packages/h5py/_hl/files.py", line 173, in make_fid
    fid = h5f.open(name, flags, fapl=fapl)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5f.pyx", line 88, in h5py.h5f.open
OSError: Unable to open file (unable to open file: name = 'weights/darknet53.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "yolo.py", line 379, in <module>
    yolo = YOLO_np(**vars(args))
  File "yolo.py", line 59, in __init__
    self.yolo_model = self._generate_model()
  File "yolo.py", line 79, in _generate_model
    assert yolo_model.layers[-1].output_shape[-1] == \
UnboundLocalError: local variable 'yolo_model' referenced before assignment

The code is going to the darknet53.h5 weights even though I specified the paths for my own weights. The anchors file is the same as full YOLOv3 and I have replaced the classes file. How to resolve this ?

tanmay-bhatnagar commented 5 years ago

Ok if I download the pretrained weights and load them then how do I replace them with my own weights ? Secondly, what part of the code do I need to change to avoid auto pre-load ?

david8862 commented 5 years ago

The pretrained weights is only for bring up an initial model for training or inference, so it would be overwritten by yours if you specified "--model_path". If you DO want to avoid that, you can change "yolo3_model_map" or "yolo3_tiny_model_map" in model.py

Ok if I download the pretrained weights and load them then how do I replace them with my own weights ? Secondly, what part of the code do I need to change to avoid auto pre-load ?

tanmay-bhatnagar commented 4 years ago

So what you are saying is even though it says that the default weights are loaded, those default weights will be replaced by my own weights ?

david8862 commented 4 years ago

Yes, sure

So what you are saying is even though it says that the default weights are loaded, those default weights will be replaced by my own weights ?