python.data.ops.dataset_ops.BatchDataset - 如何使用它来创建训练和测试数据集

Cil*_*hlu 7 python split tensorflow

使用 TensorFlow 遍历目录并拍摄我想用于训练神经网络的图像。

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    wk_dir,
    labels="inferred",
    label_mode="int",
    class_names=None,
    color_mode="grayscale",
    batch_size=batches,
    image_size=image_dim,
    shuffle=True,
    seed=1968,
    validation_split=0.2,
    subset="training",
    interpolation="bilinear",
    follow_links=False,
)
Run Code Online (Sandbox Code Playgroud)

找到属于 3 个类别的 127561 个文件。使用102049个文件进行训练。

结果 - 它有效......现在我正在尝试使用它输入到模型中,但不知道如何管理它......

print(train_ds)
<BatchDataset shapes: ((None, 576, 432, None), (None,)), types: (tf.float32, tf.int32)>
Run Code Online (Sandbox Code Playgroud)

那么我在数组中有 2 个元素吗?第一个有 4 个元素,其中 2 个为空,第二个元素是其分类?
我尝试拆分 BatchDatashape 并收到错误TypeError: 'BatchDataset' object is not subscriptable

如何操作对象类型 python.data.ops.dataset_ops.BatchDataset 的 TF ?

小智 6

  1. 如果你想看看这个 BatchDataset 是什么样子,你可以尝试:

    print(list(train_ds.as_numpy_iterator()))
    
    Run Code Online (Sandbox Code Playgroud)
  2. 有关 TensorFlow 数据和 BatchDataset 的更多信息:https://www.tensorflow.org/guide/data#batching_dataset_elements

  3. 看起来没有足够的信息来告诉您如何准确地构建模型,但我可以推荐本课程来了解如何使用 BatchDataset 作为模型输入来构建模型: https: //www.coursera.org/projects/fine-tune -bert-张量流