Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 为keras图像生成器迭代tf.data.Dataset.from_生成器。flow_from_dir抛出错误_Python 3.x_Tensorflow_Tensorflow Datasets_Tf.keras - Fatal编程技术网

Python 3.x 为keras图像生成器迭代tf.data.Dataset.from_生成器。flow_from_dir抛出错误

Python 3.x 为keras图像生成器迭代tf.data.Dataset.from_生成器。flow_from_dir抛出错误,python-3.x,tensorflow,tensorflow-datasets,tf.keras,Python 3.x,Tensorflow,Tensorflow Datasets,Tf.keras,这是将Keras ImageDataGenerator与来自_目录的.flow_一起使用,并用来自_generator的tf.data.Dataset.from(…)包装它的情况。对数据集进行任何迭代的尝试都失败 错误摘要: InvalidArgumentError:TypeError:endswith first arg必须是字节或字节元组,而不是str 代码段: import tensorflow as tf # version 2.1.0 DATA_URL = 'https://st

这是将Keras ImageDataGenerator与来自_目录的.flow_一起使用,并用来自_generator的tf.data.Dataset.from(…)包装它的情况。对数据集进行任何迭代的尝试都失败

错误摘要:

InvalidArgumentError:TypeError:endswith first arg必须是字节或字节元组,而不是str

代码段:

import tensorflow as tf   # version 2.1.0

DATA_URL = 'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz'
flowers_root_path = tf.keras.utils.get_file(origin=DATA_URL, fname='flower_photos', untar=True)

img_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, rotation_range=20)
gen = img_gen.flow_from_directory(flowers_root_path)

ds = tf.data.Dataset.from_generator(
  # lambda: gen,            # this works
  img_gen.flow_from_directory, args=[flowers_root_path],    # this failed.
  output_types=(tf.float32, tf.float32), 
  output_shapes=([32,256,256,3], [32,5])
)

it = iter(ds)
batch = next(it)
print(batch)
使用“lambda:gen”看起来不错。知道为什么吗

完整堆栈跟踪:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/context.py in execution_mode(mode)
   1896     ctx.executor = executor_new
-> 1897     yield
   1898   finally:

10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/iterator_ops.py in _next_internal(self)
    658             output_types=self._flat_output_types,
--> 659             output_shapes=self._flat_output_shapes)
    660 

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/gen_dataset_ops.py in iterator_get_next_sync(iterator, output_types, output_shapes, name)
   2478     except _core._NotOkStatusException as e:
-> 2479       _ops.raise_from_not_ok_status(e, name)
   2480   # Add nodes to the TensorFlow graph.

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in raise_from_not_ok_status(e, name)
   6605   # pylint: disable=protected-access
-> 6606   six.raise_from(core._status_to_exception(e.code, message), None)
   6607   # pylint: enable=protected-access

/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: TypeError: endswith first arg must be bytes or a tuple of bytes, not str
Traceback (most recent call last):

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 673, in get_iterator
    return self._iterators[iterator_id]

KeyError: 0


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/script_ops.py", line 236, in __call__
    ret = func(*args)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 789, in generator_py_func
    values = next(generator_state.get_iterator(iterator_id))

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 675, in get_iterator
    iterator = iter(self._generator(*self._args.pop(iterator_id)))

  File "/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/image_data_generator.py", line 540, in flow_from_directory
    interpolation=interpolation

  File "/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/directory_iterator.py", line 126, in __init__
    classes, filenames = res.get()

  File "/usr/lib/python3.6/multiprocessing/pool.py", line 644, in get
    raise self._value

  File "/usr/lib/python3.6/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))

  File "/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/utils.py", line 216, in _list_valid_filenames_in_directory
    for root, fname in valid_files:

  File "/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/utils.py", line 172, in _iter_valid_files
    if fname.lower().endswith('.tiff'):

TypeError: endswith first arg must be bytes or a tuple of bytes, not str


     [[{{node PyFunc}}]] [Op:IteratorGetNextSync]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-56-a2623f5ab104> in <module>()
      1 it = iter(ds)
----> 2 batch = next(it)
      3 print(batch)

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/iterator_ops.py in __next__(self)
    628 
    629   def __next__(self):  # For Python 3 compatibility
