xuannianz / EfficientDet

EfficientDet (Scalable and Efficient Object Detection) implementation in Keras and Tensorflow
Apache License 2.0
1.38k stars 395 forks source link

Error - ValueError: It appears you are trying to construct a functional model, but not all of the inputs in the first positional argument of your layer call are symbolic tensors. (Input objects, or the output of another layer) Functional models cannot correctly track custom layers unless all values in the first call argument are symbolic. #240

Closed VeereshVS closed 3 years ago

VeereshVS commented 3 years ago

As i am running the training code with coco dataset. Getting the error - ValueError: It appears you are trying to construct a functional model, but not all of the inputs in the first positional argument of your layer call are symbolic tensors. (Input objects, or the output of another layer) Functional models cannot correctly track custom layers unless all values in the first call argument are symbolic.

Getting error in efficientdef function in model.py

def efficientdet(phi, num_classes=20, num_anchors=9, weighted_bifpn=False, freeze_bn=False,
                 score_threshold=0.01, detect_quadrangle=False, anchor_parameters=None, separable_conv=True):
    assert phi in range(7)
    input_size = image_sizes[phi]
    input_shape = (input_size, input_size, 3)
    image_input = layers.Input(input_shape)
    w_bifpn = w_bifpns[phi]
    d_bifpn = d_bifpns[phi]
    w_head = w_bifpn
    d_head = d_heads[phi]
    backbone_cls = backbones[phi]
    features = backbone_cls(input_tensor=image_input, freeze_bn=freeze_bn)
    if weighted_bifpn:
        fpn_features = features
        for i in range(d_bifpn):
            fpn_features = build_wBiFPN(fpn_features, w_bifpn, i, freeze_bn=freeze_bn)
    else:
        fpn_features = features
        for i in range(d_bifpn):
            fpn_features = build_BiFPN(fpn_features, w_bifpn, i, freeze_bn=freeze_bn)
    box_net = BoxNet(w_head, d_head, num_anchors=num_anchors, separable_conv=separable_conv, freeze_bn=freeze_bn,
                     detect_quadrangle=detect_quadrangle, name='box_net')
    class_net = ClassNet(w_head, d_head, num_classes=num_classes, num_anchors=num_anchors,
                         separable_conv=separable_conv, freeze_bn=freeze_bn, name='class_net')
    classification = [class_net([feature, i]) for i, feature in enumerate(fpn_features)]
    classification = layers.Concatenate(axis=1, name='classification')(classification)
    regression = [box_net([feature, i]) for i, feature in enumerate(fpn_features)]
    regression = layers.Concatenate(axis=1, name='regression')(regression)
    model = models.Model(inputs=[image_input], outputs=[classification, regression], name='efficientdet')
    # apply predicted regression to anchors
    anchors = anchors_for_shape((input_size, input_size), anchor_params=anchor_parameters)
    anchors_input = np.expand_dims(anchors, axis=0)
    boxes = RegressBoxes(name='boxes')([anchors_input, regression[..., :4]])
    boxes = ClipBoxes(name='clipped_boxes')([image_input, boxes])

    # filter detections (apply NMS / score threshold / select top-k)
    if detect_quadrangle:
        detections = FilterDetections(
            name='filtered_detections',
            score_threshold=score_threshold,
            detect_quadrangle=True
        )([boxes, classification, regression[..., 4:8], regression[..., 8]])
    else:
        detections = FilterDetections(
            name='filtered_detections',
            score_threshold=score_threshold
        )([boxes, classification])

    prediction_model = models.Model(inputs=[image_input], outputs=detections, name='efficientdet_p')
    return model, prediction_model

getting above error at line -> _classification = [class_net([feature, i]) for i, feature in enumerate(fpn_features)]_

tensorflow and keras versions used are same as the one's in requirement.txt

DuyHuynhLe commented 3 years ago

Which TensorFlow and Keras version you are using? I encountered the same error on keras 2.3.1/tf2.keras. It is due to how the newer version handles input requirements. You can either downgrade your keras/tf or make some changes to the code base to make it work on tf2.

VeereshVS commented 3 years ago

Thank you, I was using tf 2.3.1 version only. I upgraded the tf to 2.4 and made some changes in box net and class net call function it works now. Sry. Haven't updated the issue here.

peterdors commented 3 years ago

Thank you, I was using tf 2.3.1 version only. I upgraded the tf to 2.4 and made some changes in box net and class net call function it works now. Sry. Haven't updated the issue here.

Can you elaborate on what changes were made in the call() function to get this to work?

IdeaKing commented 2 years ago

@VeereshVS What changes did you make to the box net and class net call functions?

Theresaliu commented 1 year ago

I also encountered the same problem.