Home > Back-end >  Convert df.unit8 to df.float32 in TensorFlow
Convert df.unit8 to df.float32 in TensorFlow

Time:11-22

I have a ds_train of MNIST data of data type unit8 and i want to convert it to float32 but i am getting the following error.

ValueError                                Traceback (most recent call last)
<ipython-input-14-ac6926bc60db> in <module>
----> 1 tf.image.convert_image_dtype(ds_trn,dtype=tf.float32, saturate=False)

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
    100       dtype = dtypes.as_dtype(dtype).as_datatype_enum
    101   ctx.ensure_initialized()
--> 102   return ops.EagerTensor(value, ctx.device_name, dtype)
    103 
    104 

ValueError: Attempt to convert a value (<PrefetchDataset element_spec=(TensorSpec(shape=(28, 28, 1), dtype=tf.uint8, name=None), TensorSpec(shape=(), dtype=tf.int64, name=None))>) with an unsupported type (<class 'tensorflow.python.data.ops.dataset_ops.PrefetchDataset'>) to a Tensor.

I was trying to convert it using tf.cast in order normalize it and getting it ready for further use of data.

CodePudding user response:

there are multiple causes

  1. It is between the process and the eager process
  2. Target conversion does not support, image type array *
  3. Variable update

Sample: Resizes is a lossless process, grays scales and conversion the command are line in order of the program designed with image process knowledge. To protect information loss in conversion you need to order less information loss to most conversions for the answer.

import tensorflow as tf

import matplotlib.pyplot as plt

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
@tf.function
def f(  ):
    image = plt.imread( "F:\\datasets\\downloads\\dark\\train\\01.jpg" )
    image = tf.keras.utils.img_to_array( image )
    image = tf.convert_to_tensor(image, dtype=tf.int64)
    image = tf.image.resize(image, [32,32], method='nearest')
    image = tf.image.rgb_to_grayscale( image )

    return image

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Tasks
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
image = f( )
print( image )
plt.imshow( image )
plt.show()

Output: Conversion, Resizes RGB.!

[[ 23]
  [ 19]
  [ 21]
  ...
  [ 15]
  [ 44]
  [ 42]]], shape=(32, 32, 1), dtype=int64)

Sample

  • Related