Error inputs and shape of tensor hub while feature extraction

34 Views Asked by At

I am doing a process of feature extraction, by loading CenteNet from tf hub. However, I am facing the issue that when trying to fit the model I get the error of a requested tensor with different shape and data type.

TypeError: Exception encountered when calling layer "feature_ext_layer" (type KerasLayer).

in user code:

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

    TypeError: Binding inputs to tf.function `f` failed due to `Tensor conversion requested dtype uint8 for Tensor with dtype float32: <tf.Tensor 'Placeholder:0' shape=(None, 512, 512, 3) dtype=float32>`. Received args: (<tf.Tensor 'Placeholder:0' shape=(None, 512, 512, 3) dtype=float32>,) and kwargs: {} for signature: (input_tensor: TensorSpec(shape=(1, None, None, 3), dtype=tf.uint8, name=None)).


Call arguments received by layer "feature_ext_layer" (type KerasLayer):
  • inputs=tf.Tensor(shape=(None, 512, 512, 3), dtype=float32)
  • training=None

I have already try to match these by applying the fucntion of tf.cast to the dataset, and changed the data type and input_shape for the KerasLayer where I loaded the model. However, the same problem appears, but now when creating the model when joining the layers.

Here is my code:

def new_model(model_url):
  feature_extractor_layer = hub.KerasLayer(model_url,
                                           trainable = False,
                                           name = 'feature_ext_layer',
                                           dtype = 'float32',
                                           input_shape=(512, 512, 3))
  # creating model
  model = tf.keras.Sequential([
      feature_extractor_layer, 
      layers.Dense(2, activation='softmax', name='output_layer') 
      ])

  return model


CenterNet_URL = "https://tfhub.dev/tensorflow/centernet/hourglass_512x512_kpts/1"

# Create model
new_CenterNet = new_model(CenterNet_URL)
0

There are 0 best solutions below