我知道在TensorFlow中,tf.string张量基本上是一个字节字符串.我需要使用tf.train.string_input_producer()存储在队列中的文件名进行一些操作.
下面显示了一个小片段:
key, value = reader.read(filename_queue)
filename = value.eval(session=sess)
print(filename)
Run Code Online (Sandbox Code Playgroud)
但是,作为字节字符串,它提供如下输出:
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'
Run Code Online (Sandbox Code Playgroud)
我试着转换使用
filename = tf.decode_raw(filename, tf.uint8)
filename = ''.join(chr(i) for i in filename)
Run Code Online (Sandbox Code Playgroud)
但是Tensor对象不可迭代,因此失败.
我哪里错了?
它是TensorFlow中缺少的一个特性,tf.string可以很容易地转换为Python字符串,还是有其他一些我不知道的功能?
更多信息
filename_queue的编写如下:
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)
Run Code Online (Sandbox Code Playgroud) 我有一个简单的问题是tf.py_func功能.
我有一个my_img形状张量的形状(1,224,224,3).为了测试py_func,我将张量提供给python函数return_tf,该函数应该返回相同的张量(根据文档转换为numpy数组之后).
这是代码:
def return_tf(x):
return np.array(x)
test = tf.py_func(return_tf,[my_img],[tf.float32])
Run Code Online (Sandbox Code Playgroud)
但当我检查返回张量的形状时test,我得到:
tf.Tensor 'PyFunc:0' shape=unknown dtype=float32
Run Code Online (Sandbox Code Playgroud)
我也无法eval()在张量上运行,因为我收到了错误:
AttributeError: 'list' object has no attribute 'eval'.
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何修复由张量返回的张量形状tf.py_func?
To reproduce my problem, try this first (mapping with py_func):
import tensorflow as tf
import numpy as np
def image_parser(image_name):
a = np.array([1.0,2.0,3.0], dtype=np.float32)
return a
images = [[1,2,3],[4,5,6]]
im_dataset = tf.data.Dataset.from_tensor_slices(images)
im_dataset = im_dataset.map(lambda image:tuple(tf.py_func(image_parser, [image], [tf.float32])), num_parallel_calls = 2)
im_dataset = im_dataset.prefetch(4)
iterator = im_dataset.make_initializable_iterator()
print(im_dataset.output_shapes)
Run Code Online (Sandbox Code Playgroud)
It will give you (TensorShape(None),)
However, if you try this (using direct tensorflow mapping instead of py_func):
import tensorflow as tf
import numpy as np
def image_parser(image_name)
return image_name
images = [[1,2,3],[4,5,6]] …Run Code Online (Sandbox Code Playgroud)