尝试使用 tensorflow.io.gfile.GFile 和 tensorflow.dataset.map,得到"预期的二进制或 unicode 字符串,得到张量 dtype=string



我对使用Tensorflow很陌生,想象有人会很快告诉我我在做一些愚蠢的事情,所以在这里。

我正在使用 MSTAR 数据集并尝试将其读取。这些文件的格式非常奇怪,但足以说明,如果急切执行,则以下代码会读取并显示数据集中的图像。

import tensorflow as tf
import matplotlib.pyplot as plt
tf.enable_eager_execution()
img1Path='HB15000.018'
img2Path='HB15001.018'
def pathToImgTF(path):
with tf.io.gfile.GFile(path,'rb') as filePath:
step1=filePath.readlines()
step2=[x.strip(b'n') for x in step1]
for x in step2:
if b'PhoenixHeaderLength' in x:
line=x.strip().split(b'=')
PhoenixHeaderLength=int(line[1])
elif b'native_header_length' in x:
line=x.strip().split(b'=')
native_header_length=int(line[1])
elif b'NumberOfColumns' in x:
line=x.strip().split(b'=')
NumberOfColumns=int(line[1])
elif b'NumberOfRows' in x:
line=x.strip().split(b'=')
NumberOfRows=int(line[1])
filePath.seek(PhoenixHeaderLength+native_header_length)
step3=tf.decode_raw(filePath.read(),out_type=tf.float32,little_endian=False)
depth_major=tf.reshape(step3,[2,NumberOfRows,NumberOfColumns])
image=tf.transpose(depth_major,[1,2,0])[:,:,0] #Cut off phase for now
return image
img=pathToImgTF(imgPath)
plt.imshow(img,cmap='gray')

我想使用tf.dataset.from_tensor_slices,但似乎这不是一个选项,因为以下代码:

ds=tf.data.Dataset.from_tensor_slices([img1Path,img2Path])
ds=ds.map(pathToImgTF)

给出错误"类型错误:预期的二进制或 unicode 字符串,得到 tf。张量 'args_0:0' 形状=() dtype=string">

在我看来,回溯就像是专门针对"filePath.readlines()"中断的,任何帮助将不胜感激。

完整错误输出:

---------------------------------------------------------------------------类型错误回溯(最近一次调用) 最后) IPYTHON-input-6-e12909FB73cd in module 1 ds=tf.data.Dataset.from_tensor_slices([img1Path,img2Path]) ----> 2 ds=ds.map(pathToImgTF)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py 在地图(自我,map_func,num_parallel_calls)1770 if num_parallel_calls为无:1771 返回数据集V1适配器( -> 1772 MapDataset(self, map_func, preserve_cardinality=False)) 1773 其他: 1774 返回 数据集V1适配器(

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py 在初始化(自我、input_dataset、map_func、use_inter_op_parallelism、 preserve_cardinality, use_legacy_function) 3188
self._transformation_name(), 3189 数据集=input_dataset, -> 3190 use_legacy_function=use_legacy_function) 3191 variant_tensor = gen_dataset_ops.map_dataset( 3192
input_dataset._variant_tensor, # pylint: disable=protected-access

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py ininit(self, func, transformation_name, dataset, input_classes, input_shapes、input_types、input_structure、add_to_graph、 use_legacy_function, defun_kwargs) 2553 resource_tracker = 跟踪。资源跟踪器() 2554 与 tracking.resource_tracker_scope(resource_tracker): -> 2555 self._function = wrapper_fn._get_concrete_function_internal() 2556 if add_to_graph: 2557
self._function.add_to_graph(ops.get_default_graph())

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _get_concrete_function_internal(self, *args, **kwargs) 1353
""在获取图函数时绕过错误检查。 1354
graph_function = self._get_concrete_function_internal_garbage_collected( -> 1355 *args, **kwargs) 1356 # 我们将这个具体函数返回给某人,他们可能会保留 1357 # 对 FuncGraph 的引用而不保留对

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\eager\function.py 在_get_concrete_function_internal_garbage_collected(自我,*参数, **kwargs) 1347 如果self.input_signature:1348 args,kwargs = 无,无 -> 1349 graph_function, _, _ = self._maybe_define_function(args, kwargs) 1350 返回 graph_function 1351

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\eager\function.py 在 _maybe_define_function(self, args, kwargs) 1650 graph_function
= self._function_cache.primary.get(cache_key, None)
1651 如果graph_function为 None: -> 1652 graph_function = self._create_graph_function(args, kwargs) 1653 self._function_cache.primary[cache_key] = graph_function 1654年返回graph_function,args,kwargs

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\eager\function.py 在_create_graph_function(自我,args,kwargs, override_flat_arg_shapes) 1543 arg_names=
arg_names,1544 override_flat_arg_shapes=override_flat_arg_shapes, -> 1545 capture_by_value=self._capture_by_value), 1546 self._function_attributes) 1547

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py 在func_graph_from_py_func(姓名,python_func,参数,夸格,签名, func_graph、亲笔签名、autograph_options、add_control_dependencies、 arg_names、op_return_value、收藏capture_by_value、 override_flat_arg_shapes) 713 converted_func) 714 --> 715 func_outputs = python_func(*func_args, **func_kwargs) 716717#不变量:func_outputs只包含张量、复合张量、

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py 在 wrapper_fn(*args) 2547 属性=defun_kwargs)
2548 def wrapper_fn(*args): # pylint: disable=missing-docstring -> 2549 ret = _wrapper_helper(*args) 2550 ret = self._output_structure._to_tensor_list(ret) 2551 返回 [ops.convert_to_tensor(t) 表示 t in re]

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py 在 _wrapper_helper(*args) 2487 nested_args = (nested_args,) 2488 -> 2489 ret = func(*nested_args) 2490 # 如果func返回张量列表,则nest.flatten()和 2491 #ops.convert_to_tensor()会合谋试图堆叠

in pathToImgTF(path) 9 def pathToImgTF(path): 10 使用 tf.io.gfile.GFile(path,'rb') 作为 filePath: ---> 11 step1=filePath.readlines() 12 step2=[x.strip(b'') for x in step1] 步骤 2 中的 x 为 13:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\lib\io\file_io.py 在阅读行中(自我) 181 定义 阅读线(自我): 182 ""返回列表中文件的所有行。"" --> 183 self._preread_check() 184 行 = [] 185 而真:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\lib\io\file_io.py 在_preread_check(个体经营) 82 "文件未打开以供读取") 83 self._read_buf = pywrap_tensorflow。CreateBufferedInputStream( ---> 84 compat.as_bytes(self.__name),1024*512) 85 86 定义_prewrite_check(个体经营):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\util\compat.py 在as_bytes(bytes_or_text,编码) 63 其他: 64 提高类型错误('预期的二进制或 unicode 字符串,得到 %r' % ---> 65 (bytes_or_text,)) 66 67

类型错误:预期的二进制或统一码字符串,得到 tf。张肌 'args_0:0' 形状=() dtype=string

我不确定imgPath1imgPath2拥有什么样的数据。无论如何,它们必须是具有预定义数据类型的张量类型。这两个包含实际数据。

如果是这种情况,则将方括号更改为圆形括号,如下所示-ds=tf.data.Dataset.from_tensor_slices((imgPath1,imgPath2))

如果这仍然抛出相同的错误,那么请提供它抛出错误的确切位置的信息。