# How to preprocess data for image classification from a .txt file?

Basically my issue is that im building an image classification model using AlexNet. I have this pre-split dataset thats already split into training, test, validation. However the issue is that these splits are in .txt files (e.g. trainingsplit.txt) and inside the .txt file is a list of image_001.png files etc. How do I extract this data and pass it through my preprocessor? This is what I would normally do for preprocessing:

train_dir = '/content/gdrive/My Drive/alexNet/Training'
validation_dir = '/content/gdrive/My Drive/alexNet/Validation'

train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
train_dir,  # This is the source directory for training images
target_size=(224, 224),
batch_size=20,
class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
validation_dir,
target_size=(224, 224),
batch_size=20,
class_mode='binary')


Edit: Content of training.txt

image_001.jpg
image_002.jpg
image_014.jpg
image_017.jpg
etc.


The training data is a .txt file containing several rows of text, which are the names of different images. This txt file is inside the main directory Dataset, which contains class directories like Apples, Bananas, Oranges, etc.

• Can please add content of 2 .txt files inside you question for more clarity. May 31 '20 at 4:17
• I have edited my post with the extra information. May 31 '20 at 6:14
• So you have a parent folder containing the subfolders containing the images. The subfolders are the categories. In the trainingsplit.txt, you have images from different subfolders, right?
– user119783
Jul 1 at 12:33
• @aguythatneedshelp So you need to collect all images from subfolders and put them together in one folder for training based on the trainingsplit.txt, that all you need to achieve?
– user119783
Jul 1 at 12:43

I am assuming from the description inside main directory there are more directories of Apples, Bananas, Oranges, etc. and inside them you have .txt files containing information about the images.

import shutil

with open(file_path, 'r') as f: