i have tried csv data tfrecords format , read tfrecords file. can't read tfrecord file.
first had converted csv file tfrecords file.
my csv data this
1,2,3,4,5,1
2,3,4,5,6,2
3,4,5,6,7,3
4,5,6,7,8,4
column 1~5 features , column 6 label
i converted csv file using code
import pandas pd import tensorflow tf import argparse import os import sys flags = none name = 'test' filename = name + '.tfrecords' print('writing', filename) writer = tf.python_io.tfrecordwriter(filename) 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()
and got tfrecords file. (test.tfrecords)
my problem can't read tfrecords file.
i had tried code , got error message.
def read_and_decode(filename_queue): reader = tf.tfrecordreader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example( serialized_example, # defaults not specified since both keys 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): tf.session() 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) in range(100): example, l = sess.run([image, label]) print (example,l) coord.request_stop() coord.join(threads) get_all_records('test.tfrecords')
this error message
info:tensorflow:error reported coordinator: <class 'tensorflow.python.framework.errors_impl.cancellederror'>, run call 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 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 above exception, 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 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 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 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 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"]()]]
what's wrong this?
the error occurs because in program wrote data, created "features"
feature type int64
:
example.features.feature['features'].int64_list.value.extend(features)
...whereas in program try read data, specify "features"
feature has type tf.string
:
features = tf.parse_single_example( ... features={ 'features': tf.fixedlenfeature([], tf.string), ... })
the solution work change call tf.parse_single_example()
in reading program match true type of data:
features = tf.parse_single_example( serialized_example, features={ 'features': tf.fixedlenfeature([], tf.int64), 'label': tf.fixedlenfeature([], tf.int64) })
Comments
Post a Comment