7

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?

1
  • When while loop will stop in while True:?? Commented Nov 8, 2018 at 19:55

1 Answer 1

15
from tensorflow.python.keras.utils import Sequence
import numpy as np   

class Mygenerator(Sequence):
    def __init__(self, x_set, y_set, batch_size):
        self.x, self.y = x_set, y_set
        self.batch_size = batch_size

    def __len__(self):
        return int(np.ceil(len(self.x) / float(self.batch_size)))

    def __getitem__(self, idx):
        batch_x = self.x[idx * self.batch_size:(idx + 1) * self.batch_size]
        batch_y = self.y[idx * self.batch_size:(idx + 1) * self.batch_size]

        # read your data here using the batch lists, batch_x and batch_y
        x = [my_readfunction(filename) for filename in batch_x] 
        y = [my_readfunction(filename) for filename in batch_y]
        return np.array(x), np.array(y)
Sign up to request clarification or add additional context in comments.

8 Comments

you can now call model.fit_generator() using this generator
I apologize if my question is dummy since I'm not good at python, but how would I use this as it's class, not a function? Thank you
train_gen = mygenerator(x_set, y_set, batch_size) then use this train_gen in your model.fit_generator call
Thanks. It's what I was looking for :) Other solutions in the web are complicated and I couldn't use them.
The suggested import is tensorflow.keras and not tensorflow.python.keras which is intended only for development use.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.