Tensorflow tfrecords读取错误(CSV转换数据)

时间:2017-04-05 22:27:03

标签: csv tensorflow record

我已将我的CSV数据尝试为tfrecords格式并读取tfrecords文件。 但是我无法读取tfrecord文件。

首先我将CSV文件转换为tfrecords文件。

我的CSV数据就是这个

1,2,3,4,5,1

2,3,4,5,6,2

3,4,5,6,7,3

4,5,6,7,8,4

第1~5列是功能,第6列是标签

我使用此代码转换了此CSV文件

import pandas as pd
import tensorflow as tf
import argparse
import os
import sys
FLAGS = None

name = 'test'
filename = name + '.tfrecords'
print('Writing', filename)
writer = tf.python_io.TFRecordWriter(filename)    
for row in csv:
    features, label = row[:-1], row[-1]
    print(features)
    print(label)
    example = tf.train.Example()
    example.features.feature['features'].int64_list.value.extend(features)
    example.features.feature['label'].int64_list.value.append(label)
    writer.write(example.SerializeToString())
writer.close()  

然后我有一个tfrecords文件。 (test.tfrecords)

我的问题是我无法读取此tfrecords文件。

我尝试过此代码,但收到了错误消息。

def read_and_decode(filename_queue):
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(
    serialized_example,
    # Defaults are not specified since both keys are required.
    features={
        'features': tf.FixedLenFeature([], tf.string),
        'label': tf.FixedLenFeature([], tf.int64)
    })
    image = tf.decode_raw(features['features'], tf.int64)
    label = tf.cast(features['label'], tf.int32)
    return image, label

def get_all_records(FILE):
    with tf.Session() as sess:
        filename_queue = tf.train.string_input_producer([ FILE ])
        image, label = read_and_decode(filename_queue)
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        coord = tf.train.Coordinator()        
        threads = tf.train.start_queue_runners(coord=coord)
        for i in range(100):
            example, l = sess.run([image, label])
            print (example,l)
        coord.request_stop()
        coord.join(threads)

get_all_records('test.tfrecords')

这是错误消息

INFO:tensorflow:Error reported to Coordinator: <class 'tensorflow.python.framework.errors_impl.CancelledError'>, Run call was cancelled
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1021     try:
-> 1022       return fn(*args)
   1023     except errors.OpError as e:

C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
   1003                                  feed_dict, fetch_list, target_list,
-> 1004                                  status, run_metadata)
   1005 

C:\Program Files\Anaconda3\envs\tf\lib\contextlib.py in __exit__(self, type, value, traceback)
     65             try:
---> 66                 next(self.gen)
     67             except StopIteration:

C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\framework\errors_impl.py in raise_exception_on_not_ok_status()
    468           compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 469           pywrap_tensorflow.TF_GetCode(status))
    470   finally:

