I’m loading images via
JavaScript
x
11
11
1
data = keras.preprocessing.image_dataset_from_directory(
2
'./data',
3
labels='inferred',
4
label_mode='binary',
5
validation_split=0.2,
6
subset="training",
7
image_size=(img_height, img_width),
8
batch_size=sz_batch,
9
crop_to_aspect_ratio=True
10
)
11
I want to use the obtained data in non-tensorflow routines too. Therefore, I want to extract the data e.g. to numpy arrays. How can I achieve this? I can’t use tfds
Advertisement
Answer
I would suggest unbatching your dataset and using tf.data.Dataset.map
:
JavaScript
1
21
21
1
import numpy as np
2
import tensorflow as tf
3
4
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
5
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
6
data_dir = pathlib.Path(data_dir)
7
batch_size = 32
8
9
train_ds = tf.keras.utils.image_dataset_from_directory(
10
data_dir,
11
validation_split=0.2,
12
subset="training",
13
seed=123,
14
image_size=(180, 180),
15
batch_size=batch_size,
16
shuffle=False)
17
18
train_ds = train_ds.unbatch()
19
images = np.asarray(list(train_ds.map(lambda x, y: x)))
20
labels = np.asarray(list(train_ds.map(lambda x, y: y)))
21
Or as suggested in the comments, you could also try just working with the batches and concatenating them afterwards:
JavaScript
1
3
1
images = np.concatenate(list(train_ds.map(lambda x, y: x)))
2
labels = np.concatenate(list(train_ds.map(lambda x, y: y)))
3
Or set shuffle=True
and use tf.TensorArray
:
JavaScript
1
10
10
1
images = tf.TensorArray(dtype=tf.float32, size=0, dynamic_size=True)
2
labels = tf.TensorArray(dtype=tf.int32, size=0, dynamic_size=True)
3
4
for x, y in train_ds.unbatch():
5
images = images.write(images.size(), x)
6
labels = labels.write(labels.size(), y)
7
8
images = tf.stack(images.stack(), axis=0)
9
labels = tf.stack(labels.stack(), axis=0)
10