How to use keras flow method? I am building a Convolutional Neural Network with Keras. I want to use my own dataset. This dataset consists of matrices of specified size and filled with 0's which contain a submatrix of specified size and filled with 1's. The submatrix is optional and the goal is to train the NN to predict whether a matrix contains the submatrix or whether it doesnt. To make it more difficult to detect, I am adding various types of noise to the matrices.

Here is a picture of what the matrices look like, the black parts are 0's and the white part are 1's. There is a 1:1 correspondance between the pixels of the image and the entries in the matrix.


Now I want to create a CNN which can classify these images. The easiest approach so far was to use

test_set = test_datagen.flow_from_directory('dataset/training_set',
                                            target_size = (imageSize, imageSize),
                                            batch_size = batchSize,
                                            class_mode = 'binary')


And then I fill the directory training_set with 1000's of images. This works well, but has one major problem:

It is really inefficient, as I'm first creating images, saving them on a hard-dsik, then loading them again. These are a lot of unecessary steps, considering that keras transforms the images back into matrices (or tensors, idk), and my images were initially matrices.

Thus I was thinking about improving the efficiency by having the matrices in a .csv or even having my matrixgenerator.py "feed" a list or array directly into keras. I did some research and found the method flow from Keras which specifies as parameter an input matrix.

Unfortunately this is where I am stuck. I do not understand how to get my matrices into the form keras wants. I also don't know the exact shape the matrices and labels need to have to be able to use the flow method. 

It would really help if someone could explain me how to use the flow method/tell me where I find more information on it, or give me a better option to feed my dataset into a keras CNN. Thank you for your help. I'm also ok with using a lower-level tensorflow implementation as long as I understand it.
Assuming you already have resized and other preprocessing your image data into a multi-dimensional numpy array and split the data into training and test. 

To use the flow() method. You first want to create a generator using ImageDataGenerator(). The example below DOES NOT DO image augmentation. It will feed only the images in your training set. I added the rescale parameter in case one had not normalized the pixel data.

You then pass your training data to the fit() method of the generator (datagen.fit()).

datagen = ImageDataGenerator(rescale=1./255) 
datagen.fit(x_train)


Now you can train your model using the fit_generator() method for your model.

model.fit_generator(datagen.flow(x_train, y_train, batch_size=batchSize),
                    steps_per_epoch=len(x_train) / batchSize, epochs=epochs)


You can do it the manual way:

for epoch in range(epochs):
    for x_batch, y_batch in datagen.flow(x_train, y_train, batch_size=batchSize):
        model.fit(x_batch, y_batch)


Here's the link to the corresponding Keras documentation: https://keras.io/preprocessing/image/