derBai
derBai

Reputation: 33

How to access Feature dictionaries of Datasets in TensorFlow

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.

Upvotes: 3

Views: 8472

Answers (1)

a-sam
a-sam

Reputation: 481

eager execution

first write the code tf.enable_eager_execution()

why?

because if you dont you'll need to create graphs and do session.run() to get some samples

eager execution definition (reference):

TensorFlow's eager execution is an imperative programming environment that evaluates >operations immediately, without building graphs: operations return concrete values >instead of constructing a computational graph to run later

then

how to access the samples inside Dataset object

all you need is to iterate over DatasetV1Adapter object

several ways to access some samples via conversion to numpy:

1.

mnist_example = mnist_train.take(50)
for sample in mnist_example:
    image, label = sample["image"].numpy(), sample["label"].numpy()
    plt.imshow(image[:, :, 0].astype(np.uint8), cmap=plt.get_cmap("gray"))
    plt.show()
    print("Label: %d" % label)

2.

mnist_example = tfds.as_numpy(mnist_example, graph=None)
for sample in mnist_example:
    image, label = sample["image"], sample["label"]
    plt.imshow(image[:, :, 0].astype(np.uint8), cmap=plt.get_cmap("gray"))
    plt.show()
    print("Label: %d" % label)

Note1: if you want all of the 50 samples in a numpy array you can create a empty array such as np.zeros((28, 28, 50), dtype=np.uint8) array and assign those images to the elements of it.

Note2: for the purpose of imshow, don't convert into np.float32, its useless, the images are in uint8 format/range (not normalized by default)

Upvotes: 5

Related Questions