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

PlaidML Keras脚本调用flow_from_directory时触发OSError:路径语法错误

解决Windows下Keras flow_from_directory路径语法错误

问题场景

在Windows 10环境运行基于PlaidML后端的Keras脚本时,调用ImageDataGenerator.flow_from_directory加载数据集触发OSError,报错提示路径'D:\\projects\record\\input_frames\train'的文件名、目录名或卷标语法不正确,但用户认为路径语法无误。

相关代码片段

from __future__ import print_function
import plaidml.keras
plaidml.keras.install_backend()
import os
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
os.environ['KMP_DUPLICATE_LIB_OK']='True'
import numpy as np
from keras.preprocessing.image import img_to_array
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Flatten, Activation, Input
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
import pandas as pd
import time
import glob
import cv2
import keras
from keras_tqdm import TQDMNotebookCallback
# keras, model definition...
cb = TQDMNotebookCallback()
setattr(cb,'on_train_batch_begin',lambda x,y:None)
setattr(cb,'on_train_batch_end',lambda x,y:None)

src_path_train = "D:\projects\record\input_frames\train"
src_path_test = "D:\projects\record\input_frames\train\test"

train_gen = ImageDataGenerator()
test_gen = ImageDataGenerator()

train_generator=train_gen.flow_from_directory(
    directory=src_path_train,
    classes=None,
    class_mode="categorical",
    color_mode="rgb",
    batch_size=32,
    target_size=(1920, 1080),
    shuffle=True,
    seed=None,
    subset='training',
    interpolation="bilinear",
    follow_links=True,
)

test_generator = test_gen.flow_from_directory(
    directory=src_path_test,
    target_size=(1920, 1080),
    color_mode="rgb",
    batch_size=32,
    class_mode=None,
    shuffle=False,
    seed=42
)

报错信息

Traceback (most recent call last):
  File "C:\Users\p\Documents\model_train.py", line 43, in <module>
    follow_links=True,
  File "C:\Users\p\python\lib\site-packages\keras_preprocessing\image\image_data_generator.py", line 543, in flow_from_directory
    dtype=self.dtype
  File "C:\Users\p\python\lib\site-packages\keras_preprocessing\image\directory_iterator.py", line 115, in __init__
    for subdir in sorted(os.listdir(directory)):
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'D:\\projects\record\\input_frames\train'

错误原因

Python字符串中的反斜杠\是转义字符,比如\r会被解析为回车符,\t会被解析为制表符。原路径中的\record和\train会被转义,导致实际传递给系统的路径被篡改,系统无法识别正确的目录位置。从报错信息里的路径'D:\\projects\record\\input_frames\train'也能看出:\r和\t没有被转成普通的反斜杠,而是保留了转义字符的形式。

解决方案

修复路径字符串的转义问题,任选以下一种方法即可:

方法1:使用原始字符串

在路径字符串前添加r,让Python将字符串中的反斜杠视为普通字符:

src_path_train = r"D:\projects\record\input_frames\train"
src_path_test = r"D:\projects\record\input_frames\train\test"

方法2:使用双反斜杠

将每个反斜杠替换为双反斜杠,避免转义:

src_path_train = "D:\\projects\\record\\input_frames\\train"
src_path_test = "D:\\projects\\record\\input_frames\\train\\test"

方法3:使用正斜杠

Windows系统支持正斜杠作为路径分隔符,直接替换反斜杠:

src_path_train = "D:/projects/record/input_frames/train"
src_path_test = "D:/projects/record/input_frames/train/test"

验证

修改路径后,重新运行脚本,flow_from_directory就能正确识别指定目录并加载数据集。

内容的提问来源于stack exchange,提问作者Gaming with Sam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 17:08:17