tensorflow数据加强

[复制链接]
发表于 2026-1-12 08:22:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
T10 使用 TensorFlow 实现数据加强

在深度学习的图像分类任务中,数据加强是一种常用的技能,它通过对现有练习样本举行随机变更(比方翻转、旋转、缩放等),以天生更多的练习数据,资助模子更好地泛化,提拔模子在未知数据上的体现。Pytorch框架数据加强方式较为方便,但对于tensorflow还不熟悉,这周告急学习tensorflow框架下数据加强方法。
1. 情况设置和数据加载

将数据集分为练习集、验证集和测试集。
  1. # 设置 GPU 显存按需使用
  2. gpus = tf.config.list_physical_devices("GPU")
  3. if gpus:
  4.     tf.config.experimental.set_memory_growth(gpus[0], True)
  5.     tf.config.set_visible_devices([gpus[0]], "GPU")
  6. # 数据路径和参数设定
  7. data_dir = "./34-data/"
  8. img_height, img_width = 224, 224
  9. batch_size = 32
  10. # 加载训练和验证数据集
  11. train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  12.     data_dir,
  13.     validation_split=0.3,
  14.     subset="training",
  15.     seed=12,
  16.     image_size=(img_height, img_width),
  17.     batch_size=batch_size)
  18. val_ds = tf.keras.preprocessing.image_dataset_from_directory(
  19.     data_dir,
  20.     validation_split=0.3,
  21.     subset="validation",
  22.     seed=12,
  23.     image_size=(img_height, img_width),
  24.     batch_size=batch_size
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表