【问题标题】:What is the best way to create a custom federated image dataset for TFF in SQLite format?为 SQLite 格式的 TFF 创建自定义联合图像数据集的最佳方法是什么?
【发布时间】:2021-11-21 20:57:14
【问题描述】:

我查看了 CIFAR-100 内置数据集的源代码,并决定为 FairFace 数据集创建一个兼容版本,以便在我将 FairFace 转换为结构后,无需在任何地方进行大量修改即可利用其他内置函数与 CIFAR-100 非常相似。

我进行了搜索,但无法找到 CIFAR-100 SQLite 数据库是如何创建的 - 特别是图像如何转换为 BLOB 进行存储。经过一番反复试验,我尝试这样做:

sample = getDatabyIndex(train_labels, index)
example = tf.train.Example(features=tf.train.Features(feature={
  'image' : bytes_feature(sample[0].tobytes()),
  'label' : int64_feature(sample[1])
}))
example = example.SerializeToString()
cur.execute("insert into examples('split_name','client_id','serialized_example_proto') values(?,?,?)", ('train', i, sqlite3.Binary(example)))

对训练数据中的每个样本执行此操作,对测试数据执行类似操作。我可以使用这种解码方法来加载它:

def parse_proto(tensor_proto):
  parse_spec = {
    'image': tf.io.FixedLenFeature(shape=(), dtype=tf.string),
    'label': tf.io.FixedLenFeature(shape=(), dtype=tf.int64),
  }
  decoded_example = tf.io.parse_example(tensor_proto, parse_spec)
  return collections.OrderedDict(
            image=tf.reshape(tf.io.decode_raw(decoded_example['image'], tf.uint8), (224,224,3)),
            label=decoded_example['label'])

然而,我注意到,最终的 sqlite.lzma 压缩存档大小为 6.4 GB,而数据集的源存档大小为 555 MB。我猜测由于我存储图像的方式,如果它们以更兼容的方式存储,压缩效果不会很好。我从 CIFAR-100 代码中看到,图像直接加载为形状为 (32,32,3) 的 FixedLenFeatures,这意味着它们是按原样存储的,但我一直无法找到这样存储我的图像的方法。唯一对我有用的方法是 bytes_feature 路由。

最好/推荐的方法是什么?

【问题讨论】:

  • 我是否正确地回答了这个问题:SQLite 数据库压缩前 的大小为 555 MiB,压缩后为 6.5 GiB(压缩后的文件 much i> 更大)?能否将问题扩展为包括如何将 LZMA 压缩应用于 SQLite 数据库文件?

标签: tensorflow image-classification tensorflow-federated federated-learning


【解决方案1】:

如果没有更多关于 LZMA 压缩的信息被应用,很难回答关于尺寸增加的问题。

要直接使用与来自tff.simulation.datasets.cifar100.load_data 的CIFAR-100 数据集相同的tf.io.FixedLenFeaturetf.train.Example 需要使用int64_feature() 构造'image' 键而不是字节。这可能需要将sample[0] 转换为不同的数据类型(假设它是np.ndarray)。

解码过程中:

  1. 首先解析为具有 int64 的 (N, M, 3) 张量。来自tensorflow_federated/python/simulation/datasets/cifar100.py#L31

    'image': tf.io.FixedLenFeature(shape=(32, 32, 3), dtype=tf.int64),
    
  2. 转换为tf.unit8。来自tensorflow_federated/python/simulation/datasets/cifar100.py#L37

    image=tf.cast(parsed_features['image'], tf.uint8),
    

注意:由于协议缓冲区 (https://developers.google.com/protocol-buffers/docs/encoding#varints) 中使用了 varint 编码,预计使用 int64 不会为序列化表示增加显着开销(至少小于 4 倍)。

【讨论】:

    猜你喜欢
    • 2011-12-15
    • 2021-04-20
    • 2012-05-11
    • 2014-01-29
    • 2016-01-30
    • 1970-01-01
    • 2023-01-24
    • 2013-01-30
    • 1970-01-01
    相关资源
    最近更新 更多