Issue
I have a trouble in loading images with DataGenerator. It's not sort like my real path as you can see from images. It should be like img(1), img(2), ... but img(1), img(10), img(100), ...
How can I solve this ? Thank you in advance.
Solution
The reason the order is NOT what you expect is that the generator processes images in alphanumeric order. For example if your images are labelled 1.jpg, 2.jpg, ...9.jpg, 10.jpg, 11.jpg ...etc They will be processed in the order 1.jpg, 10,jpg,11.jpg, etc, 2.jpg,20.jpg etc. One way to preserve the order is to name the files using "zeros'' padding. For example if you have 20 files label them as 01.jpg, 02.jpg, etc 09.jpg, 10.jpg etc. Be aware if you are using flow_from_directory the class directories are also processed in alphanumeric order. Included below is the code for a function that will rename all the files in a directory (source_dir) numerically starting from an integer value (snum) with appropriate 'zeros' padding.
def rename (source_dir, snum, ):
import os
import shutil
flist=os.listdir(source_dir)
temp_dir=os.path.join(source_dir, 'temp')
if os.path.isdir(temp_dir):
shutil.rmtree(temp_dir)
os.mkdir(temp_dir)
for f in flist:
fpath=os.path.join(source_dir,f)
dpath=os.path.join(temp_dir,f)
shutil.copy(fpath, dpath)
tlist=os.listdir(temp_dir)
for f in tlist:
fpath=os.path.join(source_dir,f)
os.remove(fpath)
tlist=os.listdir(temp_dir)
fc=len(tlist) # determine number of d files to process which determines amout of zeros padding needed
pad=0
mod = 10
for i in range(1, fc + 1): # skip i=0 because 0 modulo anything is 0 and we don't want to increment pad
if i % mod == 0:
pad=pad+1
mod =mod * 10
for i,f in enumerate(tlist):
fpath=os.path.join(temp_dir,f) #full path to the file
index=fpath.rfind('.') # find location of last . in file name
new_path=os.path.join(source_dir, str(i + snum).zfill(pad+1) + fpath[index :] )
shutil.copy(fpath, new_path)
shutil.rmtree(temp_dir)
Answered By - Gerry P
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.