0
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ Can please add content of 2 .txt files inside you question for more clarity. $\endgroup$ Commented May 31, 2020 at 4:17
  • $\begingroup$ I have edited my post with the extra information. $\endgroup$ Commented May 31, 2020 at 6:14
  • $\begingroup$ 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? $\endgroup$
    – user119783
    Commented Jul 1, 2021 at 12:33
  • $\begingroup$ @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? $\endgroup$
    – user119783
    Commented Jul 1, 2021 at 12:43

1 Answer 1

0
$\begingroup$

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:
    img_names = f.readlines()
    img_names = [img.strip() for img in img_names]

for i in range(len(img_names)): shutil.move(img_names[i], folder_path)

Once images are moved into there respective folder you can use ImageDataGenerator approach to train model.

$\endgroup$
1
  • $\begingroup$ I should've specified more. Inside the directories Apple, Bananas, etc. are images. The training.txt file has just the name of those images (image_01.jpg etc.) while the actual directories have images called image_01.jpg. $\endgroup$ Commented May 31, 2020 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.