I am new to tensorflow and trying to build a model using Functional API and pretrained models from tensorflow hub. The code that I am running is below:
import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import tensorflow_hub as hub
x = tf.random.normal(shape=(5, 299, 299, 3))
y = tf.constant([0, 1, 2, 3, 4], dtype=tf.float32)
url= "https://tfhub.dev/google/imagenet/inception_v3/feature_vector/5"
base_model = hub.KerasLayer(url, input_shape=(299, 299, 3)) #This is loaded as keras layer.
base_model.trainable = False
#Functional API
inputs = keras.Input(shape=(299, 299, 3))
x = base_model(inputs)
x = layers.Dense(128, activation="relu")(x)
x = layers.Dense(64, activation="relu")(x)
outputs = layers.Dense(5)(x)
model = keras.Model(inputs=inputs, outputs=outputs)
model.summary()
model.compile(optimizer=keras.optimizers.Adam(),
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.fit(x, y, epochs=5)
But when I run model.fit(x, y, epochs=5), I am getting the following error:
WARNING:tensorflow:Keras is training/fitting/evaluating on array-like data. Keras may not be optimized for this format, so if your input data format is supported by TensorFlow I/O (https://github.com/tensorflow/io) we recommend using that to load a Dataset instead.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-e50843ff60d9> in <cell line: 33>()
31 metrics=['accuracy'])
32
---> 33 model.fit(x, y, epochs=5)
1 frames
/usr/local/lib/python3.10/dist-packages/keras/engine/data_adapter.py in <genexpr>(.0)
257
258 num_samples = set(
--> 259 int(i.shape[0]) for i in tf.nest.flatten(inputs)
260 ).pop()
261 _check_data_cardinality(inputs)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
I am not sure why I am getting this error because when I create the same model using Sequential in keras (code below), it works fine.
model = keras.Sequential([
base_model,
layers.Dense(128, activation="relu"),
layers.Dense(64, activation="relu"),
layers.Dense(5)
])