Shi*_*d77 2 python keras tensorflow tensorflow-datasets
我正在尝试使用 Python 在 TensorFlow 中构建 CNN。我已将图像加载到数据集中,如下所示:
dataset = tf.keras.preprocessing.image_dataset_from_directory(
"train_data", shuffle=True, image_size=(578, 260),
batch_size=BATCH_SIZE)
Run Code Online (Sandbox Code Playgroud)
但是,如果我想在此数据集上使用 train_test_split 或 fit_resample,我需要将其分成数据和标签。我是 TensorFlow 新手,不知道该怎么做。非常感谢任何帮助。
您可以使用该subset参数将数据分隔为training和validation。
import tensorflow as tf
import pathlib
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
image_size=(256, 256),
seed=1,
batch_size=32)
val_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=1,
image_size=(256, 256),
batch_size=32)
for x, y in train_ds.take(1):
print('Image --> ', x.shape, 'Label --> ', y.shape)
Run Code Online (Sandbox Code Playgroud)
Found 3670 files belonging to 5 classes.
Using 2936 files for training.
Found 3670 files belonging to 5 classes.
Using 734 files for validation.
Image --> (32, 256, 256, 3) Label --> (32,)
Run Code Online (Sandbox Code Playgroud)
至于你的标签,根据文档:
“推断”(标签从目录结构生成)、无(无标签)或与目录中找到的图像文件数量相同大小的整数标签列表/元组。标签应根据图像文件路径的字母数字顺序排序(通过 Python 中的 os.walk(directory) 获得)。
因此,只需尝试迭代train_ds并查看它们是否存在。您还可以使用参数label_mode来引用您拥有的标签类型并class_names显式列出您的类。
如果你的类不平衡,你可以使用class_weights的参数model.fit(*)。欲了解更多信息,请查看这篇文章。
| 归档时间: |
|
| 查看次数: |
3275 次 |
| 最近记录: |