xuannianz / EfficientDet

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

Inference.py has errors in Layer #399 #220

Closed cyrilzakka closed 4 years ago

cyrilzakka commented 4 years ago

I tried running inference.py but ran into the following error:

WARNING:tensorflow:From /content/EfficientDet/layers.py:153: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
Traceback (most recent call last):
  File "inference.py", line 64, in <module>
    main()
  File "inference.py", line 32, in main
    model.load_weights(model_path, by_name=True)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training.py", line 182, in load_weights
    return super(Model, self).load_weights(filepath, by_name)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/network.py", line 1371, in load_weights
    saving.load_weights_from_hdf5_group_by_name(f, self.layers)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/saving/hdf5_format.py", line 754, in load_weights_from_hdf5_group_by_name
    str(weight_values[i].shape) + '.')
ValueError: Layer #399 (named "class_net/class-predict"), weight <tf.Variable 'class_net/class_net/class-predict/pointwise_kernel:0' shape=(1, 1, 64, 99) dtype=float32> has shape (1, 1, 64, 99), but the saved weight has shape (1, 1, 64, 810).

Any ideas?

Here is my inference.py:
import cv2
import json
import numpy as np
import os
import time
import glob

from model import efficientdet
from utils import preprocess_image, postprocess_boxes
from utils.draw_boxes import draw_boxes

def main():
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    phi = 0
    weighted_bifpn = False
    model_path = 'checkpoints/2020-10-18/coco_01_57.4951_82.4522.h5'
    image_sizes = (512, 640, 768, 896, 1024, 1280, 1408)
    image_size = image_sizes[phi]
    # coco classes
    classes = [
        'Class1', 'Class2', 'Class3', etc.
    ]
    num_classes = len(classes)
    score_threshold = 0.3
    colors = [np.random.randint(0, 256, 3).tolist() for _ in range(num_classes)]
    _, model = efficientdet(phi=phi,
                            weighted_bifpn=weighted_bifpn,
                            num_classes=num_classes,
                            score_threshold=score_threshold)
    model.load_weights(model_path, by_name=True)

    for image_path in glob.glob('/content/EfficientDet/datasets/coco/images/val2017/frame_00000*.PNG'):
        image = cv2.imread(image_path)
        src_image = image.copy()
        # BGR -> RGB
        image = image[:, :, ::-1]
        h, w = image.shape[:2]

        image, scale = preprocess_image(image, image_size=image_size)
        # run network
        start = time.time()
        boxes, scores, labels = model.predict_on_batch([np.expand_dims(image, axis=0)])
        boxes, scores, labels = np.squeeze(boxes), np.squeeze(scores), np.squeeze(labels)
        print(time.time() - start)
        boxes = postprocess_boxes(boxes=boxes, scale=scale, height=h, width=w)

        # select indices which have a score above the threshold
        indices = np.where(scores[:] > score_threshold)[0]

        # select those detections
        boxes = boxes[indices]
        labels = labels[indices]

        draw_boxes(src_image, boxes, scores, labels, colors, classes)

        cv2.namedWindow('image', cv2.WINDOW_NORMAL)
        cv2.imshow('image', src_image)
        cv2.waitKey(0)

if __name__ == '__main__':
    main()

I've checked, the classes match! Here is the excerpt from the JSON:

{"licenses": [{"name": "", "id": 0, "url": ""}], "info": {"contributor": "", "date_created": "", "description": "", "url": "", "version": "", "year": ""}, "categories": [{"id": 1, "name": "Class1", "supercategory": ""}, {"id": 2, "name": "Class2", "supercategory": ""}, {"id": 3, "name": "Class3", "supercategory": ""}, {"id": 4, "name": "Class4", "supercategory": ""}, {"id": 5, "name": "Class5", "supercategory": ""}, {"id": 6, "name": "Class6", "supercategory": ""}, {"id": 7, "name": "Class7", "supercategory": ""}, {"id": 8, "name": "Class8", "supercategory": ""}, {"id": 9, "name": "Class9", "supercategory": ""}, {"id": 10, "name": "Class10", "supercategory": ""}, {"id": 11, "name": "Class11", "supercategory": ""}]

cyrilzakka commented 4 years ago

I've just realized this might be because when training, the number of classes was never specified. Is there a way of specifying the number of classes for the training script?

cyrilzakka commented 4 years ago

Alright so this officially worked! Unfortunately runninginference.py still returns the image with no bounding boxes at all.

datleeee commented 3 years ago

Alright so this officially worked! Unfortunately runninginference.py still returns the image with no bounding boxes at all.

Hi, I'm having the same problem. How did you solve this? Thank you.