--> 630     return self.next()
    631 
    632   def _next_internal(self):

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/iterator_ops.py in next(self)
    672     """Returns a nested structure of `Tensor`s containing the next element."""
    673     try:
--> 674       return self._next_internal()
    675     except errors.OutOfRangeError:
    676       raise StopIteration

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/iterator_ops.py in _next_internal(self)
    663         return self._element_spec._from_compatible_tensor_list(ret)  # pylint: disable=protected-access
    664       except AttributeError:
--> 665         return structure.from_compatible_tensor_list(self._element_spec, ret)
    666 
    667   @property

/usr/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
     97                 value = type()
     98             try:
---> 99                 self.gen.throw(type, value, traceback)
    100             except StopIteration as exc:
    101                 # Suppress StopIteration *unless* it's the same exception that

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/context.py in execution_mode(mode)
   1898   finally:
   1899     ctx.executor = executor_old
-> 1900     executor_new.wait()
   1901 
   1902 

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/executor.py in wait(self)
     65   def wait(self):
     66     """Waits for ops dispatched in this executor to finish."""
---> 67     pywrap_tensorflow.TFE_ExecutorWaitForAllPendingNodes(self._handle)
     68 
     69   def clear_error(self):

InvalidArgumentError: TypeError: endswith first arg must be bytes or a tuple of bytes, not str
Traceback (most recent call last):

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 673, in get_iterator
    return self._iterators[iterator_id]

KeyError: 0


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/ops/script_ops.py", line 236, in __call__
    ret = func(*args)

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 789, in generator_py_func
    values = next(generator_state.get_iterator(iterator_id))

  File "/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/dataset_ops.py", line 675, in get_iterator
    iterator = iter(self._generator(*self._args.pop(iterator_id)))

  File "/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/image_data_generator.py", line 540, in flow_from_directory
    interpolation=interpolation

  File "/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/directory_iterator.py", line 126, in __init__
    classes, filenames = res.get()

  File "/usr/lib/python3.6/multiprocessing/pool.py", line 644, in get
    raise self._value

  File "/usr/lib/python3.6/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))

  File "/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/utils.py", line 216, in _list_valid_filenames_in_directory
    for root, fname in valid_files:

  File "/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/utils.py", line 172, in _iter_valid_files
    if fname.lower().endswith('.tiff'):

TypeError: endswith first arg must be bytes or a tuple of bytes, not str


     [[{{node PyFunc}}]]
