'TypeError: <tf.Tensor ... has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: numbers.Real

I am writing a function to save images to TFRecord files in order to then read then using the Data API of TensorFlow. However, when trying to create a TFRecord to save it, I receive the following error message:

TypeError: <tf.Tensor ...> has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: numbers.Real

The function used to create the TFRecord is:

def create_tfrecord(filepath, label):
    
    image = tf.io.read_file(filepath)
    image = tf.image.decode_jpeg(image, channels=1)
    image = tf.image.convert_image_dtype(image, tf.float32)
    image = tf.image.resize(image, [299, 299])
    
    tfrecord = Example(
        features = Features(
            feature = {
                'image' : Feature(float_list=FloatList(value=[image])),
                'label' : Feature(int64_list=Int64List(value=[label]))
    })).SerializeToString()
    
    return tfrecord

If you need additional information, please let me know.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source