Reputation: 41
Beginners question:
I collect images (128x128x3, batch_size=32) via tf.keras.preprocessing.image_dataset_from_directory
and try to process these images via DenseNet121
.
But it ends up in:
Found unexpected instance while processing input tensors for keras functional model. Expecting KerasTensor which is from tf.keras.Input() or output from keras layer call(). Got: <BatchDataset shapes: ((None, 128, 128, 3), (None,)), types: (tf.float32, tf.int32)>
How do I have to shape my Dataset that it is compatible with DenseNet?
Upvotes: 3
Views: 1292
Reputation: 26708
You do not need to use tf.data.Dataset.from_tensor_slices
at all. Try something like this:
import tensorflow as tf
import pathlib
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
batch_size = 32
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(128, 128),
batch_size=batch_size)
train_ds = train_ds.map(lambda x, y: (tf.keras.applications.densenet.preprocess_input(x), y))
dense_net = tf.keras.applications.DenseNet121(include_top=False, weights="imagenet",input_shape=(128, 128, 3))
inputs = tf.keras.layers.Input((128, 128, 3))
x = dense_net(inputs)
x = tf.keras.layers.GlobalMaxPool2D()(x)
outputs = tf.keras.layers.Dense(5)(x)
model = tf.keras.Model(inputs, outputs)
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_ds, epochs=5)
Upvotes: 2
Reputation: 976
Densenet with Keras required input to be in a specific shape and type. Before you pass your inputs into your model you have to run the images through tf.keras.applications.densenet.preprocess_input
to get your inputs into the correct shape and format.
Note that tf.keras.applications.densenet.preprocess_input
expects input to be a numpy.array
or a tf.Tensor
that is 3D or 4D (https://www.tensorflow.org/api_docs/python/tf/keras/applications/densenet/preprocess_input).
Also, tf.keras.preprocessing.image_dataset_from_directory
returns a tf.data.Dataset
which may need (I am not sure if the preprocessing function accepts it) processing to get it into a tensor or numpy array.
Hope this helped!
Upvotes: 1