TensorFlow TypeError Value passed to parameter input has DataType uint8 not in list of allowed values float16, float32
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
This TensorFlow error means you passed an integer tensor (typically image pixel values stored as uint8, 0-255) to an operation that requires floating-point input (float16 or float32). The fix is to cast your tensor before the operation: tf.cast(tensor, tf.float32). For images, also normalize to the 0-1 range by dividing by 255.0. This error commonly appears when loading images, building preprocessing pipelines, or feeding raw data into model layers.
The Error
Fix: Cast to float32
Common Scenarios and Fixes
Image Loading Pipeline
Note: tf.image.resize() automatically casts to float32, but other operations like tf.image.random_flip_left_right() preserve the input dtype.
Using tf.data with numpy Arrays
Keras ImageDataGenerator
tf.keras.utils.image_dataset_from_directory
Mathematical Operations
Using Preprocessing Layers
The cleanest approach for Keras models — include normalization in the model:
Checking and Converting dtypes
Common Pitfalls
- Casting to float32 without normalizing:
tf.cast(image, tf.float32)converts pixel values to floats but keeps them in the 0-255 range. Most models expect input in 0-1 (divide by 255.0) or -1 to 1 (divide by 127.5, subtract 1). Unnormalized inputs cause poor training and NaN losses. - Assuming
tf.image.resize()preserves uint8:tf.image.resize()casts to float32 internally for interpolation. This can be surprising — the output is float32 even if the input was uint8, but the values are still in the 0-255 range unless you normalize. - Using integer division instead of float division: In Python 3,
image / 255works correctly (float division). Butimage // 255performs integer division, converting all values to 0 or 1. Always use/ 255.0to be explicit. - Applying data augmentation before casting: Some
tf.imagefunctions (likerandom_brightness) require float input, while others (likerandom_flip_left_right) accept uint8. Cast to float32 early in the preprocessing pipeline to avoid errors. - Double normalization: If using
Rescaling(1.0/255.0)in the model and also dividing by 255 in the data pipeline, images are normalized twice (values become 0 to 1/255). The model receives near-zero inputs, leading to very poor performance without obvious errors.
Summary
- Cast uint8 tensors to float32 with
tf.cast(tensor, tf.float32) / 255.0 - Use
tf.keras.layers.Rescaling(1.0/255.0)as a model layer for automatic handling ImageDataGenerator(rescale=1.0/255.0)handles conversion in legacy pipelines- Normalize images to 0-1 or -1 to 1 range — do not feed raw 0-255 float values to models
- Cast early in your preprocessing pipeline before applying mathematical operations
Related reading
- Tensorflow understanding tf.train.shuffle_batch
- TensorFlow Understanding the collections argument in tf.summary.scalar
- TensorFlow Unpooling
- Tensorflow upsampling by zero insertion with multiple dimensions
- TensorFlow ValueError Cannot feed value of shape 64, 64, 3 for Tensor u''Placeholder0'', which has shape ''?, 64, 64, 3''
- Tensorflow ValueError Can't load save_path when it is None in single shot detection
- tensorflow using 2 GPU at the same time
- TensorFlow using a tensor to index another tensor
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.