July 5, 2025
Transfer Learning - Xception, tfds, mnist
Goal: Use Xception model for transfer learning with mnist dataset, loaded with tfds, based on code from Chapter
9.2 in Jeff Heaton class.
Starting point:
One basic difference between the cats vs dogs dataset and the mnist dataset is that the former has two labels
which allows it to be treated as a binary classification problem, whereas the latter has 10 labels (number from
0 to 9).
Here are some examples of training with the mnist dataset, without transfer learning:
MNIST: Keras Simple CNN (99.6%)
https://www.tensorflow.org/datasets/keras_example
July 7, 2025
Transfer Learning - Xception, tfds, mnist
Goal: Use Xception model for transfer learning with mnist dataset, loaded with tfds, based on code from Chapter
9.2 in Jeff Heaton class.
(train_ds, validation_ds), metadata= tfds.load(
'mnist',
Result:
model.fit(train_ds, epochs=epochs, validation_data=validation_ds)
gives error
Input 0 of layer "block1_conv1" is incompatible with the layer: expected axis -1 of input shape to have value
3, but received input with shape (None, 150, 150, 1)
Call arguments received by layer 'xception' (type Functional):
- inputs=tf.Tensor(shape=(None, 150, 150, 1), dtype=float32)
- training=False
- mask=None
This refers to the input of the base_model
base_model = keras.applications.Xception(
weights="imagenet", # Load weights pre-trained on ImageNet.
input_shape=(150, 150, 3),
include_top=False,
)
Details about the mnist dataset:
https://www.tensorflow.org/api_docs/python/tf/keras/datasets/mnist/load_data
Note:
"grayscale image data with shapes (60000, 28, 28)"
"Pixel values range from 0 to 255."
When I use the 'cifar10' dataset, no run time error occurs.
July 8, 2025
Explanation of predict output:
https://forum.freecodecamp.org/t/model-predict-output/470349
'In many classification models you have a threshold. The common threshold is 0.5, if the result is above of
this, then is more likely to be true.'
dog
[0.70762724]
cat
[0.26925486]
Brave search:
"tfds model predict"
result includes
predictions = model.predict(train_ds)
did not work until I applied map function with batch function.
batch function, tfds, model.fit
Running the example as is from
?
gives error
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 256, 256, 3),
found shape=(256, 256, 3)
adding the lines
batch_size = 32
train_dataset = train_dataset.cache().batch(batch_size).prefetch(buffer_size=10)
test_dataset = test_dataset.cache().batch(batch_size).prefetch(buffer_size=10)
solves the problem.
The API documentation for model.fit and model.predict states that both function expect the data in batches:
batch_size:
Integer or None. Number of samples per batch. If unspecified, batch_size will default to 32. Do not specify
the
batch_size if your data is in the form of dataset, generators, or keras.utils.PyDataset instances (since they
generate batches).
API documentation for model.fit:
https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit
API documentation for model.predict:
https://www.tensorflow.org/api_docs/python/tf/keras/Model#predict
More resources
Tutorial has example with prediction:
https://www.tensorflow.org/tutorials/images/transfer_learning_with_hub
Tutorial on dataset preparation:
https://www.tensorflow.org/tutorials/images/data_augmentation
Blog - Introducing TensorFlow Datasets:
https://blog.tensorflow.org/2019/02/introducing-tensorflow-datasets.html
July 15, 2025
Goal:
display dataset image, label, and prediction on one row
Solution:
new_df=tfds.as_dataframe(test_dataset.take(number_of_images), metadata)
new_df['predictions'] = allpreds[0:number_of_images]
Add a new column to the Pandas dataframe that was created with tfds.as_dataframe.
July 18, 2025
Adapt model for datasets with different number of labels and color depths
The following model
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(256, 256, 3)),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
is used in the following web pages:
https://www.christianhaller.me/blog/projectblog/2020-06-02-TFCatsVsDogsI/
https://colab.research.google.com/github/lmoroney/dlaicourse/blob/master/Course%202%20-%20Part%202%20-%20Lesson%202%20-%20Notebook.ipynb
This model will work with the tfds 'cats_vs_dogs' dataset.
mnist dataset
When using the model with the tfds mnist dataset, the following consideration have to be taken into account:
- the cats_vs_dogs images have three RGB color channels
- the images have 2 labels
whereas
- the mnist images have only one gray level channel
- the images have 10 labels
To make the code work with mnist images, we have to
- change the first model layer from/to
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(256, 256, 3)),
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(256, 256, 1)),
- change the last model layer from/to
tf.keras.layers.Dense(1, activation='sigmoid')
tf.keras.layers.Dense(10, activation='sigmoid')
- change the model compile code from
model.compile(
optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'
])
model.compile(
optimizer=tf.keras.optimizers.Adam(0.001),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
)
tf_flower dataset
When using the model with the tfds tf_flower dataset, the following consideration have to be taken into
account:
- the cats_vs_dogs images have three RGB color channels
- the images have 5 labels
whereas
- the tf_flowers images have three RGB color channels
- the images have 10 labels
To make the code work with tf_flower images, we have to
- change the last model layer from/to
outputs = keras.layers.Dense(1)(x)
outputs = keras.layers.Dense(5)(x)
- change the model compile code from
# model.compile(
# optimizer=keras.optimizers.Adam(),
# loss=keras.losses.BinaryCrossentropy(from_logits=True),
# metrics=[keras.metrics.BinaryAccuracy()],
# )
model.compile(
# optimizer=tf.keras.optimizers.Adam(0.001),
optimizer=tf.keras.optimizers.Adam(),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
)
Transfer Learning - Xception, tfds, mnist
The transfer learning example code by fchollet and Jeff Heaton uses the pre-trained Xception model
https://keras.io/guides/transfer_learning/
https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_09_2_keras_xfer_cv.ipynb
This model will work with the tfds 'cats_vs_dogs' dataset.
Goal: Use Xception model for transfer learning with mnist dataset, loaded with tfds, based on code from Chapter
9.2 in Jeff Heaton class.
Xception expects RGB images with 3 channels, mnist offers only 1 graylevel channel.
Solution:
Convert images from gray to color.
Add this code before the other pre-processing code:
def reshape_image(image, label):
image = tf.reshape(image, (28, 28, 1))
return image, label
train_dataset = train_dataset.map(reshape_image)
def convert_to_rgb(image, label):
image = tf.image.grayscale_to_rgb(image)
return image, label
train_dataset = train_dataset.map(convert_to_rgb)
validation_dataset = validation_dataset.map(convert_to_rgb)
and change the last model layer to 10 dimensions:
outputs = keras.layers.Dense(10)(x)
Solution found by searching in Brave for
"mnist tfds convert to RGB"
https://www.tensorflow.org/api_docs/python/tf/image/grayscale_to_rgb
Interesting Links
resource for transfer learning: list of pre-trained models
https://pyimagesearch.com/2017/03/20/imagenet-vggnet-resnet-inception-xception-keras/
Use a GAN to generate colorful mnist numbers:
https://www.wouterbulten.nl/posts/getting-started-with-gans-2-colorful-mnist/
Model Zoo - Discover open source deep learning code and pretrained models.
https://modelzoo.co/
Papers with Code
https://paperswithcode.com/
July 23, 2025
Goal: load my own images for inference with model from tfds dataset
The code for pre-processing works when the dataset is a tuple.
This is the case when loading a dataset as in example at
https://www.tensorflow.org/datasets/keras_example
(ds_train, ds_test), ds_info = tfds.load(
'mnist',
split=['train', 'test'],
shuffle_files=True,
as_supervised=True,
with_info=True,
)
Here, the type is a tuple:
print(train_ds)
test_dataset= <_PrefetchDataset
element_spec=
(
TensorSpec(shape=(None, None, 3), dtype=tf.uint8, name=None),
TensorSpec(shape=(), dtype=tf.int64, name=None)
)>
</_PrefetchDataset>
When using the example code from
https://www.tensorflow.org/datasets/api_docs/python/tfds/folder_dataset/ImageFolder
ds = builder.as_dataset(
split='train',
shuffle_files=True
)
the type is a dictionary, not a tuple:
print(train_ds)
test_dataset= <_PrefetchDataset
element_spec=
{
'image': TensorSpec(shape=(None, None, 3), dtype=tf.uint8, name=None),
'image/filename': TensorSpec(shape=(), dtype=tf.string, name=None),
'label': TensorSpec(shape=(), dtype=tf.int64, name=None)
}
>
July 24, 2025
Goal: load my own images for inference with model from tfds dataset
Attempt: use tfds.folder_dataset.ImageFolder to load the images into tfds dataset
https://www.tensorflow.org/datasets/api_docs/python/tfds/folder_dataset/ImageFolder
The example code works
builder = tfds.ImageFolder('path/to/image_dir/')
print(builder.info) # num examples, labels... are automatically calculated
ds = builder.as_dataset(split='train', shuffle_files=True)
tfds.show_examples(ds, builder.info)
but when I try to extract images and labels
for images, labels in train_dataset.take(1):
print(images)
I get error
ValueError: too many values to unpack (expected 2)
However,
for images in train_dataset.take(1):
print(images)
works, the ouput is a dictionary:
{
'image': <Tensor: shape=(530, 500, 3), dtype=uint8, numpy=array([[[126, 124, 123],.., [170, 166, 160]]],
dtype=uint8)>,
'image/filename': <Tensor: shape=(), dtype=string,
numpy=b'../../../../../local_data/datasets/animals/train/cat/cat_0002.png'>,
'label': <Tensor: shape=(), dtype=int64, numpy=0>
}
Helpful?
https://www.tensorflow.org/datasets/api_docs/python/tfds/folder_dataset/write_metadata
https://www.tensorflow.org/datasets/external_tfrecord
next attempt:
https://www.tensorflow.org/tutorials/load_data/images
July 26, 2025
Goal: load my own images for inference with model from tfds dataset
Solution:
set the parameter as_supervised to True.
(train_dataset, test_dataset)= builder.as_dataset(
split=['train', 'test'],
shuffle_files=False,
as_supervised=True
)
Documentation at
https://www.tensorflow.org/datasets/api_docs/python/tfds/folder_dataset/ImageFolder
The output types vary depending on the parameters. Examples:
# Default parameters: Returns the dict of tf.data.Dataset
# With as_supervised: tf.data.Dataset only contains (feature, label) tuples
Documentation at
https://www.tensorflow.org/datasets/api_docs/python/tfds/load
as_supervised:
bool, if True, the returned tf.data.Dataset will have a 2-tuple structure (input, label) according to
builder.info.supervised_keys.
If False, the default, the returned tf.data.Dataset will have a dictionary with
all the features.