Home > front end >  How to find input parameter for Sequential in keras for tensorflow object detection
How to find input parameter for Sequential in keras for tensorflow object detection

Time:04-23

I was successful in running the inference for TensorFlow Hub Object Detection Colab on google colab

the model I have loaded

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

Trying to use Transfer learning with TensorFlow Hub

IMAGE_SHAPE = (None, None)

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

I get this error

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

How do I alter IMAGE_SHAPE for this, I am confused?

Need help, thank you

CodePudding user response:

Two issues, this model expects the input type tf.uint8 when specifying the input_shape and according to the source it returns an output dictionary. Since the Sequential API does not work with multiple outputs, you will have to use the Functional API.

Simple example:

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)

And with the 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
  • Related