我有两个文件夹的数据(训练集有10000张图像,验证集有1000张图像),每个文件夹内有10个子文件夹(分别代表不同的类别)。我将所有这些数据放入了一个数据框架中,以便后续使用。然而,当我使用Tensorflow中的”flow_from_dataframe”函数时,某些文件夹内的图像被认为具有无效的名称,因此被忽略了。
当我尝试在Tensorflow之外访问任何图像时,例如通过简单地打开图像,我仍然无法访问某些文件,尽管路径完全正确。
from PIL import Imageim = Image.open("D:\Ensino Superior\ISCTE-IUL\Mestrado em Engenharia Informatica\Tese\Testagem\TomatoLeafDisease\data\Train\Tomato___Tomato_mosaic_virus\Tomato___Tomato_mosaic_virus_original_f16eeb0f-5219-4a81-9941-351b3d9ba5fc___PSU_CG 2089.JPG_a88e521f-cec2-4755-871f-782de8192056.JPG") im.show()
我已经研究过,发现使用绝对路径可能有帮助并使其工作,但即使如此,仍有一些图像被忽略。我该怎么做才能确保没有图像被忽略?
输出错误前的代码:
def create_gen(): train_generator = tf.keras.preprocessing.image.ImageDataGenerator( preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input, validation_split=0.2 ) test_generator = tf.keras.preprocessing.image.ImageDataGenerator( preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input ) train_images = train_generator.flow_from_dataframe( dataframe=train_df, x_col='Filepath', y_col='Class', target_size=(224, 224), color_mode='rgb', class_mode='categorical', batch_size=32, shuffle=True, seed=0, subset='training', rotation_range=30, # 取消注释以使用数据增强 zoom_range=0.15, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, horizontal_flip=True, fill_mode="nearest" ) val_images = train_generator.flow_from_dataframe( dataframe=train_df, x_col='Filepath', y_col='Class', target_size=(224, 224), color_mode='rgb', class_mode='categorical', batch_size=32, shuffle=True, seed=0, subset='validation', rotation_range=30, # 取消注释以使用数据增强 zoom_range=0.15, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, horizontal_flip=True, fill_mode="nearest" ) test_images = test_generator.flow_from_dataframe( dataframe=validation_df, x_col='Filepath', y_col='Class', target_size=(224, 224), color_mode='rgb', class_mode='categorical', batch_size=32, shuffle=False ) return train_generator,test_generator,train_imagespretrained_model = tf.keras.applications.MobileNetV2(input_shape=(224, 224, 3),include_top=False,weights='imagenet',pooling='avg'train_generator,test_generator,train_images,val_images,test_images = create_gen()
在Tensorflow中使用”flow_from_data_frame”后的输出
回答:
问题解决了。原因是图像的路径和图像名称本身太长了。