Skip to content

Import custom model error ValueError: No such layer: conv2d_32 #65

Description

@bessszilard

Hello! I am trying to import a custom Yolov4 model with 5 classes. I trained the model with the darknet library. The weights are proper because I have processed with them a video already.

I used this code based on the convert_darknet_weights function.

from tf2_yolov4.anchors import YOLOV4_ANCHORS
from tf2_yolov4.model import YOLOv4
from tf2_yolov4.tools.weights import load_darknet_weights_in_yolo
import numpy as np

num_classes = 5
INPUT_SHAPE = (416, 416, 3)
output_weights_path = '/content/yolov4.h5'
_weight_path='/content/drive/My\ Drive/20200722/yolow_backup/yolov4-custom_best.weights'

model = YOLOv4(
        input_shape=INPUT_SHAPE, num_classes=num_classes, anchors=YOLOV4_ANCHORS,
        weights=None,
        training=False
)
model.predict(np.random.random((1, *INPUT_SHAPE)))
model = load_darknet_weights_in_yolo(model, darknet_weights_path=_weight_path)
model.save(output_weights_path)

And I got this error:

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-46-94ab431f8673> in <module>()
     17 
     18 model.predict(np.random.random((1, *INPUT_SHAPE)))
---> 19 model = load_darknet_weights_in_yolo(model, darknet_weights_path=_weight_path)
     20 model.save(output_weights_path)

1 frames

/content/tf2-yolov4/tf2_yolov4/tools/weights.py in load_darknet_weights_in_yolo(yolo_model, darknet_weights_path)
     31     """
     32     sample_conv_weights = (
---> 33         yolo_model.get_layer("CSPDarknet53").get_layer("conv2d_32").get_weights()[0]
     34     )
     35 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py in get_layer(self, name, index)
    561       if layer.name == name:
    562         return layer
--> 563     raise ValueError('No such layer: ' + name)
    564 
    565   @property

ValueError: No such layer: conv2d_32

What causes the error?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions