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.
JavaScript
x
9
1
global_feeded_images = []
2
global_feeded_labels = []
3
4
5
def feed_image(self, img):
6
global global_feeded_images
7
global_feeded_images.append(bytes(img))
8
global_feeded_labels.append(0)
9
After collecting all images, I want to start model training.
JavaScript
1
2
1
model.fit(image_train_ds, np.array(global_feeded_labels), epochs=10)
2
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?
Advertisement
Answer
Correct solution was simple.
JavaScript
1
11
11
1
fp = io.BytesIO(bytes(img)) # our bytes array
2
with fp:
3
im = Image.open(fp)
4
im = im.convert('L') # do grayscale or set corrent shape for flatten layer
5
numpydata = np.asarray(im) # tf could consume numpy
6
7
global_feeded_images.append(numpydata)
8
global_feeded_labels.append(0)
9
10
train_dataset = tf.data.Dataset.from_tensor_slices((global_feeded_images, global_feeded_labels))
11