InvalidArgumentError: Name: <unknown>, Key: features, Index: 0.  Data types don't match. Data type: int64Expected type: string
     [[Node: ParseSingleExample/ParseExample/ParseExample = ParseExample[Ndense=2, Nsparse=0, Tdense=[DT_STRING, DT_INT64], dense_shapes=[[], []], sparse_types=[], _device="/job:localhost/replica:0/task:0/cpu:0"](ParseSingleExample/ExpandDims, ParseSingleExample/ParseExample/ParseExample/names, ParseSingleExample/ParseExample/ParseExample/dense_keys_0, ParseSingleExample/ParseExample/ParseExample/dense_keys_1, ParseSingleExample/ParseExample/Const, ParseSingleExample/ParseExample/Const_1)]]
     [[Node: ParseSingleExample/ParseExample/ParseExample/_3 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/gpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_13_ParseSingleExample/ParseExample/ParseExample", tensor_type=DT_INT64, _device="/job:localhost/replica:0/task:0/gpu:0"]()]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-6-0bf78295d664> in <module>()
----> 1 get_all_records('test.tfrecords')

<ipython-input-5-afbcd262993d> in get_all_records(FILE)
      8         threads = tf.train.start_queue_runners(coord=coord)
      9         for i in range(100):
---> 10             example, l = sess.run([image, label])
     11             print (example,l)
     12         coord.request_stop()

C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    765     try:
    766       result = self._run(None, fetches, feed_dict, options_ptr,
--> 767                          run_metadata_ptr)
    768       if run_metadata:
    769         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    963     if final_fetches or final_targets:
    964       results = self._do_run(handle, final_targets, final_fetches,
--> 965                              feed_dict_string, options, run_metadata)
    966     else:
    967       results = []

C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1013     if handle is None:
   1014       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-> 1015                            target_list, options, run_metadata)
   1016     else:
   1017       return self._do_call(_prun_fn, self._session, handle, feed_dict,

C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1033         except KeyError:
   1034           pass
-> 1035       raise type(e)(node_def, op, message)
   1036 
   1037   def _extend_graph(self):

InvalidArgumentError: Name: <unknown>, Key: features, Index: 0.  Data types don't match. Data type: int64Expected type: string
     [[Node: ParseSingleExample/ParseExample/ParseExample = ParseExample[Ndense=2, Nsparse=0, Tdense=[DT_STRING, DT_INT64], dense_shapes=[[], []], sparse_types=[], _device="/job:localhost/replica:0/task:0/cpu:0"](ParseSingleExample/ExpandDims, ParseSingleExample/ParseExample/ParseExample/names, ParseSingleExample/ParseExample/ParseExample/dense_keys_0, ParseSingleExample/ParseExample/ParseExample/dense_keys_1, ParseSingleExample/ParseExample/Const, ParseSingleExample/ParseExample/Const_1)]]
     [[Node: ParseSingleExample/ParseExample/ParseExample/_3 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/gpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_13_ParseSingleExample/ParseExample/ParseExample", tensor_type=DT_INT64, _device="/job:localhost/replica:0/task:0/gpu:0"]()]]

Caused by op 'ParseSingleExample/ParseExample/ParseExample', defined at:
  File "C:\Program Files\Anaconda3\envs\tf\lib\runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Program Files\Anaconda3\envs\tf\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\ipykernel\__main__.py", line 3, in <module>
    app.launch_new_instance()
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
    app.start()
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\ipykernel\kernelapp.py", line 474, in start
    ioloop.IOLoop.instance().start()
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tornado\ioloop.py", line 887, in start
    handler_func(fd_obj, events)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 276, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 228, in dispatch_shell
    handler(stream, idents, msg)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 390, in execute_request
    user_expressions, allow_stdin)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\ipykernel\zmqshell.py", line 501, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\IPython\core\interactiveshell.py", line 2717, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\IPython\core\interactiveshell.py", line 2827, in run_ast_nodes
    if self.run_code(code, result):
  File "C:\Program Files\Anaconda3\envs\tf\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-6-0bf78295d664>", line 1, in <module>
    get_all_records('test.tfrecords')
  File "<ipython-input-5-afbcd262993d>", line 4, in get_all_records
    image, label = read_and_decode(filename_queue)
  File "<ipython-input-4-a06d5222e475>", line 9, in read_and_decode
    'label': tf.FixedLenFeature([], tf.int64)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\ops\parsing_ops.py", line 595, in parse_single_example
    dense_types, dense_defaults, dense_shapes, name)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\ops\parsing_ops.py", line 669, in _parse_single_example_raw
    name=name)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\ops\parsing_ops.py", line 544, in _parse_example_raw
    name=name)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\ops\gen_parsing_ops.py", line 167, in _parse_example
    dense_shapes=dense_shapes, name=name)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 763, in apply_op
    op_def=op_def)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\framework\ops.py", line 2395, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "C:\Program Files\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\framework\ops.py", line 1264, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Name: <unknown>, Key: features, Index: 0.  Data types don't match. Data type: int64Expected type: string
     [[Node: ParseSingleExample/ParseExample/ParseExample = ParseExample[Ndense=2, Nsparse=0, Tdense=[DT_STRING, DT_INT64], dense_shapes=[[], []], sparse_types=[], _device="/job:localhost/replica:0/task:0/cpu:0"](ParseSingleExample/ExpandDims, ParseSingleExample/ParseExample/ParseExample/names, ParseSingleExample/ParseExample/ParseExample/dense_keys_0, ParseSingleExample/ParseExample/ParseExample/dense_keys_1, ParseSingleExample/ParseExample/Const, ParseSingleExample/ParseExample/Const_1)]]
     [[Node: ParseSingleExample/ParseExample/ParseExample/_3 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/gpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_13_ParseSingleExample/ParseExample/ParseExample", tensor_type=DT_INT64, _device="/job:localhost/replica:0/task:0/gpu:0"]()]]

这有什么不对?

1 个答案:

答案 0 :(得分:1)

发生错误是因为在您编写数据的程序中,您创建了"features"类型为int64的功能:

example.features.feature['features'].int64_list.value.extend(features)

...而在您尝试阅读数据的程序中,您指定"features"功能的类型为tf.string

features = tf.parse_single_example(
    ...
    features={
        'features': tf.FixedLenFeature([], tf.string),
        ...
    })

最有可能解决的解决方案是将读取程序中的调用更改为tf.parse_single_example()以匹配数据的真实类型:

features = tf.parse_single_example(
    serialized_example,
    features={
        'features': tf.FixedLenFeature([], tf.int64),
        'label': tf.FixedLenFeature([], tf.int64)
    })