如何在keras中找到用于张量流对象检测的Sequential的输入参数

How to find input parameter for Sequential in keras for tensorflow object detection

我在 运行 google colab

TensorFlow Hub Object Detection Colab 的推理成功了

我加载的模型

model_display_name = 'CenterNet HourGlass104 Keypoints 512x512'
model_handle = 'https://tfhub.dev/tensorflow/centernet/hourglass_512x512/1'

正在尝试使用 Transfer learning with TensorFlow Hub

IMAGE_SHAPE = (None, None)

classifier = tf.keras.Sequential([
    hub.KerasLayer(model_handle, input_shape=IMAGE_SHAPE+(3,))
])

我收到这个错误

ValueError                                Traceback (most recent call last)
<ipython-input-23-081693dbe40a> in <module>()
      4 
      5 object_detector = tf.keras.Sequential([
----> 6     hub.KerasLayer(object_detector_model, input_shape=Object_Detector_IMAGE_SHAPE+(3,))
      7 ])

2 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
    690       except Exception as e:  # pylint:disable=broad-except
    691         if hasattr(e, 'ag_error_metadata'):
--> 692           raise e.ag_error_metadata.to_exception(e)
    693         else:
    694           raise

ValueError: Exception encountered when calling layer "keras_layer" (type KerasLayer).

in user code:

    File "/usr/local/lib/python3.7/dist-packages/tensorflow_hub/keras_layer.py", line 229, in call  *
        result = f()

    ValueError: Python inputs incompatible with input_signature:
      inputs: (
        Tensor("Placeholder:0", shape=(None, 1, None, None, 3), dtype=float32))
      input_signature: (
        TensorSpec(shape=(1, None, None, 3), dtype=tf.uint8, name=None)).


Call arguments received:
  • inputs=tf.Tensor(shape=(None, 1, None, None, 3), dtype=float32)
  • training=None

我该如何修改 IMAGE_SHAPE,我很困惑?

需要帮助,谢谢

两个问题,此模型在指定 input_shape 时期望输入类型 tf.uint8 并根据 source 它 returns 一个输出字典。由于 Sequential API 不适用于多个输出,您将不得不使用 Functional API.

简单示例:

import tensorflow as tf
import tensorflow_hub as hub

model_display_name = 'CenterNet HourGlass104 Keypoints 512x512'
model_handle = 'https://tfhub.dev/tensorflow/centernet/hourglass_512x512/1'

k_layer = hub.KerasLayer(model_handle, input_shape=(None, None, 3), dtype=tf.uint8)

Functional API:

import tensorflow as tf
import tensorflow_hub as hub

model_display_name = 'CenterNet HourGlass104 Keypoints 512x512'
model_handle = 'https://tfhub.dev/tensorflow/centernet/hourglass_512x512/1'

k_layer = hub.KerasLayer(model_handle, input_shape=(None, None, 3))

inputs = tf.keras.layers.Input(shape=(None, None, 3), dtype=tf.uint8)
outputs = k_layer(inputs)
classifier = tf.keras.Model(inputs, outputs)
print(classifier.summary())
Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_2 (InputLayer)        [(None, None, None, 3)]   0         
                                                                 
 keras_layer_9 (KerasLayer)  {'num_detections': (1,),  0         
                              'detection_boxes': (1,             
                             100, 4),                            
                              'detection_classes': (1            
                             , 100),                             
                              'detection_scores': (1,            
                              100)}                              
                                                                 
=================================================================
Total params: 0
Trainable params: 0
Non-trainable params: 0
_________________________________________________________________
None