【发布时间】: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
我研究并发现使用 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()
【问题讨论】:
标签: python pandas tensorflow machine-learning tf.keras