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

将TensorFlow字符串转换为python字符串,python,tensorflow,Python,Tensorflow,我知道在TensorFlow中,tf.string张量基本上是一个字节字符串。我需要使用存储在队列中的文件名执行一些操作 下面显示了一个小片段: key, value = reader.read(filename_queue) filename = value.eval(session=sess) print(filename) 但是,作为字节字符串,它提供如下输出: b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x

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

下面显示了一个小片段:

 key, value = reader.read(filename_queue)
 filename = value.eval(session=sess)
 print(filename)
但是,作为字节字符串,它提供如下输出:

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)
然而,张量对象是不可迭代的,因此这是失败的

我哪里做错了

在TensorFlow中,tf.string可以轻松地转换为Python字符串,这是缺少的特性,还是我不知道还有其他特性

更多信息

文件名_队列已准备好,如下所示:

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)                  

在这种情况下,读者只需读取您提供的文件,因此value是文件的内容,而不是文件名,但您可以输出key,然后获得文件名使用compat中的as_text函数(来自tensorflow.python.util)转换tensorflow的字节字符串。即


filename=compat。在tensorflow 2.0.0中,作为文本(filename)

,可以通过以下方式完成:

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”类的字符串,您可以通过tf.numpy_函数包装器来实现

def get_img(path):
    path = bytes.decode(path) # called when use dataset since dataset is generator
    img = skimage.io.MultiImage(path)[-1]
    print(img.shape, type(img))
    return path

def wrap_get_img(path):  # turn tf.Tensor to tf.EagerTensor through the wrapper
    return tf.numpy_function(get_img, [path], [tf.string]) # [<tf.Tensor 'EagerPyFunc:0'


dataset = tf.data.Dataset.list_files("../prostate-cancer-grade-assessment/train_images/*.tiff") \
            .repeat()  \
            .shuffle(buffer_size=len(files)) \
            .map(wrap_get_img )    

for x in dataset:
    print(x) # Eager Tensor which can get string
    break
def get_img(路径):
path=bytes.decode(path)#使用数据集时调用,因为数据集是生成器
img=skimage.io.MultiImage(路径)[-1]
打印(img.形状、类型(img))
返回路径
def wrap_get_img(路径):#通过包装器将tf.Tensor转换为tf.Tensor

返回tf.numpy_函数(get_img,[path],[tf.string])#[如果您想在Python中使用该字符串,您需要首先使用它。正如您所看到的,我已经在会话中执行了该图。您的第二种方法很好(
decode_raw
),您只需要首先计算张量。尽管我感觉在第一种方法中您没有得到想要的结果的原因是,这是二进制数据,而不是合理的文件名。已使用
eval()计算了张量
。之后,当我使用decode\u raw时,我得到了问题中所述的错误。至于数据的有效性,它是有效的,因为
tf.train.string\u input\u producer().嗨,@Ujjwal,你解决过这个问题吗?我正在寻找解决方案。谢谢。这很简单,但我猜不出来,嗯,numpy()也是非数值属性错误:“Tensor”对象在tensorflow 2.1.0AttributeError中没有属性“numpy”:“Tensor”对象在tensorflow 2.2.0中没有属性“numpy”?有人能为新版本的tensorflow解决这个问题吗?我可以确认上述转换代码是否有效:
a=tf.constant('hello'))
后跟
a.numpy().decode('ascii')
将返回一个带有TF2.2的python字符串。但是,我认为这只适用于急切执行模式。例如,它在命令行中工作正常,但在定义图形时可能不工作,但在执行图形时工作。
def get_img(path):
    path = bytes.decode(path) # called when use dataset since dataset is generator
    img = skimage.io.MultiImage(path)[-1]
    print(img.shape, type(img))
    return path

def wrap_get_img(path):  # turn tf.Tensor to tf.EagerTensor through the wrapper
    return tf.numpy_function(get_img, [path], [tf.string]) # [<tf.Tensor 'EagerPyFunc:0'


dataset = tf.data.Dataset.list_files("../prostate-cancer-grade-assessment/train_images/*.tiff") \
            .repeat()  \
            .shuffle(buffer_size=len(files)) \
            .map(wrap_get_img )    

for x in dataset:
    print(x) # Eager Tensor which can get string
    break