Arrange Database for ImageGenerator
2 min readJan 5, 2021
For train in keras deep learning model we have two options 1) write our generator 2) arrange the images as python expects .
The second options seems to me more interesting
for documentation we can go to https://keras.io/api/preprocessing/image/
Im based on story of https://vijayabhaskar96.medium.com/tutorial-image-classification-with-keras-flow-from-directory-and-generators-95f75ebe5720
so lets write small script to arrange images in this format
Imports
import os
import random
import numpy as np
import shutil
Paths
This is where all classes of images exist , lets say we have 3 classes.
pathClassA = r”pathClassA”
pathClassB = r”pathClassB”
pathClassC = r”pathClassC”
allInputPaths = [pathClassA,pathClassB,pathClassC]
classes = ['pathClassA','pathClassB','pathClassC']
Lets define output folder
outfolder = r"Data"
subFolders = ['Train','Test','Valid']
lets define split of data
splitlen = np.array([0.7 ,0.2 ,0.1])
splitlen[0] = splitlen[0]
splitlen[1] = splitlen[0]+splitlen[1]
splitlen[2] = 1
Main copy loop
mainClass = 0
for name in allInputPaths:
print(name)
filelist = os.listdir(name)
print(len(filelist))
random.shuffle(filelist)
n = len(filelist)
k = 0
for file_name in filelist:
full_file_name = os.path.join(name, file_name)
if (k < n*splitlen[0]):
dest = os.path.join(os.path.join(outfolder, subFolders[0]), classes[mainClass])
elif (n*splitlen[0] <= k <= n*splitlen[1]):
dest = os.path.join(os.path.join(outfolder, subFolders[1]), classes[mainClass])
else:
dest = os.path.join(os.path.join(outfolder, subFolders[2]), classes[mainClass])
if os.path.isfile(full_file_name):
shutil.copy(full_file_name, dest)
k = k + 1
mainClass = mainClass + 1