---------------------------------------------------------------------------
InvalidArgumentError回溯(最后一次最近调用)
/执行模式下的usr/local/lib/python3.6/dist-packages/tensorflow\u core/python/eager/context.py(模式)
1896 ctx.executor=executor_new
->1897年产量
1898最后:
10帧
/usr/local/lib/python3.6/dist-packages/tensorflow\u core/python/data/ops/iterator\u ops.py in\u next\u internal(self)
658输出类型=自。_平面输出类型,
-->659输出_形状=自。_平面_输出_形状)
660
/迭代器中的usr/local/lib/python3.6/dist-packages/tensorflow\u core/python/ops/gen\u dataset\u ops.py下一步同步(迭代器、输出类型、输出形状、名称)
2478除“核心”外,其他状态除外,如e:
->2479操作。从不正常状态(e,名称)提升
2480#将节点添加到TensorFlow图中。
/usr/local/lib/python3.6/dist-packages/tensorflow\u core/python/framework/ops.py处于从非正常状态(e,名称)提升到正常状态(e)
6605#pylint:disable=受保护访问
->6606六。从(核心状态)提升到异常(例如代码、消息),无
6607#pylint:enable=受保护访问
/raise_from(value,from_value)中的usr/local/lib/python3.6/dist-packages/six.py
InvalidArgumentError:TypeError:endswith first arg必须是字节或字节元组,而不是str
回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python3.6/dist packages/tensorflow_core/python/data/ops/dataset_ops.py”,第673行,在get_迭代器中
返回self.\u迭代器[iterator\u id]
关键错误:0
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python3.6/dist packages/tensorflow_core/python/ops/script_ops.py”,第236行,在调用中__
ret=func(*args)
文件“/usr/local/lib/python3.6/dist-packages/tensorflow\u-core/python/data/ops/dataset\u-ops.py”,第789行,在generator\u-py\u-func中
values=next(生成器\状态.获取\迭代器(迭代器\ id))
文件“/usr/local/lib/python3.6/dist packages/tensorflow_core/python/data/ops/dataset_ops.py”,第675行,在get_迭代器中
迭代器=iter(self.\u生成器(*self.\u args.pop(迭代器\u id)))
文件“/usr/local/lib/python3.6/dist packages/keras_preprocessing/image/image_data_generator.py”,第540行,在flow_from_目录中
插值=插值
文件“/usr/local/lib/python3.6/dist packages/keras_preprocessing/image/directory_iterator.py”,第126行,在uu init中__
类,文件名=res.get()
get中第644行的文件“/usr/lib/python3.6/multiprocessing/pool.py”
提升自我价值
worker中的文件“/usr/lib/python3.6/multiprocessing/pool.py”,第119行
结果=(True,func(*args,**kwds))
文件“/usr/local/lib/python3.6/dist-packages/keras\u preprocessing/image/utils.py”,第216行,在\u-list\u-valid\u-filenames\u目录中
对于root,有效的\u文件中的fname:
文件“/usr/local/lib/python3.6/dist packages/keras_preprocessing/image/utils.py”,第172行,在有效文件中
如果fname.lower().endswith('.tiff'):
TypeError:endswith first arg必须是字节或字节元组,而不是str
[{{node PyFunc}}][Op:IteratorGetNextSync]
在处理上述异常期间,发生了另一个异常:
InvalidArgumentError回溯(最后一次最近调用)
在()
1 it=国际热核实验堆(ds)
---->2批=下一批(it)
3打印(批)
/usr/local/lib/python3.6/dist-packages/tensorflow\u core/python/data/ops/iterator\u ops.py in\uuuuuuuuuuuuuuuuuuuuuu(self)
628
629 def_uunext_u(self):#用于Python 3兼容性
-->630返回self.next()
631
632 def_下一个_内部(自身):
/下一步中的usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/iterator_ops.py(self)
672“返回包含下一个元素的`张量'嵌套结构。”“”
673尝试:
-->674返回自我。\u下一个\u内部()
675错误除外。超出范围错误:
676上升停止迭代
/usr/local/lib/python3.6/dist-packages/tensorflow\u core/python/data/ops/iterator\u ops.py in\u next\u internal(self)
663从兼容的张量列表(ret)返回自元素规格pylint:disable=受保护访问
664除属性错误外:
-->665返回结构。来自兼容张量列表(self.\u元素\u规范,ret)
666
667@property
/usr/lib/python3.6/contextlib.py in\uuuuuu exit\uuuuuu(self、type、value、traceback)
97值=类型()
98尝试:
--->99自我生成抛出(类型、值、回溯)
100除作为exc的停止迭代外:
101#抑制StopIteration*除非*与
/执行模式下的usr/local/lib/python3.6/dist-packages/tensorflow\u core/python/eager/context.py(模式)
1898最后:
1899 ctx.executor=executor_old
->1900执行人_新。等待()
1901
1902
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/eager/executor.py in wait(self)
65 def等待(自我):
66“等待在此执行器中分派的操作完成。”“”
--->67 pywrap_tensorflow.TFE_执行器等待所有挂起节点(self._句柄)
68
69 def清除_错误(自身):
InvalidArgumentError:TypeError:endswith first arg必须是字节或字节元组,而不是str
回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python3.6/dist packages/tensorflow_core/python/data/ops/dataset_ops.py”,第673行,在get_迭代器中
返回self.\u迭代器[iterator\u id]
关键错误:0
在h
gen = img_gen.flow_from_directory(flowers_root_path)
def Gen():
  gen = img_gen.flow_from_directory(flowers_root_path)
  for (x,y) in gen:
    yield (x,y)
import tensorflow as tf   # version 2.1.0

DATA_URL = 'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz'
flowers_root_path = tf.keras.utils.get_file(origin=DATA_URL, fname='flower_photos', untar=True)

img_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255, rotation_range=20)
def Gen():
  gen = img_gen.flow_from_directory(flowers_root_path)
  for (x,y) in gen:
    yield (x,y)

ds = tf.data.Dataset.from_generator(  
  Gen,  output_types=(tf.float32, tf.float32),  output_shapes=([32,256,256,3], [32,5]))

it = iter(ds)
batch = next(it)
print(batch)