I tried:
test_image = tf.convert_to_tensor(img, dtype=tf.float32)
Then following error appears:
ValueError: Tensor conversion requested dtype float32 for Tensor with dtype int64: 'Tensor("test/ArgMax:0", shape=TensorShape([Dimension(None)]), dtype=int64)'
You can cast generally using:
Replace tf.float32 with your desired type.
Edit: It seems at the moment at least, that
tf.cast
won't cast to an unsigned dtype (e.g.tf.uint8
). To work around this, you can cast to the signed equivalent and usedtf.bitcast
to get all the way. e.g.Oops, I find the function in the API...
image
type cast you can usetf.image.convert_image_dtype()
which convert image range[0 255]
to[0 1]
:output:
if you only want to cast type and keep value range use
tf.cast
ortf.to_float
as @stackoverflowuser2010 and @Mark McDonald answeredIn case your data is actually a Pandas dataframe, we can first check for the datatype using:
To cast all the entries into
float32
(for e.g.),You can use either
tf.cast(x, tf.float32)
ortf.to_float(x)
, both of which cast to float32.Example: