python - 尝试使用 tensorflow.io.gfile.GFile 和 tensorflow.dataset.map,得到“预期的二进制或 unicode 字符串,得到 Tensor 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)
给出错误“TypeError: Expected binary or unicode string, got tf.Tensor 'args_0:0' shape=() dtype=string”
在我看来,回溯看起来像是在“filePath.readlines()”上特别中断,任何帮助将不胜感激。
完整的错误输出:
-------------------------------------------------- ------------------------- TypeError Traceback(最近一次调用最后一次)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 in map(self, map_func, num_parallel_calls) 1770 如果 num_parallel_calls 为无:1771 返回 DatasetV1Adapter( -> 1772 MapDataset (self, map_func, preserve_cardinality=False)) 1773 else: 1774 return DatasetV1Adapter(
~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in init (self, input_dataset, map_func, use_inter_op_parallelism, preserve_cardinality, use_legacy_function) 3188
self._transformation_name(), 3189 dataset=input_dataset,-> 3190 use_legacy_function=use_legacy_function)3191 variant_tensor = gen_dataset_ops.map_dataset(3192
input_dataset._variant_tensor,#pylint:禁用=保护访问~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py init (self, func, transformation_name, dataset, input_classes, input_shapes, input_types, input_structure, add_to_graph, use_legacy_function, defun_kwargs) 2553 resource_tracker = tracking.ResourceTracker() 2554 with 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 in _get_concrete_function_internal_garbage_collected(self, *args, **kwargs) 1347 if self.input_signature: 1348 args, kwargs = None,无-> 1349 graph_function,_,_ = self._maybe_define_function(args,kwargs)1350 return graph_function 1351
~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _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 return graph_function, args, kwargs~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\eager\function.py in _create_graph_function(self, 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 in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes) 713 convert_func) 714 --> 715 func_outputs = python_func(*func_args, **func_kwargs) 716 717 # invariant:
func_outputs
只包含 Tensors, CompositeTensors,~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in wrapper_fn(*args) 2547 attributes=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 return [ops.convert_to_tensor(t) for t in ret]~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py in _wrapper_helper(*args) 2487 nested_args = (nested_args,) 2488 -> 2489 ret = func(*nested_args ) 2490 # 如果
func
返回张量列表,nest.flatten()
并且 2491 #ops.convert_to_tensor()
会合谋尝试堆叠在 pathToImgTF(path) 9 def pathToImgTF(path): 10 with tf.io.gfile.GFile(path,'rb') as filePath: ---> 11 step1=filePath.readlines() 12 step2=[x.strip (b'\n') for x in step1] 13 for x in step2:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\lib\io\file_io.py in readlines(self) 181 def readlines(self): 182 """返回文件中的所有行一个列表。""" --> 183 self._preread_check() 184 lines = [] 185 while True:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\lib\io\file_io.py in _preread_check(self) 82“文件未打开读取”) 83 self._read_buf = pywrap_tensorflow。 CreateBufferedInputStream(---> 84 compat.as_bytes(self.__name), 1024 * 512) 85 86 def _prewrite_check(self):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\util\compat.py in as_bytes(bytes_or_text, encoding) 63 else: 64 raise TypeError('Expected binary or unicode string, got %r' % ---> 65 (bytes_or_text,)) 66 67
TypeError: Expected binary or unicode string, got tf.Tensor 'args_0:0' shape=() dtype=string
解决方案
我不确定有什么样的imgPath1
数据imgPath2
。无论如何,它们必须是具有预定义数据类型的张量类型。这两个包括实际数据。
如果是这种情况,则将方括号更改为圆括号,如下所示 -
ds=tf.data.Dataset.from_tensor_slices((imgPath1,imgPath2))
如果这仍然抛出相同的错误,那么请提供它究竟是在哪里抛出错误的信息。
推荐阅读
- spring-boot - 我正在尝试将带有预签名 URL 的图像上传到 Google Cloud Storage,但对象元数据已写入对象
- matplotlib - 无法解释 matplotlib 绘图
- ionic-framework - 离子 + Android 9 + 文件传输
- r - R Github 热图颜色修改
- java - 在 Java 中使用 GUI jButton 更新 JDBC 会导致 java.lang.NullPointerException
- c# - 我没有在 Y 坐标图上绘制正确数据的错误在哪里
- python - 使 Python 脚本可执行并添加包 (Windows)
- reactjs - Adding items to commandbar fabricui
- bash - zsh: command not found: nest
- java - How can I remove an element of array without using list or collection methods