获取 InvalidArgumentError:NewRandomAccessFile 在自定义图像数据集上使用 tf.data 时无法创建/打开

Posted

技术标签:

【中文标题】获取 InvalidArgumentError:NewRandomAccessFile 在自定义图像数据集上使用 tf.data 时无法创建/打开【英文标题】:Getting InvalidArgumentError: NewRandomAccessFile failed to Create/Open while working with tf.data on the custom image dataset 【发布时间】:2019-04-04 03:46:56 【问题描述】:

代码:

dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
dataset = dataset.map(_parseJpeg, num_parallel_calls = nthread)

dataset = dataset.batch(batch_size)     
dataset = dataset.prefetch(batch_size)

iterator = dataset.make_one_shot_iterator()
images, labels = iterator.get_next()

 with tf.Session() as sess:
    sess.run([images, labels])

Traceback(最近一次调用最后一次): _do_call 中的文件“D:\Python\Anaconda\envs\tf\lib\site-packages\tensorflow\python\client\session.py”,第 1278 行 返回 fn(*args) _run_fn 中的文件“D:\Python\Anaconda\envs\tf\lib\site-packages\tensorflow\python\client\session.py”,第 1263 行 选项,feed_dict,fetch_list,target_list,run_metadata) _call_tf_sessionrun 中的文件“D:\Python\Anaconda\envs\tf\lib\site-packages\tensorflow\python\client\session.py”,第 1350 行 运行元数据) tensorflow.python.framework.errors_impl.InvalidArgumentError: NewRandomAccessFile failed to Create/Open: Tensor("arg0:0", shape=(), dtype=string) : 文件名、目录名或卷标语法不正确。 ;未知错误 [[节点:ReadFile = ReadFile]] [[节点:IteratorGetNext = IteratorGetNextoutput_shapes=[[?,224,224,3], [?,1000]], output_types=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device: CPU:0"]] [[节点:IteratorGetNext/_1 = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0 /device:CPU:0", send_device_incarnation=1, tensor_name="edge_37_IteratorGetNext", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"]]

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次): 文件“vgg16_train_batch_processing.py”,第 231 行,在 sess.run(优化器) 文件“D:\Python\Anaconda\envs\tf\lib\site-packages\tensorflow\python\client\session.py”,第 877 行,运行中 run_metadata_ptr) 文件“D:\Python\Anaconda\envs\tf\lib\site-packages\tensorflow\python\client\session.py”,第 1100 行,在 _run feed_dict_tensor、选项、run_metadata) 文件“D:\Python\Anaconda\envs\tf\lib\site-packages\tensorflow\python\client\session.py”,第 1272 行,在 _do_run 运行元数据) _do_call 中的文件“D:\Python\Anaconda\envs\tf\lib\site-packages\tensorflow\python\client\session.py”,第 1291 行 raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.InvalidArgumentError: NewRandomAccessFile failed to Create/Open: Tensor("arg0:0", shape=(), dtype=string) : 文件名、目录名或卷标语法不正确。 ;未知错误 [[节点:ReadFile = ReadFile]] [[节点:IteratorGetNext = IteratorGetNextoutput_shapes=[[?,224,224,3], [?,1000]], output_types=[DT_FLOAT, DT_FLOAT], _device="/job:localhost/replica:0/task:0/device: CPU:0"]] [[节点:IteratorGetNext/_1 = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0 /device:CPU:0", send_device_incarnation=1, tensor_name="edge_37_IteratorGetNext", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"]]

【问题讨论】:

【参考方案1】:

没关系,我自己解决了这个问题。

它在数据集映射的解析器函数中。

我错误地输入了tf.read_file(str(filename)),而不是tf.read_file(filename), 这是导致错误的原因,因为filename 这里是一个张量。

【讨论】:

以上是关于获取 InvalidArgumentError:NewRandomAccessFile 在自定义图像数据集上使用 tf.data 时无法创建/打开的主要内容,如果未能解决你的问题,请参考以下文章

InvalidArgumentError:找到 2 个根错误。 Tensorflow 文本分类模型中的不兼容形状

InvalidArgumentError:没有注册 OpKernel 来支持 Op 'CudnnRNN'

InvalidArgumentError:拟合模型时出错

InvalidArgumentError:输入必须是向量,得到形状:[]

Tensorflow 中带有 model.fit 的 InvalidArgumentError

InvalidArgumentError: input_1:0 已输入和提取