gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:51:20 26 4
gpt4 key购买 nike

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

下面显示了一个小片段:

 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)

但是 Tensor 对象不可迭代,因此失败。

我哪里错了?

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)

最佳答案

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

import tensorflow as tf

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

print(my_str_npy)
type(my_str_npy)

这将字符串张量转换为“字节”类的字符串

关于python - 将 TensorFlow 字符串转换为 Python 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42144915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com