Issue
I’m following the Intro to TensorFlow for Deep Learning course from Udacity. One of the lessons goes through a Google Colab Notebook that explains how to work with the Fashion MNIST dataset (Colab and GitHub links).
When showing images from the dataset, the code always extracts the first ones, using the NumPy method take()
. I would like to know how to access different parts of the datasets, but I don’t have enough knowledge of Python to do it.
The first example is this one:
# Take a single image, and remove the color dimension by reshaping
for image, label in test_dataset.take(1):
break;
image = image.numpy().reshape((28,28))
What should I do to only take the second one? If I just change the first parameter from 1 to 2, nothing changes, I guess because I’m taking the first 2 elements, but still reading only the first one. Is there a way of taking only one item but skipping some of them?
By reading some answers to this question I have found out about the skip()
method, but it looks like it takes all elements until the end, but I only want to extract a few items, in this case only one.
The second example is this one:
plt.figure(figsize=(10,10))
for i, (image, label) in enumerate(train_dataset.take(25)):
image = image.numpy().reshape((28,28))
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(image, cmap=plt.cm.binary)
plt.xlabel(class_names[label])
plt.show()
This one takes the first 25 items: how can I start reading images from let’s say the 100th?
In the third and last example they do a very similar thing:
for test_images, test_labels in test_dataset.take(1):
test_images = test_images.numpy()
test_labels = test_labels.numpy()
predictions = model.predict(test_images)
How do I start from the second or third batch of data?
Solution
for getting items after 100th, You can do this:
- Set
batch_size = 100
- Use this line :
dataset = dataset.skip(1).take(1)
. With this line you skip first 100 element and start from 101 to 200(because batch_size == 100)
.
Example Code:
import tensorflow_datasets as tfds
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
dataset = tfds.load('fashion_mnist', as_supervised=True, split = 'train').batch(100)
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
for iter, (image, label) in enumerate(dataset.skip(1).take(2)):
print(f"{1*(iter+1)}01 -> {1*(iter+1)}10 images")
fig, axes = plt.subplots(2,5,figsize=(15,6))
for idx, axe in enumerate(axes.flatten()):
axe.axis('off')
axe.imshow(image[idx][...,0])
axe.set_title(class_names[label[idx]])
plt.show()
print()
Output:
Answered By - I'mahdi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.