How to create tensorflow dataset from runtime generated images?


So, I start a small project based on tensorflow and could not understand how to prepare dataset generated from memory input.
I have a random number of sources, that generate images. Than I pass it to python script. Images created as bytes array with png format.
I collect images to array, and want to prepare dataset from it and train model based on this dataset.

global_feeded_images = []
global_feeded_labels = []

def feed_image(self, img):
    global global_feeded_images

After collecting all images, I want to start model training., np.array(global_feeded_labels), epochs=10)

As I understand, tensorflow could accept numpy array or tensor, but I could not understand, what I should convert, each image separatly or whole array?
Or, more shortly, how to convert array of images global_feeded_images to dataset image_train_ds?


Correct solution was simple.

fp = io.BytesIO(bytes(img)) # our bytes array
        with fp:
            im =
            im = im.convert('L') # do grayscale or set corrent shape for flatten layer
            numpydata = np.asarray(im) # tf could consume numpy


train_dataset =, global_feeded_labels))

Answered By – zealot

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published