将 TensorFlow 字符串转换为 python 字符串

将 TensorFlow 字符串转换为 python 字符串

问题描述:

我知道在 TensorFlow 中,tf.string 张量基本上是一个字节字符串.我需要使用存储在队列中的文件名执行一些操作 tf.train.string_input_producer().

I am aware that in TensorFlow, a tf.string tensor is basically a byte string. I need to do some operation with a filename which is stored in a queue using tf.train.string_input_producer().

下面显示了一个小片段:

A small snippet is shown below :

 key, value = reader.read(filename_queue)
 filename = value.eval(session=sess)
 print(filename)

然而,作为一个字节串,它给出如下输出:

However as a byte string it gives an output like the following :

b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08'

我尝试使用

filename = tf.decode_raw(filename, tf.uint8)
filename = ''.join(chr(i) for i in filename)

然而,Tensor 对象不可迭代,因此失败.

However Tensor objects are not iterable and hence this fails.

我哪里出错了?

TensorFlow 中是否缺少将 tf.string 轻松转换为 Python 字符串的功能,或者是否还有其他一些我不知道的功能?

Is it a missing feature in TensorFlow that tf.string be converted to a Python string easily , or is there some other feature I am not aware about ?

更多信息

filename_queue 已准备如下:

The filename_queue has been prepared as follows :

train_set = ['file1.jpg', 'file2.jpg'] # Truncated for illustration
filename_queue = tf.train.string_input_producer(train_set, num_epochs=10, seed=0, capacity=1000)                  

在 tensorflow 2.0.0 中,可以通过以下方式完成:

In tensorflow 2.0.0, it can be done in the following way:

import tensorflow as tf

my_str = tf.constant('Hello World')
my_str_npy = my_str.numpy()

print(my_str_npy)
type(my_str_npy)

这将一个字符串张量转换为一个'bytes'类的字符串

This converts a string tensor into a string of 'bytes' class