Reputation: 29
I am working on image classification (12 classes of image data) by applying CNN Model. As a result, İ have been facing overfitting therefore İ applied Data Augmentation. Although it works pretty well, however, İ have faced an error i.e.
Found 0 validated image filenames belonging to 0 classes.
Found 0 validated image filenames belonging to 0 classes.
Found 0 validated image filenames belonging to 0 classes.
/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/dataframe_iterator.py:282: UserWarning: Found 4985 invalid image filename(s) in x_col="Filepath". These filename(s) will be ignored.
.format(n_invalid, x_col)
/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/dataframe_iterator.py:282: UserWarning: Found 4985 invalid image filename(s) in x_col="Filepath". These filename(s) will be ignored.
.format(n_invalid, x_col)
/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/dataframe_iterator.py:282: UserWarning: Found 554 invalid image filename(s) in x_col="Filepath". These filename(s) will be ignored.
.format(n_invalid, x_col)
As a result, it has been quite unable to generate new images.
My code:
import shutil
aug_path = r'/content/dataset' # path to augmentation directory
if os.path.isdir(aug_path):
shutil.rmtree(aug_path) # remove anything in aug directory if it exists
os.mkdir(aug_path) # make the aug directory
for label in image_df['Label'].unique():
subpath=os.path.join(aug_path, label) # path for the sub directory
os.mkdir(subpath)
target= 110
image_shape=(256,256)
gen=ImageDataGenerator(horizontal_flip=True, vertical_flip=True, rotation_range=20, width_shift_range=.2,
height_shift_range=.2, zoom_range=.2)
groups=image_df.groupby('Label') # group by class
for label in image_df['Label'].unique(): # for every class
group=groups.get_group(label) # a dataframe holding only rows with the specified label
sample_count=len(group) # determine how many samples there are in this class
if sample_count< target: # if the class has less than target number of images
aug_img_count=0
delta=target-sample_count # number of augmented images to create
target_dir=os.path.join(aug_path, label) # define where to write the images
aug_gen=gen.flow_from_dataframe( group, x_col='Filepath', y_col=None, target_size=image_shape, class_mode=None, batch_size=1,
shuffle=False, save_to_dir=target_dir, save_prefix='aug-',save_format='jpg')
while aug_img_count<delta:
images=next(aug_gen)
aug_img_count += len(images)
def create_gen():
# Load the Images with a generator and Data Augmentation
train_generator = tf.keras.preprocessing.image.ImageDataGenerator(
preprocessing_function=tf.keras.applications.mobilenet_v2.preprocess_input,
validation_split=0.1
)
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='Label',
target_size=(128, 128),
color_mode='rgb',
class_mode='categorical',
batch_size=64,
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='Label',
target_size=(128, 128),
color_mode='rgb',
class_mode='categorical',
batch_size=64,
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=test_df,
x_col='Filepath',
y_col='Label',
target_size=(128, 128),
color_mode='rgb',
class_mode='categorical',
batch_size=64,
shuffle=False
)
return train_generator,test_generator,train_images,val_images,test_images
from sklearn.model_selection import train_test_split
# Separate in train and test data
train_df, test_df = train_test_split(image_df, train_size=0.9, shuffle=True, random_state=101)
# Create the generators
train_generator,test_generator,train_images,val_images,test_images = create_gen()
Output:
Found 0 validated image filenames belonging to 0 classes.
Found 0 validated image filenames belonging to 0 classes.
Found 0 validated image filenames belonging to 0 classes.
/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/dataframe_iterator.py:282: UserWarning: Found 4985 invalid image filename(s) in x_col="Filepath". These filename(s) will be ignored.
.format(n_invalid, x_col)
/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/dataframe_iterator.py:282: UserWarning: Found 4985 invalid image filename(s) in x_col="Filepath". These filename(s) will be ignored.
.format(n_invalid, x_col)
/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/dataframe_iterator.py:282: UserWarning: Found 554 invalid image filename(s) in x_col="Filepath". These filename(s) will be ignored.
.format(n_invalid, x_col)
Upvotes: 1
Views: 623
Reputation: 8092
You have put parameters in flow_from_dataframe that actually belong in the call to ImageDataGenerator. For example in your code below I have marked what does not belong in flow_from_dataframe. See documentation here.
train_images = train_generator.flow_from_dataframe(
dataframe=train_df,
x_col='Filepath',
y_col='Label',
target_size=(128, 128),
color_mode='rgb',
class_mode='categorical',
batch_size=64,
shuffle=True,
seed=0,
subset='training',
rotation_range=30, remove
zoom_range=0.15, remove
width_shift_range=0.2, remove
height_shift_range=0.2, remove
shear_range=0.15, remove
horizontal_flip=True, remove
fill_mode="nearest" remove
P.S. looks like you used some of my code for augmentation from a Kagle notebook. Look at the notebook to see how to setup your generators
Upvotes: 2