python - 如何在 Kaggle 笔记本中编写 TFRecords?
问题描述
我目前正在尝试将 Kaggle TPU 与 cifar10 数据集一起使用。以下代码显示了我如何在 TFRecords 中编码数据,但我现在知道如何将它们存储在文件中。
def _bytes_feature(value):
"""Returns a bytes_list from a string / byte"""
if isinstance(value, type(tf.constant(0))):
value = value.numpy() # BytesList won't unpack a string from an EagerTensor (what??)
return tf.train.Feature(bytes_list = tf.train.BytesList(value=[value]))
def _float_feature(value):
"""Returns a float_list from a float / double"""
return tf.train.Feature(float_list = tf.train.FloatList(value=[value]))
def _int64_feature(value):
""""Returns an int64_list from a bool / enum / int / uint"""
return tf.train.Feature(int64_list = tf.train.Int64List(value=[value]))
def image_example(image, label, dimension):
feature = {
'dimension': _int64_feature(dimension),
'label': _int64_feature(label),
'image_raw': _bytes_feature(image.tobytes()),
}
return tf.train.Example(features=tf.train.Features(feature=feature))
并将数据写入 TFRecords:
record_file = './cifar10.tfrecords'
n_samples = x_train.shape[0]
dimension = x_train.shape[1]
depth = x_train.shape[3]
# print(x_train.shape)
with tf.io.TFRecordWriter(record_file) as writer:
for i in range(n_samples):
image = x_train[i]
label = y_train[i]
tf_example = image_example(image, label, dimension) # function defined above
writer.write(tf_example.SerializeToString()) # serializes the input to store the data
现在我想我只需要运行它来获取我的数据:
data = tf.data.TFRecordDataset(record_file)
如果我尝试解析记录,则会收到以下错误:
UnimplementedError:文件系统方案“[本地]”未实现(文件:“./cifar10.tfrecords”)
但相反,它什么也不做(实际上重新初始化了 Kaggle 会话,就好像我之前没有运行过任何东西一样)。你知道我正在做的错误吗?
非常感谢您提供的任何帮助!!
解决方案
UnimplementedError:文件系统方案“[本地]”未实现(文件:“./cifar10.tfrecords”)。
这是因为 Cloud TPU 无法在 Kaggle/Colab 文件系统上写入(或访问)文件,它需要将文件放在 google 存储桶中。
https://cloud.google.com/tpu/docs/troubleshooting#cannot_use_local_filesystem
推荐阅读
- security - 使用密码最大限制的网站是否以纯文本形式存储密码?
- python - Scrapy蜘蛛完成抓取的时间如何写入json文件?
- node.js - 如何使用 node.js 编辑 json 文件
- python - 如何在curses的文本框中添加滚动?
- grafana - Grafana mysql 多值变量
- javascript - 从 Api 获取数据并在 react js 中使用钩子
- prometheus - 获取最后一个警报值
- r - 图例未出现在 R 图中
- docker - 从 Dockerfile 将 ENV 传递给图像父级很热
- sublimetext3 - 如何更新 Sublime Text 3 以使用 Python 3.10 语法