ImageDataGenerator无法检测图像问题咨询:路径正确却无识别
图像数据增强时ImageDataGenerator检测不到图像的问题
我尝试对dirty、vehicle、clean三类图像做数据增强,计划将增强后的图像保存至同结构的目标文件夹,且已搭建好文件夹结构,但运行代码时无法检测到图像。
原代码
import os from tensorflow.keras.preprocessing.image import ImageDataGenerator # Set the paths to your dataset folders data_dir = "\/home\/intern\/Desktop\/automated-py-scripts\/real_data" new_augment_dir = "\/home\/intern\/Desktop\/automated-py-scripts\/augmented" train_dir = os.path.join(data_dir, "train") # Define the augmentation parameters augmentation_params = { "rotation_range": 15, "width_shift_range": 0.1, "height_shift_range": 0.1, "shear_range": 0.1, # "zoom_range": 0.1, "horizontal_flip": True, "fill_mode": "nearest" } # Create an ImageDataGenerator instance with augmentation parameters datagen = ImageDataGenerator(**augmentation_params) # Get the list of class folders class_folders = ['clean', 'dirty', 'vehicle'] # Augment images in each class folder for folder in class_folders: class_dir = os.path.join(train_dir, folder) print(class_dir) save_dir = os.path.join(new_augment_dir, folder) # Create the save directory if it doesn't exist os.makedirs(save_dir, exist_ok=True) # Create a generator for the images in the class folder generator = datagen.flow_from_directory( class_dir, target_size=(224, 224), # Resize the images to your desired size batch_size=1, class_mode=None, save_to_dir=save_dir, save_format="jpg", shuffle=True ) # Generate augmented images and save them to the save directory num_images = len(os.listdir(class_dir)) num_augmented_images = 3 # Number of augmented images to generate per original image for _ in range(num_images * num_augmented_images): images = next(generator)
运行错误输出
/home/intern/Desktop/automated-py-scripts/real_data/train/clean Found 0 images belonging to 0 classes. /home/intern/Desktop/automated-py-scripts/real_data/train/dirty Found 0 images belonging to 0 classes. /home/intern/Desktop/automated-py-scripts/real_data/train/vehicle Found 0 images belonging to 0 classes.
问题原因
flow_from_directory的核心要求是:输入目录下必须嵌套一层类别子目录,哪怕是单类别场景。你的当前目录结构是train/clean/直接存放图片,没有额外的子目录,导致函数无法识别类别和图像。
解决方案
方案一:调整文件夹结构(推荐,符合Keras标准格式)
给每个类别文件夹新增一层同名子目录,将原图片放入该子目录中,最终结构如下:
real_data/train/ clean/ clean/ # 新增子目录 img1.jpg img2.jpg ... dirty/ dirty/ img1.jpg ... vehicle/ vehicle/ img1.jpg ...
调整后,原代码无需修改即可正常识别图像。
方案二:修改代码,绕开嵌套目录要求
如果不想调整文件夹结构,可以直接遍历每个类别下的图片文件,手动读取后用datagen.flow()生成增强图,修改后的代码如下:
import os from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array data_dir = "/home/intern/Desktop/automated-py-scripts/real_data" new_augment_dir = "/home/intern/Desktop/automated-py-scripts/augmented" train_dir = os.path.join(data_dir, "train") augmentation_params = { "rotation_range": 15, "width_shift_range": 0.1, "height_shift_range": 0.1, "shear_range": 0.1, "horizontal_flip": True, "fill_mode": "nearest" } datagen = ImageDataGenerator(**augmentation_params) class_folders = ['clean', 'dirty', 'vehicle'] for folder in class_folders: class_dir = os.path.join(train_dir, folder) print(class_dir) save_dir = os.path.join(new_augment_dir, folder) os.makedirs(save_dir, exist_ok=True) # 筛选出当前目录下的图像文件(支持jpg/jpeg/png) img_files = [f for f in os.listdir(class_dir) if f.lower().endswith(('.jpg', '.jpeg', '.png'))] num_augmented_images = 3 # 每张原图生成的增强图数量 for img_file in img_files: img_path = os.path.join(class_dir, img_file) # 加载并预处理图像 img = load_img(img_path, target_size=(224, 224)) x = img_to_array(img) x = x.reshape((1,) + x.shape) # 转换为(batch_size, height, width, channels)格式 # 生成增强图像并保存 i = 0 for batch in datagen.flow(x, batch_size=1, save_to_dir=save_dir, save_prefix='aug_', save_format='jpg'): i += 1 if i >= num_augmented_images: break # 达到指定数量后停止生成
方案对比
- 方案一:符合Keras官方推荐的数据格式,后续如果用
flow_from_directory做模型训练会更顺畅。 - 方案二:无需修改现有文件夹结构,代码更灵活,适合快速验证场景。
内容的提问来源于stack exchange,提问作者NevMthw
相关产品推荐
相关产品推荐

