I’m trying to display images of a dataset on a plot with their predictions. But I have this error: cannot compute Pack as input #1(zero-based) was expected to be a float tensor but is a int32 tensor [Op:Pack] name: packed
This is the code in which I plot:
JavaScript
x
8
1
for images in val_ds.take(1):
2
tf.squeeze(images, [0])
3
for i in range(18):
4
ax = plt.subplot(6, 6, i + 1)
5
plt.imshow(images[i].numpy().astype("uint8"))
6
#plt.title(predictions[i])
7
plt.axis("off")
8
I have the error on second line, on the tf.squeeze function. I want to remove first dimension of images shape (shape is (18, 360, 360, 3) and I want (360, 360, 3)).
Advertisement
Answer
You are forgetting to reference your labels in your loop. Try something like this:
JavaScript
1
24
24
1
import tensorflow as tf
2
import pathlib
3
import matplotlib.pyplot as plt
4
5
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
6
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
7
data_dir = pathlib.Path(data_dir)
8
9
batch_size = 18
10
11
val_ds = tf.keras.utils.image_dataset_from_directory(
12
data_dir,
13
validation_split=0.2,
14
subset="validation",
15
seed=123,
16
image_size=(360, 360),
17
batch_size=batch_size)
18
19
for images, _ in val_ds.take(1):
20
for i in range(18):
21
ax = plt.subplot(6, 6, i + 1)
22
plt.imshow(images[i].numpy().astype("uint8"))
23
plt.axis("off")
24