With tensorflow-datasets I integrated the MNIST dataset into Tensorflow and now want to visualize single images with Matplotlib. I did it according to this guide: https://www.tensorflow.org/datasets/overview
Unfortunately I get an error message during execution. But it works great in the Guide.
According to the guide you have to create a new dataset with only one image using the take() function. Then the features are accessed in the guide. During my attempts I always get an error message.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import tensorflow.compat.v1 as tf
import tensorflow_datasets as tfds
mnist_train, info = tfds.load(name="mnist", split=tfds.Split.TRAIN, with_info=True)
assert isinstance(mnist_train, tf.data.Dataset)
mnist_example = mnist_train.take(50)
#The error is raised in the next line.
image = mnist_example["image"]
label = mnist_example["label"]
plt.imshow(image.numpy()[:, :, 0].astype(np.float32), cmap=plt.get_cmap("gray"))
print("Label: %d" % label.numpy())
This is the error message:
Traceback (most recent call last):
File "D:/mnist/model.py", line 24, in <module>
image = mnist_example["image"]
TypeError: 'DatasetV1Adapter' object is not subscriptable
Does anyone know how I can fix this? After a lot of research I still haven't found the solution.