I have each datapoint stored in a .npy file, with shape=(1024,7,8)
. I want to load them to a Keras model by a manner similar to ImageDataGenerator
, so I wrote and tried different custom generators but none of them work, here is one I adapted from this
def find(dirpath, prefix=None, suffix=None, recursive=True):
"""Function to find recursively all files with specific prefix and suffix in a directory
Return a list of paths
"""
l = []
if not prefix:
prefix = ''
if not suffix:
suffix = ''
for (folders, subfolders, files) in os.walk(dirpath):
for filename in [f for f in files if f.startswith(prefix) and f.endswith(suffix)]:
l.append(os.path.join(folders, filename))
if not recursive:
break
l
return l
def generate_data(directory, batch_size):
i = 0
file_list = find(directory)
while True:
array_batch = []
for b in range(batch_size):
if i == len(file_list):
i = 0
random.shuffle(file_list)
sample = file_list[i]
i += 1
array = np.load(sample)
array_batch.append(array)
yield array_batch
I found this lacks of the label, so it won't be fit into the model using fit_generator
. How can I add the label into this generator, given that I can store them in a numpy array?