尝试解码张量流数据时,获取“AttributeError:'MapDataset'对象没有属性'prefetch'”

时间:2018-03-08 17:04:19

标签: python tensorflow tensorflow-datasets tfrecord

我有一个训练有素的模型,我正在尝试运行测试,但我一直收到错误。这是我解码TFRecord数据的函数:

def getTestData(filename, dataSize, batch_size):

    dataset = tf.contrib.data.TFRecordDataset(filename).map(decodeTest).prefetch(batch_size)
    dataset = dataset.shuffle(dataSize)

    dataset = dataset.batch(batch_size)
    return dataset

这是我得到的错误:

   Traceback (most recent call last):

  File "<ipython-input-1-2f1981301a24>", line 1, in <module>
    runfile('H:/Documents/project/main.py', wdir='H:/Documents/project')

  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)

  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "H:/Documents/project/main.py", line 44, in <module>
    ts.test(netOut,x,y,sess, testSize,0)

  File "H:\Documents\project\testing\test.py", line 14, in test
    dataset = d.getTestData(test, testSize, 1)

  File "H:\Documents\project\readTF\readDataTF.py", line 26, in getTestData
    dataset = tf.contrib.data.TFRecordDataset(filename).map(decodeTest).prefetch(batch_size)

AttributeError: 'MapDataset' object has no attribute 'prefetch'

然后我删除了.prefetch(batch_size)并改为犯了这个错误。

Traceback (most recent call last):

  File "<ipython-input-1-2f1981301a24>", line 1, in <module>
    runfile('H:/Documents/project/main.py', wdir='H:/Documents/project')

  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)

  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "H:/Documents/project/main.py", line 44, in <module>
    ts.test(netOut,x,y,sess, testSize,0)

  File "H:\Documents\project\testing\test.py", line 17, in test
    sess.run(iterator.initializer)

  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tensorflow\python\client\session.py", line 789, in run
    run_metadata_ptr)

  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tensorflow\python\client\session.py", line 997, in _run
    feed_dict_string, options, run_metadata)

  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tensorflow\python\client\session.py", line 1132, in _do_run
    target_list, options, run_metadata)

  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tensorflow\python\client\session.py", line 1152, in _do_call
    raise type(e)(node_def, op, message)

NotFoundError: Function tf_map_func_f6e35dc6 is not defined.
     [[Node: MapDataset = MapDataset[Targuments=[], f=tf_map_func_f6e35dc6[], output_shapes=[[96], [64], [32]], output_types=[DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](TFRecordDataset)]]

Caused by op 'MapDataset', defined at:
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 231, in <module>
    main()
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 227, in main
    kernel.start()
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\ipykernel\kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tornado\ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\ipykernel\kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\ipykernel\zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\IPython\core\interactiveshell.py", line 2717, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\IPython\core\interactiveshell.py", line 2827, in run_ast_nodes
    if self.run_code(code, result):
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-2f1981301a24>", line 1, in <module>
    runfile('H:/Documents/project/main.py', wdir='H:/Documents/project')
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
  File "H:/Documents/project/main.py", line 44, in <module>
    ts.test(netOut,x,y,sess, testSize,0)
  File "H:\Documents\project\testing\test.py", line 15, in test
    iterator = dataset.make_initializable_iterator()
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tensorflow\contrib\data\python\ops\dataset_ops.py", line 396, in make_initializable_iterator
    return Iterator.from_dataset(self, shared_name)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tensorflow\contrib\data\python\ops\dataset_ops.py", line 98, in from_dataset
    initializer = gen_dataset_ops.make_iterator(dataset.make_dataset_resource(),
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tensorflow\contrib\data\python\ops\dataset_ops.py", line 1153, in make_dataset_resource
    self._input_dataset.make_dataset_resource(),
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tensorflow\contrib\data\python\ops\dataset_ops.py", line 1076, in make_dataset_resource
    self._input_dataset.make_dataset_resource(),
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tensorflow\contrib\data\python\ops\dataset_ops.py", line 1457, in make_dataset_resource
    output_shapes=nest.flatten(self.output_shapes))
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tensorflow\python\ops\gen_dataset_ops.py", line 297, in map_dataset
    output_shapes=output_shapes, name=name)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "C:\apps\Miniconda2\envs\tensorflowGPU\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

NotFoundError (see above for traceback): Function tf_map_func_f6e35dc6 is not defined.
     [[Node: MapDataset = MapDataset[Targuments=[], f=tf_map_func_f6e35dc6[], output_shapes=[[96], [64], [32]], output_types=[DT_FLOAT, DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](TFRecordDataset)]]

我正在使用TensorFlow版本1.2并在解码我的训练数据时使用了非常类似的功能并且没有出现任何错误,所以我不确定这次是什么问题。 (我无法升级到1.4版本,所以我需要1.2的解决方案。)

任何帮助表示赞赏!感谢。

1 个答案:

答案 0 :(得分:0)

TensorFlow 1.4中引入了Dataset.prefetch()方法,因此要运行确切的代码片段,您需要升级。 TensorFlow 1.4及更高版本中添加了许多与性能相关的优化。

作为替代方案,您可以使用map()参数def getTestData(filename, dataSize, batch_size): dataset = tf.contrib.data.TFRecordDataset(filename) # NOTE: This signature for `Dataset.map()` has been deprecated, and will not # work in TensorFlow 1.7 or later. dataset = dataset.map(decodeTest, output_buffer_size=batch_size) dataset = dataset.shuffle(dataSize) dataset = dataset.batch(batch_size) return dataset 来充当预取缓冲区:

NotFoundError

tf.Session是TensorFlow 1.3中的output_buffer_size错误。要解决此问题,您必须在在程序中创建{{1}}之前定义所有数据集