【问题标题】:Why some of my images are being ignored in Tensorflow when i have images not being ignored in the same folders?当我在同一个文件夹中没有忽略图像时,为什么我的一些图像在 Tensorflow 中被忽略?
【发布时间】:2021-10-10 16:04:32
【问题描述】:

我在 2 个文件夹中有数据(训练有 10000 张图像,验证有 1000 张图像),在每个文件夹中我有 10 个文件夹(各自的类)。 我将所有这些数据放入数据框中以备后用。 事实证明,当我在 Tensorflow 中使用“flow_from_dataframe”时,某些文件夹中的某些图像被假定具有无效名称,因此被忽略。

我尝试访问 Tensorflow 之外的任何图像,例如通过简单地打开图像,当路径完全正确时我仍然无法访问某些文件

from PIL import Image
im = 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() 

Output when trying to acess an image outside Tensorflow code

The dataframe

我研究并发现使用 abs 路径可以帮助并使其工作,但是即使某些图像被忽略,我该怎么做才能不忽略任何图像?

错误输出前的代码:

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, # Uncomment to use data augmentation
        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, # Uncomment to use data augmentation
        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_images

pretrained_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()

Output after using "flow_from_data_frame" with Tensorflow

【问题讨论】:

    标签: python pandas tensorflow machine-learning tf.keras


    【解决方案1】:

    您的生成器不正确。例如你有这个代码。

     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, # Uncomment to use data augmentation
            zoom_range=0.15,
            width_shift_range=0.2,
            height_shift_range=0.2,
            shear_range=0.15,
            horizontal_flip=True,
            fill_mode="nearest"
    

    参数rotation_range、width_shift_range、height_shift_range、shear_range、horizo​​ntal_flip和fill_mode不属于.flow_from_dataframe。它们应该放在 ImageDataGenerator 中,如下所示

    train_generator = tf.keras.preprocessing.image.ImageDataGenerator(        preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input,
            validation_split=0.2, 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" )
    

    对于图像路径的问题,请尝试运行此程序。我使用 sdir 作为包含您的数据的目录,只需替换正确的目录名称即可。

    import os
    from PIL import Image
    sdir=r'C:\Temp\balls\train' # set this to you directory
    good_image_count=0
    bad_image_count=0
    processed_count=0
    bad_image_list=[]
    classlist=os.listdir(sdir)
    for klass in classlist:
        print ('processing class directory ', klass)
        classpath=os.path.join(sdir, klass)
        if os.path.isdir(classpath):
            flist=os.listdir(classpath)
            print (' number of files in class directory ', klass,' is ', len(flist))
            for f in flist:
                processed_count +=1
                fpath=os.path.join(classpath, f)
                if os.path.isfile(fpath):
                    try:
                        image=Image.open(fpath)
                        image.verify()
                        good_image_count +=1
                    except:
                        bad_image_count +=1
                        bad_image_list.append(fpath)
                else:
                    print('in class directory ', klass, ' you have sub directories, you should only have files in it')
        else:
            print ('In the sdir you have files, you should only have class subdirectories in sdir')
    print(processed_count, ' files were processed') 
    print(good_image_count, ' files were valid image files')
    print(bad_image_count, ' files were invalid images or paths did not exist')
    for f in bad_image_list:
        print (f)
    

    【讨论】:

    • 谢谢 Geery P。但是我已经这样做了以确认和比较,是的,打印的文件数是文件夹中真正的文件数。
    • 查看修改后的答案
    • 我使用你的代码,它说我在其中一个火车文件夹(“Tomato__Tomato_mosaic_virus”)中有子目录,但我只有文件。如果您想查看我正在使用的数据集,数据集就是这个kaggle.com/kaustubhb999/tomatoleaf,并且当我尝试访问所有文件是“Tomato__Tomato_mosaic_virus”。
    • 您一定是错误地复制了数据集。转到 Kagle 上的数据集并选择代码,然后选择 notebook EfficientNet B3 Test Accuracy=99% 或直接转到kaggle.com/gpiosenka/efficientnet-b3-test-accuracy-99。它有完整的代码来测试图像、构建数据帧、构建生成器、构建模型和训练它。
    • 抱歉,该页面不存在
    【解决方案2】:

    整理好了。这是因为图片的路径和图片名称本身太长。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 2011-12-23
      • 1970-01-01
      相关资源
      最近更新 更多