How to convert a string tensor to a python string in Tensorflow?
tf.train.string_input_producer()
return a queue, not a string. You have to use Graph operation, which get string tensor from this queue and read file from disk. For example you can use chain of operation:
image = tf.image.decode_jpeg(tf.read_file(filename_queue.dequeue()))
if you have jpeg files.
In TensorFlow 1.2 there is new structure Dataset for creating input pipeline. I think it is good idea to use Dataset instead queues.
Convert Tensorflow strings.split output to an Int
a_id
is a tensor of UTF-8 string bytes, not integer bytes. That's why you keep getting the wrong integer.
Use python decode() and encode() to translate between string bytes and python strings.
In your case, to get the string version of byte_id, do byte_id.decode()
how to get string value out of tf.tensor which dtype is string
You can use tf.py_func
to wrap load_audio_file()
.
import tensorflow as tf
tf.enable_eager_execution()
def load_audio_file(file_path):
# you should decode bytes type to string type
print("file_path: ",bytes.decode(file_path),type(bytes.decode(file_path)))
return file_path
train_dataset = tf.data.Dataset.list_files('clean_4s_val/*.wav')
train_dataset = train_dataset.map(lambda x: tf.py_func(load_audio_file, [x], [tf.string]))
for one_element in train_dataset:
print(one_element)
file_path: clean_4s_val/1.wav <class 'str'>
(<tf.Tensor: id=32, shape=(), dtype=string, numpy=b'clean_4s_val/1.wav'>,)
file_path: clean_4s_val/3.wav <class 'str'>
(<tf.Tensor: id=34, shape=(), dtype=string, numpy=b'clean_4s_val/3.wav'>,)
file_path: clean_4s_val/2.wav <class 'str'>
(<tf.Tensor: id=36, shape=(), dtype=string, numpy=b'clean_4s_val/2.wav'>,)
UPDATE for TF 2
The above solution will not work with TF 2 (tested with 2.2.0), even when replacing tf.py_func
with tf.py_function
, giving
InvalidArgumentError: TypeError: descriptor 'decode' requires a 'bytes' object but received a 'tensorflow.python.framework.ops.EagerTensor'
To make it work in TF 2, make the following changes:
- Remove
tf.enable_eager_execution()
(eager is enabled by default in TF 2, which you can verify withtf.executing_eagerly()
returningTrue
) - Replace
tf.py_func
withtf.py_function
- Replace all in-function references of
file_path
withfile_path.numpy()
Related Topics
If-Condition With Multiple Actions in Robot Framework
How to Make a Grade Calculator in Python
Python Number With 1000 Separator
Output the Same Amount of Rows as Asterisks Using For-Loop
How to Repeat a Function N Times
Most Efficient Way to Find Mode in Numpy Array
Cannot Find Reference 'Xxx' in _Init_.Py
How to Use Authenticated Proxy in Selenium Chromedriver
Python3: How to Print Out User Input String and Print It Out Separated by a Comma
How to Modify the Navigation Toolbar Easily in a Matplotlib Figure Window
How to Ignore Null Byte When Reading a CSV File
Django.Db.Utils.Operationalerror: (1045, Access Denied for User '<User>'@'Localhost'
Python Opencv - Add Alpha Channel to Rgb Image
How to Check If a String Contains 2 of the Same Character
Comparing Digits in an Integer in Python