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

TensorFlow中flow_from_dataframe提示无效图片路径问题求助

问题:TensorFlow图片生成器无法识别文件路径,大量文件被忽略

场景与执行代码

在TensorFlow环境下执行图像分类算法,已创建包含文件路径(files列)与对应类别(class列)的DataFrame,运行以下代码生成训练和验证生成器:

train_generator = image_datagen.flow_from_dataframe(
    df_train,
    x_col="files",
    y_col="class",
    target_size=(256, 256),
    batch_size=4,
    color_mode="rgb",
    class_mode="sparse")

valid_generator = image_datagen.flow_from_dataframe(
   df_valid,
   x_col="files",
   y_col="class",
   target_size=(256, 256),
   batch_size=4,
   color_mode="rgb",
  class_mode="sparse")

触发的警告信息

运行后出现大量文件被忽略的警告:

C:\Users\PC\anaconda3\lib\site-packages\keras\preprocessing\image.py:1137: UserWarning: Found 19856 invalid image filename(s) in x_col="files". These filename(s) will be ignored.
  warnings.warn(
C:\Users\PC\anaconda3\lib\site-packages\keras\preprocessing\image.py:1137: UserWarning: Found 2206 invalid image filename(s) in x_col="files". These filename(s) will be ignored.
  warnings.warn(

已执行的处理步骤

原本DataFrame中的files列已是object类型,但因设置class_mode="sparse"需转为字符串类型避免报错,执行了以下转换代码:

df_train["files"] = df_train["files"].astype(str)
df_train["class"] = df_train["class"].astype(str)

df_valid["files"] = df_valid["files"].astype("str")
df_valid["class"] = df_valid["class"].astype(str)

转换后路径变为类似'F:\\DATA_SCIENCE\\DATA_MAMOGRAPHY\\TRAIN_IMAGES\\16432\\484722668.dcm'的形式,双反斜杠格式看起来异常。

疑问

  • 这种双反斜杠的路径格式是否是导致文件无法被生成器识别的原因?
  • 为何生成器找不到这些图片文件?
  • 是否需要改用os.walk或glob方式重新提取文件路径?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 20:55:25