I download my dataset like this: dataset = tfds.load('cifar10', split='train', shuffle_files=True)
. After applying the batch()
method I have that dataset is:
Attributes of object: {'image': <tf.Tensor 'IteratorGetNext:0' shape=(128, 32, 32, 3) dtype=uint8>, 'label': <tf.Tensor 'IteratorGetNext:1' shape=(128,) dtype=int64>}
If I use next(iter(train_dataset))
or for elem in train_dataset.take(1): print (elem.numpy())
I need eager_execution
.
If I try dataset[image]
, I get that the object is not subscriptable.
If I try with:
iterator=dataset.make_one_shot_iterator()
next_val = iterator.get_next()
print(f'my_image: {next_val}')
print(f'my_image: {next_val['image']}')
I get:
print(f'my_image: {next_val['image']}')
^
SyntaxError: invalid syntax
Even if I enable eager execution and do batch = iter(dataset).get_next()
I get
Attributes of object: {'image': <tf.Tensor: id=156, shape=(128, 32, 32, 3), dtype=uint8, numpy=
array([[[[ 53, 53, 60],
[ 63, 62, 66],
[ 74, 72, 75],
...,
[187, 194, 232],
but I can't access the single image.
EDIT: I'm using tensorflow 1.15.5
CodePudding user response:
You can access the batched array with:
dataset.x
where it will have shape (num_of_images, rows, cols, channel)
.
If you want a single image, you can treat it as an array (because it is):
first_image = dataset.x[0]
last_image = dataset.x[-1]
pyplot.plt.axis("off")
pyplot.imshow(first_image)
Edit: Additionally, you can get the corresponding array of labels with:
dataset.y
CodePudding user response:
I found the solution (simpler than I thought):
for element in dataset:
single_element = element
break
image = single_element['image']
label = single_element['label']