You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow图像增强未生效求助:脑部MRI数据集无增强效果

脑部MRI图像增强无效果的排查与解决

我尝试用TensorFlow教程中的代码为脑部MRI扫描数据集实现图像增强,但图像完全没有任何增强效果,多次运行结果一致。代码如下:

data_augmentation = keras.Sequential(
  [
    layers.RandomFlip("horizontal",
                      input_shape=(img_height,
                                  img_width,
                                  3)),
    layers.RandomRotation(0.1),
    layers.RandomZoom(0.1),
  ]
)


plt.figure(figsize=(10, 10))
for images, _ in train_ds.take(1):
  for i in range(9):
    augmented_images = data_augmentation(images)
    ax = plt.subplot(3, 3, i + 1)
    plt.imshow(augmented_images[0].numpy().astype("uint8"))
    plt.axis("off")

问题原因

TensorFlow的随机增强层(如RandomFlip、RandomRotation)默认处于推理模式,此时不会触发随机变换逻辑,只会输出原图。当前代码在循环中直接调用增强层,没有启用训练模式,所以每次得到的都是未增强的图像。

解决办法

修改增强层调用代码,强制启用训练模式:

plt.figure(figsize=(10, 10))
for images, _ in train_ds.take(1):
  for i in range(9):
    # 添加training=True参数,触发随机增强
    augmented_images = data_augmentation(images, training=True)
    ax = plt.subplot(3, 3, i + 1)
    plt.imshow(augmented_images[0].numpy().astype("uint8"))
    plt.axis("off")

也可以将增强层整合到模型结构中,这样在模型训练时会自动启用训练模式,数据增强会在训练过程中自动应用。

内容的提问来源于stack exchange,提问作者Lookdatway

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 03:40:29